diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 8ca8ef0a198..f7eb0bbc03f 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2348,17 +2348,28 @@ void CWallet::MarkDestinationsDirty(const std::set& destinations } } +void CWallet::ForEachAddrBookEntry(const ListAddrBookFunc& func) const +{ + AssertLockHeld(cs_wallet); + for (const std::pair& item : m_address_book) { + const auto& entry = item.second; + func(item.first, entry.GetLabel(), entry.purpose, entry.IsChange()); + } +} + std::vector CWallet::ListAddrBookAddresses(const std::optional& _filter) const { AssertLockHeld(cs_wallet); std::vector result; AddrBookFilter filter = _filter ? *_filter : AddrBookFilter(); - for (const std::pair& item : m_address_book) { - if (filter.ignore_change && item.second.IsChange()) continue; - const std::string& strName = item.second.GetLabel(); - if (filter.m_op_label && *filter.m_op_label != strName) continue; - result.emplace_back(item.first); - } + ForEachAddrBookEntry([&result, &filter](const CTxDestination& dest, const std::string& label, const std::string& purpose, bool is_change) { + // Filter by change + if (filter.ignore_change && is_change) return; + // Filter by label + if (filter.m_op_label && *filter.m_op_label != label) return; + // All good + result.emplace_back(dest); + }); return result; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 970d3e2e757..3775f325ba4 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -648,6 +648,13 @@ public: */ std::vector ListAddrBookAddresses(const std::optional& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** + * Walk-through the address book entries. + * Stops when the provided 'ListAddrBookFunc' returns false. + */ + using ListAddrBookFunc = std::function; + void ForEachAddrBookEntry(const ListAddrBookFunc& func) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + /** * Marks all outputs in each one of the destinations dirty, so their cache is * reset and does not return outdated information.