From d933743a72be1e5c1031c82447cb2d104ba498f2 Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 4 Oct 2024 22:04:23 +0300 Subject: [PATCH] Cw 772 restore from qr generates different wallet (#1721) * fix derivation info for QR restoring * allow all available seed languages for Monero --------- Co-authored-by: Omar Hatem --- .../restore/restore_from_qr_vm.dart | 21 +++--- .../restore/wallet_restore_from_qr_code.dart | 4 ++ lib/view_model/wallet_creation_vm.dart | 64 ++++++++----------- 3 files changed, 40 insertions(+), 49 deletions(-) diff --git a/lib/view_model/restore/restore_from_qr_vm.dart b/lib/view_model/restore/restore_from_qr_vm.dart index 798105b9d..042a048d9 100644 --- a/lib/view_model/restore/restore_from_qr_vm.dart +++ b/lib/view_model/restore/restore_from_qr_vm.dart @@ -37,8 +37,7 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store spendKey = '', wif = '', address = '', - super(appStore, walletInfoSource, walletCreationService, seedSettingsViewModel, - type: type, isRecovery: true); + super(appStore, walletInfoSource, walletCreationService, seedSettingsViewModel, type: type, isRecovery: true); @observable int height; @@ -58,13 +57,10 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store bool get hasRestorationHeight => type == WalletType.monero || type == WalletType.wownero; @override - WalletCredentials getCredentialsFromRestoredWallet( - dynamic options, RestoredWallet restoreWallet) { + Future getWalletCredentialsFromQRCredentials( + RestoredWallet restoreWallet) async { final password = generateWalletPassword(); - DerivationInfo? derivationInfo; - derivationInfo ??= getDefaultCreateDerivation(); - switch (restoreWallet.restoreMode) { case WalletRestoreMode.keys: switch (restoreWallet.type) { @@ -116,12 +112,13 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store ); case WalletType.bitcoin: case WalletType.litecoin: + final derivationInfo = (await getDerivationInfoFromQRCredentials(restoreWallet)).first; return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials( name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password, passphrase: restoreWallet.passphrase, - derivationType: derivationInfo!.derivationType!, + derivationType: derivationInfo.derivationType!, derivationPath: derivationInfo.derivationPath!, ); case WalletType.bitcoinCash: @@ -139,11 +136,13 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store passphrase: restoreWallet.passphrase, ); case WalletType.nano: + final derivationInfo = + (await getDerivationInfoFromQRCredentials(restoreWallet)).first; return nano!.createNanoRestoreWalletFromSeedCredentials( name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password, - derivationType: derivationInfo!.derivationType!, + derivationType: derivationInfo.derivationType!, passphrase: restoreWallet.passphrase, ); case WalletType.polygon: @@ -183,8 +182,8 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store } @override - Future processFromRestoredWallet( - WalletCredentials credentials, RestoredWallet restoreWallet) async { + Future processFromRestoredWallet(WalletCredentials credentials, + RestoredWallet restoreWallet) async { try { switch (restoreWallet.restoreMode) { case WalletRestoreMode.keys: diff --git a/lib/view_model/restore/wallet_restore_from_qr_code.dart b/lib/view_model/restore/wallet_restore_from_qr_code.dart index 335b1a006..23850befa 100644 --- a/lib/view_model/restore/wallet_restore_from_qr_code.dart +++ b/lib/view_model/restore/wallet_restore_from_qr_code.dart @@ -142,6 +142,10 @@ class WalletRestoreFromQRCode { return WalletRestoreMode.seed; } + if ((type == WalletType.monero || type == WalletType.wownero)) { + return WalletRestoreMode.seed; + } + seedValue.split(' ').forEach((element) { if (!words.contains(element)) { throw Exception( diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index 9fce86712..13c1a5661 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -85,21 +85,9 @@ abstract class WalletCreationVMBase with Store { final dirPath = await pathForWalletDir(name: name, type: type); final path = await pathForWallet(name: name, type: type); - WalletCredentials credentials; - if (restoreWallet != null) { - if (restoreWallet.restoreMode == WalletRestoreMode.seed && - options == null && - (type == WalletType.nano || - type == WalletType.bitcoin || - type == WalletType.litecoin)) { - final derivationInfo = await getDerivationInfo(restoreWallet); - options ??= {}; - options["derivationInfo"] = derivationInfo.first; - } - credentials = getCredentialsFromRestoredWallet(options, restoreWallet); - } else { - credentials = getCredentials(options); - } + final credentials = restoreWallet != null + ? await getWalletCredentialsFromQRCredentials(restoreWallet) + : getCredentials(options); final walletInfo = WalletInfo.external( id: WalletBase.idFor(name, type), @@ -200,36 +188,36 @@ abstract class WalletCreationVMBase with Store { } } - Future> getDerivationInfo(RestoredWallet restoreWallet) async { - var list = []; - final walletType = restoreWallet.type; - var appStore = getIt.get(); - var node = appStore.settingsStore.getCurrentNode(walletType); + Future> getDerivationInfoFromQRCredentials(RestoredWallet restoreWallet) async { + var list = []; + final walletType = restoreWallet.type; + var appStore = getIt.get(); + var node = appStore.settingsStore.getCurrentNode(walletType); - switch (walletType) { - case WalletType.bitcoin: - case WalletType.litecoin: - return bitcoin!.getDerivationsFromMnemonic( - mnemonic: restoreWallet.mnemonicSeed!, - node: node, - ); - case WalletType.nano: - return nanoUtil!.getDerivationsFromMnemonic( - mnemonic: restoreWallet.mnemonicSeed!, - node: node, - ); - default: - break; + switch (walletType) { + case WalletType.bitcoin: + case WalletType.litecoin: + return bitcoin!.getDerivationsFromMnemonic( + mnemonic: restoreWallet.mnemonicSeed!, + node: node, + passphrase: restoreWallet.passphrase, + ); + case WalletType.nano: + return nanoUtil!.getDerivationsFromMnemonic( + mnemonic: restoreWallet.mnemonicSeed!, + node: node, + ); + default: + break; + } + return list; } - return list; - } WalletCredentials getCredentials(dynamic options) => throw UnimplementedError(); Future process(WalletCredentials credentials) => throw UnimplementedError(); - WalletCredentials getCredentialsFromRestoredWallet( - dynamic options, RestoredWallet restoreWallet) => + Future getWalletCredentialsFromQRCredentials(RestoredWallet restoreWallet) async => throw UnimplementedError(); Future processFromRestoredWallet(