bitcoin restore bug fix and move restore code into view model

This commit is contained in:
Matthew Fosse 2024-05-03 16:10:11 -07:00
parent d1870ba8b8
commit 85a9379417
4 changed files with 60 additions and 48 deletions

View file

@ -287,6 +287,10 @@ class CWBitcoin extends Bitcoin {
}) async {
List<DerivationInfo> list = [];
if (!validateMnemonic(mnemonic) && !bip39.validateMnemonic(mnemonic)) {
throw BitcoinMnemonicIsIncorrectException();
}
List<DerivationType> types = await compareDerivationMethods(mnemonic: mnemonic, node: node);
if (types.length == 1 && types.first == DerivationType.electrum) {
return [

View file

@ -96,9 +96,6 @@ class WalletRestorePage extends BasePage {
final GlobalKey<WalletRestoreFromSeedFormState> walletRestoreFromSeedFormKey;
final GlobalKey<WalletRestoreFromKeysFromState> walletRestoreFromKeysFormKey;
final FocusNode _blockHeightFocusNode;
// DerivationType derivationType = DerivationType.unknown;
// String? derivationPath = null;
DerivationInfo? derivationInfo;
@override
@ -322,7 +319,6 @@ class WalletRestorePage extends BasePage {
}
}
credentials['derivationInfo'] = this.derivationInfo;
credentials['walletType'] = walletRestoreViewModel.type;
return credentials;
}
@ -353,50 +349,10 @@ class WalletRestorePage extends BasePage {
return;
}
walletRestoreViewModel.state = IsExecutingState();
dynamic creds = _credentials();
creds['derivationInfo'] = await walletRestoreViewModel.getFinalDerivationInfo(context, creds);
DerivationInfo? dInfo;
// get info about the different derivations:
List<DerivationInfo> derivations =
await walletRestoreViewModel.getDerivationInfo(_credentials());
int derivationsWithHistory = 0;
int derivationWithHistoryIndex = 0;
for (int i = 0; i < derivations.length; i++) {
if (derivations[i].transactionsCount > 0) {
derivationsWithHistory++;
derivationWithHistoryIndex = i;
}
}
if (derivationsWithHistory > 1) {
dInfo = await Navigator.of(context).pushNamed(
Routes.restoreWalletChooseDerivation,
arguments: derivations,
) as DerivationInfo?;
} else if (derivationsWithHistory == 1) {
dInfo = derivations[derivationWithHistoryIndex];
}
// get the default derivation for this wallet type:
if (dInfo == null) {
// we only return 1 derivation if we're pretty sure we know which one to use:
if (derivations.length == 1) {
dInfo = derivations.first;
} else {
// if we have multiple possible derivations, and none have histories
// we just default to the most common one:
dInfo = walletRestoreViewModel.getCommonRestoreDerivation();
}
}
this.derivationInfo = dInfo;
if (this.derivationInfo == null) {
this.derivationInfo = walletRestoreViewModel.getDefaultDerivation();
}
walletRestoreViewModel.create(options: _credentials());
walletRestoreViewModel.create(options: creds);
}
Future<void> showNameExistsAlert(BuildContext context) {

View file

@ -1,12 +1,15 @@
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/nano/nano.dart';
import 'package:cake_wallet/ethereum/ethereum.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cw_core/nano_account_info_response.dart';
import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart';
import 'package:cake_wallet/polygon/polygon.dart';
import 'package:cake_wallet/solana/solana.dart';
import 'package:cake_wallet/tron/tron.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/store/app_store.dart';
@ -238,6 +241,54 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
return list;
}
Future<DerivationInfo?> getFinalDerivationInfo(BuildContext context, dynamic credentials) async {
state = IsExecutingState();
try {
DerivationInfo? dInfo;
// get info about the different derivations:
List<DerivationInfo> derivations = await getDerivationInfo(credentials);
int derivationsWithHistory = 0;
int derivationWithHistoryIndex = 0;
for (int i = 0; i < derivations.length; i++) {
if (derivations[i].transactionsCount > 0) {
derivationsWithHistory++;
derivationWithHistoryIndex = i;
}
}
if (derivationsWithHistory > 1) {
dInfo = await Navigator.of(context).pushNamed(
Routes.restoreWalletChooseDerivation,
arguments: derivations,
) as DerivationInfo?;
} else if (derivationsWithHistory == 1) {
dInfo = derivations[derivationWithHistoryIndex];
}
// get the default derivation for this wallet type:
if (dInfo == null) {
// we only return 1 derivation if we're pretty sure we know which one to use:
if (derivations.length == 1) {
dInfo = derivations.first;
} else {
// if we have multiple possible derivations, and none have histories
// we just default to the most common one:
dInfo = getCommonRestoreDerivation();
}
}
if (dInfo == null) {
dInfo = getDefaultDerivation();
}
return dInfo;
} catch (e) {
state = FailureState(e.toString());
rethrow;
}
}
@override
Future<WalletBase> process(WalletCredentials credentials) async {
if (mode == WalletRestoreMode.keys) {

View file

@ -105,6 +105,7 @@ import 'package:cw_bitcoin/bitcoin_transaction_credentials.dart';
import 'package:cw_bitcoin/litecoin_wallet_service.dart';
import 'package:cw_bitcoin/script_hash.dart';
import 'package:cw_bitcoin/pending_bitcoin_transaction.dart';
import 'package:cw_bitcoin/mnemonic_is_incorrect_exception.dart';
import 'package:mobx/mobx.dart';
""";
const bitcoinCwPart = "part 'cw_bitcoin.dart';";