Skip to content

Commit 6b59c75

Browse files
committed
psbt: Use static members for field numbers
Instead of including field numbers explicitly, use their names, with the numbers defined as static members of their respective classes.
1 parent bc7efb9 commit 6b59c75

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

hwilib/psbt.py

+33-14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ class PartiallySignedInput:
7676
"""
7777
An object for a PSBT input map.
7878
"""
79+
80+
PSBT_IN_NON_WITNESS_UTXO = 0x00
81+
PSBT_IN_WITNESS_UTXO = 0x01
82+
PSBT_IN_PARTIAL_SIG = 0x02
83+
PSBT_IN_SIGHASH_TYPE = 0x03
84+
PSBT_IN_REDEEM_SCRIPT = 0x04
85+
PSBT_IN_WITNESS_SCRIPT = 0x05
86+
PSBT_IN_BIP32_DERIVATION = 0x06
87+
PSBT_IN_FINAL_SCRIPTSIG = 0x07
88+
PSBT_IN_FINAL_SCRIPTWITNESS = 0x08
89+
7990
def __init__(self) -> None:
8091
self.non_witness_utxo: Optional[CTransaction] = None
8192
self.witness_utxo: Optional[CTxOut] = None
@@ -125,7 +136,7 @@ def deserialize(self, f: Readable) -> None:
125136
# First byte of key is the type
126137
key_type = struct.unpack("b", bytearray([key[0]]))[0]
127138

128-
if key_type == 0:
139+
if key_type == PartiallySignedInput.PSBT_IN_NON_WITNESS_UTXO:
129140
if key in key_lookup:
130141
raise PSBTSerializationError("Duplicate Key, input non witness utxo already provided")
131142
elif len(key) != 1:
@@ -134,15 +145,15 @@ def deserialize(self, f: Readable) -> None:
134145
utxo_bytes = BufferedReader(BytesIO(deser_string(f))) # type: ignore
135146
self.non_witness_utxo.deserialize(utxo_bytes)
136147
self.non_witness_utxo.rehash()
137-
elif key_type == 1:
148+
elif key_type == PartiallySignedInput.PSBT_IN_WITNESS_UTXO:
138149
if key in key_lookup:
139150
raise PSBTSerializationError("Duplicate Key, input witness utxo already provided")
140151
elif len(key) != 1:
141152
raise PSBTSerializationError("witness utxo key is more than one byte type")
142153
self.witness_utxo = CTxOut()
143154
tx_out_bytes = BufferedReader(BytesIO(deser_string(f))) # type: ignore
144155
self.witness_utxo.deserialize(tx_out_bytes)
145-
elif key_type == 2:
156+
elif key_type == PartiallySignedInput.PSBT_IN_PARTIAL_SIG:
146157
if len(key) != 34 and len(key) != 66:
147158
raise PSBTSerializationError("Size of key was not the expected size for the type partial signature pubkey")
148159
pubkey = key[1:]
@@ -151,34 +162,34 @@ def deserialize(self, f: Readable) -> None:
151162

152163
sig = deser_string(f)
153164
self.partial_sigs[pubkey] = sig
154-
elif key_type == 3:
165+
elif key_type == PartiallySignedInput.PSBT_IN_SIGHASH_TYPE:
155166
if key in key_lookup:
156167
raise PSBTSerializationError("Duplicate key, input sighash type already provided")
157168
elif len(key) != 1:
158169
raise PSBTSerializationError("sighash key is more than one byte type")
159170
sighash_bytes = deser_string(f)
160171
self.sighash = struct.unpack("<I", sighash_bytes)[0]
161-
elif key_type == 4:
172+
elif key_type == PartiallySignedInput.PSBT_IN_REDEEM_SCRIPT:
162173
if key in key_lookup:
163174
raise PSBTSerializationError("Duplicate key, input redeemScript already provided")
164175
elif len(key) != 1:
165176
raise PSBTSerializationError("redeemScript key is more than one byte type")
166177
self.redeem_script = deser_string(f)
167-
elif key_type == 5:
178+
elif key_type == PartiallySignedInput.PSBT_IN_WITNESS_SCRIPT:
168179
if key in key_lookup:
169180
raise PSBTSerializationError("Duplicate key, input witnessScript already provided")
170181
elif len(key) != 1:
171182
raise PSBTSerializationError("witnessScript key is more than one byte type")
172183
self.witness_script = deser_string(f)
173-
elif key_type == 6:
184+
elif key_type == PartiallySignedInput.PSBT_IN_BIP32_DERIVATION:
174185
DeserializeHDKeypath(f, key, self.hd_keypaths, [34, 66])
175-
elif key_type == 7:
186+
elif key_type == PartiallySignedInput.PSBT_IN_FINAL_SCRIPTSIG:
176187
if key in key_lookup:
177188
raise PSBTSerializationError("Duplicate key, input final scriptSig already provided")
178189
elif len(key) != 1:
179190
raise PSBTSerializationError("final scriptSig key is more than one byte type")
180191
self.final_script_sig = deser_string(f)
181-
elif key_type == 8:
192+
elif key_type == PartiallySignedInput.PSBT_IN_FINAL_SCRIPTWITNESS:
182193
if key in key_lookup:
183194
raise PSBTSerializationError("Duplicate key, input final scriptWitness already provided")
184195
elif len(key) != 1:
@@ -251,6 +262,11 @@ class PartiallySignedOutput:
251262
"""
252263
An object for a PSBT output map.
253264
"""
265+
266+
PSBT_OUT_REDEEM_SCRIPT = 0x00
267+
PSBT_OUT_WITNESS_SCRIPT = 0x01
268+
PSBT_OUT_BIP32_DERIVATION = 0x02
269+
254270
def __init__(self) -> None:
255271
self.redeem_script = b""
256272
self.witness_script = b""
@@ -288,19 +304,19 @@ def deserialize(self, f: Readable) -> None:
288304
# First byte of key is the type
289305
key_type = struct.unpack("b", bytearray([key[0]]))[0]
290306

291-
if key_type == 0:
307+
if key_type == PartiallySignedOutput.PSBT_OUT_REDEEM_SCRIPT:
292308
if key in key_lookup:
293309
raise PSBTSerializationError("Duplicate key, output redeemScript already provided")
294310
elif len(key) != 1:
295311
raise PSBTSerializationError("Output redeemScript key is more than one byte type")
296312
self.redeem_script = deser_string(f)
297-
elif key_type == 1:
313+
elif key_type == PartiallySignedOutput.PSBT_OUT_WITNESS_SCRIPT:
298314
if key in key_lookup:
299315
raise PSBTSerializationError("Duplicate key, output witnessScript already provided")
300316
elif len(key) != 1:
301317
raise PSBTSerializationError("Output witnessScript key is more than one byte type")
302318
self.witness_script = deser_string(f)
303-
elif key_type == 2:
319+
elif key_type == PartiallySignedOutput.PSBT_OUT_BIP32_DERIVATION:
304320
DeserializeHDKeypath(f, key, self.hd_keypaths, [34, 66])
305321
else:
306322
if key in self.unknown:
@@ -340,6 +356,9 @@ class PSBT(object):
340356
A class representing a PSBT
341357
"""
342358

359+
PSBT_GLOBAL_UNSIGNED_TX = 0x00
360+
PSBT_GLOBAL_XPUB = 0x01
361+
343362
def __init__(self, tx: Optional[CTransaction] = None) -> None:
344363
"""
345364
:param tx: A Bitcoin transaction that specifies the inputs and outputs to use
@@ -386,7 +405,7 @@ def deserialize(self, psbt: str) -> None:
386405
key_type = struct.unpack("b", bytearray([key[0]]))[0]
387406

388407
# Do stuff based on type
389-
if key_type == 0x00:
408+
if key_type == PSBT.PSBT_GLOBAL_UNSIGNED_TX:
390409
# Checks for correctness
391410
if key in key_lookup:
392411
raise PSBTSerializationError("Duplicate key, unsigned tx already provided")
@@ -401,7 +420,7 @@ def deserialize(self, psbt: str) -> None:
401420
for txin in self.tx.vin:
402421
if len(txin.scriptSig) != 0 or not self.tx.wit.is_null():
403422
raise PSBTSerializationError("Unsigned tx does not have empty scriptSigs and scriptWitnesses")
404-
elif key_type == 0x01:
423+
elif key_type == PSBT.PSBT_GLOBAL_XPUB:
405424
DeserializeHDKeypath(f, key, self.xpub, [79])
406425
else:
407426
if key in self.unknown:

0 commit comments

Comments
 (0)