Title: Broadcasting signed transaction from Watching wallet using bitcoinj Post by: Tyga Christed on May 26, 2018, 03:21:55 PM 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: InsufficientMoneyException 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 tx.addInput(input); 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. Title: Re: Broadcasting signed transaction from Watching wallet using bitcoinj Post by: HCP on May 27, 2018, 01:47:06 AM I think it's because you're manually creating a fully complete and signed transaction... then trying to use SendRequest.forTX() which seems to want to actually include the inputs and sign the transaction for you.
public Transaction tx So, possibly it is thinking you're just including the inputs you already have for the fee... and when it tries to find coins to attach to the output(s)... it can't as it's a watching-only wallet.A transaction, probably incomplete, that describes the outline of what you want to do. This typically will mean it has some outputs to the intended destinations, but no inputs or change address (and therefore no fees) - the wallet will calculate all that for you and update tx later. Be careful when adding outputs that you check the min output value (TransactionOutput.getMinNonDustValue(Coin)) to avoid the whole transaction being rejected because one output is dust. If there are already inputs to the transaction, make sure their out point has a connected output, otherwise their value will be added to fee. Also ensure they are either signed or are spendable by a wallet key, otherwise the behavior of Wallet#completeTx(Wallet.SendRequest) is undefined (likely RuntimeException). If you have already created and signed the transaction... I'd think you just want to use the PeerGroup.broadcastTransaction() (https://bitcoinj.github.io/javadoc/0.14.7/org/bitcoinj/core/PeerGroup.html#broadcastTransaction-org.bitcoinj.core.Transaction-) method to simply broadcast the transaction you've already made. Title: Re: Broadcasting signed transaction from Watching wallet using bitcoinj Post by: Tyga Christed on May 29, 2018, 03:27:17 PM Now i am sending tx using PeerGroup and transaction is commit to wallet and also broadcasted, but unable to receive payment on the other end.
My wallet says >>> PENDING: 0.00 BTC total value (sends 0.00 BTC and receives 0.00 BTC) confidence: In conflict. Source: SELF 1c667d704069fe92e9c99efa418426f4fe5631b710e79c4cbb95552e540ef477 updated: 2018-05-28T21:03:43Z in PUSHDATA(72)[3045022100cd0df99a1bebe77cc106bd85ce95ebaa11fcf75e7f2a345cbe9f83b887b3460b02204298250be9997b737f0b3c2b835e93d6fea268e01c3ec4fec955009d9b218ed881] PUSHDATA(33)[02b2ad03959f35b5edff39045d577173d687009bf5c5af8e66d467f3970a6b8cbc] outpoint:d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43:0 out DUP HASH160 PUSHDATA(20)[3a413cbe6538360c932345c177a0e79c74746588] EQUALVERIFY CHECKSIG 0.01 BTC prps UNKNOWN 0.00 BTC total value (sends 0.00 BTC and receives 0.00 BTC) confidence: In conflict. Source: SELF 11c72923b8667cb988d06e6f412d1583a83acda0591fef2942bbca6c1c9d9c46 updated: 2018-05-28T20:38:01Z in PUSHDATA(72)[3045022100cd0df99a1bebe77cc106bd85ce95ebaa11fcf75e7f2a345cbe9f83b887b3460b02204298250be9997b737f0b3c2b835e93d6fea268e01c3ec4fec955009d9b218ed881] PUSHDATA(33)[02b2ad03959f35b5edff39045d577173d687009bf5c5af8e66d467f3970a6b8cbc] outpoint:d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43:0 out DUP HASH160 PUSHDATA(20)[7e39849a9025c2935b518a570e8ea46884d524ae] EQUALVERIFY CHECKSIG 0.01 BTC prps UNKNOWN its showing 0.00 BTC total value but i have sent 0.01 BTC also i read about the conflict here https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/core/TransactionConfidence.java is my tx is correct now ? or still some issues on it ? also my broadcasting tx code is SendRequest request = SendRequest.forTx(tx); kit.wallet().commitTx(request.tx);ListenableFuture<Transaction> future = kit.peerGroup().broadcastTransaction(request.tx).future(); future.get(); |