change fingerprint storage from int to byte array

This commit is contained in:
Craig Raw 2020-02-13 10:40:32 +02:00
parent d3d5fd4a80
commit 89e7892d7c
2 changed files with 15 additions and 12 deletions

View file

@ -15,7 +15,7 @@ public class ExtendedPublicKey {
private static final int bip32HeaderP2WPKHZPub = 0x04B24746; // The 4 byte header that serializes in base58 to "zpub" private static final int bip32HeaderP2WPKHZPub = 0x04B24746; // The 4 byte header that serializes in base58 to "zpub"
private static final int bip32HeaderP2WHSHPub = 0x2AA7ED3; // The 4 byte header that serializes in base58 to "Zpub" private static final int bip32HeaderP2WHSHPub = 0x2AA7ED3; // The 4 byte header that serializes in base58 to "Zpub"
private int parentFingerprint; private byte[] parentFingerprint;
private String keyDerivationPath; private String keyDerivationPath;
private DeterministicKey pubKey; private DeterministicKey pubKey;
private String childDerivationPath; private String childDerivationPath;
@ -23,7 +23,7 @@ public class ExtendedPublicKey {
private DeterministicHierarchy hierarchy; private DeterministicHierarchy hierarchy;
public ExtendedPublicKey(int parentFingerprint, String keyDerivationPath, DeterministicKey pubKey, String childDerivationPath, ChildNumber pubKeyChildNumber) { public ExtendedPublicKey(byte[] parentFingerprint, String keyDerivationPath, DeterministicKey pubKey, String childDerivationPath, ChildNumber pubKeyChildNumber) {
this.parentFingerprint = parentFingerprint; this.parentFingerprint = parentFingerprint;
this.keyDerivationPath = keyDerivationPath; this.keyDerivationPath = keyDerivationPath;
this.pubKey = pubKey; this.pubKey = pubKey;
@ -33,10 +33,14 @@ public class ExtendedPublicKey {
this.hierarchy = new DeterministicHierarchy(pubKey); this.hierarchy = new DeterministicHierarchy(pubKey);
} }
public int getParentFingerprint() { public byte[] getParentFingerprint() {
return parentFingerprint; return parentFingerprint;
} }
public byte[] getFingerprint() {
return pubKey.getFingerprint();
}
public List<ChildNumber> getKeyDerivation() { public List<ChildNumber> getKeyDerivation() {
return parsePath(keyDerivationPath); return parsePath(keyDerivationPath);
} }
@ -137,7 +141,7 @@ public class ExtendedPublicKey {
int depth = 5 - childPath.size(); int depth = 5 - childPath.size();
buffer.put((byte)depth); buffer.put((byte)depth);
buffer.putInt(parentFingerprint); buffer.put(parentFingerprint);
buffer.putInt(pubKeyChildNumber.i()); buffer.putInt(pubKeyChildNumber.i());
@ -156,7 +160,8 @@ public class ExtendedPublicKey {
} }
int depth = buffer.get() & 0xFF; // convert signed byte to positive int since depth cannot be negative int depth = buffer.get() & 0xFF; // convert signed byte to positive int since depth cannot be negative
final int parentFingerprint = buffer.getInt(); byte[] parentFingerprint = new byte[4];
buffer.get(parentFingerprint);
final int i = buffer.getInt(); final int i = buffer.getInt();
ChildNumber childNumber; ChildNumber childNumber;
List<ChildNumber> path; List<ChildNumber> path;

View file

@ -5,7 +5,6 @@ import com.craigraw.drongo.Utils;
import com.craigraw.drongo.protocol.Base58; import com.craigraw.drongo.protocol.Base58;
import com.craigraw.drongo.protocol.Sha256Hash; import com.craigraw.drongo.protocol.Sha256Hash;
import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -13,7 +12,7 @@ public class DeterministicKey extends ECKey {
private final DeterministicKey parent; private final DeterministicKey parent;
private final List<ChildNumber> childNumberPath; private final List<ChildNumber> childNumberPath;
private final int depth; private final int depth;
private int parentFingerprint; // 0 if this key is root node of key hierarchy private byte[] parentFingerprint; // 0 if this key is root node of key hierarchy
/** 32 bytes */ /** 32 bytes */
private final byte[] chainCode; private final byte[] chainCode;
@ -27,7 +26,7 @@ public class DeterministicKey extends ECKey {
byte[] chainCode, byte[] chainCode,
LazyECPoint publicAsPoint, LazyECPoint publicAsPoint,
int depth, int depth,
int parentFingerprint) { byte[] parentFingerprint) {
super(compressPoint(publicAsPoint)); super(compressPoint(publicAsPoint));
if(chainCode.length != 32) { if(chainCode.length != 32) {
throw new IllegalArgumentException("Chaincode not 32 bytes in length"); throw new IllegalArgumentException("Chaincode not 32 bytes in length");
@ -51,7 +50,7 @@ public class DeterministicKey extends ECKey {
this.childNumberPath = childNumberPath; this.childNumberPath = childNumberPath;
this.chainCode = Arrays.copyOf(chainCode, chainCode.length); this.chainCode = Arrays.copyOf(chainCode, chainCode.length);
this.depth = parent == null ? 0 : parent.depth + 1; this.depth = parent == null ? 0 : parent.depth + 1;
this.parentFingerprint = (parent != null) ? parent.getFingerprint() : 0; this.parentFingerprint = (parent != null) ? parent.getFingerprint() : new byte[4];
} }
/** /**
@ -64,9 +63,8 @@ public class DeterministicKey extends ECKey {
} }
/** Returns the first 32 bits of the result of {@link #getIdentifier()}. */ /** Returns the first 32 bits of the result of {@link #getIdentifier()}. */
public int getFingerprint() { public byte[] getFingerprint() {
// TODO: why is this different than armory's fingerprint? BIP 32: "The first 32 bits of the identifier are called the fingerprint." return Arrays.copyOfRange(getIdentifier(), 0, 4);
return ByteBuffer.wrap(Arrays.copyOfRange(getIdentifier(), 0, 4)).getInt();
} }
/** /**