fix: Wallet grouping

This commit is contained in:
Blazebrain 2025-05-05 19:55:48 +01:00 committed by Czarek Nakamoto
parent 5c48598c73
commit a4cf8a2324
3 changed files with 25 additions and 46 deletions

View file

@ -1,5 +1,4 @@
import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/entities/wallet_manager.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/entities/preferences_key.dart';
@ -18,6 +17,5 @@ Future<void> loadCurrentWallet({String? password}) async {
final type = deserializeFromInt(typeRaw); final type = deserializeFromInt(typeRaw);
final walletLoadingService = getIt.get<WalletLoadingService>(); final walletLoadingService = getIt.get<WalletLoadingService>();
final wallet = await walletLoadingService.load(type, name, password: password); final wallet = await walletLoadingService.load(type, name, password: password);
await getIt.get<WalletManager>().ensureGroupHasHashedIdentifier(wallet);
await appStore.changeCurrentWallet(wallet); await appStore.changeCurrentWallet(wallet);
} }

View file

@ -1,5 +1,3 @@
import 'dart:math';
import 'package:cake_wallet/entities/hash_wallet_identifier.dart'; import 'package:cake_wallet/entities/hash_wallet_identifier.dart';
import 'package:cake_wallet/entities/wallet_group.dart'; import 'package:cake_wallet/entities/wallet_group.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
@ -34,12 +32,8 @@ class WalletManager {
return walletInfo.hashedWalletIdentifier!; return walletInfo.hashedWalletIdentifier!;
} }
// Fallback to old logic
final address = walletInfo.parentAddress ?? walletInfo.address; final address = walletInfo.parentAddress ?? walletInfo.address;
if (address.isEmpty) { return address.isNotEmpty ? address : walletInfo.id;
return Random().nextInt(100000).toString();
}
return address;
} }
WalletGroup _getOrCreateGroup(String groupKey) { WalletGroup _getOrCreateGroup(String groupKey) {
@ -100,56 +94,41 @@ class WalletManager {
_saveCustomGroupName(groupKey, name); _saveCustomGroupName(groupKey, name);
} }
// --------------------------------------------------------------------------- /// When user opens wallet, check if it has a real hash.
// This performs a Group-Based Lazy Migration: ///
// If the user opens a wallet in an old group,
// we migrate ALL wallets that share its old group key to a new hash.
// ---------------------------------------------------------------------------
/// When a user opens a wallet, check if it has a real hash.
/// If not, migrate the ENTIRE old group so they keep the same group name /// If not, migrate the ENTIRE old group so they keep the same group name
/// and end up with the same new hash (preserving grouping). /// and end up with the same new hash (preserving grouping).
Future<void> ensureGroupHasHashedIdentifier(WalletBase openedWallet) async { Future<void> ensureGroupHasHashedIdentifier(WalletBase openedWallet) async {
WalletInfo walletInfo = openedWallet.walletInfo; final info = openedWallet.walletInfo;
// If the openedWallet already has an hash, then there is nothing to do if (info.hashedWalletIdentifier?.isNotEmpty ?? false) {
if (walletInfo.hashedWalletIdentifier != null && updateWalletGroups();
walletInfo.hashedWalletIdentifier!.isNotEmpty) {
updateWalletGroups(); // Still skeptical of calling this here. Looking for a better spot.
return; return;
} }
// Identify the old group key for this wallet final oldGroupKey = info.parentAddress?.isNotEmpty == true ? info.parentAddress! : null;
final oldGroupKey = _resolveGroupKey(walletInfo); // parentAddress fallback final walletsToMigrate = oldGroupKey != null
? _walletInfoSource.values.where((w) => (w.parentAddress ?? w.address) == oldGroupKey).toList()
: [info];
// Find all wallets that share this old group key (i.e the old group) if (oldGroupKey != null && walletsToMigrate.isEmpty) return;
final oldGroupWallets = _walletInfoSource.values.where((w) {
final key = w.hashedWalletIdentifier != null && w.hashedWalletIdentifier!.isNotEmpty
? w.hashedWalletIdentifier
: (w.parentAddress ?? w.address);
return key == oldGroupKey;
}).toList();
if (oldGroupWallets.isEmpty) { final newHash = createHashedWalletIdentifier(openedWallet);
// This shouldn't happen, but just in case it does, we return.
return; if (oldGroupKey != null) {
await _migrateGroupName(oldGroupKey, newHash);
} }
// Next, we determine the new group hash for these wallets // This throttle is here so we don't overwhelm the app when we have a lot of wallets we want to migrate.
// Since they share the same seed, we can assign that group hash const maxConcurrent = 3;
// to all the wallets to preserve grouping. for (var i = 0; i < walletsToMigrate.length; i += maxConcurrent) {
final newGroupHash = createHashedWalletIdentifier(openedWallet); final batch = walletsToMigrate.skip(i).take(maxConcurrent);
await Future.wait(batch.map((w) {
// Migrate the old group name from oldGroupKey(i.e parentAddress) to newGroupHash w.hashedWalletIdentifier = newHash;
await _migrateGroupName(oldGroupKey, newGroupHash); return w.save();
}));
// Then we assign this new hash to each wallet in that old group and save them
for (final wallet in oldGroupWallets) {
wallet.hashedWalletIdentifier = newGroupHash;
await wallet.save();
} }
// Finally, we rebuild the groups so that these wallets are now in the new group
updateWalletGroups(); updateWalletGroups();
} }

View file

@ -61,6 +61,8 @@ void startCurrentWalletChangeReaction(
return; return;
} }
await getIt.get<WalletManager>().ensureGroupHasHashedIdentifier(wallet);
final node = settingsStore.getCurrentNode(wallet.type); final node = settingsStore.getCurrentNode(wallet.type);
startWalletSyncStatusChangeReaction(wallet, fiatConversionStore); startWalletSyncStatusChangeReaction(wallet, fiatConversionStore);