<p>For P2SH transactions, the redeemScript is what is put in the scriptSig for the transaction preimage, not the scriptPubKey of the output.</p>

CC BY-SA 3.0
2017-10-09T19:30:18.767Z
I still cannot make it work. The redeemScript that hashes to "37fe3f20433380be4c742c978d3a9712c509656e" is "512102930a11e92103daefde0d30b552f57d303e94a128e763ca9e69ff2006446934442103e74d2113dec75d75cde09a5b46297b1067e4b8b35e63c4c32b8cdbadfdffda1e2103067fcc39ee36d2417684511d1055fdc7d35e54911cb9de9ae30c988b666f675c2102dfb1c2a1c3456c8cb76714706dba77b3f4e7fe5afffc2503b121323a48ebbdcf54ae", with a length of "8b".
Bjarne Magnussen
CC BY-SA 3.0
2017-10-09T19:31:24.393Z
My new preimage is as above but changning subScript to:
"4c8b512102930a11e92103daefde0d30b552f57d303e94a128e763ca9e69ff2006446934442103e74d2113dec75d75cde09a5b46297b1067e4b8b35e63c4c32b8cdbadfdffda1e2103067fcc39ee36d2417684511d1055fdc7d35e54911cb9de9ae30c988b666f675c2102dfb1c2a1c3456c8cb76714706dba77b3f4e7fe5afffc2503b121323a48ebbdcf54ae"
Bjarne Magnussen
CC BY-SA 3.0
2017-10-09T19:31:58.157Z
But I still get the same error messages and when using the valid signature from bitcoin core cannot validate it using this preimage.
Bjarne Magnussen
CC BY-SA 3.0
2017-10-09T19:39:11.150Z
What is the exact error that you get when you try to send the transaction?
Ava Chow
CC BY-SA 3.0
2017-10-10T03:07:51.873Z
@Inaki It will be useful if you can provide the whole serialized transaction.
sr_gi
CC BY-SA 3.0
2017-10-10T14:33:02.167Z
Thanks for offering me help! I have created a txt-message here:
https://pastebin.com/raw/21ucHYW7
I have also tried to take a P2SH from the blockchain that is being spent and try to recreate the preimage manually to verify the provided signatures. But I can also not get that to work. If someone could make a step-by-step showing how the signature is calculated for any P2SH output being spend that this would also help me a lot!
Bjarne Magnussen
CC BY-SA 3.0
2017-10-10T15:00:36.720Z
After checking it for a hundred times I saw that in the pastebin I uploaded I forgot to add the OP_0 (0x00) in the front of the scriptSig. After adding this it works! Thanks a lot for pointing out that the redeemScript is used in the preimage! This was the final signed transaction:
Bjarne Magnussen
CC BY-SA 3.0
2017-10-10T15:05:06.520Z
0100000001bdec74e1a37efc77324bb81d2375c5af090ca02debedb7af18fa1e85756d7c2601000000d60047304402204887a352b503da6f212c09ed7e973384d7660f62d2319a15b968252012d134810220695130bc101f44348203c51a50e46f37ed4bc5622795f5dfbddef2affb27e2a8014c8b512102930a11e92103daefde0d30b552f57d303e94a128e763ca9e69ff2006446934442103e74d2113dec75d75cde09a5b46297b1067e4b8b35e63c4c32b8cdbadfdffda1e2103067fcc39ee36d2417684511d1055fdc7d35e54911cb9de9ae30c988b666f675c2102dfb1c2a1c3456c8cb76714706dba77b3f4e7fe5afffc2503b121323a48ebbdcf54aeffffffff01c05d00000000000017a914de5462e6e84cdab5064220343b9331a3af6dbbf18700000000
Bjarne Magnussen
CC BY-SA 3.0
2017-10-09T18:56:04.307Z
2017-10-09T18:56:04.307Z
Ava Chow
<p>I am trying to sign a raw transaction that spends output #2 (txindex 1) from this transaction: <code>267c6d75851efa18afb7edeb2da00c09afc575231db84b3277fc7ea3e174ecbd</code></p>

<p>The output is a 1-of-4 multisig P2SH and I think I may have a mistake in the transaction structure used to generate the signature. The signature I calculate is different (as in cryptographically different) to the one that bitcoin core gives me using signrawtransaction.</p>

<p>As I see it this is the transaction structure which must be signed:</p>

<pre><code>Version:   01000000
TxIn:      01 bdec74e1a37efc77324bb81d2375c5af090ca02debedb7af18fa1e85756d7c26 01000000 
subScript: 17 a9 14 37fe3f20433380be4c742c978d3a9712c509656e 87 
Sequence:  ffffffff 
TxOut:     OUTPUTDATA
Locktime:  00000000 
Hashtype:  01000000
</code></pre>

<p>The double-sha256 value used to calculate the signature is <code>9d384de385512525b4faf08c97651e410e77f2906aa8baeefdee151cc0994655</code> </p>

<p>But the Bitcoin network rejects my transaction calculated from this transaction structure. </p>

<p>I will happily provide more information such as redeemScript and pubKeys/privateKeys if the mistake should not be in the transaction structure.</p>

<p>Any help is appreciated! Thanks!</p>

<signature><p2sh>
Signature scheme for P2SH
Bjarne Magnussen
# Signature scheme for P2SH

(Asked by: username_0 on 2017-10-09)

I am trying to sign a raw transaction that spends output #2 (txindex 1) from this transaction: `267c6d75851efa18afb7edeb2da00c09afc575231db84b3277fc7ea3e174ecbd`

The output is a 1-of-4 multisig P2SH and I think I may have a mistake in the transaction structure used to generate the signature. The signature I calculate is different (as in cryptographically different) to the one that bitcoin core gives me using signrawtransaction.

As I see it this is the transaction structure which must be signed:

```
Version:   01000000
TxIn:      01 bdec74e1a37efc77324bb81d2375c5af090ca02debedb7af18fa1e85756d7c26 01000000 
subScript: 17 a9 14 37fe3f20433380be4c742c978d3a9712c509656e 87 
Sequence:  ffffffff 
TxOut:     OUTPUTDATA
Locktime:  00000000 
Hashtype:  01000000

```

The double-sha256 value used to calculate the signature is `9d384de385512525b4faf08c97651e410e77f2906aa8baeefdee151cc0994655`

But the Bitcoin network rejects my transaction calculated from this transaction structure.

I will happily provide more information such as redeemScript and pubKeys/privateKeys if the mistake should not be in the transaction structure.

Any help is appreciated! Thanks!

**Tags:** signature, p2sh


---

## 1 answers

### Answer by username_1 on 2017-10-09. Score: 1 | Accepted answer

For P2SH transactions, the redeemScript is what is put in the scriptSig for the transaction preimage, not the scriptPubKey of the output.

> **Comments:**
> **username_0**: I still cannot make it work. The redeemScript that hashes to "37fe3f20433380be4c742c978d3a9712c509656e" is "512102930a11e92103daefde0d30b552f57d303e94a128e763ca9e69ff2006446934442103e74d2113dec75d75cde09a5b46297b1067e4b8b35e63c4c32b8cdbadfdffda1e2103067fcc39ee36d2417684511d1055fdc7d35e54911cb9de9ae30c988b666f675c2102dfb1c2a1c3456c8cb76714706dba77b3f4e7fe5afffc2503b121323a48ebbdcf54ae", with a length of "8b".  
> **username_0**: My new preimage is as above but changning subScript to: "4c8b512102930a11e92103daefde0d30b552f57d303e94a128e763ca9e69ff2006446934442103e74d2113dec75d75cde09a5b46297b1067e4b8b35e63c4c32b8cdbadfdffda1e2103067fcc39ee36d2417684511d1055fdc7d35e54911cb9de9ae30c988b666f675c2102dfb1c2a1c3456c8cb76714706dba77b3f4e7fe5afffc2503b121323a48ebbdcf54ae"  
> **username_0**: But I still get the same error messages and when using the valid signature from bitcoin core cannot validate it using this preimage.  
> **username_1**: What is the exact error that you get when you try to send the transaction?  
> **username_2**: @Inaki It will be useful if you can provide the whole serialized transaction.  
> **username_0**: Thanks for offering me help! I have created a txt-message here: https://pastebin.com/raw/21ucHYW7 I have also tried to take a P2SH from the blockchain that is being spent and try to recreate the preimage manually to verify the provided signatures. But I can also not get that to work. If someone could make a step-by-step showing how the signature is calculated for any P2SH output being spend that this would also help me a lot!  
> **username_0**: After checking it for a hundred times I saw that in the pastebin I uploaded I forgot to add the OP_0 (0x00) in the front of the scriptSig. After adding this it works! Thanks a lot for pointing out that the redeemScript is used in the preimage! This was the final signed transaction:  
> **username_0**: 0100000001bdec74e1a37efc77324bb81d2375c5af090ca02debedb7af18fa1e85756d7c2601000000d60047304402204887a352b503da6f212c09ed7e973384d7660f62d2319a15b968252012d134810220695130bc101f44348203c51a50e46f37ed4bc5622795f5dfbddef2affb27e2a8014c8b512102930a11e92103daefde0d30b552f57d303e94a128e763ca9e69ff2006446934442103e74d2113dec75d75cde09a5b46297b1067e4b8b35e63c4c32b8cdbadfdffda1e2103067fcc39ee36d2417684511d1055fdc7d35e54911cb9de9ae30c988b666f675c2102dfb1c2a1c3456c8cb76714706dba77b3f4e7fe5afffc2503b121323a48ebbdcf54aeffffffff01c05d00000000000017a914de5462e6e84cdab5064220343b9331a3af6dbbf18700000000  

---

