Add watched scripts to the wallet toString output.

Support watching scripts/addresses in WalletTool
This commit is contained in:
Mike Hearn 2013-11-30 15:52:37 +01:00
parent da2e3e6c98
commit 963978c468
2 changed files with 48 additions and 10 deletions

View File

@ -1942,6 +1942,11 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
} }
} }
/**
* Returns all the outputs that match addresses or scripts added via {@link #addWatchedAddress(Address)} or
* {@link #addWatchedScripts(java.util.List)}.
* @param excludeImmatureCoinbases Whether to ignore outputs that are unspendable due to being immature.
*/
public LinkedList<TransactionOutput> getWatchedOutputs(boolean excludeImmatureCoinbases) { public LinkedList<TransactionOutput> getWatchedOutputs(boolean excludeImmatureCoinbases) {
lock.lock(); lock.lock();
try { try {
@ -2031,18 +2036,18 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
return isWatchedScript(script); return isWatchedScript(script);
} }
/** See {@link #addWatchedAddress(Address, long)} */ /**
* Same as {@link #addWatchedAddress(Address, long)} with the current time as the creation time.
*/
public boolean addWatchedAddress(final Address address) { public boolean addWatchedAddress(final Address address) {
long now = Utils.now().getTime() / 1000; long now = Utils.now().getTime() / 1000;
return addWatchedAddresses(Lists.newArrayList(address), now) == 1; return addWatchedAddresses(Lists.newArrayList(address), now) == 1;
} }
/** /**
* Adds the given address to the wallet to be watched. Outputs can be retrieved * Adds the given address to the wallet to be watched. Outputs can be retrieved by {@link #getWatchedOutputs(boolean)}.
* by {@link #getWatchedOutputs(boolean)}.
* *
* @param creationTime creation time in seconds since the epoch, for scanning the blockchain * @param creationTime creation time in seconds since the epoch, for scanning the blockchain
*
* @return whether the address was added successfully (not already present) * @return whether the address was added successfully (not already present)
*/ */
public boolean addWatchedAddress(final Address address, long creationTime) { public boolean addWatchedAddress(final Address address, long creationTime) {
@ -2289,6 +2294,16 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
builder.append(includePrivateKeys ? key.toStringWithPrivate() : key.toString()); builder.append(includePrivateKeys ? key.toStringWithPrivate() : key.toString());
builder.append("\n"); builder.append("\n");
} }
if (!watchedScripts.isEmpty()) {
builder.append("\nWatched scripts:\n");
for (Script script : watchedScripts) {
builder.append(" ");
builder.append(script.toString());
builder.append("\n");
}
}
if (includeTransactions) { if (includeTransactions) {
// Print the transactions themselves // Print the transactions themselves
if (unspent.size() > 0) { if (unspent.size() > 0) {

View File

@ -88,6 +88,7 @@ public class WalletTool {
" If --privkey is specified, use as a hex/base58 encoded private key.\n" + " If --privkey is specified, use as a hex/base58 encoded private key.\n" +
" Don't specify --pubkey in that case, it will be derived automatically.\n" + " Don't specify --pubkey in that case, it will be derived automatically.\n" +
" If --pubkey is specified, use as a hex/base58 encoded non-compressed public key.\n" + " If --pubkey is specified, use as a hex/base58 encoded non-compressed public key.\n" +
" --action=ADD_ADDR Requires --addr to be specified, and adds it as a watching address.\n" +
" --action=DELETE_KEY Removes the key specified by --pubkey or --addr from the wallet.\n" + " --action=DELETE_KEY Removes the key specified by --pubkey or --addr from the wallet.\n" +
" --action=SYNC Sync the wallet with the latest block chain (download new transactions).\n" + " --action=SYNC Sync the wallet with the latest block chain (download new transactions).\n" +
" If the chain file does not exist this will RESET the wallet.\n" + " If the chain file does not exist this will RESET the wallet.\n" +
@ -199,6 +200,7 @@ public class WalletTool {
RAW_DUMP, RAW_DUMP,
CREATE, CREATE,
ADD_KEY, ADD_KEY,
ADD_ADDR,
DELETE_KEY, DELETE_KEY,
SYNC, SYNC,
RESET, RESET,
@ -357,6 +359,7 @@ public class WalletTool {
switch (action) { switch (action) {
case DUMP: dumpWallet(); break; case DUMP: dumpWallet(); break;
case ADD_KEY: addKey(); break; case ADD_KEY: addKey(); break;
case ADD_ADDR: addAddr(); break;
case DELETE_KEY: deleteKey(); break; case DELETE_KEY: deleteKey(); break;
case RESET: reset(); break; case RESET: reset(); break;
case SYNC: syncChain(); break; case SYNC: syncChain(); break;
@ -404,6 +407,21 @@ public class WalletTool {
shutdown(); shutdown();
} }
private static void addAddr() {
String addr = (String) options.valueOf("addr");
if (addr == null) {
System.err.println("You must specify an --addr to watch.");
return;
}
try {
Address address = new Address(params, addr);
// If no creation time is specified, assume genesis (zero).
wallet.addWatchedAddress(address, getCreationTimeSeconds());
} catch (AddressFormatException e) {
System.err.println("Could not parse given address, or wrong network: " + addr);
}
}
private static void send(List<String> outputs, BigInteger fee, String lockTimeStr, boolean allowUnconfirmed) throws VerificationException { private static void send(List<String> outputs, BigInteger fee, String lockTimeStr, boolean allowUnconfirmed) throws VerificationException {
try { try {
// Convert the input strings to outputs. // Convert the input strings to outputs.
@ -680,12 +698,7 @@ public class WalletTool {
private static void addKey() { private static void addKey() {
ECKey key; ECKey key;
long creationTimeSeconds = 0; long creationTimeSeconds = getCreationTimeSeconds();
if (options.has(unixtimeFlag)) {
creationTimeSeconds = unixtimeFlag.value(options);
} else if (options.has(dateFlag)) {
creationTimeSeconds = dateFlag.value(options).getTime() / 1000;
}
if (options.has("privkey")) { if (options.has("privkey")) {
String data = (String) options.valueOf("privkey"); String data = (String) options.valueOf("privkey");
if (data.startsWith("5J") || data.startsWith("5H") || data.startsWith("5K")) { if (data.startsWith("5J") || data.startsWith("5H") || data.startsWith("5K")) {
@ -739,6 +752,16 @@ public class WalletTool {
System.out.println(key.toAddress(params) + " " + key); System.out.println(key.toAddress(params) + " " + key);
} }
private static long getCreationTimeSeconds() {
long creationTimeSeconds = 0;
if (options.has(unixtimeFlag)) {
creationTimeSeconds = unixtimeFlag.value(options);
} else if (options.has(dateFlag)) {
creationTimeSeconds = dateFlag.value(options).getTime() / 1000;
}
return creationTimeSeconds;
}
private static void deleteKey() { private static void deleteKey() {
String pubkey = (String) options.valueOf("pubkey"); String pubkey = (String) options.valueOf("pubkey");
String addr = (String) options.valueOf("addr"); String addr = (String) options.valueOf("addr");