0

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 ?

RedGrittyBrick
  • 26,841
  • 3
  • 25
  • 51
Tepan
  • 3
  • 3

1 Answers1

0

A few observations

  1. You refer to P2SH but the witness data suggests you intend something else like P2WSH or P2SH-P2WSH etc?
  2. The witness marker and flag go after the version number.
  3. I suspect you want your marker to be b'\x00\x01 not b'00'
  4. The locktime goes after any witness data.
  5. You seem to be using input_script in both the input and the witness.
  6. AFAIK, if your sole input is really a P2SH output you shouldn't have any witness markers or other witness fields in the spending transaction.
RedGrittyBrick
  • 26,841
  • 3
  • 25
  • 51
  • P2SH Address: 2NBHpMh4gZNPAn79dxJ6ELm7kUhRYvd61zv Redeem Script: 14751e76e8199196d454941c45d1b3a323f1433bd688 Unlocking Script: 5114751e76e8199196d454941c45d1b3a323f1433bd6 Witness Script: 5114751e76e8199196d454941c45d1b3a323f1433bd614751e76e8199196d454941c45d1b3a323f1433bd688

    can you help me please, to spend that address fund on testnet network, i make that address without involving any private key.

    – Tepan Mar 14 '24 at 16:24
  • can you provide the codes to spend that ? i build anything on python from scratch, still trying and frustrated. – Tepan Mar 14 '24 at 16:26
  • https://siminchen.github.io/bitcoinIDE/build/editor.html# i check the 5114751e76e8199196d454941c45d1b3a323f1433bd614751e76e8199196d454941c45d1b3a323f1433bd688

    it can be push on that stack, it should unlock the UTXO and can spend the fund

    – Tepan Mar 14 '24 at 16:33
  • OP_1 751e76e8199196d454941c45d1b3a323f1433bd6 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY – Tepan Mar 14 '24 at 16:35