@@ -76,6 +76,17 @@ class PartiallySignedInput:
76
76
"""
77
77
An object for a PSBT input map.
78
78
"""
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
+
79
90
def __init__ (self ) -> None :
80
91
self .non_witness_utxo : Optional [CTransaction ] = None
81
92
self .witness_utxo : Optional [CTxOut ] = None
@@ -125,7 +136,7 @@ def deserialize(self, f: Readable) -> None:
125
136
# First byte of key is the type
126
137
key_type = struct .unpack ("b" , bytearray ([key [0 ]]))[0 ]
127
138
128
- if key_type == 0 :
139
+ if key_type == PartiallySignedInput . PSBT_IN_NON_WITNESS_UTXO :
129
140
if key in key_lookup :
130
141
raise PSBTSerializationError ("Duplicate Key, input non witness utxo already provided" )
131
142
elif len (key ) != 1 :
@@ -134,15 +145,15 @@ def deserialize(self, f: Readable) -> None:
134
145
utxo_bytes = BufferedReader (BytesIO (deser_string (f ))) # type: ignore
135
146
self .non_witness_utxo .deserialize (utxo_bytes )
136
147
self .non_witness_utxo .rehash ()
137
- elif key_type == 1 :
148
+ elif key_type == PartiallySignedInput . PSBT_IN_WITNESS_UTXO :
138
149
if key in key_lookup :
139
150
raise PSBTSerializationError ("Duplicate Key, input witness utxo already provided" )
140
151
elif len (key ) != 1 :
141
152
raise PSBTSerializationError ("witness utxo key is more than one byte type" )
142
153
self .witness_utxo = CTxOut ()
143
154
tx_out_bytes = BufferedReader (BytesIO (deser_string (f ))) # type: ignore
144
155
self .witness_utxo .deserialize (tx_out_bytes )
145
- elif key_type == 2 :
156
+ elif key_type == PartiallySignedInput . PSBT_IN_PARTIAL_SIG :
146
157
if len (key ) != 34 and len (key ) != 66 :
147
158
raise PSBTSerializationError ("Size of key was not the expected size for the type partial signature pubkey" )
148
159
pubkey = key [1 :]
@@ -151,34 +162,34 @@ def deserialize(self, f: Readable) -> None:
151
162
152
163
sig = deser_string (f )
153
164
self .partial_sigs [pubkey ] = sig
154
- elif key_type == 3 :
165
+ elif key_type == PartiallySignedInput . PSBT_IN_SIGHASH_TYPE :
155
166
if key in key_lookup :
156
167
raise PSBTSerializationError ("Duplicate key, input sighash type already provided" )
157
168
elif len (key ) != 1 :
158
169
raise PSBTSerializationError ("sighash key is more than one byte type" )
159
170
sighash_bytes = deser_string (f )
160
171
self .sighash = struct .unpack ("<I" , sighash_bytes )[0 ]
161
- elif key_type == 4 :
172
+ elif key_type == PartiallySignedInput . PSBT_IN_REDEEM_SCRIPT :
162
173
if key in key_lookup :
163
174
raise PSBTSerializationError ("Duplicate key, input redeemScript already provided" )
164
175
elif len (key ) != 1 :
165
176
raise PSBTSerializationError ("redeemScript key is more than one byte type" )
166
177
self .redeem_script = deser_string (f )
167
- elif key_type == 5 :
178
+ elif key_type == PartiallySignedInput . PSBT_IN_WITNESS_SCRIPT :
168
179
if key in key_lookup :
169
180
raise PSBTSerializationError ("Duplicate key, input witnessScript already provided" )
170
181
elif len (key ) != 1 :
171
182
raise PSBTSerializationError ("witnessScript key is more than one byte type" )
172
183
self .witness_script = deser_string (f )
173
- elif key_type == 6 :
184
+ elif key_type == PartiallySignedInput . PSBT_IN_BIP32_DERIVATION :
174
185
DeserializeHDKeypath (f , key , self .hd_keypaths , [34 , 66 ])
175
- elif key_type == 7 :
186
+ elif key_type == PartiallySignedInput . PSBT_IN_FINAL_SCRIPTSIG :
176
187
if key in key_lookup :
177
188
raise PSBTSerializationError ("Duplicate key, input final scriptSig already provided" )
178
189
elif len (key ) != 1 :
179
190
raise PSBTSerializationError ("final scriptSig key is more than one byte type" )
180
191
self .final_script_sig = deser_string (f )
181
- elif key_type == 8 :
192
+ elif key_type == PartiallySignedInput . PSBT_IN_FINAL_SCRIPTWITNESS :
182
193
if key in key_lookup :
183
194
raise PSBTSerializationError ("Duplicate key, input final scriptWitness already provided" )
184
195
elif len (key ) != 1 :
@@ -251,6 +262,11 @@ class PartiallySignedOutput:
251
262
"""
252
263
An object for a PSBT output map.
253
264
"""
265
+
266
+ PSBT_OUT_REDEEM_SCRIPT = 0x00
267
+ PSBT_OUT_WITNESS_SCRIPT = 0x01
268
+ PSBT_OUT_BIP32_DERIVATION = 0x02
269
+
254
270
def __init__ (self ) -> None :
255
271
self .redeem_script = b""
256
272
self .witness_script = b""
@@ -288,19 +304,19 @@ def deserialize(self, f: Readable) -> None:
288
304
# First byte of key is the type
289
305
key_type = struct .unpack ("b" , bytearray ([key [0 ]]))[0 ]
290
306
291
- if key_type == 0 :
307
+ if key_type == PartiallySignedOutput . PSBT_OUT_REDEEM_SCRIPT :
292
308
if key in key_lookup :
293
309
raise PSBTSerializationError ("Duplicate key, output redeemScript already provided" )
294
310
elif len (key ) != 1 :
295
311
raise PSBTSerializationError ("Output redeemScript key is more than one byte type" )
296
312
self .redeem_script = deser_string (f )
297
- elif key_type == 1 :
313
+ elif key_type == PartiallySignedOutput . PSBT_OUT_WITNESS_SCRIPT :
298
314
if key in key_lookup :
299
315
raise PSBTSerializationError ("Duplicate key, output witnessScript already provided" )
300
316
elif len (key ) != 1 :
301
317
raise PSBTSerializationError ("Output witnessScript key is more than one byte type" )
302
318
self .witness_script = deser_string (f )
303
- elif key_type == 2 :
319
+ elif key_type == PartiallySignedOutput . PSBT_OUT_BIP32_DERIVATION :
304
320
DeserializeHDKeypath (f , key , self .hd_keypaths , [34 , 66 ])
305
321
else :
306
322
if key in self .unknown :
@@ -340,6 +356,9 @@ class PSBT(object):
340
356
A class representing a PSBT
341
357
"""
342
358
359
+ PSBT_GLOBAL_UNSIGNED_TX = 0x00
360
+ PSBT_GLOBAL_XPUB = 0x01
361
+
343
362
def __init__ (self , tx : Optional [CTransaction ] = None ) -> None :
344
363
"""
345
364
:param tx: A Bitcoin transaction that specifies the inputs and outputs to use
@@ -386,7 +405,7 @@ def deserialize(self, psbt: str) -> None:
386
405
key_type = struct .unpack ("b" , bytearray ([key [0 ]]))[0 ]
387
406
388
407
# Do stuff based on type
389
- if key_type == 0x00 :
408
+ if key_type == PSBT . PSBT_GLOBAL_UNSIGNED_TX :
390
409
# Checks for correctness
391
410
if key in key_lookup :
392
411
raise PSBTSerializationError ("Duplicate key, unsigned tx already provided" )
@@ -401,7 +420,7 @@ def deserialize(self, psbt: str) -> None:
401
420
for txin in self .tx .vin :
402
421
if len (txin .scriptSig ) != 0 or not self .tx .wit .is_null ():
403
422
raise PSBTSerializationError ("Unsigned tx does not have empty scriptSigs and scriptWitnesses" )
404
- elif key_type == 0x01 :
423
+ elif key_type == PSBT . PSBT_GLOBAL_XPUB :
405
424
DeserializeHDKeypath (f , key , self .xpub , [79 ])
406
425
else :
407
426
if key in self .unknown :
0 commit comments