3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +00:00

Update toString() in ECKey to include private key in WIF format, adds helper methods with tests.

This commit is contained in:
Wojciech Langiewicz 2014-10-23 15:34:20 +02:00 committed by Andreas Schildbach
parent 47cdf5f70e
commit ee08ba4d5d
3 changed files with 35 additions and 10 deletions

View File

@ -1117,23 +1117,36 @@ public class ECKey implements EncryptableItem, Serializable {
@Override
public String toString() {
return toString(false);
return toString(false, null);
}
/**
* Produce a string rendering of the ECKey INCLUDING the private key.
* Unless you absolutely need the private key it is better for security reasons to just use {@link #toString()}.
*/
public String toStringWithPrivate() {
return toString(true);
public String toStringWithPrivate(NetworkParameters params) {
return toString(true, params);
}
private String toString(boolean includePrivate) {
public String getPrivateKeyAsHex() {
return Utils.HEX.encode(getPrivKeyBytes());
}
public String getPublicKeyAsHex() {
return Utils.HEX.encode(pub.getEncoded());
}
public String getPrivateKeyAsWiF(NetworkParameters params) {
return getPrivateKeyEncoded(params).toString();
}
private String toString(boolean includePrivate, NetworkParameters params) {
final ToStringHelper helper = Objects.toStringHelper(this).omitNullValues();
helper.add("pub", Utils.HEX.encode(pub.getEncoded()));
helper.add("pub HEX", getPublicKeyAsHex());
if (includePrivate) {
try {
helper.add("priv", Utils.HEX.encode(getPrivKey().toByteArray()));
helper.add("priv HEX", getPrivateKeyAsHex());
helper.add("priv WIF", getPrivateKeyAsWiF(params));
} catch (IllegalStateException e) {
// TODO: Make hasPrivKey() work for deterministic keys and fix this.
}
@ -1156,7 +1169,7 @@ public class ECKey implements EncryptableItem, Serializable {
builder.append("\n");
if (includePrivateKeys) {
builder.append(" ");
builder.append(toStringWithPrivate());
builder.append(toStringWithPrivate(params));
builder.append("\n");
}
}

View File

@ -482,7 +482,7 @@ public class DeterministicKey extends ECKey {
builder.append("\n");
if (includePrivateKeys) {
builder.append(" ");
builder.append(toStringWithPrivate());
builder.append(toStringWithPrivate(params));
builder.append("\n");
}
}

View File

@ -317,9 +317,21 @@ public class ECKeyTest {
@Test
public void testToString() throws Exception {
ECKey key = ECKey.fromPrivate(BigInteger.TEN).decompress(); // An example private key.
NetworkParameters params = MainNetParams.get();
assertEquals("ECKey{pub HEX=04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7, isEncrypted=false}", key.toString());
assertEquals("ECKey{pub HEX=04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7, priv HEX=000000000000000000000000000000000000000000000000000000000000000a, priv WIF=5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreBoNWTw6, isEncrypted=false}", key.toStringWithPrivate(params));
}
assertEquals("ECKey{pub=04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7, isEncrypted=false}", key.toString());
assertEquals("ECKey{pub=04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7, priv=0a, isEncrypted=false}", key.toStringWithPrivate());
@Test
public void testGetPrivateKeyAsHex() throws Exception {
ECKey key = ECKey.fromPrivate(BigInteger.TEN).decompress(); // An example private key.
assertEquals("000000000000000000000000000000000000000000000000000000000000000a", key.getPrivateKeyAsHex());
}
@Test
public void testGetPublicKeyAsHex() throws Exception {
ECKey key = ECKey.fromPrivate(BigInteger.TEN).decompress(); // An example private key.
assertEquals("04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7", key.getPublicKeyAsHex());
}
@Test