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:
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.
I figured out the source of my error. See comment below. Sysops can delete this question since it's now resolved.