allow restoring electrum seeds (#1665)

Matthew Fosse committed Sep 3, 2024 at 20:34 UTC 0c1ca78d298709a86bf3d67bc99d0e87ee3c7935
3 files changed +16 -17
lib/src/screens/restore/wallet_restore_page.dart
+6 -14
@@ -406,24 +406,16 @@ class WalletRestorePage extends BasePage {
406 ) as DerivationInfo?;
407 } else if (derivationsWithHistory == 1) {
408 dInfo = derivations[derivationWithHistoryIndex];
409 - }
410 -
411 - // get the default derivation for this wallet type:
412 - if (dInfo == null) {
409 + } else if (derivations.length == 1) {
410 // we only return 1 derivation if we're pretty sure we know which one to use:
414 - if (derivations.length == 1) {
415 - dInfo = derivations.first;
416 - } else {
417 - // if we have multiple possible derivations, and none have histories
418 - // we just default to the most common one:
419 - dInfo = walletRestoreViewModel.getCommonRestoreDerivation();
420 - }
411 + dInfo = derivations.first;
412 + } else {
413 + // if we have multiple possible derivations, and none (or multiple) have histories
414 + // we just default to the most common one:
415 + dInfo = walletRestoreViewModel.getCommonRestoreDerivation();
416 }
417
418 this.derivationInfo = dInfo;
424 - if (this.derivationInfo == null) {
425 - this.derivationInfo = walletRestoreViewModel.getDefaultDerivation();
426 - }
419
420 await walletRestoreViewModel.create(options: _credentials());
421 seedSettingsViewModel.setPassphrase(null);
lib/view_model/restore/restore_from_qr_vm.dart
+1 -1
@@ -62,7 +62,7 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store
62 derivationInfo = options["derivationInfo"] as DerivationInfo?;
63 passphrase = options["passphrase"] as String?;
64 }
65 - derivationInfo ??= getDefaultDerivation();
65 + derivationInfo ??= getDefaultCreateDerivation();
66
67 switch (restoreWallet.restoreMode) {
68 case WalletRestoreMode.keys:
lib/view_model/wallet_creation_vm.dart
+9 -2
@@ -97,7 +97,7 @@ abstract class WalletCreationVMBase with Store {
97 dirPath: dirPath,
98 address: '',
99 showIntroCakePayCard: (!walletCreationService.typeExists(type)) && type != WalletType.haven,
100 - derivationInfo: credentials.derivationInfo ?? getDefaultDerivation(),
100 + derivationInfo: credentials.derivationInfo ?? getDefaultCreateDerivation(),
101 hardwareWalletType: credentials.hardwareWalletType,
102 );
103
@@ -116,7 +116,7 @@ abstract class WalletCreationVMBase with Store {
116 }
117 }
118
119 - DerivationInfo? getDefaultDerivation() {
119 + DerivationInfo? getDefaultCreateDerivation() {
120 final useBip39 = seedSettingsViewModel.bitcoinSeedType.type == DerivationType.bip39;
121 switch (type) {
122 case WalletType.nano:
@@ -147,10 +147,14 @@ abstract class WalletCreationVMBase with Store {
147 }
148
149 DerivationInfo? getCommonRestoreDerivation() {
150 + final useElectrum = seedSettingsViewModel.bitcoinSeedType.type == DerivationType.electrum;
151 switch (this.type) {
152 case WalletType.nano:
153 return DerivationInfo(derivationType: DerivationType.nano);
154 case WalletType.bitcoin:
155 + if (useElectrum) {
156 + return bitcoin!.getElectrumDerivations()[DerivationType.electrum]!.first;
157 + }
158 return DerivationInfo(
159 derivationType: DerivationType.bip39,
160 derivationPath: "m/84'/0'/0'/0",
@@ -158,6 +162,9 @@ abstract class WalletCreationVMBase with Store {
162 scriptType: "p2wpkh",
163 );
164 case WalletType.litecoin:
165 + if (useElectrum) {
166 + return bitcoin!.getElectrumDerivations()[DerivationType.electrum]!.first;
167 + }
168 return DerivationInfo(
169 derivationType: DerivationType.bip39,
170 derivationPath: "m/84'/2'/0'/0",