<p>You're missing the sighash flag byte at the end of the signature.</p>
<p>&quot;Signature&quot; objects in Bitcoin Script consist of the actual DER-encoded ECDSA signature, plus the sighash type byte (in your case, 0x01 for SIGHASH_ALL).</p>

CC BY-SA 4.0
2020-10-23T03:25:06.363Z
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
CC BY-SA 4.0
2020-10-23T03:38:18.677Z
That means you signed the wrong sighash, mostly likely.
Pieter Wuille
CC BY-SA 4.0
2020-10-23T14:12:07.073Z
In the end it was because I was using a p2pwkh when creating the sighash instead of the required p2pkh :/.
Kyle
CC BY-SA 4.0
2020-10-23T02:48:36.330Z
2020-10-23T02:48:36.330Z
Pieter Wuille
<p>I'm trying to build a native Segwit transaction programmatically, however, whenever I attempt to broadcast the transaction, it returns this error:
<code>non-mandatory-script-verify-flag (Non-canonical DER signature)</code></p>
<p>My raw, unsigned tx (testnet) is here:</p>
<pre><code>0200000001fd45159c94d5dcf0804e7aba6e5862445a3c41fb5ec21184fe3a41a1a6dbb5b40000000000ffffffff02a351000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a66500000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a600000000
</code></pre>
<p>My signed tx here:</p>
<pre><code>02000000000101fd45159c94d5dcf0804e7aba6e5862445a3c41fb5ec21184fe3a41a1a6dbb5b40000000000ffffffff02a351000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a66500000000000000160014d1ca74222bb5a5c1b43fae117e811b2320a286a6024630440220465c93c725454f7bda3e3453ce09e99f7636560935be9377c3ae1138cfa0bb4702206f45290b2c06fc7e2298c6778fbccd1a805e34535247c49e098bc7229e74533421034ac1bb1bc5fd7a9b173f6a136a40e4be64841c77d7f66ead444e101e0134812700000000
</code></pre>
<p>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:
<code>e54c499bfef6f5f0b12b819de82220aa7de532f9b72ae0f74638a647e147f884</code></p>
<p>This is created using:
<a href="https://docs.rs/bitcoin/0.25.1/bitcoin/util/bip143/struct.SigHashCache.html#method.signature_hash" rel="nofollow noreferrer">https://docs.rs/bitcoin/0.25.1/bitcoin/util/bip143/struct.SigHashCache.html#method.signature_hash</a></p>
<p>using <code>SigHashType::All</code> passing the pubKeyScript and value (22,000)</p>
<p>and when signing the sighash I get:
<code>30440220465c93c725454f7bda3e3453ce09e99f7636560935be9377c3ae1138cfa0bb4702206f45290b2c06fc7e2298c6778fbccd1a805e34535247c49e098bc7229e745334</code></p>
<p>I am completely stuck on what to try next. Any direction would be appreciated.</p>

<transactions><segregated-witness><signature><raw-transaction>
Error: Non-canonical DER signature when attempting to broadcast signed Segwit Transactions
Kyle
# Error: Non-canonical DER signature when attempting to broadcast signed Segwit Transactions

(Asked by: username_0 on 2020-10-23)

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.

**Tags:** transactions, segregated-witness, signature, raw-transaction


---

## 1 answers

### Answer by username_1 on 2020-10-23. Score: 1 | Accepted answer

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).

> **Comments:**
> **username_0**: 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.  
> **username_1**: That means you signed the wrong sighash, mostly likely.  
> **username_0**: In the end it was because I was using a p2pwkh when creating the sighash instead of the required p2pkh :/.  

---

