handle zero length derivation paths

This commit is contained in:
Craig Raw 2020-08-05 16:54:09 +02:00
parent 04576bddff
commit eb07a7ffa3
2 changed files with 15 additions and 5 deletions

View file

@ -341,7 +341,7 @@ public class DeterministicSeed implements EncryptableItem {
}
public enum Type {
BIP39() {
BIP39("Mnemonic Words (BIP39)") {
public byte[] getEntropyBytes(List<String> mnemonicCode) throws MnemonicException {
return Bip39MnemonicCode.INSTANCE.toEntropy(mnemonicCode);
}
@ -354,7 +354,7 @@ public class DeterministicSeed implements EncryptableItem {
return Bip39MnemonicCode.toSeed(mnemonicCode, passphrase);
}
},
ELECTRUM() {
ELECTRUM("Mnemonic Words (Electrum Seed Version System)") {
public byte[] getEntropyBytes(List<String> mnemonicCode) throws MnemonicException {
throw new MnemonicException("Electrum seeds do not provide entropy bytes");
}
@ -368,6 +368,16 @@ public class DeterministicSeed implements EncryptableItem {
}
};
Type(String name) {
this.name = name;
}
private final String name;
public String getName() {
return name;
}
public abstract byte[] getEntropyBytes(List<String> mnemonicCode) throws MnemonicException;
public abstract void check(List<String> mnemonicCode) throws MnemonicException;

View file

@ -101,7 +101,7 @@ public class Keystore {
public ExtendedKey getExtendedPrivateKey() throws MnemonicException {
List<ChildNumber> derivation = getKeyDerivation().getDerivation();
DeterministicKey derivedKey = getExtendedMasterPrivateKey().getKey(derivation);
ExtendedKey xprv = new ExtendedKey(derivedKey, derivedKey.getParentFingerprint(), derivation.get(derivation.size() - 1));
ExtendedKey xprv = new ExtendedKey(derivedKey, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
//Recreate from xprv string to reset path to single ChildNumber at the derived depth
return ExtendedKey.fromDescriptor(xprv.toString());
}
@ -156,7 +156,7 @@ public class Keystore {
List<ChildNumber> derivation = getKeyDerivation().getDerivation();
DeterministicKey derivedKey = getExtendedMasterPrivateKey().getKey(derivation);
DeterministicKey derivedKeyPublicOnly = derivedKey.dropPrivateBytes().dropParent();
ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.get(derivation.size() - 1));
ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
if(!xpub.equals(getExtendedPublicKey())) {
return false;
}
@ -192,7 +192,7 @@ public class Keystore {
String masterFingerprint = Utils.bytesToHex(xprv.getKey().getFingerprint());
DeterministicKey derivedKey = xprv.getKey(derivation);
DeterministicKey derivedKeyPublicOnly = derivedKey.dropPrivateBytes().dropParent();
ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.get(derivation.size() - 1));
ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
keystore.setLabel(seed.getType().name());
keystore.setSource(KeystoreSource.SW_SEED);