<p>I found where I went wrong.</p>
<p>When creating the hashpreimage for the input, I was substituting in the scriptPubKey of the input instead of the redeemScript into the scriptSig.</p>

CC BY-SA 4.0
2021-10-04T10:39:19.453Z
2021-10-04T10:39:19.453Z
Keijyu
<p>I have some funds locked in a testnet P2SH address which requires 2-of-3 signatures.<br>
This is the address: <code>2NEKRqUqoDtsELzpBa5wuWEiJeVbio5cSa2</code><br>
and the 3 keys:</p>
<pre><code>#1 - cU1mPkyNgJ8ceLG5v2zN1VkZcvDCE7VK8KrnHwW82PZb6RCq7zRq
#2 - cPTFNJD7hgbZTqNJgW89HABGtRzYo5aLpCQKvmNdtRNGWo49NAky
#3 - cNUe2L9CNJZoedMU8YNrzRuxFc56dvMjFxzK4mTsSGhXwbidAyog
</code></pre>
<p>The multisig script is:
<code>OP_2 &lt;Key #1 length&gt; &lt;Key #1 PubKey&gt; &lt;Key #2 length&gt; &lt;Key #2 PubKey&gt; &lt;Key #3 length&gt; &lt;Key #3 PubKey&gt; OP_3 OP_CHECKMULTISIG</code></p>
<p>I have also created a transaction spending the funds in this address, however I cannot broadcast this transaction because it gives the error <code>Mempool accept test failed: 16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)</code>.</p>
<p>Here is the transaction:</p>
<pre><code>01000000  //Version
01        //Input Count
e29cf4dad1c36d5e316bac18edfc665f8323050d7260b94b72777b12a4eecba0  //TXID
00000000  //Out index
fb        //ScriptSig size
0046304302202848f7f4087da2e01adcaca2da30fe9004861ca284efffcae6b49d2cf9f239b2021f7794619a597f5847e92fef4bdebeba42b91544d267220bfe8e2824f3ef74190147304402201dd2ba26a81ac13da8ecd5d967f9394ec0c5356e6d303adeb71f84bd2ee62d0202203632bfc457d778a71ca72007cce9d0e6f92a1bf8226e010fc9e59e0572afe454014c695221034d0953ff49ed8e98cce462e9ecd3ead6cf641b299ec500329dc153443d4cced721024be733c116a4ca8bd7dd2aafa6fb6877d153962e0f6a21709fab771ce6572b4821038c6a2191a2e77ede17063d8633fb9e08dba8535fd1dd99377b7dc9b6bea89efe53ae     //ScriptSig (2 signatures followed by the redeem script)
ffffffff         //Sequence
01               //Output count
204e000000000000 //Output Value
16               //ScriptPubKey size
001491c6c31a62b37a16447ccf44224eb1ab78ca0299  //scriptPubKey (Segwit P2PKH)
00000000         //Locktime
</code></pre>
<p>Here are the steps I took to sign the input:</p>
<pre><code>1. Hash the input with the scriptPubKey as the scriptSig and SigHash ALL appended
2. Sign the hashed signature with Key #1 and Key #2
3. Construct the scriptSig by concatenating the 2 signatures and redeem script
4. Place the scriptSig in the scriptSig field of the input
</code></pre>
<p>Im not sure if I am missing a step or have done a step incorrectly. I did pretty much the same steps as signing a P2PKH input which I can successfully create, just with different scriptSigs.</p>
<p>Any help would be appreciated.</p>

CC BY-SA 4.0
2021-10-04T01:11:29.327Z
The signature verification seems to have failed. The calculation of the sighash used for the signature may be wrong.
BTW, `e29cf4dad1c36d5e316bac18edfc665f8323050d7260b94b72777b12a4eecba0` is tx hash, not TXID. If the endianness is reversed, it becomes TXID.
Shigeyuki Azuchi
CC BY-SA 4.0
2021-10-04T01:51:45.757Z
I found where I went wrong. 

When creating the hashpreimage for the input, I was substituting in the scriptPubKey of the input instead of the redeemScript.
When I used the redeemScript instead of the scriptPubKey, the signature was correct.
Keijyu
CC BY-SA 4.0
2021-10-04T09:01:22.597Z
@Keijyu: You could add your last comment as an answer in the "Your Answer" section below - if you think it might help others in future, or delete the question if not.
RedGrittyBrick
<multi-signature><p2sh><transaction-input>
P2SH-Multisig transaction not going through
Keijyu
# P2SH-Multisig transaction not going through

(Asked by: username_0 on 2021-10-03)

I have some funds locked in a testnet P2SH address which requires 2-of-3 signatures.  
This is the address: `2NEKRqUqoDtsELzpBa5wuWEiJeVbio5cSa2`  
and the 3 keys:

```
#1 - cU1mPkyNgJ8ceLG5v2zN1VkZcvDCE7VK8KrnHwW82PZb6RCq7zRq
#2 - cPTFNJD7hgbZTqNJgW89HABGtRzYo5aLpCQKvmNdtRNGWo49NAky
#3 - cNUe2L9CNJZoedMU8YNrzRuxFc56dvMjFxzK4mTsSGhXwbidAyog

```

The multisig script is:
`OP_2 <Key #1 length> <Key #1 PubKey> <Key #2 length> <Key #2 PubKey> <Key #3 length> <Key #3 PubKey> OP_3 OP_CHECKMULTISIG`

I have also created a transaction spending the funds in this address, however I cannot broadcast this transaction because it gives the error `Mempool accept test failed: 16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)`.

Here is the transaction:

```
01000000  //Version
01        //Input Count
e29cf4dad1c36d5e316bac18edfc665f8323050d7260b94b72777b12a4eecba0  //TXID
00000000  //Out index
fb        //ScriptSig size
0046304302202848f7f4087da2e01adcaca2da30fe9004861ca284efffcae6b49d2cf9f239b2021f7794619a597f5847e92fef4bdebeba42b91544d267220bfe8e2824f3ef74190147304402201dd2ba26a81ac13da8ecd5d967f9394ec0c5356e6d303adeb71f84bd2ee62d0202203632bfc457d778a71ca72007cce9d0e6f92a1bf8226e010fc9e59e0572afe454014c695221034d0953ff49ed8e98cce462e9ecd3ead6cf641b299ec500329dc153443d4cced721024be733c116a4ca8bd7dd2aafa6fb6877d153962e0f6a21709fab771ce6572b4821038c6a2191a2e77ede17063d8633fb9e08dba8535fd1dd99377b7dc9b6bea89efe53ae     //ScriptSig (2 signatures followed by the redeem script)
ffffffff         //Sequence
01               //Output count
204e000000000000 //Output Value
16               //ScriptPubKey size
001491c6c31a62b37a16447ccf44224eb1ab78ca0299  //scriptPubKey (Segwit P2PKH)
00000000         //Locktime

```

Here are the steps I took to sign the input:

```
1. Hash the input with the scriptPubKey as the scriptSig and SigHash ALL appended
2. Sign the hashed signature with Key #1 and Key #2
3. Construct the scriptSig by concatenating the 2 signatures and redeem script
4. Place the scriptSig in the scriptSig field of the input

```

Im not sure if I am missing a step or have done a step incorrectly. I did pretty much the same steps as signing a P2PKH input which I can successfully create, just with different scriptSigs.

Any help would be appreciated.

**Tags:** multi-signature, p2sh, transaction-input

> **Comments:**
> **username_1**: The signature verification seems to have failed. The calculation of the sighash used for the signature may be wrong. BTW, `e29cf4dad1c36d5e316bac18edfc665f8323050d7260b94b72777b12a4eecba0` is tx hash, not TXID. If the endianness is reversed, it becomes TXID.  
> **username_0**: I found where I went wrong.   When creating the hashpreimage for the input, I was substituting in the scriptPubKey of the input instead of the redeemScript. When I used the redeemScript instead of the scriptPubKey, the signature was correct.  
> **username_2**: @username_0: You could add your last comment as an answer in the "Your Answer" section below - if you think it might help others in future, or delete the question if not.  

---

## 1 answers

### Answer by username_0 on 2021-10-04. Score: 1 | Accepted answer

I found where I went wrong.

When creating the hashpreimage for the input, I was substituting in the scriptPubKey of the input instead of the redeemScript into the scriptSig.


---

