Skip to content

Commit 09649bc

Browse files
committed
refactor: implement general 'ListAddrBookAddresses' for addressbook destinations lookup
1 parent 192eb1e commit 09649bc

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/wallet/rpc/coins.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
namespace wallet {
1919
static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
2020
{
21-
std::set<CTxDestination> addresses;
21+
std::vector<CTxDestination> addresses;
2222
if (by_label) {
2323
// Get the set of addresses assigned to label
24-
addresses = wallet.GetLabelAddresses(LabelFromValue(params[0]));
24+
addresses = wallet.ListAddrBookAddresses(CWallet::AddrBookFilter{LabelFromValue(params[0])});
2525
if (addresses.empty()) throw JSONRPCError(RPC_WALLET_ERROR, "Label not found in wallet");
2626
} else {
2727
// Get the address
2828
CTxDestination dest = DecodeDestination(params[0].get_str());
2929
if (!IsValidDestination(dest)) {
3030
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
3131
}
32-
addresses.insert(dest);
32+
addresses.emplace_back(dest);
3333
}
3434

3535
// Filter by own scripts only

src/wallet/wallet.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,17 +2348,16 @@ void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations
23482348
}
23492349
}
23502350

2351-
std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) const
2351+
std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<AddrBookFilter>& _filter) const
23522352
{
23532353
AssertLockHeld(cs_wallet);
2354-
std::set<CTxDestination> result;
2355-
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book)
2356-
{
2357-
if (item.second.IsChange()) continue;
2358-
const CTxDestination& address = item.first;
2354+
std::vector<CTxDestination> result;
2355+
AddrBookFilter filter = _filter ? *_filter : AddrBookFilter();
2356+
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book) {
2357+
if (filter.ignore_change && item.second.IsChange()) continue;
23592358
const std::string& strName = item.second.GetLabel();
2360-
if (strName == label)
2361-
result.insert(address);
2359+
if (filter.m_op_label && *filter.m_op_label != strName) continue;
2360+
result.emplace_back(item.first);
23622361
}
23632362
return result;
23642363
}

src/wallet/wallet.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,18 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
635635

636636
std::optional<int64_t> GetOldestKeyPoolTime() const;
637637

638-
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
638+
// Filter struct for 'ListAddrBookAddresses'
639+
struct AddrBookFilter {
640+
// Fetch addresses with the provided label
641+
std::optional<std::string> m_op_label{std::nullopt};
642+
// Don't include change addresses by default
643+
bool ignore_change{true};
644+
};
645+
646+
/**
647+
* Filter and retrieve destinations stored in the addressbook
648+
*/
649+
std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
639650

640651
/**
641652
* Marks all outputs in each one of the destinations dirty, so their cache is

0 commit comments

Comments
 (0)