1

I'm trying to send all coins from my accounts (generated by HD Wallet) to one account with this code:

fun sendAllFundsOnAddress(address: String) {
        val receiver = Address.fromString(params, address)
        val tx = Transaction(params)

        val balance = Coin.valueOf(walletAppKit.wallet().unspents.map { it.value.value }.sum())
        tx.addOutput(balance, receiver)

        walletAppKit.wallet().unspents.forEach {
            val key = walletAppKit.wallet().findKeyFromAddress(Address.fromString(params, it.scriptPubKey.getToAddress(params).toString()))
            tx.addSignedInput(it.outPointFor, it.scriptPubKey, key, Transaction.SigHash.ALL, true)
        }

        walletAppKit.wallet().commitTx(tx)
        val sendRequest = SendRequest.forTx(tx)
        walletAppKit.wallet().completeTx(sendRequest)
    }

But I get NullPointerException, while it calculates witness (in input signing process).

Could you give any ideas, what's wrong?

1
  • Hi snitron, do you perhaps have the stacktrace of the error? It may help potential answerers to narrow down the issue.
    – Murch
    Apr 3 at 2:54
0
SendRequest sendRequest = SendRequest.emptyWallet(receiver);
Transaction tx = walletAppKit().wallet().sendCoinsOffline(sendRequest);
System.out.println(Hex.toHexString(tx.bitcoinSerialize()));

In your original code, you loop through each UTXO in your wallet and add it as a signed input, essentially emptying the wallet to the output specified.

The code above does the same but using a different method. You should also be calling completeTx() before commitTx(), but sendCoinsOffline() takes care of that for you.

The raw transaction hex is then printed out in my code, which you can then send to a node to broadcast. If you want bitcoinj's WalletAppKit to do that for you as well, look into Wallet.sendCoins().

The transaction generated with your code would be invalid either way, as you aren't subtracting enough (or anything, in your case) for transaction fees.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.