From bb422ed22465c9c8ad6dfcc7bb7ad3f490b3941d Mon Sep 17 00:00:00 2001 From: fosse Date: Mon, 9 Oct 2023 17:19:58 -0400 Subject: [PATCH] additional refactor improvements --- .../bitcoin_wallet_creation_credentials.dart | 20 +++++++++---- cw_bitcoin/lib/bitcoin_wallet_service.dart | 9 ++---- .../lib/nano_wallet_creation_credentials.dart | 23 ++++++++------- cw_nano/lib/nano_wallet_service.dart | 7 +++-- lib/bitcoin/cw_bitcoin.dart | 14 ++++----- lib/entities/default_settings_migration.dart | 5 +++- lib/main.dart | 2 +- lib/nano/cw_nano.dart | 27 +++++++---------- .../restore/restore_from_qr_vm.dart | 17 +++++++---- lib/view_model/wallet_creation_vm.dart | 23 +++++++++++++++ lib/view_model/wallet_restore_view_model.dart | 29 ++++++------------- tool/configure.dart | 12 ++++++-- 12 files changed, 108 insertions(+), 80 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_wallet_creation_credentials.dart b/cw_bitcoin/lib/bitcoin_wallet_creation_credentials.dart index 3089fc7e4..ca8cda153 100644 --- a/cw_bitcoin/lib/bitcoin_wallet_creation_credentials.dart +++ b/cw_bitcoin/lib/bitcoin_wallet_creation_credentials.dart @@ -3,10 +3,18 @@ import 'package:cw_core/wallet_info.dart'; class BitcoinNewWalletCredentials extends WalletCredentials { BitcoinNewWalletCredentials( - {required String name, WalletInfo? walletInfo, this.derivationType, this.derivationPath}) - : super(name: name, walletInfo: walletInfo); - DerivationType? derivationType; - String? derivationPath; + {required String name, + WalletInfo? walletInfo, + DerivationType? derivationType, + String? derivationPath}) + : super( + name: name, + walletInfo: walletInfo, + derivationInfo: DerivationInfo( + derivationType: derivationType, + derivationPath: derivationPath, + ), + ); } class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials { @@ -15,8 +23,8 @@ class BitcoinRestoreWalletFromSeedCredentials extends WalletCredentials { required String password, required this.mnemonic, WalletInfo? walletInfo, - DerivationType? derivationType, - String? derivationPath, + required DerivationType derivationType, + required String derivationPath, }) : super( name: name, password: password, diff --git a/cw_bitcoin/lib/bitcoin_wallet_service.dart b/cw_bitcoin/lib/bitcoin_wallet_service.dart index 7be406a72..ceb785cb2 100644 --- a/cw_bitcoin/lib/bitcoin_wallet_service.dart +++ b/cw_bitcoin/lib/bitcoin_wallet_service.dart @@ -46,12 +46,9 @@ class BitcoinWalletService extends WalletService create(BitcoinNewWalletCredentials credentials) async { - // set default if not present: - DerivationType derivationType = credentials.derivationType ?? DerivationType.electrum2; - String derivationPath = credentials.derivationPath ?? "m/0'/1"; - // only set if not in the walletInfo already: - credentials.walletInfo!.derivationInfo ??= - DerivationInfo(derivationType: derivationType, derivationPath: derivationPath); + + // set the walletInfo's derivationInfo if not present: + credentials.walletInfo!.derivationInfo ??= credentials.derivationInfo; final wallet = await BitcoinWalletBase.create( mnemonic: await generateElectrumMnemonic(strength: 132), diff --git a/cw_nano/lib/nano_wallet_creation_credentials.dart b/cw_nano/lib/nano_wallet_creation_credentials.dart index a713d3e44..428ac99a1 100644 --- a/cw_nano/lib/nano_wallet_creation_credentials.dart +++ b/cw_nano/lib/nano_wallet_creation_credentials.dart @@ -19,15 +19,13 @@ class NanoRestoreWalletFromSeedCredentials extends WalletCredentials { required this.mnemonic, int height = 0, String? password, - DerivationType? derivationType, + required DerivationType derivationType, }) : super( - name: name, - password: password, - height: height, - derivationInfo: DerivationInfo( - derivationType: derivationType, - height: height, - )); + name: name, + password: password, + height: height, + derivationInfo: DerivationInfo(derivationType: derivationType), + ); final String mnemonic; } @@ -41,10 +39,13 @@ class NanoRestoreWalletFromKeysCredentials extends WalletCredentials { NanoRestoreWalletFromKeysCredentials({ required String name, required String password, + required DerivationType derivationType, required this.seedKey, - this.derivationType, - }) : super(name: name, password: password); + }) : super( + name: name, + password: password, + derivationInfo: DerivationInfo(derivationType: derivationType), + ); final String seedKey; - final DerivationType? derivationType; } diff --git a/cw_nano/lib/nano_wallet_service.dart b/cw_nano/lib/nano_wallet_service.dart index 85bd9cc78..b04635f62 100644 --- a/cw_nano/lib/nano_wallet_service.dart +++ b/cw_nano/lib/nano_wallet_service.dart @@ -32,7 +32,8 @@ class NanoWalletService extends WalletService BitcoinTransactionPriority.medium; @override - WalletCredentials createBitcoinRestoreWalletFromSeedCredentials( - {required String name, - required String mnemonic, - required String password, - DerivationType? derivationType, - String? derivationPath}) => + WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({ + required String name, + required String mnemonic, + required String password, + required DerivationType derivationType, + required String derivationPath, + }) => BitcoinRestoreWalletFromSeedCredentials( name: name, mnemonic: mnemonic, diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index c0202d895..5f95b427f 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -49,7 +49,7 @@ Future defaultSettingsMigration( sharedPreferences.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null; await _validateWalletInfoBoxData(walletInfoSource); - + await sharedPreferences.setBool(PreferencesKey.isNewInstall, isNewInstall); final currentVersion = @@ -166,6 +166,9 @@ Future defaultSettingsMigration( await changeNanoCurrentPowNodeToDefault( sharedPreferences: sharedPreferences, nodes: powNodes); break; + case 23: + // TODO: await updateBtcAndNanoWalletInfos() + break; default: break; diff --git a/lib/main.dart b/lib/main.dart index 98cf2aa84..0f7740902 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -163,7 +163,7 @@ Future initializeAppConfigs() async { transactionDescriptions: transactionDescriptions, secureStorage: secureStorage, anonpayInvoiceInfo: anonpayInvoiceInfo, - initialMigrationVersion: 22); + initialMigrationVersion: 23); } Future initialSetup( diff --git a/lib/nano/cw_nano.dart b/lib/nano/cw_nano.dart index 24b894b6e..6f72506dd 100644 --- a/lib/nano/cw_nano.dart +++ b/lib/nano/cw_nano.dart @@ -104,15 +104,11 @@ class CWNano extends Nano { required String name, required String password, required String mnemonic, - DerivationType? derivationType, + required DerivationType derivationType, }) { - if (derivationType == null) { - // figure out the derivation type as best we can, otherwise set it to "unknown" - if (mnemonic.split(" ").length == 12) { - derivationType = DerivationType.bip39; - } else { - derivationType = DerivationType.unknown; - } + // figure out the derivation type as best we can, otherwise set it to "unknown" + if (mnemonic.split(" ").length == 12) { + derivationType = DerivationType.bip39; } return NanoRestoreWalletFromSeedCredentials( @@ -128,15 +124,11 @@ class CWNano extends Nano { required String name, required String password, required String seedKey, - DerivationType? derivationType, + required DerivationType derivationType, }) { - if (derivationType == null) { - // figure out the derivation type as best we can, otherwise set it to "unknown" - if (seedKey.length == 64) { - derivationType = DerivationType.nano; - } else { - derivationType = DerivationType.unknown; - } + // figure out the derivation type as best we can, otherwise set it to "unknown" + if (seedKey.length == 64) { + derivationType = DerivationType.nano; } return NanoRestoreWalletFromKeysCredentials( @@ -430,7 +422,8 @@ class CWNanoUtil extends NanoUtil { AccountInfoResponse? accountInfo = await nanoClient.getAccountInfo(publicAddress); if (accountInfo == null) { - accountInfo = AccountInfoResponse(frontier: "", balance: "0", representative: "", confirmationHeight: 0); + accountInfo = AccountInfoResponse( + frontier: "", balance: "0", representative: "", confirmationHeight: 0); } accountInfo.address = publicAddress; return accountInfo; diff --git a/lib/view_model/restore/restore_from_qr_vm.dart b/lib/view_model/restore/restore_from_qr_vm.dart index 39a7b682f..b0a9e0c8a 100644 --- a/lib/view_model/restore/restore_from_qr_vm.dart +++ b/lib/view_model/restore/restore_from_qr_vm.dart @@ -26,8 +26,7 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store spendKey = '', wif = '', address = '', - super(appStore, walletInfoSource, walletCreationService, - type: type, isRecovery: true); + super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true); @observable int height; @@ -47,8 +46,10 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store bool get hasRestorationHeight => type == WalletType.monero; @override - WalletCredentials getCredentialsFromRestoredWallet(dynamic options, RestoredWallet restoreWallet) { + WalletCredentials getCredentialsFromRestoredWallet( + dynamic options, RestoredWallet restoreWallet) { final password = generateWalletPassword(); + DerivationInfo? derivationInfo = options["derivationInfo"] as DerivationInfo?; switch (restoreWallet.restoreMode) { case WalletRestoreMode.keys: @@ -83,7 +84,12 @@ abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store case WalletType.bitcoin: case WalletType.litecoin: return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials( - name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password); + name: name, + mnemonic: restoreWallet.mnemonicSeed ?? '', + password: password, + derivationType: derivationInfo!.derivationType!, + derivationPath: derivationInfo.derivationPath!, + ); case WalletType.ethereum: return ethereum!.createEthereumRestoreWalletFromSeedCredentials( name: name, mnemonic: restoreWallet.mnemonicSeed ?? '', password: password); @@ -96,7 +102,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/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index b22fe484c..4a653cbf8 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -47,6 +47,13 @@ abstract class WalletCreationVMBase with Store { name = await generateName(); } + if (options == null || options["derivationInfo"] == null) { + if (options == null) { + options = {}; + } + options["derivationInfo"] = getDefaultDerivation(); + } + walletCreationService.checkIfExists(name); final dirPath = await pathForWalletDir(name: name, type: type); final path = await pathForWallet(name: name, type: type); @@ -82,6 +89,22 @@ abstract class WalletCreationVMBase with Store { } } + DerivationInfo getDefaultDerivation() { + switch (this.type) { + case WalletType.nano: + return DerivationInfo( + derivationType: DerivationType.nano, + ); + case WalletType.bitcoin: + case WalletType.litecoin: + default: + return DerivationInfo( + derivationType: DerivationType.electrum2, + derivationPath: "m/0'/1", + ); + } + } + WalletCredentials getCredentials(dynamic options) => throw UnimplementedError(); Future process(WalletCredentials credentials) => throw UnimplementedError(); diff --git a/lib/view_model/wallet_restore_view_model.dart b/lib/view_model/wallet_restore_view_model.dart index 6606c2660..2757a5606 100644 --- a/lib/view_model/wallet_restore_view_model.dart +++ b/lib/view_model/wallet_restore_view_model.dart @@ -83,12 +83,17 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store { name: name, mnemonic: seed, password: password, - derivationType: derivationInfo?.derivationType, - derivationPath: derivationInfo?.derivationPath, + derivationType: derivationInfo!.derivationType!, + derivationPath: derivationInfo.derivationPath!, ); case WalletType.litecoin: return bitcoin!.createBitcoinRestoreWalletFromSeedCredentials( - name: name, mnemonic: seed, password: password); + name: name, + mnemonic: seed, + password: password, + derivationType: derivationInfo!.derivationType!, + derivationPath: derivationInfo.derivationPath!, + ); case WalletType.haven: return haven!.createHavenRestoreWalletFromSeedCredentials( name: name, height: height, mnemonic: seed, password: password); @@ -100,7 +105,7 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store { name: name, mnemonic: seed, password: password, - derivationType: derivationInfo?.derivationType, + derivationType: derivationInfo!.derivationType!, ); default: break; @@ -232,22 +237,6 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store { return [DerivationType.def]; } - DerivationInfo getDefaultDerivation() { - switch (type) { - case WalletType.nano: - return DerivationInfo( - derivationType: DerivationType.nano, - ); - case WalletType.bitcoin: - case WalletType.litecoin: - default: - return DerivationInfo( - derivationType: DerivationType.electrum2, - derivationPath: "m/0'/1", - ); - } - } - @override Future process(WalletCredentials credentials) async { if (mode == WalletRestoreMode.keys) { diff --git a/tool/configure.dart b/tool/configure.dart index 232f73898..5843efc85 100644 --- a/tool/configure.dart +++ b/tool/configure.dart @@ -80,7 +80,13 @@ import 'package:cw_bitcoin/script_hash.dart'; abstract class Bitcoin { TransactionPriority getMediumTransactionPriority(); - WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({required String name, required String mnemonic, required String password, DerivationType? derivationType, String? derivationPath}); + WalletCredentials createBitcoinRestoreWalletFromSeedCredentials({ + required String name, + required String mnemonic, + required String password, + required DerivationType derivationType, + required String derivationPath, + }); WalletCredentials createBitcoinRestoreWalletFromWIFCredentials({required String name, required String password, required String wif, WalletInfo? walletInfo}); WalletCredentials createBitcoinNewWalletCredentials({required String name, WalletInfo? walletInfo}); List getWordList(); @@ -634,14 +640,14 @@ abstract class Nano { required String name, required String password, required String mnemonic, - DerivationType? derivationType, + required DerivationType derivationType, }); WalletCredentials createNanoRestoreWalletFromKeysCredentials({ required String name, required String password, required String seedKey, - DerivationType? derivationType, + required DerivationType derivationType, }); List getNanoWordList(String language);