I am working with watching wallet and Offline wallet using bitcoinj, I created Watching Wallet using tpub key of Offline Wallet

I am receiving payment on watching wallet Fine, now I want to spend coins from watching wallet after Signing my transaction from offline 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:


But my wallet balance is 0.23389 BTC , its not spendable balance as my watching wallet shows

Wallet containing 0.23389 BTC (spendable: 0.00 BTC)

This is not spendable because we cannot spend coins from watching wallet unless you sign it with the corresponding private key

Below is my code: As i am trying to sign transaction from my offline wallet, so i can spend coins from watching wallet

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 = null;
    List<Transaction> list = kit.wallet().getTransactionsByTime();
    for(Transaction t: list)
        input = t.getInput(0);

    //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
    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()));

After that my raw transaction looks like this

     in   PUSHDATA(64)[35666630316436316536376337303663623739363533616131653761643263373235346238343131363765306133343035353530346333356337323430343639]
     in   PUSHDATA(71)[30440220598202f4822d3c18853f08df4c75bc65f8b42e1c815c6bfeecf53e389c764de80220060b6df8b206eebbd01400f3cc902a0afb24b365b4313f00de87b744a986378501] PUSHDATA(33)[02a0fbbfc754cdda4bc84874af8d5d8bdb8b0271a08a847a856afb3d4e2cd657e0]
          outpoint:c90cb5e1149430b1c99983626ce57588666c7d4e595089235ec0b2496ecae525:1 hash160:ff63fce58aeea04b03bfe29b85ec1cb4827eafad
     in   PUSHDATA(72)[30450221009b12dae5c6edb6f34df18637b06ae742ecb448b6a6797aadb22b6fb728dcba3c02205c95d2553a5d14d6c5bf3891f2dbc7cc3d49ba97ed5fdb6180c3e6225ea88efb81] PUSHDATA(33)[02a656694f40553fe33c06161189f13038a44ab545548b8d2f988ba54b766215d7]
     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

        catch (InsufficientMoneyException e)

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.

  • i'm not able to find used input, are you using vaild input with correct vout number?
    – Adam
    May 26, 2018 at 12:35
  • i am creating input by invoking TransactionInput object, i think i didn't provide the vout and also no idea about it, how to use it here.
    – Zombie
    May 26, 2018 at 13:58


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.