electrum restore fixes

This commit is contained in:
fosse 2023-08-31 09:39:15 -04:00
parent 60f2c3c450
commit 287f3e06f7
3 changed files with 26 additions and 13 deletions

View file

@ -108,6 +108,17 @@ Future<String> generateElectrumMnemonic({int strength = 264, String prefix = seg
return result; return result;
} }
Future<bool> checkIfMnemonicIsElectrum2(String mnemonic) async {
return prefixMatches(mnemonic, [segwit]).first;
}
Future<String> 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<Uint8List> mnemonicToSeedBytes(String mnemonic, {String prefix = segwit}) async { Future<Uint8List> mnemonicToSeedBytes(String mnemonic, {String prefix = segwit}) async {
final pbkdf2 = final pbkdf2 =
cryptography.Pbkdf2(macAlgorithm: cryptography.Hmac.sha512(), iterations: 2048, bits: 512); cryptography.Pbkdf2(macAlgorithm: cryptography.Hmac.sha512(), iterations: 2048, bits: 512);

View file

@ -113,17 +113,11 @@ class BitcoinWalletService extends WalletService<BitcoinNewWalletCredentials,
static Future<List<DerivationType>> compareDerivationMethods( static Future<List<DerivationType>> compareDerivationMethods(
{required String mnemonic, required Node node}) async { {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 if (await checkIfMnemonicIsElectrum2(mnemonic)) {
// bip39 is possible with any number of words return [DerivationType.electrum2];
// 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];
// }
return [DerivationType.bip39, DerivationType.electrum2]; return [DerivationType.bip39, DerivationType.electrum2];
} }

View file

@ -229,12 +229,19 @@ class WalletRestorePage extends BasePage {
return false; return false;
} }
if ((walletRestoreViewModel.type == WalletType.bitcoin || if ((walletRestoreViewModel.type == WalletType.litecoin) &&
walletRestoreViewModel.type == WalletType.litecoin) &&
(seedWords.length != WalletRestoreViewModelBase.electrumSeedMnemonicLength && (seedWords.length != WalletRestoreViewModelBase.electrumSeedMnemonicLength &&
seedWords.length != WalletRestoreViewModelBase.electrumShortSeedMnemonicLength)) { seedWords.length != WalletRestoreViewModelBase.electrumShortSeedMnemonicLength)) {
return false; return false;
} }
// bip39:
const validSeedLengths = [12, 15, 18, 21, 24];
if (walletRestoreViewModel.type == WalletType.bitcoin &&
!(validSeedLengths.contains(seedWords.length))) {
return false;
}
final words = final words =
walletRestoreFromSeedFormKey.currentState!.seedWidgetStateKey.currentState!.words.toSet(); walletRestoreFromSeedFormKey.currentState!.seedWidgetStateKey.currentState!.words.toSet();
return seedWords.toSet().difference(words).toSet().isEmpty; return seedWords.toSet().difference(words).toSet().isEmpty;
@ -331,6 +338,7 @@ class WalletRestorePage extends BasePage {
this.derivationType = derivationInfo.derivationType; this.derivationType = derivationInfo.derivationType;
this.derivationPath = derivationInfo.derivationPath; this.derivationPath = derivationInfo.derivationPath;
} else { } else {
// electrum derivation:
this.derivationType = derivationTypes[0]; this.derivationType = derivationTypes[0];
this.derivationPath = "m/0'/1"; this.derivationPath = "m/0'/1";
} }