3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Change printed format of the wallet to be less verbose and make wallet-tool not dump privkeys by default.

This commit is contained in:
Mike Hearn 2014-08-11 18:19:24 +02:00
parent e8ba287029
commit 90492b61f7
5 changed files with 42 additions and 12 deletions

View File

@ -1095,8 +1095,13 @@ public class ECKey implements EncryptableItem, Serializable {
private String toString(boolean includePrivate) { private String toString(boolean includePrivate) {
final ToStringHelper helper = Objects.toStringHelper(this).omitNullValues(); final ToStringHelper helper = Objects.toStringHelper(this).omitNullValues();
helper.add("pub", Utils.HEX.encode(pub.getEncoded())); helper.add("pub", Utils.HEX.encode(pub.getEncoded()));
if (includePrivate && priv != null) if (includePrivate) {
helper.add("priv", Utils.HEX.encode(priv.toByteArray())); try {
helper.add("priv", Utils.HEX.encode(getPrivKey().toByteArray()));
} catch (IllegalStateException e) {
// TODO: Make hasPrivKey() work for deterministic keys and fix this.
}
}
if (creationTimeSeconds > 0) if (creationTimeSeconds > 0)
helper.add("creationTimeSeconds", creationTimeSeconds); helper.add("creationTimeSeconds", creationTimeSeconds);
helper.add("keyCrypter", keyCrypter); helper.add("keyCrypter", keyCrypter);

View File

@ -1007,18 +1007,33 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
} }
} }
// For internal usage only (for printing keys in KeyChainGroup). // For internal usage only
/* package */ List<ECKey> getKeys() { /* package */ List<ECKey> getKeys(boolean includeLookahead) {
return basicKeyChain.getKeys(); List<ECKey> keys = basicKeyChain.getKeys();
if (!includeLookahead) {
int treeSize = internalKey.getPath().size();
List<ECKey> issuedKeys = new LinkedList<ECKey>();
for (ECKey key : keys) {
DeterministicKey detkey = (DeterministicKey) key;
DeterministicKey parent = detkey.getParent();
if (parent == null) continue;
if (detkey.getPath().size() <= treeSize) continue;
if (parent.equals(internalKey) && detkey.getChildNumber().i() > issuedInternalKeys) continue;
if (parent.equals(externalKey) && detkey.getChildNumber().i() > issuedExternalKeys) continue;
issuedKeys.add(detkey);
}
return issuedKeys;
}
return keys;
} }
/** /**
* Returns leaf keys issued by this chain (not including lookahead zone) * Returns leaf keys issued by this chain (including lookahead zone)
*/ */
public List<DeterministicKey> getLeafKeys() { public List<DeterministicKey> getLeafKeys() {
ImmutableList.Builder<DeterministicKey> keys = ImmutableList.builder(); ImmutableList.Builder<DeterministicKey> keys = ImmutableList.builder();
for (ECKey key : getKeys()) { for (ECKey key : getKeys(true)) {
DeterministicKey dKey = (DeterministicKey) key; DeterministicKey dKey = (DeterministicKey) key;
if (dKey.getPath().size() > 2) { if (dKey.getPath().size() > 2) {
keys.add(dKey); keys.add(dKey);

View File

@ -872,7 +872,7 @@ public class KeyChainGroup implements KeyBag {
for (RedeemData redeemData : marriedKeysRedeemData.values()) for (RedeemData redeemData : marriedKeysRedeemData.values())
formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2); formatScript(ScriptBuilder.createP2SHOutputScript(redeemData.redeemScript), builder2);
} else { } else {
for (ECKey key : chain.getKeys()) for (ECKey key : chain.getKeys(false))
formatKeyWithAddress(includePrivateKeys, key, builder2); formatKeyWithAddress(includePrivateKeys, key, builder2);
} }
chainStrs.add(builder2.toString()); chainStrs.add(builder2.toString());
@ -895,9 +895,17 @@ public class KeyChainGroup implements KeyBag {
builder.append(address.toString()); builder.append(address.toString());
builder.append(" hash160:"); builder.append(" hash160:");
builder.append(Utils.HEX.encode(key.getPubKeyHash())); builder.append(Utils.HEX.encode(key.getPubKeyHash()));
builder.append("\n "); if (key instanceof DeterministicKey) {
builder.append(includePrivateKeys ? key.toStringWithPrivate() : key.toString()); builder.append(" (");
builder.append((((DeterministicKey) key).getPathAsString()));
builder.append(")");
}
builder.append("\n"); builder.append("\n");
if (includePrivateKeys) {
builder.append(" ");
builder.append(key.toStringWithPrivate());
builder.append("\n");
}
} }
/** Returns a copy of the current list of chains. */ /** Returns a copy of the current list of chains. */

View File

@ -222,6 +222,7 @@ public class WalletTool {
OptionSpec<String> paymentRequestLocation = parser.accepts("payment-request").withRequiredArg(); OptionSpec<String> paymentRequestLocation = parser.accepts("payment-request").withRequiredArg();
parser.accepts("no-pki"); parser.accepts("no-pki");
parser.accepts("tor"); parser.accepts("tor");
parser.accepts("dump-privkeys");
options = parser.parse(args); options = parser.parse(args);
final String HELP_TEXT = Resources.toString(WalletTool.class.getResource("wallet-tool-help.txt"), Charsets.UTF_8); final String HELP_TEXT = Resources.toString(WalletTool.class.getResource("wallet-tool-help.txt"), Charsets.UTF_8);
@ -1057,6 +1058,6 @@ public class WalletTool {
// there just for the dump case. // there just for the dump case.
if (chainFileName.exists()) if (chainFileName.exists())
setup(); setup();
System.out.println(wallet.toString(true, true, true, chain)); System.out.println(wallet.toString(options.has("dump-privkeys"), true, true, chain));
} }
} }

View File

@ -4,7 +4,8 @@ Usage: wallet-tool --flags action-name
wallet-tool action-name --flags wallet-tool action-name --flags
>>> ACTIONS >>> ACTIONS
dump Loads and prints the given wallet in textual form to stdout. dump Loads and prints the given wallet in textual form to stdout. Private keys are only printed
if --dump-privkeys is specified.
raw-dump Prints the wallet as a raw protobuf with no parsing or sanity checking applied. 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. create Makes a new wallet in the file specified by --wallet.
Will complain and require --force if the wallet already exists. Will complain and require --force if the wallet already exists.