Skip to content

Commit fa9f2ab

Browse files
committed
refactor: RPC 'listlabels', encapsulate 'CWallet::ListAddrBookLabels' functionality
Mainly to not access 'm_address_book' externally.
1 parent 83e42c4 commit fa9f2ab

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/wallet/rpc/addresses.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,7 @@ RPCHelpMan listlabels()
733733
}
734734

735735
// Add to a set to sort by label name, then insert into Univalue array
736-
std::set<std::string> label_set;
737-
for (const std::pair<const CTxDestination, CAddressBookData>& entry : pwallet->m_address_book) {
738-
if (entry.second.IsChange()) continue;
739-
if (purpose.empty() || entry.second.purpose == purpose) {
740-
label_set.insert(entry.second.GetLabel());
741-
}
742-
}
736+
std::set<std::string> label_set = pwallet->ListAddrBookLabels(purpose);
743737

744738
UniValue ret(UniValue::VARR);
745739
for (const std::string& name : label_set) {

src/wallet/wallet.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,20 @@ std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<A
23732373
return result;
23742374
}
23752375

2376+
std::set<std::string> CWallet::ListAddrBookLabels(const std::string& purpose) const
2377+
{
2378+
AssertLockHeld(cs_wallet);
2379+
std::set<std::string> label_set;
2380+
ForEachAddrBookEntry([&](const CTxDestination& _dest, const std::string& _label,
2381+
const std::string& _purpose, bool _is_change) {
2382+
if (_is_change) return;
2383+
if (purpose.empty() || _purpose == purpose) {
2384+
label_set.insert(_label);
2385+
}
2386+
});
2387+
return label_set;
2388+
}
2389+
23762390
bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal, bilingual_str& error)
23772391
{
23782392
m_spk_man = pwallet->GetScriptPubKeyMan(type, internal);

src/wallet/wallet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
648648
*/
649649
std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
650650

651+
/**
652+
* Retrieve all the known labels in the address book
653+
*/
654+
std::set<std::string> ListAddrBookLabels(const std::string& purpose) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
655+
651656
/**
652657
* Walk-through the address book entries.
653658
* Stops when the provided 'ListAddrBookFunc' returns false.

0 commit comments

Comments
 (0)