diff --git a/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java b/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java index db3c3a6f1..247177b1f 100644 --- a/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java +++ b/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java @@ -656,26 +656,22 @@ public class WalletTool implements Callable { return; } } - SendRequest req = SendRequest.forTx(tx); - if (coinSelector != null) { - req.coinSelector = coinSelector; - req.recipientsPayFees = true; - } - if (tx.getOutputs().size() == 1 && tx.getOutput(0).getValue().equals(balance)) { + boolean emptyWallet = tx.getOutputs().size() == 1 && tx.getOutput(0).getValue().equals(balance); + if (emptyWallet) { log.info("Emptying out wallet, recipient may get less than what you expect"); - req.emptyWallet = true; - } - if (feePerVkb != null) - req.setFeePerVkb(feePerVkb); - if (allowUnconfirmed) { - req.allowUnconfirmed(); } + + AesKey aesKey; if (password != null) { - req.aesKey = passwordToKey(true); - if (req.aesKey == null) + aesKey = passwordToKey(true); + if (aesKey == null) return; // Error message already printed. + } else { + aesKey = null; } + SendRequest req = buildSendRequest(tx, emptyWallet, allowUnconfirmed, coinSelector, feePerVkb, aesKey); + try { wallet.completeTx(req); } catch (InsufficientMoneyException e) { @@ -723,6 +719,24 @@ public class WalletTool implements Callable { } } + // "Atomically" create a SendRequest. In the future SendRequest may be immutable and this method will be updated + private SendRequest buildSendRequest(Transaction tx, boolean emptyWallet, boolean allowUnconfirmed, @Nullable CoinSelector coinSelector, @Nullable Coin feePerVkb, @Nullable AesKey aesKey) { + SendRequest req = SendRequest.forTx(tx); + req.emptyWallet = emptyWallet; + if (coinSelector != null) { + req.coinSelector = coinSelector; + req.recipientsPayFees = true; + } + if (allowUnconfirmed) { + // Note that this will overwrite the CoinSelector set above + req.allowUnconfirmed(); + } + if (feePerVkb != null) + req.setFeePerVkb(feePerVkb); + req.aesKey = aesKey; + return req; + } + static class OutputSpec { public final Coin value; public final Address addr;