1

I've created a raw transaction, and I get this error when I try to send it with sendrawtransaction to my local bitcoin regtest.

{
    "error": {
        "code": -26,
        "message": "mandatory-script-verify-flag-failed (Data push larger than necessary)"
    },
    "id": "1",
    "result": null
}

This is the transaction from which I want to spend:

{
    "error": null,
    "id": "1",
    "result": {
        "hash": "5fd0ca7c1fa3ef3b04dbf8aa096999604d30c47b88cd41d3ba1b6fba91f919a3",
        "hex": "0200000000010116402fe74ebc194f15f19ca11c5d49266608b024363c0b077a17c02bee4d1d820100000000feffffff0200ca9a3b000000001976a91485defe3226b7ec0a1dda4130a7808771a251fbfc88ace81a70eb0000000016001457026508dac7e8d071392868751eaebd6dc4f4d50247304402203752e4d8a536427c0865033b6bafd5972e9a58f6021d45f2c9e04cf5c253f3bc022075efc85af662dbd91aff1623e54295c68ce57e32963433e5215cab0041fac530012102de4a99f805dab29bb0035a496ceca72e7078d53f7bec92ebb44560ef019d505600000000",
        "locktime": 0,
        "size": 225,
        "txid": "97ab9a72367ad6b7a4c2ee122366e625b4049f795e4f15450bf857ea49d9e43c",
        "version": 2,
        "vin": [
            {
                "scriptSig": {
                    "asm": "",
                    "hex": ""
                },
                "sequence": 4294967294,
                "txid": "821d4dee2bc0177a070b3c3624b0086626495d1ca19cf1154f19bc4ee72f4016",
                "txinwitness": [
                    "304402203752e4d8a536427c0865033b6bafd5972e9a58f6021d45f2c9e04cf5c253f3bc022075efc85af662dbd91aff1623e54295c68ce57e32963433e5215cab0041fac53001",
                    "02de4a99f805dab29bb0035a496ceca72e7078d53f7bec92ebb44560ef019d5056"
                ],
                "vout": 1
            }
        ],
        "vout": [
            {
                "n": 0,
                "scriptPubKey": {
                    "addresses": [
                        "msioJPYMmXYkXieUQfVWyFwcXBvoCYhy2Q"
                    ],
                    "asm": "OP_DUP OP_HASH160 85defe3226b7ec0a1dda4130a7808771a251fbfc OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a91485defe3226b7ec0a1dda4130a7808771a251fbfc88ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash"
                },
                "value": 10
            },
            {
                "n": 1,
                "scriptPubKey": {
                    "addresses": [
                        "bcrt1q2upx2zx6cl5dqufe9p58284wh4kufax477qlqr"
                    ],
                    "asm": "0 57026508dac7e8d071392868751eaebd6dc4f4d5",
                    "hex": "001457026508dac7e8d071392868751eaebd6dc4f4d5",
                    "reqSigs": 1,
                    "type": "witness_v0_keyhash"
                },
                "value": 39.4999268
            }
        ],
        "vsize": 144,
        "weight": 573
    }
}

I performed the following steps:

  1. Create the raw transaction. This is the raw transaction:
01000000013ce4d949ea57f80b45154f5e799f04b425e6662312eec2a4b7d67a36729aab970000000000ffffffff0100ca9a3b000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
{
    "error": null,
    "id": "1",
    "result": {
        "hash": "1495eb8ada986cfaed1dc6dce56dc667fe7264c924ee75018190e3c39947b391",
        "locktime": 0,
        "size": 85,
        "txid": "1495eb8ada986cfaed1dc6dce56dc667fe7264c924ee75018190e3c39947b391",
        "version": 1,
        "vin": [
            {
                "scriptSig": {
                    "asm": "",
                    "hex": ""
                },
                "sequence": 4294967295,
                "txid": "97ab9a72367ad6b7a4c2ee122366e625b4049f795e4f15450bf857ea49d9e43c",
                "vout": 0
            }
        ],
        "vout": [
            {
                "n": 0,
                "scriptPubKey": {
                    "addresses": [
                        "moinqaiuWShfsemB9nGcAGFZNMa3ZwPMq6"
                    ],
                    "asm": "OP_DUP OP_HASH160 59fe1a9f9f0a5af33396c59990f064be1ca51274 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash"
                },
                "value": 10
            }
        ],
        "vsize": 85,
        "weight": 340
    }
}
  1. Sign the raw transaction. I don't have access to the private keys, because they are stored in a HSM. To sign the transaction, I have to send the serialized transaction and the public key to the HSM endpoint. It signs the transaction with the private key, which belongs to the public key and returns the signature.

This is the singed transaction:

01000000013ce4d949ea57f80b45154f5e799f04b425e6662312eec2a4b7d67a36729aab9700000000440101410454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576ffffffff01e07b9a3b000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
{
    "error": null,
    "id": "1",
    "result": {
        "hash": "24dbf04b3070c9b8eb4b2eac37a7a1ec355763d9c008b5dc1ebc636867b322b8",
        "locktime": 0,
        "size": 153,
        "txid": "24dbf04b3070c9b8eb4b2eac37a7a1ec355763d9c008b5dc1ebc636867b322b8",
        "version": 1,
        "vin": [
            {
                "scriptSig": {
                    "asm": "1 0454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576",
                    "hex": "0101410454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576"
                },
                "sequence": 4294967295,
                "txid": "97ab9a72367ad6b7a4c2ee122366e625b4049f795e4f15450bf857ea49d9e43c",
                "vout": 0
            }
        ],
        "vout": [
            {
                "n": 0,
                "scriptPubKey": {
                    "addresses": [
                        "moinqaiuWShfsemB9nGcAGFZNMa3ZwPMq6"
                    ],
                    "asm": "OP_DUP OP_HASH160 59fe1a9f9f0a5af33396c59990f064be1ca51274 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash"
                },
                "value": 9.9998
            }
        ],
        "vsize": 153,
        "weight": 612
    }
}

What am I doing wrong?

Update 1: Based on the answer of MCCCS I changed 0101 to 0x51. Now my signed transaction looks like this:

{
    "error": null,
    "id": "1",
    "result": {
        "hash": "f1b87be3d529004e14a9e13cb59849a027191862872fc8c964e37c1a60d05e81",
        "locktime": 0,
        "size": 153,
        "txid": "f1b87be3d529004e14a9e13cb59849a027191862872fc8c964e37c1a60d05e81",
        "version": 1,
        "vin": [
            {
                "scriptSig": {
                    "asm": "81 0454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576",
                    "hex": "0151410454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576"
                },
                "sequence": 4294967295,
                "txid": "97ab9a72367ad6b7a4c2ee122366e625b4049f795e4f15450bf857ea49d9e43c",
                "vout": 0
            }
        ],
        "vout": [
            {
                "n": 0,
                "scriptPubKey": {
                    "addresses": [
                        "moinqaiuWShfsemB9nGcAGFZNMa3ZwPMq6"
                    ],
                    "asm": "OP_DUP OP_HASH160 59fe1a9f9f0a5af33396c59990f064be1ca51274 OP_EQUALVERIFY OP_CHECKSIG",
                    "hex": "76a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac",
                    "reqSigs": 1,
                    "type": "pubkeyhash"
                },
                "value": 9.9998
            }
        ],
        "vsize": 153,
        "weight": 612
    }
}

Now I get the following error when broadcasting the transaction:

{
    "error": {
        "code": -26,
        "message": "mandatory-script-verify-flag-failed (Non-canonical DER signature)"
    },
    "id": "1",
    "result": null
}

What does that mean?

2

In your signed transaction

                "scriptSig": {
                    "asm": "1 0454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576",
                    "hex": "0101410454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576"
                },

The 0101 in hex should've been OP_1, which is 0x51. Both are equivalent, and the shorter one has been required since a soft fork.

This is to prevent 3rd parties from changing parts of transaction not covered by a transaction to change the TXID. Yours disobeys BIP62 case 3.

12
  • I changed 0101 to 0x51 (see in my original question Update 1), but now I get the following error when broadcasting the transaction: "mandatory-script-verify-flag-failed (Non-canonical DER signature)" – Bumblebee Apr 25 '20 at 12:52
  • @Bumblebee The transaction is not signed, it contains 0454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576 as signature, but it's a public key. Uncompressed public keys start with 0x04 and encode the 32-bytes of x coordinate and 32-bytes of the y coordinate, so this is a public key. – MCCCS Apr 25 '20 at 13:04
  • My private key looks like this: 3074020101042059b1b460e0b74f217be10f0f3ec4c5438053b0ea5fa7fced6da838b1044f47afa00706052b8104000aa1440342000454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576 How can I convert the ec private key into a private key that I can use to sign the transaction? – Bumblebee Apr 25 '20 at 13:10
  • 1
    I created a new keypair and I finally was able to broadcast the transaction. – Bumblebee Apr 25 '20 at 20:21
  • 1
    MCCCS: that privkey format is the one defined in C.4 of SEC1 from secg.org . It is one of the formats supported by OpenSSL (in both DER and PEM encodings), and is used in many ECC applications other than cryptocoin, like SSL/TLS and SSH. In addition to the 'raw' private number (here 32 octets) it contains an 'object identifier' (OID) identifying the curve (because many applications aren't limited to a single curve), a (redundant) copy of the raw public point, and some ASN.1 metadata ('tag' and 'length' octets) – dave_thompson_085 Apr 26 '20 at 7:19
1

Just in case somebody else runs into the same issue. Here is how I created the new key pair and derived the Wif private key from it. I used bx which is a part of https://github.com/libbitcoin/libbitcoin-explorer.

Here are the steps:

  1. Create a new private key
# bx seed | bx ec-new
> d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc

  1. Convert the private key to Wif
# bx ec-to-wif -v 239 d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc
> cUqPKAMJJ81psjeY2ZXM5S2HG6azieuqMypRb9viHoY1QvubReUp

Note: the -v 239 option is used for a testnet address. If you want to create an address/Wif private key for the mainnet, you can skip -v 239

  1. Create the public
# bx ec-to-public d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc
> 02a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2e

  1. Create the address
# bx ec-to-address -v 111 02a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2e
> mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5

  1. Send some btc to this address
# bitcoin-cli sendtoaddress mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5 40.0
> 2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1

  1. Get the raw transaction
#bitcoin-cli getrawtransaction 2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1
> 020000000001011ec966bff03a8b5af9ac98cf3ba606c558baf5112fff11b99c024743630bb3bd0000000000feffffff02e8e9d039000000001600148c579973d42679c854837ffebf5b6612d5673d1000286bee000000001976a9141af72e49ec135151f1421c185b6cb1ce836576c388ac0247304402206525f5763b0c427059ffcea4605492515bd9599250600a3771d4c7af8258ca0502201c54db8b24a146b74ca4a8e64fe1666016f6f9636c9cb39ad1bdb926149f7eef012103962b2c8bcff7291e647ae077b205a9b06660826e1e9754177f4366d074df7a9700000000

  1. Decode the raw transaction
# bitcoin-cli decoderawtransaction 020000000001011ec966bff03a8b5af9ac98cf3ba606c558baf5112fff11b99c024743630bb3bd0000000000feffffff02e8e9d039000000001600148c579973d42679c854837ffebf5b6612d5673d1000286bee000000001976a9141af72e49ec135151f1421c185b6cb1ce836576c388ac0247304402206525f5763b0c427059ffcea4605492515bd9599250600a3771d4c7af8258ca0502201c54db8b24a146b74ca4a8e64fe1666016f6f9636c9cb39ad1bdb926149f7eef012103962b2c8bcff7291e647ae077b205a9b06660826e1e9754177f4366d074df7a9700000000
{
  "txid": "2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1",
  "hash": "945df62297918f141d235c42623bb6f39bb6c5418a9630462658d548e931455e",
  "version": 2,
  "size": 225,
  "vsize": 144,
  "weight": 573,
  "locktime": 0,
  "vin": [
    {
      "txid": "bdb30b634347029cb911ff2f11f5ba58c506a63bcf98acf95a8b3af0bf66c91e",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "304402206525f5763b0c427059ffcea4605492515bd9599250600a3771d4c7af8258ca0502201c54db8b24a146b74ca4a8e64fe1666016f6f9636c9cb39ad1bdb926149f7eef01",
        "03962b2c8bcff7291e647ae077b205a9b06660826e1e9754177f4366d074df7a97"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 9.69992680,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 8c579973d42679c854837ffebf5b6612d5673d10",
        "hex": "00148c579973d42679c854837ffebf5b6612d5673d10",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1q33teju75yeuus4yr0llt7kmxzt2kw0gskkdjh7"
        ]
      }
    },
    {
      "value": 40.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 1af72e49ec135151f1421c185b6cb1ce836576c3 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a9141af72e49ec135151f1421c185b6cb1ce836576c388ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5"
        ]
      }
    }
  ]
}

  1. Create raw transaction. To create the raw transaction I created this quick and dirty shell script:
#!/bin/bash

# Adjust the options of bitcoin-cli according to your setup
CLI='bitcoin-cli -regtest -rpcuser=user -rpcpassword=password -rpcport=8332'

# Private Key from Step 1 (This is not used, just for verification)
PRIVATE_KEY='d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc'
# Wif Private Key from step 2
PRIVATE_KEY_BASE58='cUqPKAMJJ81psjeY2ZXM5S2HG6azieuqMypRb9viHoY1QvubReUp'

# Transaction id from Step 5
TX_IN_ID='2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1'

# Transaction hash from Ste 6
TX_IN_HASH='020000000001011ec966bff03a8b5af9ac98cf3ba606c558baf5112fff11b99c024743630bb3bd0000000000feffffff02e8e9d039000000001600148c579973d42679c854837ffebf5b6612d5673d1000286bee000000001976a9141af72e49ec135151f1421c185b6cb1ce836576c388ac0247304402206525f5763b0c427059ffcea4605492515bd9599250600a3771d4c7af8258ca0502201c54db8b24a146b74ca4a8e64fe1666016f6f9636c9cb39ad1bdb926149f7eef012103962b2c8bcff7291e647ae077b205a9b06660826e1e9754177f4366d074df7a9700000000'

# Receiver address
TO_ADDRESS='moinqaiuWShfsemB9nGcAGFZNMa3ZwPMq6'

# Amount to be sent
TO_AMOUNT=39.9998

echo ""
echo ""
echo "Raw In Transaction"
echo "=================="
$CLI decoderawtransaction $TX_IN_HASH

RAWTX=`$CLI \
    createrawtransaction \
    '[{"txid":"'$TX_IN_ID'","vout":'$TX_IN_VOUT'}]' \
    '{"'$TO_ADDRESS'":'$TO_AMOUNT'}'`

echo ""
echo ""
echo "Raw Transaction"
echo "==============="
echo $RAWTX

echo ""
echo ""
echo "Raw Transaction Decoded"
echo "======================="
$CLI decoderawtransaction $RAWTX

# scriptPubKey from Step 7 (Make sure to take the on from the vout from which you want to spend)
SCRIPT_PUB_KEY='76a9141af72e49ec135151f1421c185b6cb1ce836576c388ac'

echo ""
echo ""
echo "Sign Raw Transaction 1"
echo "======================"
# Make sure to set the vout from which you want to sped
RAWTX_SIGNED=`$CLI \
    signrawtransactionwithkey \
    $RAWTX \
    '["'$PRIVATE_KEY_BASE58'"]' \
    '[{"txid":"'$TX_IN_ID'","vout":1,"scriptPubKey":"'$SCRIPT_PUB_KEY'","redeemScript":""}]' `

echo ""
echo ""
echo "Raw Transaction Signed"
echo "======================"
echo $RAWTX_SIGNED

  1. Send the signed raw transaction
# bitcoin-cli sendrawtransaction 0200000001c1c8886fe4e25ee471423e000532412b387fb0fea11a17a68cea09c122229e2c010000006a473044022029296a6ac3fdaf0a0c4616c783bee1b3b5f7ea24a63f3df3544e74aa4c1a65b102207baa71419ef0ceff6218e78b6fb93bfe6ac7445584e23d20be1fbf6454bab50f012102a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2effffffff01e0d96aee000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
> 95d4f6bbdda2ec79350c0559d5d9e9c279f73e5d37f8267834034a40c6d520c2

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.