From 47bf8cc7c19a0adde6d22e187d899225392b9d56 Mon Sep 17 00:00:00 2001 From: fosse Date: Thu, 24 Aug 2023 18:06:58 -0400 Subject: [PATCH] updates --- cw_bitcoin/lib/bitcoin_derivations.dart | 2 +- cw_bitcoin/lib/bitcoin_wallet.dart | 12 ++- cw_bitcoin/lib/bitcoin_wallet_service.dart | 93 +++++++------------ cw_bitcoin/lib/electrum_wallet_snapshot.dart | 29 ++++-- cw_nano/lib/nano_wallet.dart | 2 +- .../wallet_restore_choose_derivation.dart | 13 ++- .../screens/restore/wallet_restore_page.dart | 16 ++-- ..._restore_choose_derivation_view_model.dart | 26 ++---- 8 files changed, 98 insertions(+), 95 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_derivations.dart b/cw_bitcoin/lib/bitcoin_derivations.dart index c76e5a4b6..47ae6ef95 100644 --- a/cw_bitcoin/lib/bitcoin_derivations.dart +++ b/cw_bitcoin/lib/bitcoin_derivations.dart @@ -6,7 +6,7 @@ Map> bitcoin_derivations = { derivationType: DerivationType.bip39, derivationPath: "m/0'/1", description: "cake default?", - script_type: "???", + script_type: "p2wpkh",// TODO: not really sure what cake uses by default ), DerivationInfo( derivationType: DerivationType.bip39, diff --git a/cw_bitcoin/lib/bitcoin_wallet.dart b/cw_bitcoin/lib/bitcoin_wallet.dart index 8153787d4..741fe8abf 100644 --- a/cw_bitcoin/lib/bitcoin_wallet.dart +++ b/cw_bitcoin/lib/bitcoin_wallet.dart @@ -45,7 +45,7 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { initialChangeAddressIndex: initialChangeAddressIndex, mainHd: hd, sideHd: bitcoin.HDWallet.fromSeed(seedBytes, network: networkType) - .derivePath(walletInfo.derivationPath!),// default: "m/0'/1" + .derivePath(walletInfo.derivationPath!), networkType: networkType); } @@ -78,6 +78,16 @@ abstract class BitcoinWalletBase extends ElectrumWallet with Store { required String password, }) async { final snp = await ElectrumWallletSnapshot.load(name, walletInfo.type, password); + + + walletInfo.derivationType = snp.derivationType; + walletInfo.derivationPath = snp.derivationPath; + + // set the default if not present: + if (walletInfo.derivationPath == null) { + walletInfo.derivationPath = "m/0'/1"; + } + return BitcoinWallet( mnemonic: snp.mnemonic, password: password, diff --git a/cw_bitcoin/lib/bitcoin_wallet_service.dart b/cw_bitcoin/lib/bitcoin_wallet_service.dart index 31110ebba..d04f66366 100644 --- a/cw_bitcoin/lib/bitcoin_wallet_service.dart +++ b/cw_bitcoin/lib/bitcoin_wallet_service.dart @@ -1,9 +1,11 @@ import 'dart:io'; +import 'package:cw_bitcoin/address_to_output_script.dart'; import 'package:cw_bitcoin/bitcoin_mnemonic.dart'; import 'package:cw_bitcoin/bitcoin_mnemonic_is_incorrect_exception.dart'; import 'package:cw_bitcoin/bitcoin_wallet_creation_credentials.dart'; import 'package:cw_bitcoin/electrum.dart'; import 'package:cw_bitcoin/script_hash.dart'; +import 'package:cw_bitcoin/utils.dart'; import 'package:cw_core/node.dart'; import 'package:cw_core/unspent_coins_info.dart'; import 'package:cw_core/wallet_base.dart'; @@ -112,12 +114,12 @@ class BitcoinWalletService extends WalletService> compareDerivationMethods( {required mnemonic, required Node node}) async { - return [DerivationType.bip39]; + return [DerivationType.unknown]; } static Future> getDerivationsFromMnemonic( {required String mnemonic, required Node node}) async { - var list = []; + List list = []; final electrumClient = ElectrumClient(); await electrumClient.connectToUri(node.uri); @@ -128,23 +130,44 @@ class BitcoinWalletService extends WalletService getInfoFromSeed({required String seed, required Node node}) async { diff --git a/cw_bitcoin/lib/electrum_wallet_snapshot.dart b/cw_bitcoin/lib/electrum_wallet_snapshot.dart index 6db0c23f2..449026489 100644 --- a/cw_bitcoin/lib/electrum_wallet_snapshot.dart +++ b/cw_bitcoin/lib/electrum_wallet_snapshot.dart @@ -3,6 +3,7 @@ import 'package:cw_bitcoin/bitcoin_address_record.dart'; import 'package:cw_bitcoin/electrum_balance.dart'; import 'package:cw_bitcoin/file.dart'; import 'package:cw_core/pathForWallet.dart'; +import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_type.dart'; class ElectrumWallletSnapshot { @@ -14,7 +15,10 @@ class ElectrumWallletSnapshot { required this.addresses, required this.balance, required this.regularAddressIndex, - required this.changeAddressIndex}); + required this.changeAddressIndex, + this.derivationType, + this.derivationPath, + }); final String name; final String password; @@ -25,6 +29,8 @@ class ElectrumWallletSnapshot { ElectrumBalance balance; int regularAddressIndex; int changeAddressIndex; + DerivationType? derivationType; + String? derivationPath; static Future load(String name, WalletType type, String password) async { final path = await pathForWallet(name: name, type: type); @@ -41,19 +47,24 @@ class ElectrumWallletSnapshot { var regularAddressIndex = 0; var changeAddressIndex = 0; + final derivationType = data['derivationType'] as DerivationType; + final derivationPath = data['derivationPath'] as String?; + try { regularAddressIndex = int.parse(data['account_index'] as String? ?? '0'); changeAddressIndex = int.parse(data['change_address_index'] as String? ?? '0'); } catch (_) {} return ElectrumWallletSnapshot( - name: name, - type: type, - password: password, - mnemonic: mnemonic, - addresses: addresses, - balance: balance, - regularAddressIndex: regularAddressIndex, - changeAddressIndex: changeAddressIndex); + name: name, + type: type, + password: password, + mnemonic: mnemonic, + addresses: addresses, + balance: balance, + regularAddressIndex: regularAddressIndex, + changeAddressIndex: changeAddressIndex, + derivationType: derivationType, + derivationPath: derivationPath); } } diff --git a/cw_nano/lib/nano_wallet.dart b/cw_nano/lib/nano_wallet.dart index 925e9b6f1..bcae9aff8 100644 --- a/cw_nano/lib/nano_wallet.dart +++ b/cw_nano/lib/nano_wallet.dart @@ -370,7 +370,7 @@ abstract class NanoWalletBase try { balance[currency] = await _client.getBalance(_publicAddress!); } catch (e) { - throw Exception("Failed to get balance $e"); + print("Failed to get balance $e"); } await save(); } diff --git a/lib/src/screens/restore/wallet_restore_choose_derivation.dart b/lib/src/screens/restore/wallet_restore_choose_derivation.dart index 4127c8fc3..904fc6599 100644 --- a/lib/src/screens/restore/wallet_restore_choose_derivation.dart +++ b/lib/src/screens/restore/wallet_restore_choose_derivation.dart @@ -56,7 +56,7 @@ class WalletRestoreChooseDerivationPage extends BasePage { child: InkWell( borderRadius: BorderRadius.circular(15), onTap: () async { - Navigator.pop(context, derivation.derivationType); + Navigator.pop(context, derivation); }, child: ListTile( contentPadding: EdgeInsets.all(16), @@ -73,6 +73,17 @@ class WalletRestoreChooseDerivationPage extends BasePage { subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ + if (derivation.derivationPath != null) + Text( + derivation.derivationPath!, + style: Theme.of(context).primaryTextTheme.labelMedium!.copyWith( + fontSize: 14, + fontWeight: FontWeight.w500, + color: Theme.of(context) + .extension()! + .secondaryTextColor, + ), + ), Text( derivation.address, style: Theme.of(context).primaryTextTheme.labelMedium!.copyWith( diff --git a/lib/src/screens/restore/wallet_restore_page.dart b/lib/src/screens/restore/wallet_restore_page.dart index ebcd69dea..e86d4a328 100644 --- a/lib/src/screens/restore/wallet_restore_page.dart +++ b/lib/src/screens/restore/wallet_restore_page.dart @@ -72,7 +72,8 @@ class WalletRestorePage extends BasePage { key: walletRestoreFromKeysFormKey, walletRestoreViewModel: walletRestoreViewModel, onPrivateKeyChange: (String seed) { - if (walletRestoreViewModel.type == WalletType.nano || walletRestoreViewModel.type == WalletType.banano) { + if (walletRestoreViewModel.type == WalletType.nano || + walletRestoreViewModel.type == WalletType.banano) { walletRestoreViewModel.isButtonEnabled = _isValidSeedKey(); } }, @@ -105,6 +106,7 @@ class WalletRestorePage extends BasePage { final GlobalKey walletRestoreFromKeysFormKey; final FocusNode _blockHeightFocusNode; DerivationType derivationType = DerivationType.unknown; + String? derivationPath = null; @override Widget body(BuildContext context) { @@ -281,8 +283,8 @@ class WalletRestorePage extends BasePage { } credentials['derivationType'] = this.derivationType; + credentials['derivationPath'] = this.derivationPath; credentials['walletType'] = walletRestoreViewModel.type; - return credentials; } @@ -319,16 +321,18 @@ class WalletRestorePage extends BasePage { if (derivationTypes[0] == DerivationType.unknown || derivationTypes.length > 1) { // push screen to choose the derivation type: - var derivationType = await Navigator.of(context) + var derivationInfo = await Navigator.of(context) .pushNamed(Routes.restoreWalletChooseDerivation, arguments: _credentials()) - as DerivationType?; - if (derivationType == null) { + as DerivationInfo?; + if (derivationInfo == null) { walletRestoreViewModel.state = InitialExecutionState(); return; } - this.derivationType = derivationType; + this.derivationType = derivationInfo.derivationType; + this.derivationPath = derivationInfo.derivationPath; } else { this.derivationType = derivationTypes[0]; + this.derivationPath = "m/0'/1"; } walletRestoreViewModel.state = InitialExecutionState(); diff --git a/lib/view_model/wallet_restore_choose_derivation_view_model.dart b/lib/view_model/wallet_restore_choose_derivation_view_model.dart index 9e867c5f6..9c7fbb38d 100644 --- a/lib/view_model/wallet_restore_choose_derivation_view_model.dart +++ b/lib/view_model/wallet_restore_choose_derivation_view_model.dart @@ -30,7 +30,7 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store { switch (walletType) { case WalletType.bitcoin: String? mnemonic = credentials['seed'] as String?; - await BitcoinWalletService.getDerivationsFromMnemonic(mnemonic: mnemonic!, node: node); + return await BitcoinWalletService.getDerivationsFromMnemonic(mnemonic: mnemonic!, node: node); // var standardInfo = await NanoWalletService.getInfoFromSeedOrMnemonic( // DerivationType.nano, @@ -39,24 +39,12 @@ abstract class WalletRestoreChooseDerivationViewModelBase with Store { // node: node, // ); - list.add(DerivationInfo( - balance: "0.00000", - address: "address", - height: 0, - derivationType: DerivationType.bip39, - )); - - // if (bip39Info["balance"] != null) { - // list.add(Derivation( - // NanoUtil.getRawAsUsableString(bip39Info["balance"] as String, NanoUtil.rawPerNano), - // bip39Info["address"] as String, - // DerivationType.bip39, - // int.tryParse( - // bip39Info["confirmation_height"] as String? ?? "", - // ) ?? - // 0, - // )); - // } + // list.add(DerivationInfo( + // balance: "0.00000", + // address: "address", + // height: 0, + // derivationType: DerivationType.bip39, + // )); break; case WalletType.nano: String? mnemonic = credentials['seed'] as String?;