mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-14 20:20:43 +02:00
refactor: use 'ForEachAddrBookEntry' in RPC 'getaddressesbylabel'
This commit is contained in:
parent
2b48642499
commit
83e42c4b94
@ -637,17 +637,6 @@ RPCHelpMan getaddressinfo()
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Convert CAddressBookData to JSON record. */
|
|
||||||
static UniValue AddressBookDataToJSON(const CAddressBookData& data, const bool verbose)
|
|
||||||
{
|
|
||||||
UniValue ret(UniValue::VOBJ);
|
|
||||||
if (verbose) {
|
|
||||||
ret.pushKV("name", data.GetLabel());
|
|
||||||
}
|
|
||||||
ret.pushKV("purpose", data.purpose);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
RPCHelpMan getaddressesbylabel()
|
RPCHelpMan getaddressesbylabel()
|
||||||
{
|
{
|
||||||
return RPCHelpMan{"getaddressesbylabel",
|
return RPCHelpMan{"getaddressesbylabel",
|
||||||
@ -680,10 +669,10 @@ RPCHelpMan getaddressesbylabel()
|
|||||||
// Find all addresses that have the given label
|
// Find all addresses that have the given label
|
||||||
UniValue ret(UniValue::VOBJ);
|
UniValue ret(UniValue::VOBJ);
|
||||||
std::set<std::string> addresses;
|
std::set<std::string> addresses;
|
||||||
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->m_address_book) {
|
pwallet->ForEachAddrBookEntry([&](const CTxDestination& _dest, const std::string& _label, const std::string& _purpose, bool _is_change) {
|
||||||
if (item.second.IsChange()) continue;
|
if (_is_change) return;
|
||||||
if (item.second.GetLabel() == label) {
|
if (_label == label) {
|
||||||
std::string address = EncodeDestination(item.first);
|
std::string address = EncodeDestination(_dest);
|
||||||
// CWallet::m_address_book is not expected to contain duplicate
|
// CWallet::m_address_book is not expected to contain duplicate
|
||||||
// address strings, but build a separate set as a precaution just in
|
// address strings, but build a separate set as a precaution just in
|
||||||
// case it does.
|
// case it does.
|
||||||
@ -693,9 +682,11 @@ RPCHelpMan getaddressesbylabel()
|
|||||||
// and since duplicate addresses are unexpected (checked with
|
// and since duplicate addresses are unexpected (checked with
|
||||||
// std::set in O(log(N))), UniValue::__pushKV is used instead,
|
// std::set in O(log(N))), UniValue::__pushKV is used instead,
|
||||||
// which currently is O(1).
|
// which currently is O(1).
|
||||||
ret.__pushKV(address, AddressBookDataToJSON(item.second, false));
|
UniValue value(UniValue::VOBJ);
|
||||||
|
value.pushKV("purpose", _purpose);
|
||||||
|
ret.__pushKV(address, value);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
if (ret.empty()) {
|
if (ret.empty()) {
|
||||||
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));
|
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, std::string("No addresses with label " + label));
|
||||||
|
Loading…
Reference in New Issue
Block a user