Skip to main content
3 of 4
deleted 63 characters in body
Ava Chow
  • 72.2k
  • 5
  • 84
  • 162

Broadcasting signed transaction from Watching wallet using bitcoinj

I am working with watching wallet and Offline wallet using bitcoinj,

I am receiving payment on watching wallet Fine, now I want to spend coins from watching wallet

What I am doing is creating rawTx from the Watching Wallet and then signing it with the private key from Offline Wallet but when I broadcast it, it throws an exception:

InsufficientMoneyException

But my wallet balance is 6.001 BTC

Below is my code:

For creating Raw Transaction from Watching Wallet

private void bla()
{
    Coin coin = Coin.parseCoin("0.01");
    Address address = Address.fromBase58(params, "n4oLPFUGvohSdDxvJS3amXvfE1GEdZBddd");
    Transaction tx = new Transaction(params);
    System.out.println("Raw TX: " + tx.toString());
    byte[] bytes = Script.createInputScript("5ff01d61e67c706cb79653aa1e7ad2c7254b841167e0a34055504c35c7240469".getBytes()); // tx hash 

    TransactionInput input = new TransactionInput(params, null, bytes);

    //Creates a scriptPubKey that encodes payment to the given address.
    Script scriptPubKey = ScriptBuilder.createOutputScript(address);
    // Signature Script
    Script scriptSig = input.getScriptSig();

    // Adding output that involves Amount and Script
    tx.addOutput(coin, scriptPubKey);
    System.out.println("Script pub key: " + scriptPubKey);

    //Adding input to the raw transaction
    tx.addInput(Sha256Hash.wrap(tx.getHashAsString()), 0, scriptSig);
    System.out.println("After adding inputs: " + tx.toString());
}

Signing Transaction from Offline Wallet Using Private Key

private String signTx()
{
    // TX Hash from Watching wallet
    String unSignHash = "f348ceadcb66e811799aa543107b63b9c92afebda5bc47d11222ba218df6638f";
    // Signing Hash with private key
    System.out.println("Signed Hash from prv key: " + DatatypeConverter.printHexBinary(prvKey().sign(Sha256Hash.wrap(unSignHash)).encodeToDER()));
}

Then I have added one more input in raw transaction

 input.setScriptSig(ScriptBuilder.createInputScript(new TransactionSignature(OfflineWallet.prvKey().sign(hash), Transaction.SigHash.ALL, true), OfflineWallet.ecKey()));
        tx.addInput(input);

After that my raw transaction looks like this

310cc780e414c1c64a1d8ed190df10aa5489183e72a73c000e42ff7cc00e41bb
     in   PUSHDATA(64)[35666630316436316536376337303663623739363533616131653761643263373235346238343131363765306133343035353530346333356337323430343639]
          outpoint:83c02edc7d30a63fd123835e0d966d3287ee28729e01d01a7119c055d975fd83:0
     in   PUSHDATA(71)[30440220598202f4822d3c18853f08df4c75bc65f8b42e1c815c6bfeecf53e389c764de80220060b6df8b206eebbd01400f3cc902a0afb24b365b4313f00de87b744a986378501] PUSHDATA(33)[02a0fbbfc754cdda4bc84874af8d5d8bdb8b0271a08a847a856afb3d4e2cd657e0]
          outpoint:c90cb5e1149430b1c99983626ce57588666c7d4e595089235ec0b2496ecae525:1 hash160:ff63fce58aeea04b03bfe29b85ec1cb4827eafad
     in   PUSHDATA(72)[30450221009b12dae5c6edb6f34df18637b06ae742ecb448b6a6797aadb22b6fb728dcba3c02205c95d2553a5d14d6c5bf3891f2dbc7cc3d49ba97ed5fdb6180c3e6225ea88efb81] PUSHDATA(33)[02a656694f40553fe33c06161189f13038a44ab545548b8d2f988ba54b766215d7]
          outpoint:0000000000000000000000000000000000000000000000000000000000000000:4294967295
     out  DUP HASH160 PUSHDATA(20)[ff63fce58aeea04b03bfe29b85ec1cb4827eafad] EQUALVERIFY CHECKSIG 0.00001 BTC
     out  DUP HASH160 PUSHDATA(20)[ff63fce58aeea04b03bfe29b85ec1cb4827eafad] EQUALVERIFY CHECKSIG 0.00001 BTC
     prps UNKNOWN

Then i am broadcasting it using

try
        {
            kit.wallet().sendCoins(SendRequest.forTx(tx));
        }
        catch (InsufficientMoneyException e)
        {
            e.printStackTrace();
        }

I am getting the exception here.

I don't know what I am doing wrong here, as I think my tx hash is just fine, all I have to do is broadcast the transaction into the network from watching wallet, which I am also doing but cannot succeed. I would really appreciate if someone helps me to find my mistake and broadcasting the signed transaction from watching wallet.

Zombie
  • 548
  • 3
  • 19