From 559270095bf807c4f795dc4beed422ae62aef59d Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Wed, 14 Apr 2021 21:43:52 +0200 Subject: [PATCH] WalletTool: Tighten two try blocks when sending. Conflicts: tools/src/main/java/org/bitcoinj/tools/WalletTool.java --- .../java/org/bitcoinj/tools/WalletTool.java | 105 +++++++++--------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java index dd71b14dc9d..4af22465f4d 100644 --- a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java +++ b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java @@ -647,46 +647,47 @@ private static void addAddr() { } private static void send(List outputs, Coin feePerVkb, String lockTimeStr, boolean allowUnconfirmed) throws VerificationException { - try { - // Convert the input strings to outputs. - Transaction t = new Transaction(params); - for (String spec : outputs) { - try { - OutputSpec outputSpec = new OutputSpec(spec); - if (outputSpec.isAddress()) { - t.addOutput(outputSpec.value, outputSpec.addr); - } else { - t.addOutput(outputSpec.value, outputSpec.key); - } - } catch (AddressFormatException.WrongNetwork e) { - System.err.println("Malformed output specification, address is for a different network: " + spec); - return; - } catch (AddressFormatException e) { - System.err.println("Malformed output specification, could not parse as address: " + spec); - return; - } catch (NumberFormatException e) { - System.err.println("Malformed output specification, could not parse as value: " + spec); - return; - } catch (IllegalArgumentException e) { - System.err.println(e.getMessage()); - return; + // Convert the input strings to outputs. + Transaction t = new Transaction(params); + for (String spec : outputs) { + try { + OutputSpec outputSpec = new OutputSpec(spec); + if (outputSpec.isAddress()) { + t.addOutput(outputSpec.value, outputSpec.addr); + } else { + t.addOutput(outputSpec.value, outputSpec.key); } + } catch (AddressFormatException.WrongNetwork e) { + System.err.println("Malformed output specification, address is for a different network: " + spec); + return; + } catch (AddressFormatException e) { + System.err.println("Malformed output specification, could not parse as address: " + spec); + return; + } catch (NumberFormatException e) { + System.err.println("Malformed output specification, could not parse as value: " + spec); + return; + } catch (IllegalArgumentException e) { + System.err.println(e.getMessage()); + return; } - SendRequest req = SendRequest.forTx(t); - if (t.getOutputs().size() == 1 && t.getOutput(0).getValue().equals(wallet.getBalance())) { - log.info("Emptying out wallet, recipient may get less than what you expect"); - req.emptyWallet = true; - } - if (feePerVkb != null) - req.setFeePerVkb(feePerVkb); - if (allowUnconfirmed) { - wallet.allowSpendingUnconfirmedTransactions(); - } - if (password != null) { - req.aesKey = passwordToKey(true); - if (req.aesKey == null) - return; // Error message already printed. - } + } + SendRequest req = SendRequest.forTx(t); + if (t.getOutputs().size() == 1 && t.getOutput(0).getValue().equals(wallet.getBalance())) { + log.info("Emptying out wallet, recipient may get less than what you expect"); + req.emptyWallet = true; + } + if (feePerVkb != null) + req.setFeePerVkb(feePerVkb); + if (allowUnconfirmed) { + wallet.allowSpendingUnconfirmedTransactions(); + } + if (password != null) { + req.aesKey = passwordToKey(true); + if (req.aesKey == null) + return; // Error message already printed. + } + + try { wallet.completeTx(req); try { @@ -1126,21 +1127,21 @@ private static void sendPaymentRequest(String location, boolean verifyPki) { } private static void send(PaymentSession session) { + System.out.println("Payment Request"); + System.out.println("Coin: " + session.getValue().toFriendlyString()); + System.out.println("Date: " + session.getDate()); + System.out.println("Memo: " + session.getMemo()); + if (session.pkiVerificationData != null) { + System.out.println("Pki-Verified Name: " + session.pkiVerificationData.displayName); + System.out.println("PKI data verified by: " + session.pkiVerificationData.rootAuthorityName); + } + final SendRequest req = session.getSendRequest(); + if (password != null) { + req.aesKey = passwordToKey(true); + if (req.aesKey == null) + return; // Error message already printed. + } try { - System.out.println("Payment Request"); - System.out.println("Coin: " + session.getValue().toFriendlyString()); - System.out.println("Date: " + session.getDate()); - System.out.println("Memo: " + session.getMemo()); - if (session.pkiVerificationData != null) { - System.out.println("Pki-Verified Name: " + session.pkiVerificationData.displayName); - System.out.println("PKI data verified by: " + session.pkiVerificationData.rootAuthorityName); - } - final SendRequest req = session.getSendRequest(); - if (password != null) { - req.aesKey = passwordToKey(true); - if (req.aesKey == null) - return; // Error message already printed. - } wallet.completeTx(req); // may throw InsufficientMoneyException. if (options.has("offline")) { wallet.commitTx(req.tx);