HOW TO SPEND THE P2SH only use Redeemscript hex ? no private key involved


the codes i use to build the transaction

import hashlib
import base58

# Define previous transaction details
prev_txid = 'ec5596bf71a498fc944e912f0bc21f0ead3806965402d74f1d670c2fff7c08c2'
prev_index = 0  # Assuming the output you want to spend is the first output of the previous transaction
prev_amount = 0.01  # Amount of the previous transaction output

# Define output details
output_address="tb1qyynpskfgzq7vszgra3ehvvhvu6d3xkdjqrq652"
output_amount = 0.0099  # Sending 0.01 BTC, minus 0.0001 BTC for the fee

# Construct the input script
witness_script_hex = '76a914787be176f2618457541d957dd84d2df475b5d6ec88ac'  # Witness script
witness_script = bytes.fromhex(witness_script_hex)
witness_script_hash = hashlib.new('ripemd160', hashlib.sha256(witness_script).digest()).digest()
input_script = bytes.fromhex('160014') + witness_script_hash  # OP_0 <witness_script_hash>

# Construct the output script
output_script = bytes.fromhex('0014') + base58.b58decode(output_address)[5:]  # P2WPKH output script

# Construct the transaction
version = 1
locktime = 0
tx_in_count = 1
tx_out_count = 1

tx_in = (
    bytes.fromhex(prev_txid)[::-1] +               # prev tx id, little endian
    prev_index.to_bytes(4, byteorder="little") +   # prev 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 data
witness = (
    len(input_script).to_bytes(1, byteorder="little") +  # witness script length
    input_script                                          # witness script
)

# Constructing the final transaction
final_tx = (
    version.to_bytes(4, byteorder="little") +      # version
    tx_in_count.to_bytes(1, byteorder="little") +  # number of inputs
    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 SegWit transaction
    witness                                       # witness data
)

# Double SHA256 hash of the transaction
tx_hash = hashlib.sha256(hashlib.sha256(final_tx).digest()).digest()

# Constructing the final transaction in hex format
final_tx_hex = final_tx.hex() + tx_hash.hex()

print("Unsigned Transaction Hex:")
print(final_tx_hex)

and result rawtransaction is

0100000001c2087cff2f0c671d4fd70254960638ad0e1fc20b2f914e94fc98a471bf9655ec00000000171600141ee06adc3ff6103b173eef823f8612345d3919d5ffffffff01301b0f00000000001c00141e2361ff02e8b850288e0b3a07f4420743b1deec45d744ced1ad000000003030171600141ee06adc3ff6103b173eef823f8612345d3919d5292a3cc2d63078d6441f02a42edc1be8debddf288fbb836bba289b1b266eb183

and gives rejection
Error validating transaction: witness redeem script detected in tx without witness data.

I got confused fixing the tx without witness data, anyone can help me ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top