3

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

2

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.

| improve this answer | |
  • 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 Feb 25 at 8:51
  • I presume that I must use OP_PUSHDATA1 when the data is up to 75 bytes, and my redeem script is 105 bytes. – monkeyUser Feb 25 at 15:44
  • 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. – Andrew Chow Feb 25 at 17:28
  • 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 Feb 26 at 9:33
  • 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. – Andrew Chow Feb 26 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.