11
11
from pycardano .exception import DeserializeException
12
12
from pycardano .hash import SCRIPT_HASH_SIZE , ScriptHash , VerificationKeyHash
13
13
from pycardano .serialization import ArrayCBORSerializable , Primitive , list_hook
14
+ from pycardano .types import JSON
14
15
15
16
__all__ = [
16
17
"NativeScript" ,
25
26
26
27
@dataclass
27
28
class NativeScript (ArrayCBORSerializable ):
29
+ json_tag : ClassVar [str ]
30
+ json_field : ClassVar [str ]
31
+
28
32
@classmethod
29
33
def from_primitive (
30
34
cls : Type [NativeScript ], value : Primitive
@@ -54,7 +58,7 @@ def hash(self) -> ScriptHash:
54
58
55
59
@classmethod
56
60
def from_dict (
57
- cls : NativeScript , script : dict , top_level : bool = True
61
+ cls : Type [ NativeScript ], script_json : JSON
58
62
) -> Union [
59
63
ScriptPubkey , ScriptAll , ScriptAny , ScriptNofK , InvalidBefore , InvalidHereAfter
60
64
]:
@@ -71,27 +75,48 @@ def from_dict(
71
75
InvalidHereAfter ,
72
76
]
73
77
}
78
+ script_type = script_json ["type" ]
79
+ target_class = types [script_type ]
80
+ script_primitive = cls ._script_dict_to_primitive (script_json )
81
+ return super (NativeScript , target_class ).from_primitive (script_primitive [1 :])
74
82
75
- native_script = []
76
- if isinstance (script , dict ):
83
+ @classmethod
84
+ def _script_dict_to_primitive (
85
+ cls : Type [NativeScript ], script_json : JSON
86
+ ) -> List [Primitive ]:
87
+ """Serialize a standard JSON native script into a primitive array"""
77
88
78
- for key , value in script .items ():
79
- if key == "type" :
80
- native_script .insert (0 , list (types .keys ()).index (value ))
81
- elif key == "scripts" :
82
- native_script .append (cls .from_dict (value , top_level = False ))
83
- else :
84
- native_script .append (value )
89
+ types = {
90
+ p .json_tag : p
91
+ for p in [
92
+ ScriptPubkey ,
93
+ ScriptAll ,
94
+ ScriptAny ,
95
+ ScriptNofK ,
96
+ InvalidBefore ,
97
+ InvalidHereAfter ,
98
+ ]
99
+ }
85
100
86
- elif isinstance ( script , list ): # list
87
- native_script = [cls . from_dict ( i , top_level = False ) for i in script ]
101
+ script_type : str = script_json [ "type" ]
102
+ native_script = [types [ script_type ]. _TYPE ]
88
103
89
- if not top_level :
90
- return native_script
91
- else :
92
- return super (NativeScript , types [script ["type" ]]).from_primitive (
93
- native_script [1 :]
94
- )
104
+ for key , value in script_json .items ():
105
+ if key == "type" :
106
+ continue
107
+ elif key == "scripts" :
108
+ native_script .append (cls ._script_list_to_primitive (value ))
109
+ else :
110
+ native_script .append (value )
111
+ return native_script
112
+
113
+ @classmethod
114
+ def _script_list_to_primitive (
115
+ cls : Type [NativeScript ], script_jsons : List [JSON ]
116
+ ) -> List [List [Primitive ]]:
117
+ """Parse a list of JSON scripts into a list of primitive arrays"""
118
+ native_script = [cls ._script_dict_to_primitive (i ) for i in script_jsons ]
119
+ return native_script
95
120
96
121
def to_dict (self ) -> dict :
97
122
"""Export to standard native script dictionary (potentially to dump to a JSON file)."""
0 commit comments