Skip to content

Commit 83e42c4

Browse files
committed
refactor: use 'ForEachAddrBookEntry' in RPC 'getaddressesbylabel'
1 parent 2b48642 commit 83e42c4

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/wallet/rpc/addresses.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -637,17 +637,6 @@ RPCHelpMan getaddressinfo()
637637
};
638638
}
639639

640-
/** Convert CAddressBookData to JSON record. */
641-
static UniValue AddressBookDataToJSON(const CAddressBookData& data, const bool verbose)
642-
{
643-
UniValue ret(UniValue::VOBJ);
644-
if (verbose) {
645-
ret.pushKV("name", data.GetLabel());
646-
}
647-
ret.pushKV("purpose", data.purpose);
648-
return ret;
649-
}
650-
651640
RPCHelpMan getaddressesbylabel()
652641
{
653642
return RPCHelpMan{"getaddressesbylabel",
@@ -680,10 +669,10 @@ RPCHelpMan getaddressesbylabel()
680669
// Find all addresses that have the given label
681670
UniValue ret(UniValue::VOBJ);
682671
std::set<std::string> addresses;
683-
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->m_address_book) {
684-
if (item.second.IsChange()) continue;
685-
if (item.second.GetLabel() == label) {
686-
std::string address = EncodeDestination(item.first);
672+
pwallet->ForEachAddrBookEntry([&](const CTxDestination& _dest, const std::string& _label, const std::string& _purpose, bool _is_change) {
673+
if (_is_change) return;
674+
if (_label == label) {
675+
std::string address = EncodeDestination(_dest);
687676
// CWallet::m_address_book is not expected to contain duplicate
688677
// address strings, but build a separate set as a precaution just in
689678
// case it does.
@@ -693,9 +682,11 @@ RPCHelpMan getaddressesbylabel()
693682
// and since duplicate addresses are unexpected (checked with
694683
// std::set in O(log(N))), UniValue::__pushKV is used instead,
695684
// which currently is O(1).
696-
ret.__pushKV(address, AddressBookDataToJSON(item.second, false));
685+
UniValue value(UniValue::VOBJ);
686+
value.pushKV("purpose", _purpose);
687+
ret.__pushKV(address, value);
697688
}
698-
}
689+
});
699690

700691
if (ret.empty()) {
701692
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));

0 commit comments

Comments
 (0)