<p>In your signed transaction</p>

<pre><code>                "scriptSig": {
                    "asm": "1 0454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576",
                    "hex": "0101410454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576"
                },
</code></pre>

<p>The <code>0101</code> in hex <a href="https://github.com/bitcoin/bitcoin/blob/54f812d9d29893c690ae06b84aaeab128186aa36/src/script/interpreter.cpp#L234-L236" rel="nofollow noreferrer">should've been</a> OP_1, which is 0x51. Both are equivalent, and the shorter one has been required since a soft fork.</p>

<p>This is to prevent 3rd parties from changing parts of transaction not covered by a transaction to change the TXID. Yours disobeys <a href="https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#motivation" rel="nofollow noreferrer">BIP62</a> case 3.</p>

CC BY-SA 4.0
2020-04-25T12:52:47.023Z
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
CC BY-SA 4.0
2020-04-25T13:04:52.223Z
@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
CC BY-SA 4.0
2020-04-25T13:10:41.867Z
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
CC BY-SA 4.0
2020-04-25T13:25:45.830Z
@Bumblebee A private key has 32 bytes or 64 hex characters, that can't be a private key.
MCCCS
CC BY-SA 4.0
2020-04-25T13:34:18.507Z
Do you have your private key as PEM? I'm trying to understand the format of the private key you linked.
MCCCS
CC BY-SA 4.0
2020-04-25T13:41:39.493Z
My private key is in DER.
Bumblebee
CC BY-SA 4.0
2020-04-25T15:25:24.240Z
Could you please show you exported it?
MCCCS
CC BY-SA 4.0
2020-04-25T16:59:54.943Z
It is not en exported private key. I have to use a key pair which is created by OpenSSL and I get them in DER format..
Bumblebee
CC BY-SA 4.0
2020-04-25T20:21:25.540Z
I created a new keypair and I finally was able to broadcast the transaction.
Bumblebee
CC BY-SA 4.0
2020-04-26T07:19:04.990Z
MCCCS: that privkey format is the one defined in C.4 of SEC1 from https://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
CC BY-SA 4.0
2020-04-26T07:24:53.953Z
@dave_thompson_085 Thanks, I am not familiar different ways to encode ECC private keys and I'd expect it to start with `BEGIN PRIVATE KEY` and be in base64, and TIL that, thanks.
MCCCS
CC BY-SA 4.0
2020-04-27T06:25:47.690Z
MCCCS: the text forms beginning with 5 hyphens (important), `BEGIN`, one or a few words, 5 more hyphens and linebreak, then base64 with linebreaks, then a similar `END` line, are ['PEM' format](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) and OpenSSL supports, for ECC, 4 different PEM formats: 2 standard [per RFC7468 sec 10 & 11](https://tools.ietf.org/html/rfc7468) and 2 of its own that use `BEGIN EC PRIVATE KEY`.
dave_thompson_085
CC BY-SA 4.0
2020-04-25T11:55:05.457Z
2020-04-25T12:00:27.703Z
2020-04-25T12:00:27.703Z
MCCCS
<p>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 <code>bx</code> which is a part of <a href="https://github.com/libbitcoin/libbitcoin-explorer" rel="nofollow noreferrer">https://github.com/libbitcoin/libbitcoin-explorer</a>. </p>

<p>Here are the steps:</p>

<ol>
<li>Create a new private key</li>
</ol>

<pre><code># bx seed | bx ec-new
&gt; d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc

</code></pre>

<ol start="2">
<li>Convert the private key to Wif</li>
</ol>

<pre><code># bx ec-to-wif -v 239 d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc
&gt; cUqPKAMJJ81psjeY2ZXM5S2HG6azieuqMypRb9viHoY1QvubReUp

</code></pre>

<p>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</p>

<ol start="3">
<li>Create the public</li>
</ol>

<pre><code># bx ec-to-public d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc
&gt; 02a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2e

</code></pre>

<ol start="4">
<li>Create the address</li>
</ol>

<pre><code># bx ec-to-address -v 111 02a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2e
&gt; mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5

</code></pre>

<ol start="5">
<li>Send some btc to this address</li>
</ol>

<pre><code># bitcoin-cli sendtoaddress mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5 40.0
&gt; 2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1

</code></pre>

<ol start="6">
<li>Get the raw transaction</li>
</ol>

<pre><code>#bitcoin-cli getrawtransaction 2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1
&gt; 020000000001011ec966bff03a8b5af9ac98cf3ba606c558baf5112fff11b99c024743630bb3bd0000000000feffffff02e8e9d039000000001600148c579973d42679c854837ffebf5b6612d5673d1000286bee000000001976a9141af72e49ec135151f1421c185b6cb1ce836576c388ac0247304402206525f5763b0c427059ffcea4605492515bd9599250600a3771d4c7af8258ca0502201c54db8b24a146b74ca4a8e64fe1666016f6f9636c9cb39ad1bdb926149f7eef012103962b2c8bcff7291e647ae077b205a9b06660826e1e9754177f4366d074df7a9700000000

</code></pre>

<ol start="7">
<li>Decode the raw transaction</li>
</ol>

<pre><code># 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"
        ]
      }
    }
  ]
}

</code></pre>

<ol start="8">
<li>Create raw transaction. To create the raw transaction I created this quick and dirty shell script:</li>
</ol>

<pre><code>#!/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

</code></pre>

<ol start="9">
<li>Send the signed raw transaction</li>
</ol>

<pre><code># bitcoin-cli sendrawtransaction 0200000001c1c8886fe4e25ee471423e000532412b387fb0fea11a17a68cea09c122229e2c010000006a473044022029296a6ac3fdaf0a0c4616c783bee1b3b5f7ea24a63f3df3544e74aa4c1a65b102207baa71419ef0ceff6218e78b6fb93bfe6ac7445584e23d20be1fbf6454bab50f012102a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2effffffff01e0d96aee000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
&gt; 95d4f6bbdda2ec79350c0559d5d9e9c279f73e5d37f8267834034a40c6d520c2
</code></pre>

CC BY-SA 4.0
2020-04-26T09:11:50.740Z
2020-04-26T10:50:40.957Z
2020-04-26T10:50:40.957Z
Bumblebee
<p>I've created a raw transaction, and I get this error when I try to send it with <code>sendrawtransaction</code> to my local bitcoin regtest.</p>

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

<p>This is the transaction from which I want to spend:</p>

<pre><code>{
    "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
    }
}
</code></pre>

<p>I performed the following steps:</p>

<ol>
<li>Create the raw transaction. This is the raw transaction:</li>
</ol>

<pre><code>01000000013ce4d949ea57f80b45154f5e799f04b425e6662312eec2a4b7d67a36729aab970000000000ffffffff0100ca9a3b000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
</code></pre>

<pre><code>{
    "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
    }
}
</code></pre>

<ol start="2">
<li>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.</li>
</ol>

<p>This is the singed transaction:</p>

<pre><code>01000000013ce4d949ea57f80b45154f5e799f04b425e6662312eec2a4b7d67a36729aab9700000000440101410454a3553d447e4589d165d6fbe76e7bc3e472f107d223e1d051db37875bac09ba9b2ce89676b2e3c571d797bed67a525ecc16e904186692cdee40968cffaf5576ffffffff01e07b9a3b000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
</code></pre>

<pre><code>{
    "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
    }
}
</code></pre>

<p>What am I doing wrong?</p>

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

<pre><code>{
    "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
    }
}
</code></pre>

<p>Now I get the following error when broadcasting the transaction:</p>

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

<p>What does that mean?</p>

<bitcoin-core>
Error 26: mandatory-script-verify-flag-failed (Data push larger than necessary)
Bumblebee
# Error 26: mandatory-script-verify-flag-failed (Data push larger than necessary)

(Asked by: username_0 on 2020-04-25)

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
    }
}

```

2. 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 username_1 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?

**Tags:** bitcoin-core


---

## 2 answers

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

In your signed transaction

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

```

The `0101` in hex [should've been](https://github.com/bitcoin/bitcoin/blob/54f812d9d29893c690ae06b84aaeab128186aa36/src/script/interpreter.cpp#L234-L236) 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](https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#motivation) case 3.

> **Comments:**
> **username_0**: 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)"  
> **username_1**: @username_0 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.  
> **username_0**: 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?  
> **username_1**: @username_0 A private key has 32 bytes or 64 hex characters, that can't be a private key.  
> **username_1**: Do you have your private key as PEM? I'm trying to understand the format of the private key you linked.  
> **username_0**: My private key is in DER.  
> **username_1**: Could you please show you exported it?  
> **username_0**: It is not en exported private key. I have to use a key pair which is created by OpenSSL and I get them in DER format..  
> **username_0**: I created a new keypair and I finally was able to broadcast the transaction.  
> **username_2**: username_1: that privkey format is the one defined in C.4 of SEC1 from https://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)  
> **username_1**: @username_2 Thanks, I am not familiar different ways to encode ECC private keys and I'd expect it to start with `BEGIN PRIVATE KEY` and be in base64, and TIL that, thanks.  
> **username_2**: username_1: the text forms beginning with 5 hyphens (important), `BEGIN`, one or a few words, 5 more hyphens and linebreak, then base64 with linebreaks, then a similar `END` line, are ['PEM' format](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) and OpenSSL supports, for ECC, 4 different PEM formats: 2 standard [per RFC7468 sec 10 & 11](https://tools.ietf.org/html/rfc7468) and 2 of its own that use `BEGIN EC PRIVATE KEY`.  

---

### Answer by username_0 on 2020-04-26. Score: 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

```

2. 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

3. Create the public

```
# bx ec-to-public d86a6cd08829b0e23dfb8e0eeb3abb8378147a950c638bedd368e9f51d1c45bc
> 02a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2e

```

4. Create the address

```
# bx ec-to-address -v 111 02a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2e
> mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5

```

5. Send some btc to this address

```
# bitcoin-cli sendtoaddress mhyXyTy4fnu2jcmAGeChkgd9RmNgTg7xV5 40.0
> 2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1

```

6. Get the raw transaction

```
#bitcoin-cli getrawtransaction 2c9e2222c109ea8ca6171aa1feb07f382b413205003e4271e45ee2e46f88c8c1
> 020000000001011ec966bff03a8b5af9ac98cf3ba606c558baf5112fff11b99c024743630bb3bd0000000000feffffff02e8e9d039000000001600148c579973d42679c854837ffebf5b6612d5673d1000286bee000000001976a9141af72e49ec135151f1421c185b6cb1ce836576c388ac0247304402206525f5763b0c427059ffcea4605492515bd9599250600a3771d4c7af8258ca0502201c54db8b24a146b74ca4a8e64fe1666016f6f9636c9cb39ad1bdb926149f7eef012103962b2c8bcff7291e647ae077b205a9b06660826e1e9754177f4366d074df7a9700000000

```

7. 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"
        ]
      }
    }
  ]
}

```

8. 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

```

9. Send the signed raw transaction

```
# bitcoin-cli sendrawtransaction 0200000001c1c8886fe4e25ee471423e000532412b387fb0fea11a17a68cea09c122229e2c010000006a473044022029296a6ac3fdaf0a0c4616c783bee1b3b5f7ea24a63f3df3544e74aa4c1a65b102207baa71419ef0ceff6218e78b6fb93bfe6ac7445584e23d20be1fbf6454bab50f012102a4379f994ccce7041d30026092b13a7987c12cd6e33222dabdf890a00fc50b2effffffff01e0d96aee000000001976a91459fe1a9f9f0a5af33396c59990f064be1ca5127488ac00000000
> 95d4f6bbdda2ec79350c0559d5d9e9c279f73e5d37f8267834034a40c6d520c2

```


---

