This commit is contained in:
Andrey Litvitski 2025-03-11 12:21:09 -05:00 committed by GitHub
commit 2fd7169813
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -231,53 +231,47 @@ public class WalletTool implements Callable<Integer> {
private static PeerGroup peerGroup; private static PeerGroup peerGroup;
private static Wallet wallet; private static Wallet wallet;
public static class Condition { public record Condition(Type type, String value) {
public enum Type { public enum Type {
// Less than, greater than, less than or equal, greater than or equal. // Less than, greater than, less than or equal, greater than or equal.
EQUAL, LT, GT, LTE, GTE EQUAL, LT, GT, LTE, GTE
} }
Type type;
String value;
public Condition(String from) { public Condition(String from) {
if (from.length() < 2) throw new RuntimeException("Condition string too short: " + from); this(extractType(from), extractValue(from));
}
if (from.startsWith("<=")) type = Type.LTE; private static Type extractType(String from) {
else if (from.startsWith(">=")) type = Type.GTE; if (from.startsWith("<=")) return Type.LTE;
else if (from.startsWith("<")) type = Type.LT; else if (from.startsWith(">=")) return Type.GTE;
else if (from.startsWith("=")) type = Type.EQUAL; else if (from.startsWith("<")) return Type.LT;
else if (from.startsWith(">")) type = Type.GT; else if (from.startsWith("=")) return Type.EQUAL;
else throw new RuntimeException("Unknown operator in condition: " + from); else if (from.startsWith(">")) return Type.GT;
else throw new IllegalArgumentException("Unknown operator in condition: " + from);
}
String s; private static String extractValue(String from) {
switch (type) { if (from.startsWith("<=") || from.startsWith(">=")) {
case LT: //When type is LTE or GTE
case GT: return from.substring(2);
case EQUAL: } else if (from.startsWith("<") || from.startsWith(">") || from.startsWith("=")){
s = from.substring(1); return from.substring(1);
break; } else {
case LTE: throw new RuntimeException("Unreachable");
case GTE:
s = from.substring(2);
break;
default:
throw new RuntimeException("Unreachable");
} }
value = s;
} }
public boolean matchBitcoins(Coin comparison) { public boolean matchBitcoins(Coin comparison) {
try { try {
Coin units = parseCoin(value); Coin units = parseCoin(value);
switch (type) { return switch (type) {
case LT: return comparison.compareTo(units) < 0; case LT -> comparison.compareTo(units) < 0;
case GT: return comparison.compareTo(units) > 0; case GT -> comparison.compareTo(units) > 0;
case EQUAL: return comparison.compareTo(units) == 0; case EQUAL -> comparison.compareTo(units) == 0;
case LTE: return comparison.compareTo(units) <= 0; case LTE -> comparison.compareTo(units) <= 0;
case GTE: return comparison.compareTo(units) >= 0; case GTE -> comparison.compareTo(units) >= 0;
default: default -> throw new RuntimeException("Unreachable");
throw new RuntimeException("Unreachable"); };
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
System.err.println("Could not parse value from condition string: " + value); System.err.println("Could not parse value from condition string: " + value);
System.exit(1); System.exit(1);
@ -705,30 +699,36 @@ public class WalletTool implements Callable<Integer> {
return req; return req;
} }
static class OutputSpec { public record OutputSpec(Coin value, Address addr, ECKey key) {
public final Coin value;
public final Address addr;
public final ECKey key;
public OutputSpec(String spec) throws IllegalArgumentException { public OutputSpec(String spec) throws IllegalArgumentException {
this(extractValue(spec), extractAddress(spec), extractKey(spec));
}
private static Coin extractValue(String spec) {
String[] parts = spec.split(":"); String[] parts = spec.split(":");
if (parts.length != 2) { if (parts.length != 2) throw new IllegalArgumentException("Malformed output specification, must have two parts separated by :");
throw new IllegalArgumentException("Malformed output specification, must have two parts separated by :");
return "ALL".equalsIgnoreCase(parts[1]) ? null : parseCoin(parts[1]);
}
private static Address extractAddress(String spec) {
String destination = spec.split(":")[0];
if (!destination.startsWith("0")) {
// Treat as an address.
return wallet.parseAddress(destination);
} else {
return null;
} }
String destination = parts[0]; }
if ("ALL".equalsIgnoreCase(parts[1]))
value = null; private static ECKey extractKey(String spec) {
else String destination = spec.split(":")[0];
value = parseCoin(parts[1]);
if (destination.startsWith("0")) { if (destination.startsWith("0")) {
// Treat as a raw public key. // Treat as a raw public key.
byte[] pubKey = new BigInteger(destination, 16).toByteArray(); byte[] pubKey = new BigInteger(destination, 16).toByteArray();
key = ECKey.fromPublicOnly(pubKey); return ECKey.fromPublicOnly(pubKey);
addr = null;
} else { } else {
// Treat as an address. return null;
addr = wallet.parseAddress(destination);
key = null;
} }
} }