<p>I'm trying to add SegWit functionality to my app but am running into trouble building my transaction. I've read <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="nofollow noreferrer">BIP143</a> so many times I virtually have it memorized, and I've followed it to the letter. But for whatever reason my TX is being rejected.</p>

<p>Here's how my transaction is structured:</p>

<p>input #1
AF83F3F0EA5D11C3824C336250325D7597F891384282F73362AF0D7A70A94713, index=1, amount = 1000000</p>

<p>This is a P2WPKH input corresponding to address bc1qe9p5qmlqwhaxtmjkrdfp8lyswyww8ur5pzkg3c</p>

<p>input #2:
E85B128C57AD00E7439D25E1ED1BA18E74886D45E45DEE7F4C196566946D0BC9, index=1, amount = 3571510</p>

<p>This is a standard P2PKH input</p>

<p>output #1:
bc1q5ss6h56zsv56q3w2axwwv2v9zqgajtr5ud5llr, amount = 1071200</p>

<p>output #2:
bc1qj2r7lu7g3tccv4zsg54c08ptgescaxt8u8tfvs, amount = 3471510</p>

<p>I've built my transaction according to the specifications presented in <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="nofollow noreferrer">BIP143</a>. Here's the transaction in hex form:</p>

<p>010000000001021347A9707A0DAF6233F782423891F897755D325062334C82C3115DEAF0F383AF0100000000FFFFFFFFC90B6D946665194C7FEE5DE4456D88748EA11BEDE1259D43E700AD578C125BE8010000006A47304402205A551FC6B4AB3A72C7DB674570B6243DF6ED4F4345682975919E9D5F5E0D13570220783DB84871D62B3FA9A44F6BCA58A62F057E9518E5749EC6C7E889E780A8ED4E01210203DBD9B2CFF11F6C6C3227F4A0AAC6C01EB67B4B9FC0F3782F9951154992CBCBFFFFFFFF026058100000000000160014A421ABD3428329A045CAE99CE629851011D92C7496F83400000000001600149287EFF3C88AF1865450452B879C2B46618E996702483045022100EA9218E8F43A6BECDA0DA899E6AB65B7C0D0BD3D32B92C469098FCAD72665BF4022004499661079B3A3A92A647F3DCC0EDE3932FECC23FAA4B60FFD3648A96D2ADB10121026352C9EF46D168DAE9BA5347B88580F6C2366088250CB986751ACE798E7154990000000000</p>

<p>Now, if you take that transaction and plug it into a deocder like the one <a href="https://live.blockcypher.com/btc/decodetx/]" rel="nofollow noreferrer">here</a>, you'll see that everything is parsed properly. Everything looks good. Still, everyone rejects my transaction. For example:</p>

<p>Pushing through Blockchain.info or using the raw transaction pusher in Electrum yield the following error:</p>

<pre><code>Validation Error: BitcoindException(super=com.neemre.btcdcli4j.core.BitcoindException: Error #-26: 64: non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation), code=-26)
</code></pre>

<p>Pushing through BlockCypher yields a different error:</p>

<pre><code>af83f3f0ea5d11c3824c336250325d7597f891384282f73362af0d7a70a94713 at 1: Script was NOT verified successfully..
</code></pre>

<p>The errors are a bit cryptic but that second error seems to be pointing the finger at my first P2WPKH input. It says the script wasn't verified. The thing is though, P2WPKH inputs don't have a SigScript. By definition, the SigScript is supposed to be empty. So presumably it's referring to my witness which, as you can see if you used the decoder, contains two items: a signature and a public key.</p>

<p>I swear I've checked that signature 100 times. It verifies. Perhaps what I'm signing (the sighash, also described in BIP143) was improperly constructed? But again, I've looked it over and I'm following the 10 step sighash generation procedure precisely.</p>

<p>which brings me back to the error generated by Electrum:</p>

<pre><code>Error #-26: 64: non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation), code=-26)
</code></pre>

<p>I haven't a clue was this means. The only reference to non-mandatory flags that I can find is <a href="https://dev.visucore.com/bitcoin/doxygen/interpreter_8h_source.html" rel="nofollow noreferrer">here</a> and code '64' seems to refer to a constant named SCRIPT_VERIFY_MINIMAL data. There's also code "-26" in the error message. I haven't a clue what that refers to.</p>

<p>I feel like this must be signature related somehow but like I said, the signature verifies just fine. I've made sure that the sig has a "low S value," which has caused me some problems in the past. Not sure where else to look. I'm totally stumped. </p>

<p>Any ideas?</p>

<p><strong>UPDATE:</strong></p>

<p>I figured out the source of my error. See comment below. Sysops can delete this question since it's now resolved.</p>

CC BY-SA 3.0
2018-01-31T19:42:02.947Z
The error is cryptic, but it just means your signature was invalid. Most likely I think is that your sighash is computed incorrectly.
Pieter Wuille
CC BY-SA 3.0
2018-01-31T22:43:47.327Z
Finally figured it out. In step #5 of the BIP 143 sighash creation, a script is appended, the first byte of which is 0x19. I used the .NET/C# function BinaryWriter.Write(0x19) to write that byte. For reasons I don't quite understand, the 0x19 byte was treated as a 32-bit integer, effectively writing 0x00000019 and messing up the binary image. Replacing the line of code with `bw.Write((byte)0x19)` fixed the problem and the transaction now succeeds. Thanks for pointing me in the right direction.
Festus Martingale
CC BY-SA 3.0
2018-02-01T08:28:36.723Z
No please. Do not delete. This is extremely valuable information for other developers, and adds to the quality of the stackexchange here. I was trying to analyze tax, and didn’t spot this. Great that you found it!
pebwindkraft
<transactions><signature><raw-transaction><p2wpkh>
Unable to broadcast native P2WPKH (SegWit) TX; malformed, but how?
Festus Martingale
# Unable to broadcast native P2WPKH (SegWit) TX; malformed, but how?

(Asked by: username_0 on 2018-01-31)

I'm trying to add SegWit functionality to my app but am running into trouble building my transaction. I've read [BIP143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki) so many times I virtually have it memorized, and I've followed it to the letter. But for whatever reason my TX is being rejected.

Here's how my transaction is structured:

input #1
AF83F3F0EA5D11C3824C336250325D7597F891384282F73362AF0D7A70A94713, index=1, amount = 1000000

This is a P2WPKH input corresponding to address bc1qe9p5qmlqwhaxtmjkrdfp8lyswyww8ur5pzkg3c

input #2:
E85B128C57AD00E7439D25E1ED1BA18E74886D45E45DEE7F4C196566946D0BC9, index=1, amount = 3571510

This is a standard P2PKH input

output #1:
bc1q5ss6h56zsv56q3w2axwwv2v9zqgajtr5ud5llr, amount = 1071200

output #2:
bc1qj2r7lu7g3tccv4zsg54c08ptgescaxt8u8tfvs, amount = 3471510

I've built my transaction according to the specifications presented in [BIP143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki). Here's the transaction in hex form:

010000000001021347A9707A0DAF6233F782423891F897755D325062334C82C3115DEAF0F383AF0100000000FFFFFFFFC90B6D946665194C7FEE5DE4456D88748EA11BEDE1259D43E700AD578C125BE8010000006A47304402205A551FC6B4AB3A72C7DB674570B6243DF6ED4F4345682975919E9D5F5E0D13570220783DB84871D62B3FA9A44F6BCA58A62F057E9518E5749EC6C7E889E780A8ED4E01210203DBD9B2CFF11F6C6C3227F4A0AAC6C01EB67B4B9FC0F3782F9951154992CBCBFFFFFFFF026058100000000000160014A421ABD3428329A045CAE99CE629851011D92C7496F83400000000001600149287EFF3C88AF1865450452B879C2B46618E996702483045022100EA9218E8F43A6BECDA0DA899E6AB65B7C0D0BD3D32B92C469098FCAD72665BF4022004499661079B3A3A92A647F3DCC0EDE3932FECC23FAA4B60FFD3648A96D2ADB10121026352C9EF46D168DAE9BA5347B88580F6C2366088250CB986751ACE798E7154990000000000

Now, if you take that transaction and plug it into a deocder like the one [here](https://live.blockcypher.com/btc/decodetx/]), you'll see that everything is parsed properly. Everything looks good. Still, everyone rejects my transaction. For example:

Pushing through Blockchain.info or using the raw transaction pusher in Electrum yield the following error:

```
Validation Error: BitcoindException(super=com.neemre.btcdcli4j.core.BitcoindException: Error #-26: 64: non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation), code=-26)

```

Pushing through BlockCypher yields a different error:

```
af83f3f0ea5d11c3824c336250325d7597f891384282f73362af0d7a70a94713 at 1: Script was NOT verified successfully..

```

The errors are a bit cryptic but that second error seems to be pointing the finger at my first P2WPKH input. It says the script wasn't verified. The thing is though, P2WPKH inputs don't have a SigScript. By definition, the SigScript is supposed to be empty. So presumably it's referring to my witness which, as you can see if you used the decoder, contains two items: a signature and a public key.

I swear I've checked that signature 100 times. It verifies. Perhaps what I'm signing (the sighash, also described in BIP143) was improperly constructed? But again, I've looked it over and I'm following the 10 step sighash generation procedure precisely.

which brings me back to the error generated by Electrum:

```
Error #-26: 64: non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation), code=-26)

```

I haven't a clue was this means. The only reference to non-mandatory flags that I can find is [here](https://dev.visucore.com/bitcoin/doxygen/interpreter_8h_source.html) and code '64' seems to refer to a constant named SCRIPT\_VERIFY\_MINIMAL data. There's also code "-26" in the error message. I haven't a clue what that refers to.

I feel like this must be signature related somehow but like I said, the signature verifies just fine. I've made sure that the sig has a "low S value," which has caused me some problems in the past. Not sure where else to look. I'm totally stumped.

Any ideas?

**UPDATE:**

I figured out the source of my error. See comment below. Sysops can delete this question since it's now resolved.

**Tags:** transactions, signature, raw-transaction, p2wpkh

> **Comments:**
> **username_1**: The error is cryptic, but it just means your signature was invalid. Most likely I think is that your sighash is computed incorrectly.  
> **username_0**: Finally figured it out. In step #5 of the BIP 143 sighash creation, a script is appended, the first byte of which is 0x19. I used the .NET/C# function BinaryWriter.Write(0x19) to write that byte. For reasons I don't quite understand, the 0x19 byte was treated as a 32-bit integer, effectively writing 0x00000019 and messing up the binary image. Replacing the line of code with `bw.Write((byte)0x19)` fixed the problem and the transaction now succeeds. Thanks for pointing me in the right direction.  
> **username_2**: No please. Do not delete. This is extremely valuable information for other developers, and adds to the quality of the stackexchange here. I was trying to analyze tax, and didn’t spot this. Great that you found it!  

---

## 0 answers

