Hi for everyone! 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.

воскресенье, 5 августа 2018 г., 13:49:54 UTC+3 пользователь Тимур Панжиев 
написал:
>
> 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 : *
>
> 01000000018bcf82619e93eb92cc78d5bb0b7b22d6ee419820428402e721fbac1386c9673f0000000000ffffffff02087f0300000000001976a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88ace4570000000000001976a914196e59ee4c151e742af69bca308fc1b4445c577288ac00000000
> *Parsing tx from the https://live.blockcypher.com/btc/decodetx/ 
> <https://live.blockcypher.com/btc/decodetx/>  :*
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *{    "addresses": [        "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3",         
> "1BBgML68FuyRwU8RzwYn1FvqAXDDSgjVKC",         
> "13KU69194fJj13G1SajUWokudsuekhT1kw"    ],     "block_height": -1,     
> "block_index": -1,     "confirmations": 0,     "double_spend": false,     
> "fees": 0,     "hash": 
> "1e3c06bfc2f2e91db9059a0db6dd78090f635b869feb31c3d51bc4a36ed88891",     
> "inputs": [        {            "addresses": [                
> "16Yb7QP5jVmjXvHawH2ZA66k1b46FmtmE3"            ],             "age": 
> 534819,             "output_index": 0,             "output_value": 251628, 
>             "prev_hash": 
> "3f67c98613acfb21e7028442209841eed6227b0bbbd578cc92eb939e6182cf8b",         
>     "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": 22500        }    
> ],     "preference": "low",     "received": 
> "2018-08-05T10:43:36.984631491Z",     "relayed_by": "54.234.76.246",     
> "size": 119,     "total": 251628,     "ver": 1,     "vin_sz": 1,     
> "vout_sz": 2}*
>
> Here is my code of signing every in:
>
> 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 braodcasting
>
> byte[] bytesRawTransaction = transaction.bitcoinSerialize();
> String rawTransaction3 = HEX.encode(bytesRawTransaction3);
> broadcastTx(rawTransaction3);
>
>
> And here how my raw tx looking after the signing:
>
>
> 01000000028bcf82619e93eb92cc78d5bb0b7b22d6ee419820428402e721fbac1386c9673f000000006a473044022054720cc14562000ed1a6fa7952d2343bef48e2fa4fd0f1c1894579026ae83692022042735beb03c4d53c8d72126e1e4e8bee7acd5530284b05d6707a4e71cc891e9a01210312e1997fc0cb4c037c94885e7e4a8d0eca9d5cff8628cc3d870e5c158c9b0901ffffffffd50ea1cd97af5295cddcd4f0c7c0d531fed5683bde80af45ede25a0571c31add010000006a47304402206a70b6dacea9e9435c21fe920471daccf2edb636592b9d9163b1f5d740e582e102203416f80e9859fcfacc9f41ea3e04774ef47c9b0b9fa5a61f2b77dffee4c2710e012103522a472dd00a929e0f0980b98eb6a0d10a6e5a6ff7d45709cdb9f022b881ca66ffffffff02087f0300000000001976a9146fb643fe63bcc6c352899298d6072cc5d4178c0f88acfa6b0100000000001976a914196e59ee4c151e742af69bca308fc1b4445c577288ac00000000
>
> *Parsing tx from the https://live.blockcypher.com/btc/decodetx/ 
> <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???
>

-- 
You received this message because you are subscribed to the Google Groups 
"bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bitcoinj+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to