Skip to content

Commit

Permalink
test various possible derivation paths when electrum seed is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
craigraw committed Feb 25, 2021
1 parent 27f4b3b commit 47fc0e1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/sparrowwallet/sparrow/io/Electrum.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,18 @@ public Wallet importWallet(InputStream inputStream, String password) throws Impo
}

keystore.setSeed(new DeterministicSeed(mnemonic, passphrase, 0, DeterministicSeed.Type.ELECTRUM));
if(derivationPath.equals("m/0")) {
derivationPath = "m/0'";

//Ensure the derivation path from the seed matches the provided xpub
String[] possibleDerivations = {"m", "m/0", "m/0'"};
for(String possibleDerivation : possibleDerivations) {
List<ChildNumber> derivation = KeyDerivation.parsePath(possibleDerivation);
DeterministicKey derivedKey = keystore.getExtendedMasterPrivateKey().getKey(derivation);
DeterministicKey derivedKeyPublicOnly = derivedKey.dropPrivateBytes().dropParent();
ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
if(xpub.equals(xPub)) {
derivationPath = possibleDerivation;
break;
}
}
} else {
keystore.setSource(KeystoreSource.SW_WATCH);
Expand Down

0 comments on commit 47fc0e1

Please sign in to comment.