From 4baeeaf44a56ff2eb9743f66719299bd7ff7e8b2 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Tue, 19 Feb 2019 11:58:05 +0100 Subject: [PATCH] DeterministicKey, ECKey: In toString(), add a comment so that the role of the key in a keychain becomes clear. --- .../src/main/java/org/bitcoinj/core/ECKey.java | 4 +++- .../org/bitcoinj/crypto/DeterministicKey.java | 7 +++++-- .../org/bitcoinj/wallet/BasicKeyChain.java | 2 +- .../bitcoinj/wallet/DeterministicKeyChain.java | 18 ++++++++++++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/ECKey.java b/core/src/main/java/org/bitcoinj/core/ECKey.java index 17a204fe..8ff61028 100644 --- a/core/src/main/java/org/bitcoinj/core/ECKey.java +++ b/core/src/main/java/org/bitcoinj/core/ECKey.java @@ -1290,7 +1290,7 @@ public class ECKey implements EncryptableItem { } public void formatKeyWithAddress(boolean includePrivateKeys, @Nullable KeyParameter aesKey, StringBuilder builder, - NetworkParameters params, Script.ScriptType outputScriptType) { + NetworkParameters params, Script.ScriptType outputScriptType, @Nullable String comment) { builder.append(" addr:"); if (outputScriptType != null) { builder.append(Address.fromKey(params, this, outputScriptType)); @@ -1306,6 +1306,8 @@ public class ECKey implements EncryptableItem { if (creationTimeSeconds > 0) builder.append(" creationTimeSeconds:").append(creationTimeSeconds).append(" [") .append(Utils.dateTimeFormat(creationTimeSeconds * 1000)).append("]"); + if (comment != null) + builder.append(" (").append(comment).append(")"); builder.append("\n"); if (includePrivateKeys) { builder.append(" "); diff --git a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java index da8d548a..23b98b4d 100644 --- a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java +++ b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java @@ -632,10 +632,13 @@ public class DeterministicKey extends ECKey { @Override public void formatKeyWithAddress(boolean includePrivateKeys, @Nullable KeyParameter aesKey, StringBuilder builder, - NetworkParameters params, Script.ScriptType outputScriptType) { + NetworkParameters params, Script.ScriptType outputScriptType, @Nullable String comment) { builder.append(" addr:").append(Address.fromKey(params, this, outputScriptType).toString()); builder.append(" hash160:").append(Utils.HEX.encode(getPubKeyHash())); - builder.append(" (").append(getPathAsString()).append(")\n"); + builder.append(" (").append(getPathAsString()); + if (comment != null) + builder.append(", ").append(comment); + builder.append(")\n"); if (includePrivateKeys) { builder.append(" ").append(toStringWithPrivate(aesKey, params)).append("\n"); } diff --git a/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java index 31bbd27e..bbbe72f0 100644 --- a/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/BasicKeyChain.java @@ -626,7 +626,7 @@ public class BasicKeyChain implements EncryptableKeyChain { List keys = getKeys(); Collections.sort(keys, ECKey.AGE_COMPARATOR); for (ECKey key : keys) - key.formatKeyWithAddress(includePrivateKeys, aesKey, builder, params, null); + key.formatKeyWithAddress(includePrivateKeys, aesKey, builder, params, null, "imported"); return builder.toString(); } } diff --git a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java index 1db8a407..a4078c1f 100644 --- a/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java +++ b/core/src/main/java/org/bitcoinj/wallet/DeterministicKeyChain.java @@ -1402,8 +1402,22 @@ public class DeterministicKeyChain implements EncryptableKeyChain { protected void formatAddresses(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params, StringBuilder builder) { - for (ECKey key : getKeys(false, true)) - key.formatKeyWithAddress(includePrivateKeys, aesKey, builder, params, outputScriptType); + for (DeterministicKey key : getKeys(false, true)) { + String comment = null; + if (key.equals(rootKey)) + comment = "root"; + else if (key.equals(getWatchingKey())) + comment = "account"; + else if (key.equals(internalParentKey)) + comment = "internal"; + else if (key.equals(externalParentKey)) + comment = "external"; + else if (internalParentKey.equals(key.getParent()) && key.getChildNumber().i() >= issuedInternalKeys) + comment = "*"; + else if (externalParentKey.equals(key.getParent()) && key.getChildNumber().i() >= issuedExternalKeys) + comment = "*"; + key.formatKeyWithAddress(includePrivateKeys, aesKey, builder, params, outputScriptType, comment); + } } /** The number of signatures required to spend coins received by this keychain. */