0

I'm trying to build a native Segwit transaction programmatically, however, whenever I attempt to broadcast the transaction, it returns this error: non-mandatory-script-verify-flag (Non-canonical DER signature)

My raw, unsigned tx (testnet) is here:

0200000001fd45159c94d5dcf0804e7aba6e5862445a3c41fb5ec21184fe3a41a1a6dbb5b40000000000ffffffff02a351000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a66500000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a600000000

My signed tx here:

02000000000101fd45159c94d5dcf0804e7aba6e5862445a3c41fb5ec21184fe3a41a1a6dbb5b40000000000ffffffff02a351000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a66500000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a6024630440220465c93c725454f7bda3e3453ce09e99f7636560935be9377c3ae1138cfa0bb4702206f45290b2c06fc7e2298c6778fbccd1a805e34535247c49e098bc7229e74533421034ac1bb1bc5fd7a9b173f6a136a40e4be64841c77d7f66ead444e101e0134812700000000

Both decoded look okay to me, so I I think it it's either the sighash or signing of the sighash that's going wrong. The sighash I get of the unsigned tx, for the only input is: e54c499bfef6f5f0b12b819de82220aa7de532f9b72ae0f74638a647e147f884

This is created using: https://docs.rs/bitcoin/0.25.1/bitcoin/util/bip143/struct.SigHashCache.html#method.signature_hash

using SigHashType::All passing the pubKeyScript and value (22,000)

and when signing the sighash I get: 30440220465c93c725454f7bda3e3453ce09e99f7636560935be9377c3ae1138cfa0bb4702206f45290b2c06fc7e2298c6778fbccd1a805e34535247c49e098bc7229e745334

I am completely stuck on what to try next. Any direction would be appreciated.

1

You're missing the sighash flag byte at the end of the signature.

"Signature" objects in Bitcoin Script consist of the actual DER-encoded ECDSA signature, plus the sighash type byte (in your case, 0x01 for SIGHASH_ALL).

| improve this answer | |
  • Thanks for the quick reply. I had noticed that, however, after adding that byte I get Signature must be zero for failed CHECK(MULTI)SIG operation instead. – Kyle Oct 23 at 3:25
  • 1
    That means you signed the wrong sighash, mostly likely. – Pieter Wuille Oct 23 at 3:38
  • In the end it was because I was using a p2pwkh when creating the sighash instead of the required p2pkh :/. – Kyle Oct 23 at 14:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.