@@ -47,16 +47,18 @@ def from_primitive(
47
47
)
48
48
49
49
script_type : int = value [0 ]
50
- for t in [
51
- ScriptPubkey ,
52
- ScriptAll ,
53
- ScriptAny ,
54
- ScriptNofK ,
55
- InvalidBefore ,
56
- InvalidHereAfter ,
57
- ]:
58
- if t ._TYPE == script_type : # type: ignore
59
- return super (NativeScript , t ).from_primitive (value [1 :]) # type: ignore
50
+ if script_type == ScriptPubkey ._TYPE :
51
+ return super (NativeScript , ScriptPubkey ).from_primitive (value [1 :])
52
+ elif script_type == ScriptAll ._TYPE :
53
+ return super (NativeScript , ScriptAll ).from_primitive (value [1 :])
54
+ elif script_type == ScriptAny ._TYPE :
55
+ return super (NativeScript , ScriptAny ).from_primitive (value [1 :])
56
+ elif script_type == ScriptNofK ._TYPE :
57
+ return super (NativeScript , ScriptNofK ).from_primitive (value [1 :])
58
+ elif script_type == InvalidBefore ._TYPE :
59
+ return super (NativeScript , InvalidBefore ).from_primitive (value [1 :])
60
+ elif script_type == InvalidHereAfter ._TYPE :
61
+ return super (NativeScript , InvalidHereAfter ).from_primitive (value [1 :])
60
62
else :
61
63
raise DeserializeException (f"Unknown script type indicator: { script_type } " )
62
64
@@ -73,43 +75,16 @@ def from_dict(
73
75
ScriptPubkey , ScriptAll , ScriptAny , ScriptNofK , InvalidBefore , InvalidHereAfter
74
76
]:
75
77
"""Parse a standard native script dictionary (potentially parsed from a JSON file)."""
76
-
77
- types = {
78
- p .json_tag : p # type: ignore
79
- for p in [
80
- ScriptPubkey ,
81
- ScriptAll ,
82
- ScriptAny ,
83
- ScriptNofK ,
84
- InvalidBefore ,
85
- InvalidHereAfter ,
86
- ]
87
- }
88
- script_type = script_json ["type" ]
89
- target_class = types [script_type ]
90
78
script_primitive = cls ._script_json_to_primitive (script_json )
91
- return super ( NativeScript , target_class ) .from_primitive (script_primitive [ 1 :]) # type: ignore
79
+ return cls .from_primitive (script_primitive )
92
80
93
81
@classmethod
94
82
def _script_json_to_primitive (
95
83
cls : Type [NativeScript ], script_json : JsonDict
96
84
) -> List [Primitive ]:
97
85
"""Serialize a standard JSON native script into a primitive array"""
98
-
99
- types = {
100
- p .json_tag : p # type: ignore
101
- for p in [
102
- ScriptPubkey ,
103
- ScriptAll ,
104
- ScriptAny ,
105
- ScriptNofK ,
106
- InvalidBefore ,
107
- InvalidHereAfter ,
108
- ]
109
- }
110
-
111
86
script_type : str = script_json ["type" ]
112
- native_script : List [Primitive ] = [types [script_type ]. _TYPE ] # type: ignore
87
+ native_script : List [Primitive ] = [JSON_TAG_TO_INT [script_type ]]
113
88
114
89
for key , value in script_json .items ():
115
90
if key == "type" :
@@ -225,3 +200,13 @@ class InvalidHereAfter(NativeScript):
225
200
_TYPE : int = field (default = 5 , init = False )
226
201
227
202
after : int
203
+
204
+
205
+ JSON_TAG_TO_INT = {
206
+ ScriptPubkey .json_tag : ScriptPubkey ._TYPE ,
207
+ ScriptAll .json_tag : ScriptAll ._TYPE ,
208
+ ScriptAny .json_tag : ScriptAny ._TYPE ,
209
+ ScriptNofK .json_tag : ScriptNofK ._TYPE ,
210
+ InvalidBefore .json_tag : InvalidBefore ._TYPE ,
211
+ InvalidHereAfter .json_tag : InvalidHereAfter ._TYPE ,
212
+ }
0 commit comments