Skip to content

Commit 115c378

Browse files
CIP-0008: Allow for signing with stake key directly
In the browser, if a user specifies the stake key as part of CIP-0030 the message returned in JSON has the COSE key as the staking key. The verify() method correctly detects this, but the sign() method does not. This change detects if a stake SigningKey type was passed in and if so, sign with a stake key. [ Documentation: pydoc updates ] [ Testing: python -m build, manual test in CLI ]
1 parent 3527aa4 commit 115c378

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pycardano/cip/cip8.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from pycardano.key import (
1414
PaymentVerificationKey,
1515
SigningKey,
16+
StakeExtendedSigningKey,
17+
StakeSigningKey,
1618
StakeVerificationKey,
1719
VerificationKey,
1820
)
@@ -27,7 +29,9 @@ def sign(
2729
attach_cose_key: bool = False,
2830
network: Network = Network.MAINNET,
2931
) -> Union[str, dict]:
30-
"""Sign an arbitrary message with a payment key following CIP-0008.
32+
"""Sign an arbitrary message with a payment or stake key following CIP-0008.
33+
Note that a stake key passed in must be of type StakeSigningKey or
34+
StakeExtendedSigningKey to be detected.
3135
3236
Args:
3337
message (str): Message to be signed
@@ -43,11 +47,16 @@ def sign(
4347
# derive the verification key
4448
verification_key = VerificationKey.from_signing_key(signing_key)
4549

50+
if isinstance(signing_key, StakeSigningKey) or isinstance(signing_key, StakeExtendedSigningKey):
51+
address = Address(payment_part=None, staking_part=verification_key.hash(), network=network)
52+
else:
53+
address = Address(payment_part=verification_key.hash(), staking_part=None, network=network)
54+
4655
# create the message object, attach verification key to the header
4756
msg = Sign1Message(
4857
phdr={
4958
Algorithm: EdDSA,
50-
"address": Address(verification_key.hash(), network=network).to_primitive(),
59+
"address": address.to_primitive(),
5160
},
5261
payload=message.encode("utf-8"),
5362
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pycardano"
3-
version = "0.7.2"
3+
version = "0.7.3"
44
classifiers = [
55
"Intended Audience :: Developers",
66
"License :: OSI Approved :: MIT License",

0 commit comments

Comments
 (0)