def sig_hash(self, input_index, hash_type): # get the relevant input tx_in = self.tx_ins[input_index] # get the script_pubkey of the input script_pubkey = tx_in.script_pubkey(network=self.network) # grab the RedeemScript if we have a p2sh if script_pubkey.is_p2sh(): # the last command of the ScriptSig is the raw RedeemScript raw_redeem_script = tx_in.script_sig.commands[-1] # convert to RedeemScript redeem_script = RedeemScript.convert(raw_redeem_script) else: redeem_script = None # grab the WitnessScript if we have a p2wsh if script_pubkey.is_p2wsh() or (redeem_script and redeem_script.is_p2wsh()): # the last item of the Witness is the raw WitnessScript raw_witness_script = tx_in.witness.items[-1] # convert to WitnessScript witness_script = WitnessScript.convert(raw_witness_script) else: witness_script = None # check to see if the ScriptPubKey or the RedeemScript is p2wpkh or p2wsh if (script_pubkey.is_p2wpkh() or (redeem_script and redeem_script.is_p2wpkh()) or script_pubkey.is_p2wsh() or (redeem_script and redeem_script.is_p2wsh())): return self.sig_hash_bip143( input_index, redeem_script=redeem_script, witness_script=witness_script, hash_type=hash_type, ) elif script_pubkey.is_p2tr(): if len(tx_in.witness) > 1: ext_flag = 1 else: ext_flag = 0 return self.sig_hash_bip341(input_index, ext_flag=ext_flag, hash_type=hash_type) else: return self.sig_hash_legacy(input_index, redeem_script, hash_type=hash_type)
def test_p2sh_address(self): witness_script_hex = "5221026ccfb8061f235cc110697c0bfb3afb99d82c886672f6b9b5393b25a434c0cbf32103befa190c0c22e2f53720b1be9476dcf11917da4665c44c9c71c3a2d28a933c352102be46dc245f58085743b1cc37c82f0d63a960efa43b5336534275fc469b49f4ac53ae" witness_script = WitnessScript.convert(bytes.fromhex(witness_script_hex)) want = "2MvVx9ccWqyYVNa5Xz9pfCEVk99zVBZh9ms" self.assertEqual(witness_script.p2sh_address(network="testnet"), want) self.assertEqual(witness_script.p2sh_address(network="signet"), want)
def test_address(self): witness_script_hex = "52210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae" witness_script = WitnessScript.convert(bytes.fromhex(witness_script_hex)) want = "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej" self.assertEqual(witness_script.address(), want)