Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.
Sign up to join this communityAnybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
I want to be able to create raw BTC transaction in Java. I'm using BitcoinJ library. And this raw transaction I will broadcast to blockchain. Here is my code:
NetworkParameters params = TestNet3Params.get(); final String privateKeyString = "cRbC8kbqTe**********************"; final String receiverAddress = "tb1qjxj6z4z42w25jk0r8pxj48e8dqj70ufanqxlmf"; final ECKey key = DumpedPrivateKey.fromBase58(params, privateKeyString).getKey(); final String filePrefix = "test"; WalletAppKit walletAppKit = new WalletAppKit(params, new File("*********************\\test"), filePrefix); walletAppKit.startAsync(); walletAppKit.awaitRunning(); Wallet wallet = walletAppKit.wallet(); wallet.importKey(key); List<TransactionOutput> unspents = wallet.getUnspents(); Address forwardingAddress = Address.fromString(params, receiverAddress); Coin coin = Coin.parseCoin("0.00001"); final Coin amountToSend = coin.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE); Transaction tx = new Transaction(params); tx.addOutput(amountToSend, forwardingAddress); for (TransactionOutput utxo : unspents) { TransactionOutPoint outPoint = new TransactionOutPoint(params, utxo.getIndex(), utxo.getHash()); tx.addSignedInput(outPoint, utxo.getScriptPubKey(), key, Transaction.SigHash.ALL, true); } tx.verify(); tx.getConfidence().setSource(TransactionConfidence.Source.SELF); tx.setPurpose(Transaction.Purpose.USER_PAYMENT); System.out.println("Transaction hash for broadcasting: " + tx.getHashAsString());
My problem is with this line:
List<TransactionOutput> unspents = wallet.getUnspents();
It always return empty list. Here is the address to my account, to which I own private keys and create ECKey object with it - tb1q8crg0hujn4p024pncx4nwzwn269cvkh2cxqvf7 - link to BTC explorer - https://tbtc.bitaps.com/tb1q8crg0hujn4p024pncx4nwzwn269cvkh2cxqvf7. You can see that on the address is a BTCs.
I found same question here How to get unspent bitcoins using bitcoinj, but the proposed solution by Ander Acosta returns a blank map.
Is there any possibility to get UTXOs using Bitcoinj or will I have to use some APIs of third party for that?
You can get bonuses upto $100 FREE BONUS when you:
💰 Install these recommended apps:
💲 SocialGood - 100% Crypto Back on Everyday Shopping
💲 xPortal - The DeFi For The Next Billion
💲 CryptoTab Browser - Lightweight, fast, and ready to mine!
💰 Register on these recommended exchanges:
🟡 Binance🟡 Bitfinex🟡 Bitmart🟡 Bittrex🟡 Bitget
🟡 CoinEx🟡 Crypto.com🟡 Gate.io🟡 Huobi🟡 Kucoin.
Comments