3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 07:12:17 +00:00

Move NetworkParameters to first argument in DeterministicKey serialize/deserialize methods, it's more conventional that way.

This commit is contained in:
Mike Hearn 2014-11-14 16:42:14 +01:00
parent 41f8f5ccb4
commit 17fae952a7
2 changed files with 20 additions and 20 deletions

View File

@ -312,14 +312,14 @@ public class DeterministicKey extends ECKey {
}
public byte[] serializePublic(NetworkParameters params) {
return serialize(true, params);
return serialize(params, true);
}
public byte[] serializePrivate(NetworkParameters params) {
return serialize(false, params);
return serialize(params, false);
}
private byte[] serialize(boolean pub, NetworkParameters params) {
private byte[] serialize(NetworkParameters params, boolean pub) {
ByteBuffer ser = ByteBuffer.allocate(78);
ser.putInt(pub ? params.getBip32HeaderPub() : params.getBip32HeaderPriv());
ser.put((byte) getDepth());
@ -336,11 +336,11 @@ public class DeterministicKey extends ECKey {
}
public String serializePubB58(NetworkParameters params) {
return toBase58(serialize(true, params));
return toBase58(serialize(params, true));
}
public String serializePrivB58(NetworkParameters params) {
return toBase58(serialize(false, params));
return toBase58(serialize(params, false));
}
static String toBase58(byte[] ser) {
@ -358,7 +358,7 @@ public class DeterministicKey extends ECKey {
*/
public static DeterministicKey deserializeB58(@Nullable DeterministicKey parent, String base58, NetworkParameters params) {
try {
return deserialize(parent, Base58.decodeChecked(base58), params);
return deserialize(params, Base58.decodeChecked(base58), parent);
} catch (AddressFormatException e) {
throw new IllegalArgumentException(e);
}
@ -367,15 +367,15 @@ public class DeterministicKey extends ECKey {
/**
* Deserialize an HD Key with no parent
*/
public static DeterministicKey deserialize(byte[] serializedKey, NetworkParameters params) {
return deserialize(null, serializedKey, params);
public static DeterministicKey deserialize(NetworkParameters params, byte[] serializedKey) {
return deserialize(params, serializedKey, null);
}
/**
* Deserialize an HD Key.
* @param parent The parent node in the given key's deterministic hierarchy.
*/
public static DeterministicKey deserialize(@Nullable DeterministicKey parent, byte[] serializedKey, NetworkParameters params) {
public static DeterministicKey deserialize(NetworkParameters params, byte[] serializedKey, @Nullable DeterministicKey parent) {
ByteBuffer buffer = ByteBuffer.wrap(serializedKey);
int header = buffer.getInt();
if (header != params.getBip32HeaderPriv() && header != params.getBip32HeaderPub())

View File

@ -215,10 +215,10 @@ public class ChildKeyDerivationTest {
assertEquals(DeterministicKey.deserializeB58(priv58, params), key1);
assertEquals(DeterministicKey.deserializeB58(null, pub58, params).getPubKeyPoint(), key1.getPubKeyPoint());
assertEquals(DeterministicKey.deserializeB58(pub58, params).getPubKeyPoint(), key1.getPubKeyPoint());
assertEquals(DeterministicKey.deserialize(null, priv, params), key1);
assertEquals(DeterministicKey.deserialize(priv, params), key1);
assertEquals(DeterministicKey.deserialize(null, pub, params).getPubKeyPoint(), key1.getPubKeyPoint());
assertEquals(DeterministicKey.deserialize(pub, params).getPubKeyPoint(), key1.getPubKeyPoint());
assertEquals(DeterministicKey.deserialize(params, priv, null), key1);
assertEquals(DeterministicKey.deserialize(params, priv), key1);
assertEquals(DeterministicKey.deserialize(params, pub, null).getPubKeyPoint(), key1.getPubKeyPoint());
assertEquals(DeterministicKey.deserialize(params, pub).getPubKeyPoint(), key1.getPubKeyPoint());
}
{
final String pub58 = key2.serializePubB58(params);
@ -227,8 +227,8 @@ public class ChildKeyDerivationTest {
final byte[] priv = key2.serializePrivate(params);
assertEquals(DeterministicKey.deserializeB58(key1, priv58, params), key2);
assertEquals(DeterministicKey.deserializeB58(key1, pub58, params).getPubKeyPoint(), key2.getPubKeyPoint());
assertEquals(DeterministicKey.deserialize(key1, priv, params), key2);
assertEquals(DeterministicKey.deserialize(key1, pub, params).getPubKeyPoint(), key2.getPubKeyPoint());
assertEquals(DeterministicKey.deserialize(params, priv, key1), key2);
assertEquals(DeterministicKey.deserialize(params, pub, key1).getPubKeyPoint(), key2.getPubKeyPoint());
}
}
@ -240,9 +240,9 @@ public class ChildKeyDerivationTest {
DeterministicKey key3 = HDKeyDerivation.deriveChildKey(key2, ChildNumber.ZERO_HARDENED);
DeterministicKey key4 = HDKeyDerivation.deriveChildKey(key3, ChildNumber.ZERO_HARDENED);
assertEquals(key4.getPath().size(), 3);
assertEquals(DeterministicKey.deserialize(key3, key4.serializePrivate(params), params).getPath().size(), 3);
assertEquals(DeterministicKey.deserialize(null, key4.serializePrivate(params), params).getPath().size(), 1);
assertEquals(DeterministicKey.deserialize(key4.serializePrivate(params), params).getPath().size(), 1);
assertEquals(DeterministicKey.deserialize(params, key4.serializePrivate(params), key3).getPath().size(), 3);
assertEquals(DeterministicKey.deserialize(params, key4.serializePrivate(params), null).getPath().size(), 1);
assertEquals(DeterministicKey.deserialize(params, key4.serializePrivate(params)).getPath().size(), 1);
}
private static String hexEncodePub(DeterministicKey pubKey) {