diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index 2649fa586c..6050ad7b4b 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -18,10 +18,10 @@ namespace wallet { static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) { - std::set addresses; + std::vector addresses; if (by_label) { // Get the set of addresses assigned to label - addresses = wallet.GetLabelAddresses(LabelFromValue(params[0])); + addresses = wallet.ListAddrBookAddresses(CWallet::AddrBookFilter{LabelFromValue(params[0])}); if (addresses.empty()) throw JSONRPCError(RPC_WALLET_ERROR, "Label not found in wallet"); } else { // Get the address @@ -29,7 +29,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b if (!IsValidDestination(dest)) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address"); } - addresses.insert(dest); + addresses.emplace_back(dest); } // Filter by own scripts only diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 910562e669..8ca8ef0a19 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2348,17 +2348,16 @@ void CWallet::MarkDestinationsDirty(const std::set& destinations } } -std::set CWallet::GetLabelAddresses(const std::string& label) const +std::vector CWallet::ListAddrBookAddresses(const std::optional& _filter) const { AssertLockHeld(cs_wallet); - std::set result; - for (const std::pair& item : m_address_book) - { - if (item.second.IsChange()) continue; - const CTxDestination& address = item.first; + 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 (strName == label) - result.insert(address); + if (filter.m_op_label && *filter.m_op_label != strName) continue; + result.emplace_back(item.first); } return result; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 7da601c3b7..970d3e2e75 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -635,7 +635,18 @@ public: std::optional GetOldestKeyPoolTime() const; - std::set GetLabelAddresses(const std::string& label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + // Filter struct for 'ListAddrBookAddresses' + struct AddrBookFilter { + // Fetch addresses with the provided label + std::optional m_op_label{std::nullopt}; + // Don't include change addresses by default + bool ignore_change{true}; + }; + + /** + * Filter and retrieve destinations stored in the addressbook + */ + std::vector ListAddrBookAddresses(const std::optional& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); /** * Marks all outputs in each one of the destinations dirty, so their cache is