From a59e4d1ef59cfccdd53bac37f7e1fa9afdb881d9 Mon Sep 17 00:00:00 2001 From: Jerry Date: Sun, 6 Oct 2024 08:26:08 -0700 Subject: [PATCH] Fix StakeRegistration deser --- pycardano/certificate.py | 5 ++++- test/pycardano/test_certificate.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pycardano/certificate.py b/pycardano/certificate.py index b866055a..c25ba0c2 100644 --- a/pycardano/certificate.py +++ b/pycardano/certificate.py @@ -61,7 +61,10 @@ def __post_init__(self): def from_primitive( cls: Type[StakeRegistration], values: Union[list, tuple] ) -> StakeRegistration: - return cls(stake_credential=StakeCredential.from_primitive(values[1])) + if values[0] == 0: + return cls(stake_credential=StakeCredential.from_primitive(values[1])) + else: + raise DeserializeException(f"Invalid StakeRegistration type {values[0]}") @dataclass(repr=False) diff --git a/test/pycardano/test_certificate.py b/test/pycardano/test_certificate.py index d534de22..58159d42 100644 --- a/test/pycardano/test_certificate.py +++ b/test/pycardano/test_certificate.py @@ -1,3 +1,6 @@ +import os + +from pycardano import StakeSigningKey, TransactionBody from pycardano.address import Address from pycardano.certificate import ( PoolRegistration, @@ -121,3 +124,28 @@ def test_pool_retirement(): ) assert PoolRetirement.from_cbor(pool_retirement_cbor_hex) == pool_retirement + + +def test_staking_certificate_serdes(): + staking_key = StakeSigningKey.generate() + stake_pool_key_hash = os.urandom(28) + + transaction_body = TransactionBody( + certificates=[ + StakeRegistration( + stake_credential=StakeCredential( + staking_key.to_verification_key().hash() + ) + ), + StakeDelegation( + stake_credential=StakeCredential( + staking_key.to_verification_key().hash() + ), + pool_keyhash=PoolKeyHash(stake_pool_key_hash), + ), + ] + ) + + after_serdes = TransactionBody.from_cbor(transaction_body.to_cbor()) + + assert after_serdes == transaction_body