diff --git a/cw_bitcoin/lib/bitcoin_mnemonic.dart b/cw_bitcoin/lib/bitcoin_mnemonic.dart index 15374ed5d..d3bc23c06 100644 --- a/cw_bitcoin/lib/bitcoin_mnemonic.dart +++ b/cw_bitcoin/lib/bitcoin_mnemonic.dart @@ -108,6 +108,17 @@ Future generateElectrumMnemonic({int strength = 264, String prefix = seg return result; } +Future checkIfMnemonicIsElectrum2(String mnemonic) async { + return prefixMatches(mnemonic, [segwit]).first; +} + +Future getMnemonicHash(String mnemonic) async { + final hmacSha512 = Hmac(sha512, utf8.encode('Seed version')); + final digest = hmacSha512.convert(utf8.encode(normalizeText(mnemonic))); + final hx = digest.toString(); + return hx; +} + Future mnemonicToSeedBytes(String mnemonic, {String prefix = segwit}) async { final pbkdf2 = cryptography.Pbkdf2(macAlgorithm: cryptography.Hmac.sha512(), iterations: 2048, bits: 512); diff --git a/cw_bitcoin/lib/bitcoin_wallet_service.dart b/cw_bitcoin/lib/bitcoin_wallet_service.dart index 20941294f..df81e2cf8 100644 --- a/cw_bitcoin/lib/bitcoin_wallet_service.dart +++ b/cw_bitcoin/lib/bitcoin_wallet_service.dart @@ -113,17 +113,11 @@ class BitcoinWalletService extends WalletService> compareDerivationMethods( {required String mnemonic, required Node node}) async { - // if the mnemonic is 12 words, then it could be electrum 1.0, - // if the mnemonic is 24 words, then it could be electrum 2.0 - // bip39 is possible with any number of words - // int wordCount = mnemonic.split(" ").length; - // if (wordCount == 24) { - // return [DerivationType.bip39, DerivationType.electrum1]; - // } else if (wordCount == 12) { - // return [DerivationType.bip39, DerivationType.electrum2]; - // } else { - // return [DerivationType.bip39]; - // } + + if (await checkIfMnemonicIsElectrum2(mnemonic)) { + return [DerivationType.electrum2]; + } + return [DerivationType.bip39, DerivationType.electrum2]; } diff --git a/lib/src/screens/restore/wallet_restore_page.dart b/lib/src/screens/restore/wallet_restore_page.dart index 12c205aca..839f8930e 100644 --- a/lib/src/screens/restore/wallet_restore_page.dart +++ b/lib/src/screens/restore/wallet_restore_page.dart @@ -229,12 +229,19 @@ class WalletRestorePage extends BasePage { return false; } - if ((walletRestoreViewModel.type == WalletType.bitcoin || - walletRestoreViewModel.type == WalletType.litecoin) && + if ((walletRestoreViewModel.type == WalletType.litecoin) && (seedWords.length != WalletRestoreViewModelBase.electrumSeedMnemonicLength && seedWords.length != WalletRestoreViewModelBase.electrumShortSeedMnemonicLength)) { return false; } + + // bip39: + const validSeedLengths = [12, 15, 18, 21, 24]; + if (walletRestoreViewModel.type == WalletType.bitcoin && + !(validSeedLengths.contains(seedWords.length))) { + return false; + } + final words = walletRestoreFromSeedFormKey.currentState!.seedWidgetStateKey.currentState!.words.toSet(); return seedWords.toSet().difference(words).toSet().isEmpty; @@ -331,6 +338,7 @@ class WalletRestorePage extends BasePage { this.derivationType = derivationInfo.derivationType; this.derivationPath = derivationInfo.derivationPath; } else { + // electrum derivation: this.derivationType = derivationTypes[0]; this.derivationPath = "m/0'/1"; }