<p>I try to sign the following transaction:</p>
<pre><code>020000000001012ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d727980100000000ffffffff01e803000000000000160014c221f41ace22b7272a0ab3289bb2df3c36a9e0250000000000
</code></pre>
<p>Parsed:</p>
<pre><code>{
&quot;addresses&quot;: [
    &quot;tb1q4rmg2ah0f5skv9yg43kwzgp457430navtmvwv6&quot;,
    &quot;tb1qcgslgxkwy2mjw2s2kv5fhvkl8sm2ncp9xanaux&quot;
],
&quot;block_height&quot;: -1,
&quot;block_index&quot;: -1,
&quot;confirmations&quot;: 0,
&quot;double_spend&quot;: false,
&quot;fees&quot;: 0,
&quot;hash&quot;: &quot;01071bbceb3ab921873325bfadef0382fba0e6a74709f96cc4f8799af635cab3&quot;,
&quot;inputs&quot;: [
    {
        &quot;addresses&quot;: [
            &quot;tb1q4rmg2ah0f5skv9yg43kwzgp457430navtmvwv6&quot;
        ],
        &quot;age&quot;: 2868502,
        &quot;output_index&quot;: 1,
        &quot;output_value&quot;: 1000,
        &quot;prev_hash&quot;: &quot;9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c&quot;,
        &quot;script_type&quot;: &quot;pay-to-witness-pubkey-hash&quot;,
        &quot;sequence&quot;: 4294967295
    }
],
&quot;outputs&quot;: [
    {
        &quot;addresses&quot;: [
            &quot;tb1qcgslgxkwy2mjw2s2kv5fhvkl8sm2ncp9xanaux&quot;
        ],
        &quot;script&quot;: &quot;0014c221f41ace22b7272a0ab3289bb2df3c36a9e025&quot;,
        &quot;script_type&quot;: &quot;pay-to-witness-pubkey-hash&quot;,
        &quot;value&quot;: 1000
    }
],
&quot;preference&quot;: &quot;low&quot;,
&quot;received&quot;: &quot;2024-07-22T11:23:44.915543755Z&quot;,
&quot;relayed_by&quot;: &quot;44.192.67.93&quot;,
&quot;size&quot;: 85,
&quot;total&quot;: 1000,
&quot;ver&quot;: 2,
&quot;vin_sz&quot;: 1,
&quot;vout_sz&quot;: 1,
&quot;vsize&quot;: 83
</code></pre>
<p>}</p>
<p>I use the following input (index 1): <a href="https://blockstream.info/testnet/tx/9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c?expand" rel="nofollow noreferrer">https://blockstream.info/testnet/tx/9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c?expand</a></p>
<p>According to this doc (<a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#user-content-Native_P2WPKH" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#user-content-Native_P2WPKH</a>), the pre-image hash is:</p>
<pre><code>Double SHA256 of the serialization of:
 1. nVersion of the transaction (4-byte little endian)
 2. hashPrevouts (32-byte hash)
 3. hashSequence (32-byte hash)
 4. outpoint (32-byte hash + 4-byte little endian) 
 5. scriptCode of the input (serialized as scripts inside CTxOuts)
 6. value of the output spent by this input (8-byte little endian)
 7. nSequence of the input (4-byte little endian)
 8. hashOutputs (32-byte hash)
 9. nLocktime of the transaction (4-byte little endian)
10. sighash type of the signature (4-byte little endian)
</code></pre>
<p>My pre-image before double sha256: <code>0200000032a17c92a47c5d4be8d6238132c848017f59ddacdac33b6c8d3c3ac4508a64ef3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e706650442ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d72798010000001976a914a8f68576ef4d21661488ac6ce12035a7ab17cfac88ace803000000000000ffffffff39faa805af04ff44720fa4e3de5476ee3cb833676531f48ad8ebf1e6d3001fc20000000001000000</code></p>
<p>After the signature: <code>020000000001012ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d727980100000000ffffffff01e803000000000000160014c221f41ace22b7272a0ab3289bb2df3c36a9e025024830450221008dab2d55de0b7862b5f2f4bca0d8517915fad356ac38ae1f802034e6e9ec15aa02200f84f0f9e1a239a4bbd1bde5c3d8b0900d91ed0af9fd666922a794c182b658fb01210318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd70300000000</code></p>
<p>Public Key: <code>0418e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd703cd7cd3767ad5330d27ef4db83adb02554decc2ed45fd7a16aa0cd663384693a7</code></p>
<p>Compressed Public Key: <code>0318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd703</code></p>
<p>when I try to broadcast it, I get: <code>error code: -25 error message: bad-txns-inputs-missingorspent</code></p>
<p>my questions are:</p>
<ol>
<li><p>What should be the output script 76a914 output_address 88ac, or 0014 output_address? Why does this doc describe the legacy format of the output <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#user-content-Native_P2WPKH" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#user-content-Native_P2WPKH</a> ?</p>
</li>
<li><p>Is my pre-image correct?</p>
</li>
<li><p>is my segwit script for the input correct?</p>
<p>{
&quot;stackitems&quot;: &quot;02&quot;,
&quot;0&quot;: {
&quot;size&quot;: &quot;48&quot;,
&quot;item&quot;: &quot;30450221008dab2d55de0b7862b5f2f4bca0d8517915fad356ac38ae1f802034e6e9ec15aa02200f84f0f9e1a239a4bbd1bde5c3d8b0900d91ed0af9fd666922a794c182b658fb01&quot;
},
&quot;1&quot;: {
&quot;size&quot;: &quot;21&quot;,
&quot;item&quot;: &quot;0318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd703&quot;
}</p>
</li>
<li><p>What type of public key must be used for address generation and segwit? Full or compressed?</p>
</li>
<li><p>What else can be wrong here? :)</p>
</li>
</ol>
<p><strong>Update:</strong></p>
<p>I reduced the output amount:
Now pre-image is: <code>0200000032a17c92a47c5d4be8d6238132c848017f59ddacdac33b6c8d3c3ac4508a64ef3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e706650442ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d72798010000001976a914a8f68576ef4d21661488ac6ce12035a7ab17cfac88ace803000000000000ffffffff9ad50ca3ecd096afa146edd608c66350fd1546c36876d728004cc40428b474250000000001000000</code></p>
<p>The final transaction is: <code>020000000001012ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d727980100000000ffffffff019001000000000000160014c221f41ace22b7272a0ab3289bb2df3c36a9e02502483045022100d13d1f8ec58cf0df2963986f36977bd7d12304bc69d375c8f02d959b23261f3702206d2e7f08eb3f67cb517a0ae9515ff22d676b944cf564d40ab54df6410b92a5c101210318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd70300000000</code></p>
<p>And the error is: <code>{&quot;code&quot;:-26,&quot;message&quot;:&quot;non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation)&quot;}</code></p>
<ol start="6">
<li>How can I verify the signature?</li>
</ol>

CC BY-SA 4.0
2024-07-22T12:03:24.747Z
How are you broadcasting it? Are you sure the node broadcasting it is on the right network (testnet), and is fully synchronized? The error indicates that the UTXO being spent does not exist, not a problem with the signature or witness.
Pieter Wuille
CC BY-SA 4.0
2024-07-22T13:02:55.063Z
I use bitcoin-cli -testnet sendrawtransaction.
WWTLF
CC BY-SA 4.0
2024-07-22T13:06:11.650Z
Is the node you're sending the transaction to fully synchronized? If it hasn't synchronized to the point where the UTXO you're spending (9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c:1) is created, you would see this error.
Pieter Wuille
CC BY-SA 4.0
2024-07-22T13:10:25.237Z
Now I faced another error: {"code":-26,"message":"non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation)"}
WWTLF
CC BY-SA 4.0
2024-07-22T13:27:26.193Z
Ok that last error means the signature is actually invalid (or computed over the wrong sighash).
Pieter Wuille
<transactions><segregated-witness><bip143>
Manual Bitcoin transaction assembly: which output type should be used and how to compose a segwit field?
WWTLF
# Manual Bitcoin transaction assembly: which output type should be used and how to compose a segwit field?

(Asked by: username_0 on 2024-07-22)

I try to sign the following transaction:
020000000001012ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d727980100000000ffffffff01e803000000000000160014c221f41ace22b7272a0ab3289bb2df3c36a9e0250000000000

Parsed:
{
"addresses": [
    "tb1q4rmg2ah0f5skv9yg43kwzgp457430navtmvwv6",
    "tb1qcgslgxkwy2mjw2s2kv5fhvkl8sm2ncp9xanaux"
],
"block_height": -1,
"block_index": -1,
"confirmations": 0,
"double_spend": false,
"fees": 0,
"hash": "01071bbceb3ab921873325bfadef0382fba0e6a74709f96cc4f8799af635cab3",
"inputs": [
    {
        "addresses": [
            "tb1q4rmg2ah0f5skv9yg43kwzgp457430navtmvwv6"
        ],
        "age": 2868502,
        "output_index": 1,
        "output_value": 1000,
        "prev_hash": "9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c",
        "script_type": "pay-to-witness-pubkey-hash",
        "sequence": 4294967295
    }
],
"outputs": [
    {
        "addresses": [
            "tb1qcgslgxkwy2mjw2s2kv5fhvkl8sm2ncp9xanaux"
        ],
        "script": "0014c221f41ace22b7272a0ab3289bb2df3c36a9e025",
        "script_type": "pay-to-witness-pubkey-hash",
        "value": 1000
    }
],
"preference": "low",
"received": "2024-07-22T11:23:44.915543755Z",
"relayed_by": "44.192.67.93",
"size": 85,
"total": 1000,
"ver": 2,
"vin_sz": 1,
"vout_sz": 1,
"vsize": 83

}
I use the following input (index 1): https://blockstream.info/testnet/tx/9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c?expand
According to this doc (https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#user-content-Native_P2WPKH), the pre-image hash is:
Double SHA256 of the serialization of:
 1. nVersion of the transaction (4-byte little endian)
 2. hashPrevouts (32-byte hash)
 3. hashSequence (32-byte hash)
 4. outpoint (32-byte hash + 4-byte little endian) 
 5. scriptCode of the input (serialized as scripts inside CTxOuts)
 6. value of the output spent by this input (8-byte little endian)
 7. nSequence of the input (4-byte little endian)
 8. hashOutputs (32-byte hash)
 9. nLocktime of the transaction (4-byte little endian)
10. sighash type of the signature (4-byte little endian)

My pre-image before double sha256: 0200000032a17c92a47c5d4be8d6238132c848017f59ddacdac33b6c8d3c3ac4508a64ef3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e706650442ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d72798010000001976a914a8f68576ef4d21661488ac6ce12035a7ab17cfac88ace803000000000000ffffffff39faa805af04ff44720fa4e3de5476ee3cb833676531f48ad8ebf1e6d3001fc20000000001000000
After the signature: 020000000001012ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d727980100000000ffffffff01e803000000000000160014c221f41ace22b7272a0ab3289bb2df3c36a9e025024830450221008dab2d55de0b7862b5f2f4bca0d8517915fad356ac38ae1f802034e6e9ec15aa02200f84f0f9e1a239a4bbd1bde5c3d8b0900d91ed0af9fd666922a794c182b658fb01210318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd70300000000
Public Key: 0418e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd703cd7cd3767ad5330d27ef4db83adb02554decc2ed45fd7a16aa0cd663384693a7
Compressed Public Key: 0318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd703
when I try to broadcast it, I get: error code: -25 error message: bad-txns-inputs-missingorspent
my questions are:

What should be the output script 76a914 output_address 88ac, or 0014 output_address? Why does this doc describe the legacy format of the output https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#user-content-Native_P2WPKH ?

Is my pre-image correct?

is my segwit script for the input correct?
{
"stackitems": "02",
"0": {
"size": "48",
"item": "30450221008dab2d55de0b7862b5f2f4bca0d8517915fad356ac38ae1f802034e6e9ec15aa02200f84f0f9e1a239a4bbd1bde5c3d8b0900d91ed0af9fd666922a794c182b658fb01"
},
"1": {
"size": "21",
"item": "0318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd703"
}

What type of public key must be used for address generation and segwit? Full or compressed?

What else can be wrong here? :)


Update:
I reduced the output amount:
Now pre-image is: 0200000032a17c92a47c5d4be8d6238132c848017f59ddacdac33b6c8d3c3ac4508a64ef3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e706650442ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d72798010000001976a914a8f68576ef4d21661488ac6ce12035a7ab17cfac88ace803000000000000ffffffff9ad50ca3ecd096afa146edd608c66350fd1546c36876d728004cc40428b474250000000001000000
The final transaction is: 020000000001012ca7795bc5ca18f0fd5a1ffb27f31d693b813f6b73b0fd31f4868f88f2d727980100000000ffffffff019001000000000000160014c221f41ace22b7272a0ab3289bb2df3c36a9e02502483045022100d13d1f8ec58cf0df2963986f36977bd7d12304bc69d375c8f02d959b23261f3702206d2e7f08eb3f67cb517a0ae9515ff22d676b944cf564d40ab54df6410b92a5c101210318e6317e8cb99d5be4fe863a63c9a365f58361f9196448c569a8ee3802ddd70300000000
And the error is: {"code":-26,"message":"non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation)"}

How can I verify the signature?

**Tags:** transactions, segregated-witness, bip143

> **Comments:**
> **username_1**: How are you broadcasting it? Are you sure the node broadcasting it is on the right network (testnet), and is fully synchronized? The error indicates that the UTXO being spent does not exist, not a problem with the signature or witness.  
> **username_0**: I use bitcoin-cli -testnet sendrawtransaction.  
> **username_1**: Is the node you're sending the transaction to fully synchronized? If it hasn't synchronized to the point where the UTXO you're spending (9827d7f2888f86f431fdb0736b3f813b691df327fb1f5afdf018cac55b79a72c:1) is created, you would see this error.  
> **username_0**: Now I faced another error: {"code":-26,"message":"non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation)"}  
> **username_1**: Ok that last error means the signature is actually invalid (or computed over the wrong sighash).  

---

## 0 answers

