<p>Finally I found solution and what i was doing wrong. The way of signing tx is totally correct, the problem was occurred in the other thing. Before I want to broadcast tx I calculate fee per kb (not fixed one). After signing tx - size of raw tx increses and fee amount increases accordingly. After that I should provide new change for address for change (value of the second output of tx). And after that I should sign inputs one more time, because the value of the output for change was changed. Unfortunately, I didn`t do this before. Maybe it'll be useful for someone.</p>

CC BY-SA 4.0
2018-08-11T12:05:37.753Z
2018-08-11T12:05:37.753Z
Timur Panzhiev
<p>Good day!
I'm working with low level API to make tx and sign it offline. But after all preparations I try to broadcast it and than i get an error from network:  </p>

<p><code>16: mandatory-script-verify-flag-failed (signature must be zero for failed check(multi)sig operation)</code></p>

<p>First of all I create tx without signing inputs, and after that i'm trying to sign every input with my priv keys. </p>

<p><strong>Here is raw tx before signing :</strong> </p>

<p><code>01000000028bcf82619e93eb92cc78d5bb0b7b22d6ee419820428402e721fbac1386c9673f0000000000ffffffffd50ea1cd97af5295cddcd4f0c7c0d531fed5683bde80af45ede25a0571c31add0100000000ffffffff02087f0300000000001976a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88acfa6b0100000000001976a914196e59ee4c151e742af69bca308fc1b4445c577288ac00000000</code></p>

<p>Parsing tx from the <a href="https://live.blockcypher.com/btc/decodetx/" rel="nofollow noreferrer">https://live.blockcypher.com/btc/decodetx/</a>  :</p>

<pre><code>{
    "addresses": [
        "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3", 
        "13KU69194fJj13G1SajUWokudsuekhT1kw", 
        "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 55194, 
    "hash": "2010928be1525a89aae1e0ece367757f6deb3754bfc4c891ffddbad403ecc3c9", 
    "inputs": [
        {
            "addresses": [
                "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3"
            ], 
            "age": 534819, 
            "output_index": 0, 
            "output_value": 251628, 
            "prev_hash": "3f67c98613acfb21e7028442209841eed6227b0bbbd578cc92eb939e6182cf8b", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "age": 534818, 
            "output_index": 1, 
            "output_value": 125872, 
            "prev_hash": "dd1ac371055ae2ed45af80de3b68d5fe31d5c0c7f0d4dccd9552af97cda10ed5", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "outputs": [
        {
            "addresses": [
                "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
            ], 
            "script": "76a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 229128
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "script": "76a914196e59ee4c151e742af69bca308fc1b4445c577288ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 93178
        }
    ], 
    "preference": "high", 
    "received": "2018-08-05T18:04:52.877018121Z", 
    "relayed_by": "54.162.141.93", 
    "size": 160, 
    "total": 322306, 
    "ver": 1, 
    "vin_sz": 2, 
    "vout_sz": 2
}
</code></pre>

<p><strong>Here is my code of signing every input:</strong></p>

<pre><code>for (int i = 0; i &lt; transaction.getInputs().size(); i++) {

    TransactionInput transactionInput = transaction.getInput(i);
    String addressFromUtxo = mUTXOs.get(i).getAddress();
    byte[] privKeyBytes = getPrivKeyBitesForAddress(addressFromUtxo);
    ECKey ecKey = ECKey.fromPrivate(privKeyBytes);

    Script scriptPubKey = ScriptBuilder.createOutputScript(Address.fromBase58(params, mUTXOs.get(i).getAddress()));

    Sha256Hash hash = transaction.hashForSignature(i, scriptPubKey, Transaction.SigHash.ALL, false);
    ECKey.ECDSASignature ecSig = ecKey.sign(hash);   
    TransactionSignature txSig = new TransactionSignature(ecSig, Transaction.SigHash.ALL, false);
    transactionInput.setScriptSig(ScriptBuilder.createInputScript(txSig, ecKey));
}

//serialization and broadcasting
byte[] bytesRawTransaction = transaction.bitcoinSerialize();
String rawTransaction = HEX.encode(bytesRawTransaction);
broadcastTx(rawTransaction);
</code></pre>

<p><strong>And here how my raw tx looks after the signing:</strong></p>

<p><code>01000000028bcf82619e93eb92cc78d5bb0b7b22d6ee419820428402e721fbac1386c9673f000000006a473044022054720cc14562000ed1a6fa7952d2343bef48e2fa4fd0f1c1894579026ae83692022042735beb03c4d53c8d72126e1e4e8bee7acd5530284b05d6707a4e71cc891e9a01210312e1997fc0cb4c037c94885e7e4a8d0eca9d5cff8628cc3d870e5c158c9b0901ffffffffd50ea1cd97af5295cddcd4f0c7c0d531fed5683bde80af45ede25a0571c31add010000006a47304402206a70b6dacea9e9435c21fe920471daccf2edb636592b9d9163b1f5d740e582e102203416f80e9859fcfacc9f41ea3e04774ef47c9b0b9fa5a61f2b77dffee4c2710e012103522a472dd00a929e0f0980b98eb6a0d10a6e5a6ff7d45709cdb9f022b881ca66ffffffff02087f0300000000001976a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88acfa6b0100000000001976a914196e59ee4c151e742af69bca308fc1b4445c577288ac00000000</code></p>

<p>Parsing tx from the <a href="https://live.blockcypher.com/btc/decodetx/" rel="nofollow noreferrer">https://live.blockcypher.com/btc/decodetx/</a>  :</p>

<pre><code>{
    "addresses": [
        "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3", 
        "13KU69194fJj13G1SajUWokudsuekhT1kw", 
        "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 55194, 
    "hash": "26859671de7bd5f0ab758517d90dcb2ef7c8a8a331a2d4fc59c7d98e07857d09", 
    "inputs": [
        {
            "addresses": [
                "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3"
            ], 
            "age": 534819, 
            "output_index": 0, 
            "output_value": 251628, 
            "prev_hash": "3f67c98613acfb21e7028442209841eed6227b0bbbd578cc92eb939e6182cf8b", 
            "script": "473044022054720cc14562000ed1a6fa7952d2343bef48e2fa4fd0f1c1894579026ae83692022042735beb03c4d53c8d72126e1e4e8bee7acd5530284b05d6707a4e71cc891e9a01210312e1997fc0cb4c037c94885e7e4a8d0eca9d5cff8628cc3d870e5c158c9b0901", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "age": 534818, 
            "output_index": 1, 
            "output_value": 125872, 
            "prev_hash": "dd1ac371055ae2ed45af80de3b68d5fe31d5c0c7f0d4dccd9552af97cda10ed5", 
            "script": "47304402206a70b6dacea9e9435c21fe920471daccf2edb636592b9d9163b1f5d740e582e102203416f80e9859fcfacc9f41ea3e04774ef47c9b0b9fa5a61f2b77dffee4c2710e012103522a472dd00a929e0f0980b98eb6a0d10a6e5a6ff7d45709cdb9f022b881ca66", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "outputs": [
        {
            "addresses": [
                "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
            ], 
            "script": "76a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 229128
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "script": "76a914196e59ee4c151e742af69bca308fc1b4445c577288ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 93178
        }
    ], 
    "preference": "high", 
    "received": "2018-08-05T10:44:42.584042561Z", 
    "relayed_by": "54.162.141.93", 
    "size": 372, 
    "total": 322306, 
    "ver": 1, 
    "vin_sz": 2, 
    "vout_sz": 2
}
</code></pre>

<p>And after all above I got: </p>

<p><code>16: mandatory-script-verify-flag-failed (signature must be zero for failed check(multi)sig operation)</code></p>

<p>Please, help me, what I'm doing wrong???</p>

CC BY-SA 4.0
2018-08-05T18:13:54.923Z
That error means that the signature is invalid. Make sure that you are using the correct private keys and signing the correct thing.
Ava Chow
CC BY-SA 4.0
2018-08-05T18:23:53.520Z
I`m understand that signatures are invalid, but in what way I should to sign tx with several inputs?
Timur Panzhiev
<signature><bitcoinj><transaction-input><signrawtransaction>
Sign Bitcoin transaction with several inputs using bitcoinj. Broadcasting of transaction failed
Timur Panzhiev
# Sign Bitcoin transaction with several inputs using bitcoinj. Broadcasting of transaction failed

(Asked by: username_0 on 2018-08-05)

Good day!
I'm working with low level API to make tx and sign it offline. But after all preparations I try to broadcast it and than i get an error from network:

`16: mandatory-script-verify-flag-failed (signature must be zero for failed check(multi)sig operation)`

First of all I create tx without signing inputs, and after that i'm trying to sign every input with my priv keys.

**Here is raw tx before signing :**

`01000000028bcf82619e93eb92cc78d5bb0b7b22d6ee419820428402e721fbac1386c9673f0000000000ffffffffd50ea1cd97af5295cddcd4f0c7c0d531fed5683bde80af45ede25a0571c31add0100000000ffffffff02087f0300000000001976a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88acfa6b0100000000001976a914196e59ee4c151e742af69bca308fc1b4445c577288ac00000000`

Parsing tx from the <https://live.blockcypher.com/btc/decodetx/> :

```
{
    "addresses": [
        "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3", 
        "13KU69194fJj13G1SajUWokudsuekhT1kw", 
        "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 55194, 
    "hash": "2010928be1525a89aae1e0ece367757f6deb3754bfc4c891ffddbad403ecc3c9", 
    "inputs": [
        {
            "addresses": [
                "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3"
            ], 
            "age": 534819, 
            "output_index": 0, 
            "output_value": 251628, 
            "prev_hash": "3f67c98613acfb21e7028442209841eed6227b0bbbd578cc92eb939e6182cf8b", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "age": 534818, 
            "output_index": 1, 
            "output_value": 125872, 
            "prev_hash": "dd1ac371055ae2ed45af80de3b68d5fe31d5c0c7f0d4dccd9552af97cda10ed5", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "outputs": [
        {
            "addresses": [
                "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
            ], 
            "script": "76a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 229128
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "script": "76a914196e59ee4c151e742af69bca308fc1b4445c577288ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 93178
        }
    ], 
    "preference": "high", 
    "received": "2018-08-05T18:04:52.877018121Z", 
    "relayed_by": "54.162.141.93", 
    "size": 160, 
    "total": 322306, 
    "ver": 1, 
    "vin_sz": 2, 
    "vout_sz": 2
}

```

**Here is my code of signing every input:**

```
for (int i = 0; i < transaction.getInputs().size(); i++) {

    TransactionInput transactionInput = transaction.getInput(i);
    String addressFromUtxo = mUTXOs.get(i).getAddress();
    byte[] privKeyBytes = getPrivKeyBitesForAddress(addressFromUtxo);
    ECKey ecKey = ECKey.fromPrivate(privKeyBytes);

    Script scriptPubKey = ScriptBuilder.createOutputScript(Address.fromBase58(params, mUTXOs.get(i).getAddress()));

    Sha256Hash hash = transaction.hashForSignature(i, scriptPubKey, Transaction.SigHash.ALL, false);
    ECKey.ECDSASignature ecSig = ecKey.sign(hash);   
    TransactionSignature txSig = new TransactionSignature(ecSig, Transaction.SigHash.ALL, false);
    transactionInput.setScriptSig(ScriptBuilder.createInputScript(txSig, ecKey));
}

//serialization and broadcasting
byte[] bytesRawTransaction = transaction.bitcoinSerialize();
String rawTransaction = HEX.encode(bytesRawTransaction);
broadcastTx(rawTransaction);

```

**And here how my raw tx looks after the signing:**

`01000000028bcf82619e93eb92cc78d5bb0b7b22d6ee419820428402e721fbac1386c9673f000000006a473044022054720cc14562000ed1a6fa7952d2343bef48e2fa4fd0f1c1894579026ae83692022042735beb03c4d53c8d72126e1e4e8bee7acd5530284b05d6707a4e71cc891e9a01210312e1997fc0cb4c037c94885e7e4a8d0eca9d5cff8628cc3d870e5c158c9b0901ffffffffd50ea1cd97af5295cddcd4f0c7c0d531fed5683bde80af45ede25a0571c31add010000006a47304402206a70b6dacea9e9435c21fe920471daccf2edb636592b9d9163b1f5d740e582e102203416f80e9859fcfacc9f41ea3e04774ef47c9b0b9fa5a61f2b77dffee4c2710e012103522a472dd00a929e0f0980b98eb6a0d10a6e5a6ff7d45709cdb9f022b881ca66ffffffff02087f0300000000001976a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88acfa6b0100000000001976a914196e59ee4c151e742af69bca308fc1b4445c577288ac00000000`

Parsing tx from the <https://live.blockcypher.com/btc/decodetx/> :

```
{
    "addresses": [
        "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3", 
        "13KU69194fJj13G1SajUWokudsuekhT1kw", 
        "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 55194, 
    "hash": "26859671de7bd5f0ab758517d90dcb2ef7c8a8a331a2d4fc59c7d98e07857d09", 
    "inputs": [
        {
            "addresses": [
                "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3"
            ], 
            "age": 534819, 
            "output_index": 0, 
            "output_value": 251628, 
            "prev_hash": "3f67c98613acfb21e7028442209841eed6227b0bbbd578cc92eb939e6182cf8b", 
            "script": "473044022054720cc14562000ed1a6fa7952d2343bef48e2fa4fd0f1c1894579026ae83692022042735beb03c4d53c8d72126e1e4e8bee7acd5530284b05d6707a4e71cc891e9a01210312e1997fc0cb4c037c94885e7e4a8d0eca9d5cff8628cc3d870e5c158c9b0901", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "age": 534818, 
            "output_index": 1, 
            "output_value": 125872, 
            "prev_hash": "dd1ac371055ae2ed45af80de3b68d5fe31d5c0c7f0d4dccd9552af97cda10ed5", 
            "script": "47304402206a70b6dacea9e9435c21fe920471daccf2edb636592b9d9163b1f5d740e582e102203416f80e9859fcfacc9f41ea3e04774ef47c9b0b9fa5a61f2b77dffee4c2710e012103522a472dd00a929e0f0980b98eb6a0d10a6e5a6ff7d45709cdb9f022b881ca66", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "outputs": [
        {
            "addresses": [
                "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC"
            ], 
            "script": "76a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 229128
        }, 
        {
            "addresses": [
                "13KU69194fJj13G1SajUWokudsuekhT1kw"
            ], 
            "script": "76a914196e59ee4c151e742af69bca308fc1b4445c577288ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 93178
        }
    ], 
    "preference": "high", 
    "received": "2018-08-05T10:44:42.584042561Z", 
    "relayed_by": "54.162.141.93", 
    "size": 372, 
    "total": 322306, 
    "ver": 1, 
    "vin_sz": 2, 
    "vout_sz": 2
}

```

And after all above I got:

`16: mandatory-script-verify-flag-failed (signature must be zero for failed check(multi)sig operation)`

Please, help me, what I'm doing wrong???

**Tags:** signature, bitcoinj, transaction-input, signrawtransaction

> **Comments:**
> **username_1**: That error means that the signature is invalid. Make sure that you are using the correct private keys and signing the correct thing.  
> **username_0**: I`m understand that signatures are invalid, but in what way I should to sign tx with several inputs?  

---

## 1 answers

### Answer by username_0 on 2018-08-11. Score: 1 | Accepted answer

Finally I found solution and what i was doing wrong. The way of signing tx is totally correct, the problem was occurred in the other thing. Before I want to broadcast tx I calculate fee per kb (not fixed one). After signing tx - size of raw tx increses and fee amount increases accordingly. After that I should provide new change for address for change (value of the second output of tx). And after that I should sign inputs one more time, because the value of the output for change was changed. Unfortunately, I didn`t do this before. Maybe it'll be useful for someone.


---

