From 47fc0e11ebcf89f20864922e49102971a273ea8b Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 25 Feb 2021 11:54:47 +0200 Subject: [PATCH] test various possible derivation paths when electrum seed is provided --- .../com/sparrowwallet/sparrow/io/Electrum.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java index f91ded5e..caa36fa6 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Electrum.java @@ -161,8 +161,18 @@ public class Electrum implements KeystoreFileImport, WalletImport, WalletExport } 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 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);