0

I'm trying to add SegWit functionality to my app but am running into trouble building my transaction. I've read BIP143 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. Here's the transaction in hex form:

010000000001021347A9707A0DAF6233F782423891F897755D325062334C82C3115DEAF0F383AF0100000000FFFFFFFFC90B6D946665194C7FEE5DE4456D88748EA11BEDE1259D43E700AD578C125BE8010000006A47304402205A551FC6B4AB3A72C7DB674570B6243DF6ED4F4345682975919E9D5F5E0D13570220783DB84871D62B3FA9A44F6BCA58A62F057E9518E5749EC6C7E889E780A8ED4E01210203DBD9B2CFF11F6C6C3227F4A0AAC6C01EB67B4B9FC0F3782F9951154992CBCBFFFFFFFF026058100000000000160014A421ABD3428329A045CAE99CE629851011D92C7496F83400000000001600149287EFF3C88AF1865450452B879C2B46618E996702483045022100EA9218E8F43A6BECDA0DA899E6AB65B7C0D0BD3D32B92C469098FCAD72665BF4022004499661079B3A3A92A647F3DCC0EDE3932FECC23FAA4B60FFD3648A96D2ADB10121026352C9EF46D168DAE9BA5347B88580F6C2366088250CB986751ACE798E7154990000000000

Now, if you take that transaction and plug it into a deocder like the one here, 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 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.

  • 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 Jan 31 '18 at 19:42
  • 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 Jan 31 '18 at 22:43
  • 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! – pebwindkraft Feb 1 '18 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.