<p>A few observations</p>
<ol>
<li>You refer to P2SH but the witness data suggests you intend something else
like <a href="https://bitcoin.stackexchange.com/q/107872/13866">P2WSH or P2SH-P2WSH etc</a>?</li>
<li>The witness marker and flag go after the version number.</li>
<li>I suspect you want your marker to be <code>b'\x00\x01</code> not <code>b'00'</code></li>
<li>The locktime goes <em>after</em> any witness data.</li>
<li>You seem to be using <code>input_script</code> in both the input and the witness.</li>
<li>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.</li>
</ol>

CC BY-SA 4.0
2024-03-14T16:24:41.887Z
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
CC BY-SA 4.0
2024-03-14T16:26:55.893Z
can you provide the codes to spend that ?
i build anything on python from scratch, still trying and frustrated.
Tepan
CC BY-SA 4.0
2024-03-14T16:33:55.390Z
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
CC BY-SA 4.0
2024-03-14T16:35:23.967Z
OP_1 751e76e8199196d454941c45d1b3a323f1433bd6 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY
Tepan
CC BY-SA 4.0
2024-03-13T22:09:09.233Z
2024-03-13T22:28:11.967Z
2024-03-13T22:28:11.967Z
RedGrittyBrick
<p>the codes i use to build the transaction</p>
<pre><code>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 &lt;witness_script_hash&gt;

# 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(&quot;Unsigned Transaction Hex:&quot;)
print(final_tx_hex)
</code></pre>
<p>and result rawtransaction is</p>
<blockquote>
<p>0100000001c2087cff2f0c671d4fd70254960638ad0e1fc20b2f914e94fc98a471bf9655ec00000000171600141ee06adc3ff6103b173eef823f8612345d3919d5ffffffff01301b0f00000000001c00141e2361ff02e8b850288e0b3a07f4420743b1deec45d744ced1ad000000003030171600141ee06adc3ff6103b173eef823f8612345d3919d5292a3cc2d63078d6441f02a42edc1be8debddf288fbb836bba289b1b266eb183</p>
</blockquote>
<p>and gives rejection
<code>Error validating transaction: witness redeem script detected in tx without witness data.</code></p>
<p>I got confused fixing the tx without witness data, anyone can help me ?</p>

<p2sh><p2sh-p2wpkh>
HOW TO SPEND THE P2SH only use Redeemscript hex ? no private key involved
Tepan
# HOW TO SPEND THE P2SH only use Redeemscript hex ? no private key involved

(Asked by: username_0 on 2024-03-13)

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 ?

**Tags:** p2sh, p2sh-p2wpkh


---

## 1 answers

### Answer by username_1 on 2024-03-13. Score: 0 | Accepted answer

A few observations

1. You refer to P2SH but the witness data suggests you intend something else
   like [P2WSH or P2SH-P2WSH etc](https://bitcoin.stackexchange.com/q/107872/13866)?
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.

> **Comments:**
> **username_0**: 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.  
> **username_0**: can you provide the codes to spend that ? i build anything on python from scratch, still trying and frustrated.  
> **username_0**: 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  
> **username_0**: OP_1 751e76e8199196d454941c45d1b3a323f1433bd6 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY  

---

