From 23d62213022858a36812fdb6a459323ff0a889fc Mon Sep 17 00:00:00 2001 From: Rafael Saes Date: Fri, 12 Apr 2024 19:33:00 -0300 Subject: [PATCH] fix: restore not getting all wallet addresses by type --- cw_bitcoin/lib/electrum_wallet.dart | 66 +++++++++++++++++------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index d3aac7ca9..0ec535ab3 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1359,34 +1359,8 @@ abstract class ElectrumWalletBase final addressesSet = walletAddresses.allAddresses.map((addr) => addr.address).toSet(); currentChainTip ??= await electrumClient.getCurrentBlockChainTip() ?? 0; - await Future.wait(ADDRESS_TYPES.map((type) { - final addressesByType = walletAddresses.allAddresses.where((addr) => addr.type == type); - - return Future.wait(addressesByType.map((addressRecord) async { - final history = await _fetchAddressHistory(addressRecord, addressesSet, currentChainTip!); - - if (history.isNotEmpty) { - addressRecord.txCount = history.length; - historiesWithDetails.addAll(history); - - final matchedAddresses = - addressesByType.where((addr) => addr.isHidden == addressRecord.isHidden); - - final isLastUsedAddress = - history.isNotEmpty && addressRecord.address == matchedAddresses.last.address; - - if (isLastUsedAddress) { - await walletAddresses.discoverAddresses( - matchedAddresses.toList(), - addressRecord.isHidden, - (address, addressesSet) => - _fetchAddressHistory(address, addressesSet, currentChainTip!) - .then((history) => history.isNotEmpty ? address.address : null), - type: type); - } - } - })); - })); + await Future.wait(ADDRESS_TYPES.map( + (type) => fetchTransactionsForAddressType(addressesSet, historiesWithDetails, type))); return historiesWithDetails; } catch (e) { @@ -1395,6 +1369,42 @@ abstract class ElectrumWalletBase } } + Future fetchTransactionsForAddressType( + Set addressesSet, + Map historiesWithDetails, + BitcoinAddressType type, + ) async { + final addressesByType = walletAddresses.allAddresses.where((addr) => addr.type == type); + final hiddenAddresses = addressesByType.where((addr) => addr.isHidden == true); + final receiveAddresses = addressesByType.where((addr) => addr.isHidden == false); + + await Future.wait(addressesByType.map((addressRecord) async { + final history = await _fetchAddressHistory(addressRecord, addressesSet, currentChainTip!); + + if (history.isNotEmpty) { + addressRecord.txCount = history.length; + historiesWithDetails.addAll(history); + + final matchedAddresses = addressRecord.isHidden ? hiddenAddresses : receiveAddresses; + final isLastUsedAddress = history.isNotEmpty && matchedAddresses.last == addressRecord; + + if (isLastUsedAddress) { + // The last address by gap limit is used, discover new addresses for the same address type + await walletAddresses.discoverAddresses( + matchedAddresses.toList(), + addressRecord.isHidden, + (address, addressesSet) => _fetchAddressHistory(address, addressesSet, currentChainTip!) + .then((history) => history.isNotEmpty ? address.address : null), + type: type, + ); + + // Continue until the last address by this address type is not used yet + await fetchTransactionsForAddressType(addressesSet, historiesWithDetails, type); + } + } + })); + } + Future> _fetchAddressHistory( BitcoinAddressRecord addressRecord, Set addressesSet, int currentHeight) async { try {