<p>You have made a mistake in the transaction data that you signed. For P2SH, you actually have to sign the redeemScript, not the scriptPubKey. You have to sign it as if that redeemScript were the scriptPubKey.</p>

<hr>

<p>There is another mistake in your redeemScript as well. An <code>OP_CHECKMULTISIG</code> requires you to push the number of signers needed, then the keys, then the number of keys. So for a 1-of-3 as you have, it should be</p>

<pre><code>1 &lt;key1&gt; &lt;key2&gt; &lt;key3&gt; 3 OP_CHECKMULTISIG
</code></pre>

<p>But you have</p>

<pre><code>1 &lt;key1&gt; &lt;key2&gt; &lt;key3&gt; 1 OP_CHECKMULTISIG
</code></pre>

<p>which makes the entire redeemScript itself invalid.</p>

<p>Your redeemScript really should be</p>

<pre><code>512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c53ae
</code></pre>

<p>Because the redeemScript is not valid, the output you are trying to spend is also invalid and thus unspendable.</p>

CC BY-SA 4.0
2020-02-25T08:51:48.763Z
Thanks again for your time. I updated the question and I changed scriptPubKey with redeem script and sign it. I have the same problem. I don't understand when use `4c`, `OP_PUSHDATA1`. I tried even 4c before sign the transaction, but I get the same issue
monkeyUser
CC BY-SA 4.0
2020-02-25T15:44:45.293Z
I presume that I must use `OP_PUSHDATA1` when the data is up to 75 bytes, and my redeem script is 105 bytes.
monkeyUser
CC BY-SA 4.0
2020-02-25T17:28:29.037Z
You use `OP_PUSHDATA1` when the data you want to push onto the stack is between 76 and 255 bytes in length, inclusive. I've found another issue with your transaction and have updated my answer.
Ava Chow
CC BY-SA 4.0
2020-02-26T09:33:15.247Z
Thanks again. very dumb mistake! Anyway it works thanks. Just to clarify, I add 4c in front of redeem script because it's 105 bytes after add Signature in my transaction data. Why I don't need to add 4c in front of redeemscript before sign it?
monkeyUser
CC BY-SA 4.0
2020-02-26T17:20:54.890Z
The way that P2SH works is that you push the entire redeemScript as a single stack element to the top of the stack. It then pops it off the stack and executes it as a script during script execution. So when you put it in the scriptSig, you need to make it a push to the stack, hence the need to put `OP_PUSHDATA1`. But when you are signing the transaction, you need to put the actual script that will be executed, so this can't be a push. You put it as if the redeemScript were in a scriptPubKey. If it were in a scriptPubKey, you would not have a push because it is executed as is.
Ava Chow
CC BY-SA 4.0
2020-02-27T10:57:33.923Z
to understand which part of transaction is signed I can see that file? https://github.com/bitcoin/bitcoin/blob/master/src/script/sign.cpp#L98
monkeyUser
CC BY-SA 4.0
2020-02-25T00:36:26.120Z
2020-02-25T17:26:35.107Z
2020-02-25T17:26:35.107Z
Ava Chow
<p>I have an P2SH 1-3 and I want to use openssl to sign a transaction.
my redem script is</p>

<pre><code>512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE
</code></pre>

<p>My transaction data is:</p>

<pre><code>02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e48010000000069512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac0000000001000000
</code></pre>

<p>Inside it you can find Redeem Script and SIGHASH.</p>

<p>I do SHA256 twice and save it in <code>a.txt</code> file</p>

<pre><code>printf $A | xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b | xxd -r -p &gt; a.txt
</code></pre>

<p>Now I can sign it with my private key</p>

<pre><code>FIRMA=`openssl pkeyutl -inkey chiave_priv_1.pem -sign -in a.txt -pkeyopt digest:sha256 | xxd -p -c 256`
</code></pre>

<p>append to <code>FIRMA</code> the byte 01 (SIGHASH_ALL) and prepend the length and OP_0 for OP_CHECKMULTISIG's bug.</p>

<pre><code>00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f01
</code></pre>

<p>The redeem script length is 210 char hex, 69 in base16.
prepend <code>4c</code> (OP_PUSHDATA1).</p>

<pre><code>4c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE
</code></pre>

<p>My whole scriptSig is:</p>

<pre><code>00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE
</code></pre>

<p>The ScriptSig Length is <code>B5</code>, the my whole transaction is:</p>

<pre><code>02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
</code></pre>

<p>Decoderawtransaction</p>

<pre><code>bitcoin-cli decoderawtransaction 02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
{
  "txid": "e3d1d4fba5ebd8e6042ece0e5ecc40caf7f81b35fd06e17f76dace8565e2f15f",
  "hash": "e3d1d4fba5ebd8e6042ece0e5ecc40caf7f81b35fd06e17f76dace8565e2f15f",
  "version": 2,
  "size": 266,
  "vsize": 266,
  "weight": 1064,
  "locktime": 0,
  "vin": [
    {
      "txid": "01481ee0b940ba2f0b9796512cf1630cc78aa9e29d43c568dd5b5dd48b693224",
      "vout": 0,
      "scriptSig": {
        "asm": "0 3045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f[ALL] 512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51ae",
        "hex": "00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51ae"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 49.98200000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 a6cdf0c6e4850e2114cbc96df01f155b8792ed8c OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mviwAtz3zQj4xzH5mqLhRFrMSHwEcz7rYn"
        ]
      }
    }
  ]
}
</code></pre>

<p>Sendrawtransaction:</p>

<pre><code>$ bitcoin-cli sendrawtransaction 02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
error code: -26
error message:
mandatory-script-verify-flag-failed (unknown error) (code 16)
</code></pre>

<p>My goal is create custom script. I tried even with 2-3 but without lucky.   </p>

<p><strong>UPDATED QUESTION AFTER ANDREW'S ANSWER</strong></p>

<multi-signature><signature><p2sh>
Sign P2SH transaction with Openssl
monkeyUser
# Sign P2SH transaction with Openssl

(Asked by: username_0 on 2020-02-24)

I have an P2SH 1-3 and I want to use openssl to sign a transaction.
my redem script is

```
512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE

```

My transaction data is:

```
02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e48010000000069512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac0000000001000000

```

Inside it you can find Redeem Script and SIGHASH.

I do SHA256 twice and save it in `a.txt` file

```
printf $A | xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b | xxd -r -p > a.txt

```

Now I can sign it with my private key

```
FIRMA=`openssl pkeyutl -inkey chiave_priv_1.pem -sign -in a.txt -pkeyopt digest:sha256 | xxd -p -c 256`

```

append to `FIRMA` the byte 01 (SIGHASH\_ALL) and prepend the length and OP\_0 for OP\_CHECKMULTISIG's bug.

```
00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f01

```

The redeem script length is 210 char hex, 69 in base16.
prepend `4c` (OP\_PUSHDATA1).

```
4c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE

```

My whole scriptSig is:

```
00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AE

```

The ScriptSig Length is `B5`, the my whole transaction is:

```
02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000

```

Decoderawtransaction

```
bitcoin-cli decoderawtransaction 02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
{
  "txid": "e3d1d4fba5ebd8e6042ece0e5ecc40caf7f81b35fd06e17f76dace8565e2f15f",
  "hash": "e3d1d4fba5ebd8e6042ece0e5ecc40caf7f81b35fd06e17f76dace8565e2f15f",
  "version": 2,
  "size": 266,
  "vsize": 266,
  "weight": 1064,
  "locktime": 0,
  "vin": [
    {
      "txid": "01481ee0b940ba2f0b9796512cf1630cc78aa9e29d43c568dd5b5dd48b693224",
      "vout": 0,
      "scriptSig": {
        "asm": "0 3045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f[ALL] 512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51ae",
        "hex": "00483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51ae"
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 49.98200000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 a6cdf0c6e4850e2114cbc96df01f155b8792ed8c OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mviwAtz3zQj4xzH5mqLhRFrMSHwEcz7rYn"
        ]
      }
    }
  ]
}

```

Sendrawtransaction:

```
$ bitcoin-cli sendrawtransaction 02000000012432698bd45d5bdd68c5439de2a98ac70c63f12c5196970b2fba40b9e01e480100000000B500483045022019d6d4efa4c714c3936e87acd69b1c3d791fb5a9c956e611a33c5a0cb007b209022100de2c9447adc07d1a27f0364a41db87cba09b50f68119a807fc5328df6ce1da1f014c69512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c51AEffffffff01c07aea29010000001976a914a6cdf0c6e4850e2114cbc96df01f155b8792ed8c88ac00000000
error code: -26
error message:
mandatory-script-verify-flag-failed (unknown error) (code 16)

```

My goal is create custom script. I tried even with 2-3 but without lucky.

**UPDATED QUESTION AFTER ANDREW'S ANSWER**

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


---

## 1 answers

### Answer by username_1 on 2020-02-25. Score: 2 | Accepted answer

You have made a mistake in the transaction data that you signed. For P2SH, you actually have to sign the redeemScript, not the scriptPubKey. You have to sign it as if that redeemScript were the scriptPubKey.

---

There is another mistake in your redeemScript as well. An `OP_CHECKMULTISIG` requires you to push the number of signers needed, then the keys, then the number of keys. So for a 1-of-3 as you have, it should be

```
1 <key1> <key2> <key3> 3 OP_CHECKMULTISIG

```

But you have

```
1 <key1> <key2> <key3> 1 OP_CHECKMULTISIG

```

which makes the entire redeemScript itself invalid.

Your redeemScript really should be

```
512103e6b11909698c11ff8232c02f4fbac409fcb5ef2cdacaf22a3c496f334d349a462103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c2103f026ba3cff269ae6ba6269b5728829c7879d9d1024fb1ede192cb4088bf86e5c53ae

```

Because the redeemScript is not valid, the output you are trying to spend is also invalid and thus unspendable.

> **Comments:**
> **username_0**: Thanks again for your time. I updated the question and I changed scriptPubKey with redeem script and sign it. I have the same problem. I don't understand when use `4c`, `OP_PUSHDATA1`. I tried even 4c before sign the transaction, but I get the same issue  
> **username_0**: I presume that I must use `OP_PUSHDATA1` when the data is up to 75 bytes, and my redeem script is 105 bytes.  
> **username_1**: You use `OP_PUSHDATA1` when the data you want to push onto the stack is between 76 and 255 bytes in length, inclusive. I've found another issue with your transaction and have updated my answer.  
> **username_0**: Thanks again. very dumb mistake! Anyway it works thanks. Just to clarify, I add 4c in front of redeem script because it's 105 bytes after add Signature in my transaction data. Why I don't need to add 4c in front of redeemscript before sign it?  
> **username_1**: The way that P2SH works is that you push the entire redeemScript as a single stack element to the top of the stack. It then pops it off the stack and executes it as a script during script execution. So when you put it in the scriptSig, you need to make it a push to the stack, hence the need to put `OP_PUSHDATA1`. But when you are signing the transaction, you need to put the actual script that will be executed, so this can't be a push. You put it as if the redeemScript were in a scriptPubKey. If it were in a scriptPubKey, you would not have a push because it is executed as is.  
> **username_0**: to understand which part of transaction is signed I can see that file? https://github.com/bitcoin/bitcoin/blob/master/src/script/sign.cpp#L98  

---

