From 4356e101f57b6b2330f3a3b55d5594f1709daa33 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 17 Jan 2024 12:52:33 -0600 Subject: [PATCH] xmr/wow wallet restore init fixes --- .../restore_wallet_view.dart | 22 +++++++++++++--- lib/wallets/wallet/impl/monero_wallet.dart | 18 ++++++------- lib/wallets/wallet/impl/wownero_wallet.dart | 18 ++++++------- lib/wallets/wallet/wallet.dart | 26 ++++++++++++++++++- 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart b/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart index f2eb0d779..f03a284fd 100644 --- a/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart +++ b/lib/pages/add_wallet_views/restore_wallet_view/restore_wallet_view.dart @@ -52,6 +52,8 @@ import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/wallets/isar/models/wallet_info.dart'; import 'package:stackwallet/wallets/wallet/impl/epiccash_wallet.dart'; +import 'package:stackwallet/wallets/wallet/impl/monero_wallet.dart'; +import 'package:stackwallet/wallets/wallet/impl/wownero_wallet.dart'; import 'package:stackwallet/wallets/wallet/supporting/epiccash_wallet_info_extension.dart'; import 'package:stackwallet/wallets/wallet/wallet.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; @@ -314,10 +316,22 @@ class _RestoreWalletViewState extends ConsumerState { mnemonic: mnemonic, ); - if (wallet is EpiccashWallet) { - await wallet.init(isRestore: true); - } else { - await wallet.init(); + // TODO: extract interface with isRestore param + switch (wallet.runtimeType) { + case EpiccashWallet: + await (wallet as EpiccashWallet).init(isRestore: true); + break; + + case MoneroWallet: + await (wallet as MoneroWallet).init(isRestore: true); + break; + + case WowneroWallet: + await (wallet as WowneroWallet).init(isRestore: true); + break; + + default: + await wallet.init(); } await wallet.recover(isRescan: false); diff --git a/lib/wallets/wallet/impl/monero_wallet.dart b/lib/wallets/wallet/impl/monero_wallet.dart index 46b64403e..aa99a4002 100644 --- a/lib/wallets/wallet/impl/monero_wallet.dart +++ b/lib/wallets/wallet/impl/monero_wallet.dart @@ -247,26 +247,26 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { } @override - Future init() async { + Future init({bool? isRestore}) async { cwWalletService = xmr_dart.monero .createMoneroWalletService(DB.instance.moneroWalletInfoBox); - if (!(await cwWalletService!.isWalletExit(walletId))) { + if (!(await cwWalletService!.isWalletExit(walletId)) && isRestore != true) { WalletInfo walletInfo; WalletCredentials credentials; try { - String name = walletId; final dirPath = - await pathForWalletDir(name: name, type: WalletType.monero); - final path = await pathForWallet(name: name, type: WalletType.monero); + await pathForWalletDir(name: walletId, type: WalletType.monero); + final path = + await pathForWallet(name: walletId, type: WalletType.monero); credentials = xmr_dart.monero.createMoneroNewWalletCredentials( - name: name, + name: walletId, language: "English", ); walletInfo = WalletInfo.external( - id: WalletBase.idFor(name, WalletType.monero), - name: name, + id: WalletBase.idFor(walletId, WalletType.monero), + name: walletId, type: WalletType.monero, isRecovery: false, restoreHeight: credentials.height ?? 0, @@ -332,7 +332,7 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { var restoreHeight = cwWalletBase?.walletInfo.restoreHeight; highestPercentCached = 0; - await cwWalletBase?.rescan(height: restoreHeight); + await cwWalletBase?.rescan(height: restoreHeight ?? 0); }); unawaited(refresh()); return; diff --git a/lib/wallets/wallet/impl/wownero_wallet.dart b/lib/wallets/wallet/impl/wownero_wallet.dart index a0df9e2e2..4dfd32be6 100644 --- a/lib/wallets/wallet/impl/wownero_wallet.dart +++ b/lib/wallets/wallet/impl/wownero_wallet.dart @@ -246,27 +246,27 @@ class WowneroWallet extends CryptonoteWallet with CwBasedInterface { } @override - Future init() async { + Future init({bool? isRestore}) async { cwWalletService = wow_dart.wownero .createWowneroWalletService(DB.instance.moneroWalletInfoBox); - if (!(await cwWalletService!.isWalletExit(walletId))) { + if (!(await cwWalletService!.isWalletExit(walletId)) && isRestore != true) { WalletInfo walletInfo; WalletCredentials credentials; try { - String name = walletId; final dirPath = - await pathForWalletDir(name: name, type: WalletType.wownero); - final path = await pathForWallet(name: name, type: WalletType.wownero); + await pathForWalletDir(name: walletId, type: WalletType.wownero); + final path = + await pathForWallet(name: walletId, type: WalletType.wownero); credentials = wow_dart.wownero.createWowneroNewWalletCredentials( - name: name, + name: walletId, language: "English", seedWordsLength: 14, ); walletInfo = WalletInfo.external( - id: WalletBase.idFor(name, WalletType.wownero), - name: name, + id: WalletBase.idFor(walletId, WalletType.wownero), + name: walletId, type: WalletType.wownero, isRecovery: false, restoreHeight: credentials.height ?? 0, @@ -373,7 +373,7 @@ class WowneroWallet extends CryptonoteWallet with CwBasedInterface { var restoreHeight = cwWalletBase?.walletInfo.restoreHeight; highestPercentCached = 0; - await cwWalletBase?.rescan(height: restoreHeight); + await cwWalletBase?.rescan(height: restoreHeight ?? 0); }); unawaited(refresh()); return; diff --git a/lib/wallets/wallet/wallet.dart b/lib/wallets/wallet/wallet.dart index 638011c9a..796760dbd 100644 --- a/lib/wallets/wallet/wallet.dart +++ b/lib/wallets/wallet/wallet.dart @@ -148,7 +148,31 @@ abstract class Wallet { if (wallet is MnemonicInterface) { if (wallet is CryptonoteWallet) { // currently a special case due to the xmr/wow libraries handling their - // own mnemonic generation + // own mnemonic generation on new wallet creation + // if its a restore we must set them + if (mnemonic != null) { + if ((await secureStorageInterface.read( + key: mnemonicKey(walletId: walletInfo.walletId), + )) == + null) { + await secureStorageInterface.write( + key: mnemonicKey(walletId: walletInfo.walletId), + value: mnemonic, + ); + } + + if (mnemonicPassphrase != null) { + if ((await secureStorageInterface.read( + key: mnemonicPassphraseKey(walletId: walletInfo.walletId), + )) == + null) { + await secureStorageInterface.write( + key: mnemonicPassphraseKey(walletId: walletInfo.walletId), + value: mnemonicPassphrase, + ); + } + } + } } else { await secureStorageInterface.write( key: mnemonicKey(walletId: walletInfo.walletId),