feat: initial hive -> SQLite (#2502)

* feat: initial hive -> SQLite

Includes migration script and initial work done to
remove dependency on hive generators.

Also contains some refactoring to make sync code
async.

Features new dev screen that lists all SQLite tables

Aspects that need testing:
- backups (from this version, and from previous versions as well)
- wallet loading / wallet list
- almost everything else
- renaming
- deletion
- sorting in wallet list
- wallet groups
- wallet seed/keys
- wallet switching
- wallet creation / restore - all methods
- wallet address page
- hardware wallet connection (and prompts from mall screens)
- contacts
- advanced settings in wallet creation
- derivation paths
- some minor wownero fixes
- base, hardware wallets

* fix: build errors

* fix: Bad state: No element in backup restore

* address comments from review

* fix: HardwareWalletType
This commit is contained in:
cyan
2025-10-31 22:37:15 +01:00
committed by GitHub
parent 718525926e
commit 76876181e5
121 changed files with 2623 additions and 917 deletions
@@ -22,16 +22,22 @@ class ContactListViewModel = ContactListViewModelBase with _$ContactListViewMode
abstract class ContactListViewModelBase with Store {
ContactListViewModelBase(
this.contactSource, this.walletInfoSource, this._currency, this.settingsStore)
this.contactSource, List<WalletInfo> wallets, this._currency, this.settingsStore)
: contacts = ObservableList<ContactRecord>(),
walletContacts = [],
isAutoGenerateEnabled =
settingsStore.autoGenerateSubaddressStatus == AutoGenerateSubaddressStatus.enabled {
walletInfoSource.values.forEach((info) {
unawaited(_init());
}
Future<void> _init() async {
final walletInfos = await WalletInfo.getAll();
for (final info in walletInfos) {
final addressInfos = await info.getAddressInfos();
final addresses = await info.getAddresses();
if ([WalletType.monero, WalletType.wownero, WalletType.haven].contains(info.type) &&
info.addressInfos != null) {
for (var key in info.addressInfos!.keys) {
final value = info.addressInfos![key];
addressInfos.isNotEmpty) {
for (var key in addressInfos.keys) {
final value = addressInfos[key];
final address = value?.first;
if (address != null) {
final name = _createName(info.name, address.label, key: key);
@@ -42,7 +48,7 @@ abstract class ContactListViewModelBase with Store {
));
}
}
} else if (info.addresses?.isNotEmpty == true && info.addresses!.length > 1) {
} else if (addresses.isNotEmpty == true && addresses.length > 1) {
if ([WalletType.monero, WalletType.wownero, WalletType.haven, WalletType.decred]
.contains(info.type)) {
final address = info.address;
@@ -53,7 +59,7 @@ abstract class ContactListViewModelBase with Store {
walletTypeToCryptoCurrency(info.type),
));
} else {
info.addresses!.forEach((address, label) {
addresses.forEach((address, label) {
if (label.isEmpty) {
return;
}
@@ -78,13 +84,14 @@ abstract class ContactListViewModelBase with Store {
walletTypeToCryptoCurrency(info.type),
));
}
});
}
_subscription = contactSource.bindToListWithTransform(
contacts, (Contact contact) => ContactRecord(contactSource, contact),
initialFire: true);
setOrderType(settingsStore.contactListOrder);
walletContacts = walletContacts.toList(); // rebuild
}
String _createName(String walletName, String label, {int? key = null}) {
@@ -97,9 +104,9 @@ abstract class ContactListViewModelBase with Store {
final bool isAutoGenerateEnabled;
final Box<Contact> contactSource;
final Box<WalletInfo> walletInfoSource;
final ObservableList<ContactRecord> contacts;
final List<WalletContact> walletContacts;
@observable
List<WalletContact> walletContacts = [];
final CryptoCurrency? _currency;
StreamSubscription<BoxEvent>? _subscription;
final SettingsStore settingsStore;
@@ -1098,8 +1098,8 @@ abstract class DashboardViewModelBase with Store {
void setSyncAll(bool value) => settingsStore.currentSyncAll = value;
Future<List<String>> checkForHavenWallets() async {
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
return walletInfoSource.values
final walletInfos = await WalletInfo.getAll();
return walletInfos
.where((element) => element.type == WalletType.haven)
.map((e) => e.name)
.toList();
@@ -1112,10 +1112,9 @@ abstract class DashboardViewModelBase with Store {
.loadString('assets/text/cakewallet_weak_bitcoin_seeds_hashed_sorted_version1.txt');
final vulnerableSeeds = vulnerableSeedsString.split("\n");
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
List<String> affectedWallets = [];
for (var walletInfo in walletInfoSource.values) {
final walletInfos = await WalletInfo.getAll();
for (var walletInfo in walletInfos) {
if (walletInfo.type == WalletType.bitcoin) {
final password = await keyService.getWalletPassword(walletName: walletInfo.name);
final path = await pathForWallet(name: walletInfo.name, type: walletInfo.type);
@@ -44,6 +44,7 @@ import 'package:cake_wallet/utils/token_utilities.dart';
import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart';
import 'package:cake_wallet/view_model/send/fees_view_model.dart';
import 'package:cake_wallet/view_model/unspent_coins/unspent_coins_list_view_model.dart';
import 'package:cw_core/cake_hive.dart';
import 'package:cw_core/crypto_amount_format.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/erc20_token.dart';
@@ -82,7 +83,6 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
this.contactListViewModel,
this.unspentCoinsListViewModel,
this.feesViewModel,
this.walletInfoSource,
this.fiatConversionStore,
) : _cryptoNumberFormat = NumberFormat(),
isSendAllEnabled = false,
@@ -369,8 +369,6 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
final FeesViewModel feesViewModel;
final Box<WalletInfo> walletInfoSource;
@observable
double bestRate = 0.0;
@@ -1096,7 +1094,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
@action
Future<void> _injectUserEthTokensIntoCurrencyLists() async {
final userTokens = await TokenUtilities.loadAllUniqueEvmTokens(walletInfoSource);
final userTokens = await TokenUtilities.loadAllUniqueEvmTokens();
final toAddReceive = <CryptoCurrency>[];
final toAddDeposit = <CryptoCurrency>[];
@@ -1134,7 +1132,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
@action
Future<void> _injectUserSplTokensIntoCurrencyLists() async {
final userTokens = await TokenUtilities.loadAllUniqueSolTokens(walletInfoSource);
final userTokens = await TokenUtilities.loadAllUniqueSolTokens();
final toAddReceive = <CryptoCurrency>[];
final toAddDeposit = <CryptoCurrency>[];
@@ -1162,7 +1160,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
@action
Future<void> _injectUserTronTokensIntoCurrencyLists() async {
final userTokens = await TokenUtilities.loadAllUniqueTronTokens(walletInfoSource);
final userTokens = await TokenUtilities.loadAllUniqueTronTokens();
final toAddReceive = <CryptoCurrency>[];
final toAddDeposit = <CryptoCurrency>[];
@@ -98,15 +98,15 @@ abstract class BitboxViewModelBase extends HardwareWalletViewModel with Store {
}
@override
void initWallet(WalletBase wallet) {
Future<void> initWallet(WalletBase wallet) async {
switch (wallet.type) {
case WalletType.bitcoin:
case WalletType.litecoin:
return bitcoin!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return bitcoin!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.ethereum:
return ethereum!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return ethereum!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.polygon:
return polygon!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return polygon!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
default:
throw Exception('Unexpected wallet type: ${wallet.type}');
}
@@ -22,7 +22,7 @@ abstract class HardwareWalletViewModel {
HardwareWalletService getHardwareWalletService(WalletType type);
void initWallet(WalletBase wallet);
Future<void> initWallet(WalletBase wallet);
String? interpretErrorCode(String error) => null;
}
@@ -178,18 +178,18 @@ abstract class LedgerViewModelBase extends HardwareWalletViewModel with Store {
sdk.LedgerConnection get connection => _connection!;
@override
void initWallet(WalletBase wallet) {
Future<void> initWallet(WalletBase wallet) async {
switch (wallet.type) {
case WalletType.monero:
return monero!.setLedgerConnection(wallet, connection);
case WalletType.bitcoin:
return bitcoin!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return bitcoin!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.litecoin:
return bitcoin!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return bitcoin!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.ethereum:
return ethereum!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return ethereum!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.polygon:
return polygon!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return polygon!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
default:
throw Exception('Unexpected wallet type: ${wallet.type}');
}
@@ -66,15 +66,15 @@ abstract class TrezorViewModelBase extends HardwareWalletViewModel with Store {
}
@override
void initWallet(WalletBase wallet) {
Future<void> initWallet(WalletBase wallet) async {
switch (wallet.type) {
case WalletType.bitcoin:
case WalletType.litecoin:
return bitcoin!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return bitcoin!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.ethereum:
return ethereum!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return ethereum!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
case WalletType.polygon:
return polygon!.setHardwareWalletService(wallet, getHardwareWalletService(wallet.type));
return polygon!.setHardwareWalletService(wallet, await getHardwareWalletService(wallet.type));
default:
throw Exception('Unexpected wallet type: ${wallet.type}');
}
@@ -1,5 +1,3 @@
import 'package:cw_core/wallet_info.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
part 'new_wallet_type_view_model.g.dart';
@@ -7,10 +5,7 @@ part 'new_wallet_type_view_model.g.dart';
class NewWalletTypeViewModel = NewWalletTypeViewModelBase with _$NewWalletTypeViewModel;
abstract class NewWalletTypeViewModelBase with Store {
NewWalletTypeViewModelBase(this._walletInfoSource);
NewWalletTypeViewModelBase(this.hasExisitingWallet);
@computed
bool get hasExisitingWallet => _walletInfoSource.isNotEmpty;
final Box<WalletInfo> _walletInfoSource;
final bool hasExisitingWallet;
}
@@ -15,11 +15,9 @@ class PaymentViewModel = PaymentViewModelBase with _$PaymentViewModel;
abstract class PaymentViewModelBase with Store {
PaymentViewModelBase({
required this.appStore,
required this.walletInfoSource,
});
final AppStore appStore;
final Box<WalletInfo> walletInfoSource;
@observable
WalletType? detectedWalletType;
@@ -52,7 +50,7 @@ abstract class PaymentViewModelBase with Store {
return PaymentFlowResult.currentWalletCompatible();
}
final compatibleWallets = getWalletsByType(detectedWalletType!);
final compatibleWallets = await getWalletsByType(detectedWalletType!);
switch (compatibleWallets.length) {
case 0:
@@ -70,8 +68,8 @@ abstract class PaymentViewModelBase with Store {
}
}
List<WalletInfo> getWalletsByType(WalletType walletType) {
return walletInfoSource.values.where((wallet) => wallet.type == walletType).toList();
Future<List<WalletInfo>> getWalletsByType(WalletType walletType) async {
return (await WalletInfo.getAll()).where((wallet) => wallet.type == walletType).toList();
}
}
@@ -45,11 +45,11 @@ abstract class RestoreFromBackupViewModelBase with Store {
try {
await backupService.importBackupFile(file, password);
} catch (e) {
} catch (e, s) {
if (e.toString().contains("unknown_backup_version")) {
state = FailureState('This is not a valid backup file, please make sure you have selected the correct one');
} else {
state = FailureState(e.toString());
state = FailureState(e.toString() + "\n" + s.toString());
}
}
+1 -5
View File
@@ -100,8 +100,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
this.transactionDescriptionBox,
this.hardwareWalletViewModel,
this.unspentCoinsListViewModel,
this.feesViewModel,
this.walletInfoSource, {
this.feesViewModel, {
this.coinTypeToSpendFrom = UnspentCoinType.nonMweb,
}) : state = InitialExecutionState(),
currencies = appStore.wallet!.balance.keys.toList(),
@@ -130,8 +129,6 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
ObservableList<Output> outputs;
final Box<WalletInfo> walletInfoSource;
@observable
UnspentCoinType coinTypeToSpendFrom;
@@ -1214,7 +1211,6 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
Future<void> fetchTokenForContractAddress(String contractAddress) async {
final token = await TokenUtilities.findTokenByAddress(
walletType: wallet.type,
walletInfoSource: walletInfoSource,
address: contractAddress,
);
@@ -116,7 +116,7 @@ abstract class OtherSettingsViewModelBase with Store {
@action
Future<void> onHardwareWalletTypeChanged(HardwareWalletType hwType) async {
_wallet.walletInfo.hardwareWalletType = hwType;
return _wallet.walletInfo.save();
await _wallet.walletInfo.save();
}
@computed
+17 -9
View File
@@ -26,7 +26,7 @@ part 'wallet_creation_vm.g.dart';
class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM;
abstract class WalletCreationVMBase with Store {
WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService,
WalletCreationVMBase(this._appStore, this.walletCreationService,
this.seedSettingsViewModel,
{required this.type, required this.isRecovery})
: state = InitialExecutionState(),
@@ -54,7 +54,6 @@ abstract class WalletCreationVMBase with Store {
WalletType type;
final bool isRecovery;
final WalletCreationService walletCreationService;
final Box<WalletInfo> _walletInfoSource;
final AppStore _appStore;
final SeedSettingsViewModel seedSettingsViewModel;
@@ -62,9 +61,9 @@ abstract class WalletCreationVMBase with Store {
[WalletType.monero, WalletType.wownero].contains(type) &&
(Polyseed.isValidSeed(seed) || (seed.split(" ").length == 14));
bool nameExists(String name) => walletCreationService.exists(name);
Future<bool> nameExists(String name) => walletCreationService.exists(name);
bool typeExists(WalletType type) => walletCreationService.typeExists(type);
Future<bool> typeExists(WalletType type) => walletCreationService.typeExists(type);
Future<void> create({dynamic options}) async {
final type = this.type;
@@ -88,6 +87,13 @@ abstract class WalletCreationVMBase with Store {
final credentials = getCredentials(options);
final di = ((credentials.derivationInfo?.derivationPath??"") == "")
? getDefaultCreateDerivation()
: credentials.derivationInfo;
final diId = await di!.save();
credentials.derivationInfo = di;
final walletInfo = WalletInfo.external(
id: WalletBase.idFor(name, type),
name: name,
@@ -98,19 +104,21 @@ abstract class WalletCreationVMBase with Store {
path: path,
dirPath: dirPath,
address: '',
showIntroCakePayCard: (!walletCreationService.typeExists(type)) && type != WalletType.haven,
derivationInfo: credentials.derivationInfo ?? getDefaultCreateDerivation(),
showIntroCakePayCard: (!await walletCreationService.typeExists(type)) && type != WalletType.haven,
derivationInfoId: diId,
hardwareWalletType: credentials.hardwareWalletType,
);
credentials.walletInfo = walletInfo;
// await walletInfo.save();
printV("derivationInfo: ${(await walletInfo.getDerivationInfo()).toJson()}");
final wallet = await process(credentials);
final isNonSeedWallet = isRecovery ? wallet.seed == null : false;
walletInfo.isNonSeedWallet = isNonSeedWallet;
walletInfo.hashedWalletIdentifier = createHashedWalletIdentifier(wallet);
walletInfo.address = wallet.walletAddresses.address;
await _walletInfoSource.add(walletInfo);
await walletInfo.save();
await _appStore.changeCurrentWallet(wallet);
_appStore.authenticationStore.allowedCreate();
state = ExecutedSuccessfullyState();
@@ -125,7 +133,7 @@ abstract class WalletCreationVMBase with Store {
}
}
DerivationInfo? getDefaultCreateDerivation() {
DerivationInfo getDefaultCreateDerivation() {
final useBip39ForBitcoin = seedSettingsViewModel.bitcoinSeedType.type == DerivationType.bip39;
final useBip39ForNano = seedSettingsViewModel.nanoSeedType.type == DerivationType.bip39;
switch (type) {
@@ -155,7 +163,7 @@ abstract class WalletCreationVMBase with Store {
}
return bitcoin!.getElectrumDerivations()[DerivationType.electrum]!.first;
default:
return null;
return DerivationInfo(derivationType: DerivationType.unknown);
}
}
@@ -1,3 +1,5 @@
import 'dart:async';
import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/entities/wallet_group.dart';
import 'package:cake_wallet/entities/wallet_manager.dart';
@@ -6,7 +8,6 @@ import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
import 'package:cake_wallet/wallet_types.g.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:mobx/mobx.dart';
@@ -23,11 +24,9 @@ abstract class WalletGroupsDisplayViewModelBase with Store {
this._walletManager,
this.walletListViewModel, {
required this.type,
}) : isFetchingMnemonic = false,
multiWalletGroups = ObservableList<WalletGroup>(),
singleWalletsList = ObservableList<WalletInfo>() {
reaction((_) => _appStore.wallet, (_) => updateWalletInfoSourceList());
updateWalletInfoSourceList();
}) : isFetchingMnemonic = false {
reaction((_) => _appStore.wallet, (_) => unawaited(updateWalletInfoSourceList()));
unawaited(updateWalletInfoSourceList());
}
final WalletType type;
@@ -37,10 +36,10 @@ abstract class WalletGroupsDisplayViewModelBase with Store {
final WalletListViewModel walletListViewModel;
@observable
ObservableList<WalletGroup> multiWalletGroups;
ObservableList<WalletGroup> multiWalletGroups = ObservableList<WalletGroup>();
@observable
ObservableList<WalletInfo> singleWalletsList;
ObservableList<WalletInfo> singleWalletsList = ObservableList<WalletInfo>();
@observable
WalletGroup? selectedWalletGroup;
@@ -97,31 +96,33 @@ abstract class WalletGroupsDisplayViewModelBase with Store {
}
@action
void updateWalletInfoSourceList() {
Future<void> updateWalletInfoSourceList() async {
List<WalletGroup> wallets = [];
multiWalletGroups.clear();
singleWalletsList.clear();
_walletManager.updateWalletGroups();
await _walletManager.updateWalletGroups();
final walletGroups = _walletManager.walletGroups;
// Iterate through the wallet groups to filter and categorize wallets
for (var group in walletGroups) {
// Handle group wallet filtering
bool shouldExcludeGroup = group.wallets.any((wallet) {
bool shouldExcludeGroup = false;
for (final wallet in group.wallets) {
// Check for non-BIP39 wallet types
bool isNonBIP39Wallet = !isBIP39Wallet(wallet.type);
// Check for nano derivation type
final di = await wallet.getDerivationInfo();
bool isNanoDerivationType = wallet.type == WalletType.nano &&
wallet.derivationInfo?.derivationType == DerivationType.nano;
di.derivationType == DerivationType.nano;
// Check for electrum derivation type
bool isElectrumDerivationType =
(wallet.type == WalletType.bitcoin || wallet.type == WalletType.litecoin) &&
wallet.derivationInfo?.derivationType == DerivationType.electrum;
di.derivationType == DerivationType.electrum;
// Check that selected wallet type is not present already in group
bool isSameTypeAsSelectedWallet = wallet.type == type;
@@ -129,16 +130,17 @@ abstract class WalletGroupsDisplayViewModelBase with Store {
bool isNonSeedWallet = wallet.isNonSeedWallet;
bool isNotMoneroBip39Wallet = wallet.type == WalletType.monero &&
wallet.derivationInfo?.derivationType != DerivationType.bip39;
di.derivationType != DerivationType.bip39;
// Exclude if any of these conditions are true
return isNonBIP39Wallet ||
isNanoDerivationType ||
isElectrumDerivationType ||
isSameTypeAsSelectedWallet ||
isNonSeedWallet ||
isNotMoneroBip39Wallet;
});
shouldExcludeGroup = shouldExcludeGroup ||
isNonBIP39Wallet ||
isNanoDerivationType ||
isElectrumDerivationType ||
isSameTypeAsSelectedWallet ||
isNonSeedWallet ||
isNotMoneroBip39Wallet;
}
if (shouldExcludeGroup) continue;
@@ -159,7 +161,7 @@ abstract class WalletGroupsDisplayViewModelBase with Store {
return WalletListItem(
name: info.name,
type: info.type,
key: info.key,
key: info.id,
isCurrent: info.name == _appStore.wallet?.name && info.type == _appStore.wallet?.type,
isEnabled: availableWalletTypes.contains(info.type),
isTestnet: info.network?.toLowerCase().contains('testnet') ?? false,
@@ -33,10 +33,9 @@ abstract class WalletHardwareRestoreViewModelBase extends WalletCreationVM with
this.hardwareWalletVM,
AppStore appStore,
WalletCreationService walletCreationService,
Box<WalletInfo> walletInfoSource,
SeedSettingsViewModel seedSettingsViewModel,
{required WalletType type})
: super(appStore, walletInfoSource, walletCreationService, seedSettingsViewModel,
: super(appStore, walletCreationService, seedSettingsViewModel,
type: type, isRecovery: true);
@observable
@@ -57,8 +56,8 @@ abstract class WalletHardwareRestoreViewModelBase extends WalletCreationVM with
@action
Future<void> getNextAvailableAccounts(int limit) async {
try {
List<HardwareAccountData> accounts = await hardwareWalletVM
.getHardwareWalletService(type)
final service = await hardwareWalletVM.getHardwareWalletService(type);
List<HardwareAccountData> accounts = await service
.getAvailableAccounts(index: _nextIndex, limit: limit);
availableAccounts.addAll(accounts);
@@ -46,7 +46,7 @@ abstract class WalletEditViewModelBase with Store {
state = WalletEditRenamePending();
if (isWalletGroup) {
_walletManager.updateWalletGroups();
await _walletManager.updateWalletGroups();
_walletManager.setGroupName(walletGroupKey!, newName);
} else {
@@ -2,12 +2,12 @@ import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/entities/wallet_group.dart';
import 'package:cake_wallet/entities/wallet_list_order_types.dart';
import 'package:cake_wallet/entities/wallet_manager.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:cake_wallet/wallet_types.g.dart';
part 'wallet_list_view_model.g.dart';
@@ -16,7 +16,6 @@ class WalletListViewModel = WalletListViewModelBase with _$WalletListViewModel;
abstract class WalletListViewModelBase with Store {
WalletListViewModelBase(
this._walletInfoSource,
this._appStore,
this._walletLoadingService,
this._walletManager,
@@ -25,7 +24,6 @@ abstract class WalletListViewModelBase with Store {
singleWalletsList = ObservableList<WalletListItem>(),
expansionTileStateTrack = ObservableMap<int, bool>() {
setOrderType(_appStore.settingsStore.walletListOrder);
reaction((_) => _appStore.wallet, (_) => updateList());
updateList();
}
@@ -63,12 +61,11 @@ abstract class WalletListViewModelBase with Store {
final AppStore _appStore;
final WalletManager _walletManager;
final Box<WalletInfo> _walletInfoSource;
final WalletLoadingService _walletLoadingService;
WalletType get currentWalletType => _appStore.wallet!.type;
bool requireHardwareWalletConnection(WalletListItem walletItem) =>
Future<bool> requireHardwareWalletConnection(WalletListItem walletItem) async =>
_walletLoadingService.requireHardwareWalletConnection(
walletItem.type, walletItem.name);
@@ -87,50 +84,62 @@ abstract class WalletListViewModelBase with Store {
bool get ascending => _appStore.settingsStore.walletListAscending;
bool isUpdating = false;
@action
void updateList() {
wallets.clear();
multiWalletGroups.clear();
singleWalletsList.clear();
for (var info in _walletInfoSource.values) {
wallets.add(convertWalletInfoToWalletListItem(info));
Future<void> updateList() async {
if (isUpdating) {
return;
}
isUpdating = true;
try {
wallets.clear();
multiWalletGroups.clear();
singleWalletsList.clear();
//========== Split into shared seed groups and single wallets list
_walletManager.updateWalletGroups();
final list = await WalletInfo.getAll();
final walletGroupsFromManager = _walletManager.walletGroups;
for (var group in walletGroupsFromManager) {
if (group.wallets.length == 1) {
singleWalletsList.add(convertWalletInfoToWalletListItem(group.wallets.first));
continue;
for (var info in list) {
wallets.add(convertWalletInfoToWalletListItem(info));
}
multiWalletGroups.add(group);
//========== Split into shared seed groups and single wallets list
await _walletManager.updateWalletGroups();
final walletGroupsFromManager = _walletManager.walletGroups;
for (var group in walletGroupsFromManager) {
if (group.wallets.length == 1) {
singleWalletsList.add(convertWalletInfoToWalletListItem(group.wallets.first));
continue;
}
multiWalletGroups.add(group);
}
} finally {
isUpdating = false;
}
}
Future<void> reorderAccordingToWalletList() async {
if (wallets.isEmpty) {
updateList();
await updateList();
return;
}
_appStore.settingsStore.walletListOrder = FilterListOrderType.Custom;
// make a copy of the walletInfoSource:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
// delete all wallets from walletInfoSource:
await _walletInfoSource.clear();
List<WalletInfo> wiList = await WalletInfo.getAll();
// Reorder single wallets using the singleWalletsList
int oldI = 0;
for (WalletListItem wallet in singleWalletsList) {
for (int i = 0; i < walletInfoSourceCopy.length; i++) {
if (walletInfoSourceCopy[i].name == wallet.name) {
await _walletInfoSource.add(walletInfoSourceCopy[i]);
walletInfoSourceCopy.removeAt(i);
for (int i = 0; i < wiList.length; i++) {
if (wiList[i].id == wallet.key) {
oldI++;
wiList[i].sortOrder = oldI;
await wiList[i].save();
break;
}
}
@@ -139,10 +148,11 @@ abstract class WalletListViewModelBase with Store {
// Reorder wallets within multi-wallet groups
for (WalletGroup group in multiWalletGroups) {
for (WalletInfo walletInfo in group.wallets) {
for (int i = 0; i < walletInfoSourceCopy.length; i++) {
if (walletInfoSourceCopy[i].name == walletInfo.name) {
await _walletInfoSource.add(walletInfoSourceCopy[i]);
walletInfoSourceCopy.removeAt(i);
for (int i = 0; i < wiList.length; i++) {
if (wiList[i].name == walletInfo.name) {
wiList[i].sortOrder = i+oldI;
await wiList[i].save();
wiList.removeAt(i);
break;
}
}
@@ -150,47 +160,52 @@ abstract class WalletListViewModelBase with Store {
}
// Rebuild the list of wallets and groups
updateList();
await updateList();
}
Future<void> sortGroupByType() async {
// sort the wallets by type:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
await _walletInfoSource.clear();
List<WalletInfo> wiList = await WalletInfo.getAll();
if (ascending) {
walletInfoSourceCopy
.sort((a, b) => a.type.toString().compareTo(b.type.toString()));
wiList.sort((a, b) => a.type.toString().compareTo(b.type.toString()));
} else {
walletInfoSourceCopy
.sort((a, b) => b.type.toString().compareTo(a.type.toString()));
wiList.sort((a, b) => b.type.toString().compareTo(a.type.toString()));
}
await _walletInfoSource.addAll(walletInfoSourceCopy);
updateList();
for (int i = 0; i < wiList.length; i++) {
wiList[i].sortOrder = i;
await wiList[i].save();
}
await updateList();
}
Future<void> sortAlphabetically() async {
// sort the wallets alphabetically:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
await _walletInfoSource.clear();
List<WalletInfo> wiList = await WalletInfo.getAll();
if (ascending) {
walletInfoSourceCopy.sort((a, b) => a.name.compareTo(b.name));
wiList.sort((a, b) => a.name.compareTo(b.name));
} else {
walletInfoSourceCopy.sort((a, b) => b.name.compareTo(a.name));
wiList.sort((a, b) => b.name.compareTo(a.name));
}
await _walletInfoSource.addAll(walletInfoSourceCopy);
updateList();
for (int i = 0; i < wiList.length; i++) {
wiList[i].sortOrder = i;
await wiList[i].save();
}
await updateList();
}
Future<void> sortByCreationDate() async {
// sort the wallets by creation date:
List<WalletInfo> walletInfoSourceCopy = _walletInfoSource.values.toList();
await _walletInfoSource.clear();
List<WalletInfo> wiList = await WalletInfo.getAll();
if (ascending) {
walletInfoSourceCopy.sort((a, b) => a.date.compareTo(b.date));
wiList.sort((a, b) => a.date.compareTo(b.date));
} else {
walletInfoSourceCopy.sort((a, b) => b.date.compareTo(a.date));
wiList.sort((a, b) => b.date.compareTo(a.date));
}
await _walletInfoSource.addAll(walletInfoSourceCopy);
for (int i = 0; i < wiList.length; i++) {
wiList[i].sortOrder = i;
await wiList[i].save();
}
updateList();
}
@@ -223,7 +238,7 @@ abstract class WalletListViewModelBase with Store {
return WalletListItem(
name: info.name,
type: info.type,
key: info.key,
key: info.id,
isCurrent: info.name == _appStore.wallet?.name &&
info.type == _appStore.wallet?.type,
isEnabled: availableWalletTypes.contains(info.type),
+1 -2
View File
@@ -34,12 +34,11 @@ abstract class WalletNewVMBase extends WalletCreationVM with Store {
WalletNewVMBase(
AppStore appStore,
WalletCreationService walletCreationService,
Box<WalletInfo> walletInfoSource,
this.advancedPrivacySettingsViewModel,
SeedSettingsViewModel seedSettingsViewModel, {
required this.newWalletArguments,
}) : selectedMnemonicLanguage = '',
super(appStore, walletInfoSource, walletCreationService, seedSettingsViewModel,
super(appStore, walletCreationService, seedSettingsViewModel,
type: newWalletArguments!.type, isRecovery: false);
final NewWalletArguments? newWalletArguments;
@@ -24,7 +24,6 @@ import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
part 'wallet_restore_view_model.g.dart';
@@ -33,12 +32,12 @@ class WalletRestoreViewModel = WalletRestoreViewModelBase with _$WalletRestoreVi
abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
WalletRestoreViewModelBase(AppStore appStore, WalletCreationService walletCreationService,
Box<WalletInfo> walletInfoSource, SeedSettingsViewModel seedSettingsViewModel,
SeedSettingsViewModel seedSettingsViewModel,
{required WalletType type, this.restoredWallet, this.hardwareWalletType})
: isButtonEnabled = restoredWallet != null,
hasPassphrase = false,
mode = restoredWallet?.restoreMode ?? WalletRestoreMode.seed,
super(appStore, walletInfoSource, walletCreationService, seedSettingsViewModel,
super(appStore, walletCreationService, seedSettingsViewModel,
type: type, isRecovery: true) {
switch (type) {
case WalletType.monero:
@@ -5,7 +5,6 @@ import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_core/utils/print_verbose.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
part 'wallet_switcher_view_model.g.dart';
@@ -16,12 +15,10 @@ abstract class WalletSwitcherViewModelBase with Store {
WalletSwitcherViewModelBase({
required this.appStore,
required this.walletLoadingService,
required this.walletInfoSource,
});
final AppStore appStore;
final WalletLoadingService walletLoadingService;
final Box<WalletInfo> walletInfoSource;
@observable
WalletInfo? selectedWallet;
@@ -30,12 +27,13 @@ abstract class WalletSwitcherViewModelBase with Store {
bool isProcessing = false;
@action
List<WalletInfo> getWallets(WalletType? walletType) {
Future<List<WalletInfo>> getWallets(WalletType? walletType) async {
final wiList = await WalletInfo.getAll();
if (walletType == null) {
return walletInfoSource.values.toList();
return wiList;
}
return walletInfoSource.values.where((wallet) => wallet.type == walletType).toList();
return wiList.where((wallet) => wallet.type == walletType).toList();
}
@action