4545from ..serializations import (
4646 CTxOut ,
4747 ExtendedKey ,
48+ is_p2pkh ,
49+ is_p2sh ,
50+ is_p2wsh ,
51+ is_witness ,
4852 ser_uint256 ,
4953)
5054from .. import bech32
@@ -200,26 +204,46 @@ def sign_tx(self, tx):
200204
201205 # Detrermine spend type
202206 scriptcode = b''
207+ utxo = None
203208 if psbt_in .witness_utxo :
204209 utxo = psbt_in .witness_utxo
205- # Check if the output is p2sh
206- if psbt_in .witness_utxo .is_p2sh ():
210+ if psbt_in .non_witness_utxo :
211+ if txin .prevout .hash != psbt_in .non_witness_utxo .sha256 :
212+ raise BadArgumentError ('Input {} has a non_witness_utxo with the wrong hash' .format (input_num ))
213+ utxo = psbt_in .non_witness_utxo .vout [txin .prevout .n ]
214+ if utxo is None :
215+ continue
216+ scriptcode = utxo .scriptPubKey
217+
218+ # Check if P2SH
219+ p2sh = False
220+ if is_p2sh (scriptcode ):
221+ # Look up redeemscript
222+ if len (psbt_in .redeem_script ) == 0 :
223+ continue
224+ scriptcode = psbt_in .redeem_script
225+ p2sh = True
226+
227+ # Check segwit
228+ is_wit , _ , _ = is_witness (scriptcode )
229+
230+ if is_wit :
231+ if p2sh :
207232 txinputtype .script_type = proto .InputScriptType .SPENDP2SHWITNESS
208233 else :
209234 txinputtype .script_type = proto .InputScriptType .SPENDWITNESS
210- scriptcode = psbt_in .witness_utxo .scriptPubKey
211- txinputtype .amount = psbt_in .witness_utxo .nValue
212- elif psbt_in .non_witness_utxo :
213- utxo = psbt_in .non_witness_utxo .vout [txin .prevout .n ]
235+ else :
214236 txinputtype .script_type = proto .InputScriptType .SPENDADDRESS
215- scriptcode = utxo .scriptPubKey
216- txinputtype .amount = psbt_in .non_witness_utxo .vout [txin .prevout .n ].nValue
217-
218- # Set the script
219- if psbt_in .witness_script :
237+ txinputtype .amount = utxo .nValue
238+
239+ # Check if P2WSH
240+ p2wsh = False
241+ if is_p2wsh (scriptcode ):
242+ # Look up witnessscript
243+ if len (psbt_in .witness_script ) == 0 :
244+ continue
220245 scriptcode = psbt_in .witness_script
221- elif psbt_in .redeem_script :
222- scriptcode = psbt_in .redeem_script
246+ p2wsh = True
223247
224248 def ignore_input ():
225249 txinputtype .address_n = [0x80000000 | 84 , 0x80000000 | (1 if self .is_testnet else 0 )]
@@ -240,11 +264,11 @@ def ignore_input():
240264 # Cannot sign bare multisig, ignore it
241265 ignore_input ()
242266 continue
243- elif not is_ms and psbt_in . non_witness_utxo and not utxo . is_p2pkh :
267+ elif not is_ms and not is_wit and not is_p2pkh ( scriptcode ) :
244268 # Cannot sign unknown spk, ignore it
245269 ignore_input ()
246270 continue
247- elif not is_ms and psbt_in . witness_utxo and psbt_in . witness_script :
271+ elif not is_ms and is_wit and p2wsh :
248272 # Cannot sign unknown witness script, ignore it
249273 ignore_input ()
250274 continue
@@ -273,7 +297,8 @@ def ignore_input():
273297 ignore_input ()
274298 continue
275299 elif not found and found_in_sigs : # All of our keys are in partial_sigs, ignore whatever signature is produced for this input
276- to_ignore .append (input_num )
300+ ignore_input ()
301+ continue
277302
278303 # append to inputs
279304 inputs .append (txinputtype )
0 commit comments