diff --git a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java index a4078c1f..fe19679b 100644 --- a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java @@ -1371,7 +1371,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain { return helper.toString(); } - public String toString(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params) { + public String toString(boolean includeLookahead, boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params) { final DeterministicKey watchingKey = getWatchingKey(); final StringBuilder builder = new StringBuilder(); if (seed != null) { @@ -1396,13 +1396,13 @@ public class DeterministicKeyChain implements EncryptableKeyChain { builder.append("Key to watch: ").append(watchingKey.serializePubB58(params, outputScriptType)) .append('\n'); builder.append("Lookahead siz/thr: ").append(lookaheadSize).append('/').append(lookaheadThreshold).append('\n'); - formatAddresses(includePrivateKeys, aesKey, params, builder); + formatAddresses(includeLookahead, includePrivateKeys, aesKey, params, builder); return builder.toString(); } - protected void formatAddresses(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params, - StringBuilder builder) { - for (DeterministicKey key : getKeys(false, true)) { + protected void formatAddresses(boolean includeLookahead, boolean includePrivateKeys, @Nullable KeyParameter aesKey, + NetworkParameters params, StringBuilder builder) { + for (DeterministicKey key : getKeys(includeLookahead, true)) { String comment = null; if (key.equals(rootKey)) comment = "root"; diff --git a/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java b/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java index 3350ef18..18be8580 100644 --- a/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java +++ b/core/src/main/java/org/bitcoinj/wallet/KeyChainGroup.java @@ -1006,13 +1006,13 @@ public class KeyChainGroup implements KeyBag { } } - public String toString(boolean includePrivateKeys, @Nullable KeyParameter aesKey) { + public String toString(boolean includeLookahead, boolean includePrivateKeys, @Nullable KeyParameter aesKey) { final StringBuilder builder = new StringBuilder(); if (basic != null) builder.append(basic.toString(includePrivateKeys, aesKey, params)); if (chains != null) for (DeterministicKeyChain chain : chains) - builder.append(chain.toString(includePrivateKeys, aesKey, params)).append('\n'); + builder.append(chain.toString(includeLookahead, includePrivateKeys, aesKey, params)).append('\n'); return builder.toString(); } diff --git a/core/src/main/java/org/bitcoinj/wallet/MarriedKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/MarriedKeyChain.java index 3d8f5afd..c8e7aadc 100644 --- a/core/src/main/java/org/bitcoinj/wallet/MarriedKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/MarriedKeyChain.java @@ -233,14 +233,14 @@ public class MarriedKeyChain extends DeterministicKeyChain { } @Override - protected void formatAddresses(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params, - StringBuilder builder2) { + protected void formatAddresses(boolean includeLookahead, boolean includePrivateKeys, @Nullable KeyParameter aesKey, + NetworkParameters params, StringBuilder builder) { for (DeterministicKeyChain followingChain : followingKeyChains) - builder2.append("Following chain: ").append(followingChain.getWatchingKey().serializePubB58(params)) + builder.append("Following chain: ").append(followingChain.getWatchingKey().serializePubB58(params)) .append('\n'); - builder2.append('\n'); + builder.append('\n'); for (RedeemData redeemData : marriedKeysRedeemData.values()) - formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2, params); + formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder, params); } private void formatScript(Script script, StringBuilder builder, NetworkParameters params) { diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index ec22845e..8327483d 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -3335,29 +3335,39 @@ public class Wallet extends BaseTaggableObject @Override public String toString() { - return toString(false, null, true, true, null); + return toString(false, false, null, true, true, null); } /** - * @deprecated Use {@link #toString(boolean, KeyParameter, boolean, boolean, AbstractBlockChain)} instead. + * @deprecated Use {@link #toString(boolean, boolean, KeyParameter, boolean, boolean, AbstractBlockChain)} instead. */ @Deprecated public String toString(boolean includePrivateKeys, boolean includeTransactions, boolean includeExtensions, @Nullable AbstractBlockChain chain) { - return toString(includePrivateKeys, null, includeTransactions, includeExtensions, chain); + return toString(false, includePrivateKeys, null, includeTransactions, includeExtensions, chain); + } + + /** + * @deprecated Use {@link #toString(boolean, boolean, KeyParameter, boolean, boolean, AbstractBlockChain)} instead. + */ + @Deprecated + public String toString(boolean includePrivateKeys, @Nullable KeyParameter aesKey, boolean includeTransactions, + boolean includeExtensions, @Nullable AbstractBlockChain chain) { + return toString(false, includePrivateKeys, aesKey, includeTransactions, includeExtensions, chain); } /** * Formats the wallet as a human readable piece of text. Intended for debugging, the format is not meant to be * stable or human readable. + * @param includeLookahead Wether lookahead keys should be included. * @param includePrivateKeys Whether raw private key data should be included. * @param aesKey for decrypting private key data for if the wallet is encrypted. * @param includeTransactions Whether to print transaction data. * @param includeExtensions Whether to print extension data. * @param chain If set, will be used to estimate lock times for block timelocked transactions. */ - public String toString(boolean includePrivateKeys, @Nullable KeyParameter aesKey, boolean includeTransactions, - boolean includeExtensions, @Nullable AbstractBlockChain chain) { + public String toString(boolean includeLookahead, boolean includePrivateKeys, @Nullable KeyParameter aesKey, + boolean includeTransactions, boolean includeExtensions, @Nullable AbstractBlockChain chain) { lock.lock(); keyChainGroupLock.lock(); try { @@ -3390,7 +3400,7 @@ public class Wallet extends BaseTaggableObject final Date keyRotationTime = getKeyRotationTime(); if (keyRotationTime != null) builder.append("Key rotation time: ").append(Utils.dateTimeFormat(keyRotationTime)).append('\n'); - builder.append(keyChainGroup.toString(includePrivateKeys, aesKey)); + builder.append(keyChainGroup.toString(includeLookahead, includePrivateKeys, aesKey)); if (!watchedScripts.isEmpty()) { builder.append("\nWatched scripts:\n"); diff --git a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java index 1b4d67e7..a01bbdcf 100644 --- a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java +++ b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java @@ -267,6 +267,7 @@ public class WalletTool { OptionSpec paymentRequestLocation = parser.accepts("payment-request").withRequiredArg(); parser.accepts("no-pki"); parser.accepts("dump-privkeys"); + parser.accepts("dump-lookahead"); OptionSpec refundFlag = parser.accepts("refund-to").withRequiredArg(); OptionSpec txHashFlag = parser.accepts("txhash").withRequiredArg(); options = parser.parse(args); @@ -1526,18 +1527,19 @@ public class WalletTool { setup(); final boolean dumpPrivkeys = options.has("dump-privkeys"); + final boolean dumpLookahead = options.has("dump-lookahead"); if (dumpPrivkeys && wallet.isEncrypted()) { if (password != null) { final KeyParameter aesKey = passwordToKey(true); if (aesKey == null) return; // Error message already printed. - System.out.println(wallet.toString(true, aesKey, true, true, chain)); + System.out.println(wallet.toString(dumpLookahead, true, aesKey, true, true, chain)); } else { System.err.println("Can't dump privkeys, wallet is encrypted."); return; } } else { - System.out.println(wallet.toString(dumpPrivkeys, null, true, true, chain)); + System.out.println(wallet.toString(dumpLookahead, dumpPrivkeys, null, true, true, chain)); } } diff --git a/tools/src/main/resources/org/bitcoinj/tools/wallet-tool-help.txt b/tools/src/main/resources/org/bitcoinj/tools/wallet-tool-help.txt index 7a51c980..be1b3fb0 100644 --- a/tools/src/main/resources/org/bitcoinj/tools/wallet-tool-help.txt +++ b/tools/src/main/resources/org/bitcoinj/tools/wallet-tool-help.txt @@ -7,6 +7,7 @@ Usage: wallet-tool --flags action-name dump Loads and prints the given wallet in textual form to stdout. Private keys and seed are only printed if --dump-privkeys is specified. If the wallet is encrypted, also specify the --password option to dump the private keys and seed. + If --dump-lookahead is present, also show pregenerated but not yet issued keys. raw-dump Prints the wallet as a raw protobuf with no parsing or sanity checking applied. create Makes a new wallet in the file specified by --wallet. Will complain and require --force if the wallet already exists.