Skip to content

Commit

Permalink
WalletTool: Tighten two try blocks when sending.
Browse files Browse the repository at this point in the history
 Conflicts:
	tools/src/main/java/org/bitcoinj/tools/WalletTool.java
  • Loading branch information
Andreas Schildbach authored and ripcurlx committed Aug 17, 2021
1 parent 2d0552b commit 5592700
Showing 1 changed file with 53 additions and 52 deletions.
105 changes: 53 additions & 52 deletions tools/src/main/java/org/bitcoinj/tools/WalletTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,46 +647,47 @@ private static void addAddr() {
}

private static void send(List<String> 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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5592700

Please sign in to comment.