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