1
1
from dataclasses import dataclass , field
2
- from typing import Optional , Union
2
+ from typing import Optional , Union , Type
3
3
4
+ from pycardano .exception import DeserializeException
4
5
from pycardano .hash import PoolKeyHash , ScriptHash , VerificationKeyHash
5
- from pycardano .serialization import ArrayCBORSerializable
6
+ from pycardano .serialization import (
7
+ ArrayCBORSerializable ,
8
+ ArrayBase ,
9
+ limit_primitive_type ,
10
+ )
6
11
7
12
__all__ = [
8
13
"Certificate" ,
@@ -25,20 +30,56 @@ def __post_init__(self):
25
30
else :
26
31
self ._CODE = 1
27
32
33
+ @classmethod
34
+ @limit_primitive_type (list , tuple )
35
+ def from_primitive (
36
+ cls : Type [ArrayBase ], values : Union [list , tuple ]
37
+ ) -> "StakeCredential" :
38
+ if len (values ) != 2 :
39
+ raise ValueError (f"Expected 2 values, got { len (values )} " )
40
+ if values [0 ] == 0 :
41
+ return StakeCredential (VerificationKeyHash .from_primitive (values [1 ]))
42
+ elif values [0 ] == 1 :
43
+ return StakeCredential (ScriptHash .from_primitive (values [1 ]))
44
+ else :
45
+ raise ValueError (f"Unknown code: { values [0 ]} " )
46
+
28
47
29
48
@dataclass (repr = False )
30
49
class StakeRegistration (ArrayCBORSerializable ):
31
50
_CODE : int = field (init = False , default = 0 )
32
51
33
52
stake_credential : StakeCredential
34
53
54
+ @classmethod
55
+ @limit_primitive_type (list , tuple )
56
+ def from_primitive (
57
+ cls : Type [ArrayBase ], values : Union [list , tuple ]
58
+ ) -> "StakeRegistration" :
59
+ if len (values ) != 2 :
60
+ raise DeserializeException (f"Expected 2 values, got { len (values )} " )
61
+ if values [0 ] != 0 :
62
+ raise DeserializeException (f"Expected 0, got { values [0 ]} " )
63
+ return StakeRegistration (StakeCredential .from_primitive (values [1 ]))
64
+
35
65
36
66
@dataclass (repr = False )
37
67
class StakeDeregistration (ArrayCBORSerializable ):
38
68
_CODE : int = field (init = False , default = 1 )
39
69
40
70
stake_credential : StakeCredential
41
71
72
+ @classmethod
73
+ @limit_primitive_type (list , tuple )
74
+ def from_primitive (
75
+ cls : Type [ArrayBase ], values : Union [list , tuple ]
76
+ ) -> "StakeDeregistration" :
77
+ if len (values ) != 2 :
78
+ raise DeserializeException (f"Expected 2 values, got { len (values )} " )
79
+ if values [0 ] != 1 :
80
+ raise DeserializeException (f"Expected 1, got { values [0 ]} " )
81
+ return StakeDeregistration (StakeCredential .from_primitive (values [1 ]))
82
+
42
83
43
84
@dataclass (repr = False )
44
85
class StakeDelegation (ArrayCBORSerializable ):
@@ -48,5 +89,19 @@ class StakeDelegation(ArrayCBORSerializable):
48
89
49
90
pool_keyhash : PoolKeyHash
50
91
92
+ @classmethod
93
+ @limit_primitive_type (list , tuple )
94
+ def from_primitive (
95
+ cls : Type [ArrayBase ], values : Union [list , tuple ]
96
+ ) -> "StakeDelegation" :
97
+ if len (values ) != 3 :
98
+ raise DeserializeException (f"Expected 3 values, got { len (values )} " )
99
+ if values [0 ] != 2 :
100
+ raise DeserializeException (f"Expected 2, got { values [0 ]} " )
101
+ return StakeDelegation (
102
+ StakeCredential .from_primitive (values [1 ]),
103
+ PoolKeyHash .from_primitive (values [2 ]),
104
+ )
105
+
51
106
52
107
Certificate = Union [StakeRegistration , StakeDeregistration , StakeDelegation ]
0 commit comments