<blockquote>
  <p>and other input(s) should have "00" in place.</p>
</blockquote>

<p>Keep in mind that you replace the "scriptsig" with "empty" and the <code>0x00</code> is the size of the empty script.</p>

<blockquote>
  <p>non-mandatory-script-verify-flag (Non-canonical DER signature) (code 64)</p>
</blockquote>

<p>The problem here is the DER encoding used in your signatures. <code>r</code> values in a signature are positive and since each <code>r</code> value that you have has its most significant bit set they need a preceding <code>0x00</code> to tell the DER-decoder that the numbers are positive:  </p>

<pre><code>0xE4 = 0b11100100 
0xC4 = 0b11000100 
</code></pre>

<p>Each signature should change to this: <code>483045022100E4A3...</code></p>

CC BY-SA 4.0
2019-12-07T16:47:13.580Z
You are right!!! I was going nuts over transaction preparing while the issue was from ECDSA lib side. Thank you mate!
Furqan Siddiqui
CC BY-SA 4.0
2019-12-07T13:38:00.900Z
2019-12-07T13:38:00.900Z
Coding Enthusiast
<p>So I am creating a simple P2PKH transaction and after I researched about this found few useful posts and answers here; I was able to successfully spend a a single P2PKH UTXO but it is really getting complicated when I try to spend transaction with multiple inputs. I have spent a day entirely on this.</p>

<p>All transactions are on Bitcoin Testnet.</p>

<p>Outputs I am redeeming:</p>

<blockquote>
  <ol>
  <li><p>Tx Hash: 9742ed783bfec215ec6484d82dae20fba582229e54c49e06d662a91ead3f6a54
  Index: 0
  scriptPubKey: OP_DUP OP_HASH160 81f52d4061313b6f63549efc03f3df6cb6f0149e OP_EQUALVERIFY OP_CHECKSIG</p></li>
  <li><p>Tx Hash: 40dbd3c25073cb7222becead1eb9bb2195cf309cf5b24af559f3391168ec6318
  Index: 0
  scriptPubKey: OP_DUP OP_HASH160 81f52d4061313b6f63549efc03f3df6cb6f0149e OP_EQUALVERIFY OP_CHECKSIG</p></li>
  </ol>
</blockquote>

<p>Now I read an answer from here that suggested if using multiple inputs, one should replace scriptSig for rest of inputs (the ones not being signed) to empty;  And I have tried these variations so far:</p>

<p><strong>1. Replacing scriptSig for inputs with "00"</strong></p>

<p>So only the input (UTXO) that is being signed will have its scriptPubKey, and other input(s) should have "00" in place.</p>

<blockquote>
  <p><strong>Serialized input #1 when signed:</strong><br>
  01000000<br>
  02<br>
  546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297<br>
  00000000<br>
  19<br>
  76a91481F52D4061313B6F63549EFC03F3DF6CB6F0149E88ac<br>
  ffffffff<br>
  1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40<br>
  00000000<br>
  00<br>
  ffffffff<br>
  01<br>
  c0fb390000000000<br>
  19<br>
  76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac<br>
  00000000<br>
  01000000</p>
</blockquote>

<p>and...</p>

<blockquote>
  <p><strong>Serialized input #2 when signed:</strong><br>
  01000000<br>
  02<br>
  546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297<br>
  00000000<br>
  00<br>
  ffffffff<br>
  1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40<br>
  00000000<br>
  19<br>
  76a914A17C43FE0E1F8E660B044C9538E2CEF4ABFDCED288ac<br>
  ffffffff<br>
  01<br>
  c0fb390000000000<br>
  19<br>
  76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac<br>
  00000000<br>
  01000000  </p>
</blockquote>

<p>main transaction with both inputs signed: (signatures without 01 hash code type suffix and public keys are bold)</p>

<blockquote>
  <p>01000000<br>
  02<br>
  546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297<br>
  00000000<br>
  6a<br>
  <strong>4730440220E4A366646391B3CFB06C0C4B0343E678B560BE8A2D20418233DEE863986105E502207245C5BA3DB516E59B95764C1716F5B39850E20429F08B91389C4780CE45430E</strong>0121<strong>025F69830D2BA35D04CA9EFB3EA46AA2645BBBDCB592A189A539480657C9696137</strong><br>
  ffffffff<br>
  1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40<br>
  00000000<br>
  6a<br>
  <strong>4730440220C41C498D0CA55E38FE85DD158FDE82BB451068124E2B3AA2391D861BA9281EDD0220165712ADF71B19DF525583D8F48B82DFF06B2E556C17A583F4D5C54E99412C6B</strong>0121<strong>039F9AF4A84A8D5C35DD7A5628F8C2DC1894C2B4BDD4815DAFFDCBC9102FE2BA26</strong><br>
  ffffffff<br>
  01<br>
  c0fb390000000000<br>
  19<br>
  76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac<br>
  00000000  </p>
</blockquote>

<p>now decode raw transaction works but when trying to broadcast it, bitcoind gives these errors:</p>

<blockquote>
  <p>mandatory-script-verify-flag-failed (Signature must be zero for failed
  CHECK(MULTI)SIG operation) (code 16)</p>
</blockquote>

<p>or</p>

<blockquote>
  <p>non-mandatory-script-verify-flag (Non-canonical DER signature) (code
  64)</p>
</blockquote>

<p>I have tried different variants:</p>

<ol>
<li>Replace scriptPubKey of inputs that are not being signed with just "00"</li>
<li>Replace scriptPubKey of inputs that are not being signed with just ""</li>
<li>Remove sequence number from inputs "ffffff"</li>
</ol>

<p>but nothing seems to work. What am I doing wrong? Your help will be appreciated :)</p>

<transactions><signature><raw-transaction><signrawtransaction>
Signing a raw transaction with multiple inputs
Furqan Siddiqui
# Signing a raw transaction with multiple inputs

(Asked by: username_0 on 2019-12-07)

So I am creating a simple P2PKH transaction and after I researched about this found few useful posts and answers here; I was able to successfully spend a a single P2PKH UTXO but it is really getting complicated when I try to spend transaction with multiple inputs. I have spent a day entirely on this.

All transactions are on Bitcoin Testnet.

Outputs I am redeeming:

> 1. Tx Hash: 9742ed783bfec215ec6484d82dae20fba582229e54c49e06d662a91ead3f6a54
>    Index: 0
>    scriptPubKey: OP\_DUP OP\_HASH160 81f52d4061313b6f63549efc03f3df6cb6f0149e OP\_EQUALVERIFY OP\_CHECKSIG
> 2. Tx Hash: 40dbd3c25073cb7222becead1eb9bb2195cf309cf5b24af559f3391168ec6318
>    Index: 0
>    scriptPubKey: OP\_DUP OP\_HASH160 81f52d4061313b6f63549efc03f3df6cb6f0149e OP\_EQUALVERIFY OP\_CHECKSIG

Now I read an answer from here that suggested if using multiple inputs, one should replace scriptSig for rest of inputs (the ones not being signed) to empty; And I have tried these variations so far:

**1. Replacing scriptSig for inputs with "00"**

So only the input (UTXO) that is being signed will have its scriptPubKey, and other input(s) should have "00" in place.

> **Serialized input #1 when signed:**  
> 01000000  
> 02  
> 546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297  
> 00000000  
> 19  
> 76a91481F52D4061313B6F63549EFC03F3DF6CB6F0149E88ac  
> ffffffff  
> 1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40  
> 00000000  
> 00  
> ffffffff  
> 01  
> c0fb390000000000  
> 19  
> 76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac  
> 00000000  
> 01000000

and...

> **Serialized input #2 when signed:**  
> 01000000  
> 02  
> 546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297  
> 00000000  
> 00  
> ffffffff  
> 1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40  
> 00000000  
> 19  
> 76a914A17C43FE0E1F8E660B044C9538E2CEF4ABFDCED288ac  
> ffffffff  
> 01  
> c0fb390000000000  
> 19  
> 76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac  
> 00000000  
> 01000000

main transaction with both inputs signed: (signatures without 01 hash code type suffix and public keys are bold)

> 01000000  
> 02  
> 546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297  
> 00000000  
> 6a  
> **4730440220E4A366646391B3CFB06C0C4B0343E678B560BE8A2D20418233DEE863986105E502207245C5BA3DB516E59B95764C1716F5B39850E20429F08B91389C4780CE45430E**0121**025F69830D2BA35D04CA9EFB3EA46AA2645BBBDCB592A189A539480657C9696137**  
> ffffffff  
> 1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40  
> 00000000  
> 6a  
> **4730440220C41C498D0CA55E38FE85DD158FDE82BB451068124E2B3AA2391D861BA9281EDD0220165712ADF71B19DF525583D8F48B82DFF06B2E556C17A583F4D5C54E99412C6B**0121**039F9AF4A84A8D5C35DD7A5628F8C2DC1894C2B4BDD4815DAFFDCBC9102FE2BA26**  
> ffffffff  
> 01  
> c0fb390000000000  
> 19  
> 76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac  
> 00000000

now decode raw transaction works but when trying to broadcast it, bitcoind gives these errors:

> mandatory-script-verify-flag-failed (Signature must be zero for failed
> CHECK(MULTI)SIG operation) (code 16)

or

> non-mandatory-script-verify-flag (Non-canonical DER signature) (code
> 64)

I have tried different variants:

1. Replace scriptPubKey of inputs that are not being signed with just "00"
2. Replace scriptPubKey of inputs that are not being signed with just ""
3. Remove sequence number from inputs "ffffff"

but nothing seems to work. What am I doing wrong? Your help will be appreciated :)

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


---

## 1 answers

### Answer by username_1 on 2019-12-07. Score: 3 | Accepted answer

> and other input(s) should have "00" in place.

Keep in mind that you replace the "scriptsig" with "empty" and the `0x00` is the size of the empty script.

> non-mandatory-script-verify-flag (Non-canonical DER signature) (code 64)

The problem here is the DER encoding used in your signatures. `r` values in a signature are positive and since each `r` value that you have has its most significant bit set they need a preceding `0x00` to tell the DER-decoder that the numbers are positive:

```
0xE4 = 0b11100100 
0xC4 = 0b11000100 

```

Each signature should change to this: `483045022100E4A3...`

> **Comments:**
> **username_0**: You are right!!! I was going nuts over transaction preparing while the issue was from ECDSA lib side. Thank you mate!  

---

