From 6e8cc9c39e938e318950b64ce42b3bcb3cfd3726 Mon Sep 17 00:00:00 2001 From: David Adegoke <64401859+Blazebrain@users.noreply.github.com> Date: Wed, 11 Dec 2024 19:51:09 +0100 Subject: [PATCH] CW-854 Monero Wallet Group Fix (#1865) * Fix: Tentative fix to wrong wallet groupings, specifically monero or non bip39 wallet types * fix: Modify logic to filter out single wallets and multi group wallets --------- Co-authored-by: Omar Hatem --- .../wallet_list/wallet_list_view_model.dart | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/view_model/wallet_list/wallet_list_view_model.dart b/lib/view_model/wallet_list/wallet_list_view_model.dart index c903b535f..407dce55a 100644 --- a/lib/view_model/wallet_list/wallet_list_view_model.dart +++ b/lib/view_model/wallet_list/wallet_list_view_model.dart @@ -2,6 +2,7 @@ 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:cake_wallet/reactions/bip39_wallet_utils.dart'; import 'package:hive/hive.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/store/app_store.dart'; @@ -90,20 +91,59 @@ abstract class WalletListViewModelBase with Store { multiWalletGroups.clear(); singleWalletsList.clear(); - wallets.addAll( - _walletInfoSource.values - .map((info) => convertWalletInfoToWalletListItem(info)), - ); + for (var info in _walletInfoSource.values) { + wallets.add(convertWalletInfoToWalletListItem(info)); + } //========== Split into shared seed groups and single wallets list _walletManager.updateWalletGroups(); - for (var group in _walletManager.walletGroups) { + final walletGroupsFromManager = _walletManager.walletGroups; + + for (var group in walletGroupsFromManager) { if (group.wallets.length == 1) { - singleWalletsList - .add(convertWalletInfoToWalletListItem(group.wallets.first)); - } else { + singleWalletsList.add(convertWalletInfoToWalletListItem(group.wallets.first)); + continue; + } + + // Identify wallets that should be moved to singleWalletsList using the filters: the type/derivation + final excludedWallets = []; + + for (var wallet in group.wallets) { + // Check for non-BIP39 wallet types + final isNonBIP39 = !isBIP39Wallet(wallet.type); + + // Check for nano derivation type + final isNanoDerivation = wallet.type == WalletType.nano && + wallet.derivationInfo?.derivationType == DerivationType.nano; + + // Check for electrum derivation type + final isElectrumDerivation = + (wallet.type == WalletType.bitcoin || wallet.type == WalletType.litecoin) && + wallet.derivationInfo?.derivationType == DerivationType.electrum; + + if (isNonBIP39 || isNanoDerivation || isElectrumDerivation) { + excludedWallets.add(wallet); + } + } + + // Add excluded wallets to singleWalletsList + for (var excludedWallet in excludedWallets) { + singleWalletsList.add(convertWalletInfoToWalletListItem(excludedWallet)); + } + + // Remove excluded wallets from the group's wallets to avoid duplication + group.wallets.removeWhere((wallet) { + return excludedWallets.any((excluded) => excluded.address == wallet.address); + }); + + // Check if the group has more than one wallet after the excluded wallets are removed. + if (group.wallets.length > 1) { + //Add the entire group to the multi wallet group list since its still a multi wallet multiWalletGroups.add(group); + } else if (group.wallets.length == 1) { + // Add the group to the wallet left to the single wallets list + singleWalletsList.add(convertWalletInfoToWalletListItem(group.wallets.first)); } } }