Bitcoin

How to use P2SH using only Redeemscript hex? No private key included

Code used to build transactions

import hashlib import base58

prev_txid = ‘ec5596bf71a498fc944e912f0bc21f0ead3806965402d74f1d670c2fff7c08c2’ prev_index = 0 # Assume the output you want to spend is the first output of the previous transaction prev_amount = 0.01 # Amount of previous transaction output

output_address=”tb1qyynpskfgzq7vszgra3ehvvhvu6d3xkdjqrq652″ output_amount = 0.0099 # Send 0.01 BTC, subtract 0.0001 BTC as fee

Witness_script_hex = ’76a914787be176f2618457541d957dd84d2df475b5d6ec88ac’ # Witness script Witness_script = bytes.fromhex(witness_script_hex) Witness_script_hash = hashlib.new(‘ripemd160’, hashlib.sha256(witness_script).digest()).di gest() input_script = bytes.fromhex(‘160014 ‘ ) + Witness_script_hash # OP_0

output_script = bytes.fromhex(‘0014’) + base58.b58decode(output_address)(5:) # P2WPKH output script

version = 1 lock time = 0 tx_in_count = 1 tx_out_count = 1

tx_in = ( bytes.fromhex(prev_txid)(::-1) + # Previous tx ID, little endian prev_index.to_bytes(4, byteorder=”little”) + # Previous index, little endian len(input_script).to_bytes(1 , byteorder=”little”) + # Script length input_script + # Input script b’\xff\xff\xff\xff’ # Sequence, little endian (0xFFFFFFFF) )

tx_out = ( int(output_amount * 100000000).to_bytes(8, byteorder=”little”) + # Value, 8 bytes len(output_script).to_bytes(1, byteorder=”little”) + # Script length output_script # Output script)

Witness = ( len(input_script).to_bytes(1, byteorder=”little”) + # Watch script length input_script # Watch script )

final_tx = ( version.to_bytes(4, byteorder=”little”) + # version tx_in_count.to_bytes(1, byteorder=”little”) + # input count tx_in + # input tx_out_count.to_bytes(1, byteorder=”little” ) + # Number of outputs tx_out + # Output locktime.to_bytes(4, byteorder=”little”) + # Lock time b’00’ + # Marker for monitoring SegWit transactions # Monitoring data)

tx_hash = hashlib.sha256(hashlib.sha256(final_tx).digest()).digest()

final_tx_hex = final_tx.hex() + tx_hash.hex()

print(“Unsigned transaction hex:”) print(final_tx_hex)

The resulting rawtransaction is:

0100000001c2087cff2f0c671d4fd70254960638ad0e1fc20b2f914e94fc98a471bf9655ec00000000171600141ee06adc3ff6103b173eef823f8612345d3919d5 ffff ffff01301b0f00000000001c00141e2361ff02e8b850288e0b3a07f4420743b1deec45d744ced1ad000000003030171600141ee06adc3ff6103b173eef823f8612345d 391 9d5292a3cc2d63078d6441f02a42edc1be8debddf288fbb836bba289b1b266eb183

and refuses
Transaction validation error: Watch redemption script detected on tx without watch data.

Confused about modifying TX without witness data. Who can help?

Related Articles

Back to top button