mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-28 13:02:38 +02:00
refactor: implement general 'ListAddrBookAddresses' for addressbook destinations lookup
This commit is contained in:
parent
192eb1e61c
commit
09649bc95d
@ -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<CTxDestination> addresses;
|
||||
std::vector<CTxDestination> 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
|
||||
|
@ -2348,17 +2348,16 @@ void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations
|
||||
}
|
||||
}
|
||||
|
||||
std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) const
|
||||
std::vector<CTxDestination> CWallet::ListAddrBookAddresses(const std::optional<AddrBookFilter>& _filter) const
|
||||
{
|
||||
AssertLockHeld(cs_wallet);
|
||||
std::set<CTxDestination> result;
|
||||
for (const std::pair<const CTxDestination, CAddressBookData>& item : m_address_book)
|
||||
{
|
||||
if (item.second.IsChange()) continue;
|
||||
const CTxDestination& address = item.first;
|
||||
std::vector<CTxDestination> result;
|
||||
AddrBookFilter filter = _filter ? *_filter : AddrBookFilter();
|
||||
for (const std::pair<const CTxDestination, CAddressBookData>& 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;
|
||||
}
|
||||
|
@ -635,7 +635,18 @@ public:
|
||||
|
||||
std::optional<int64_t> GetOldestKeyPoolTime() const;
|
||||
|
||||
std::set<CTxDestination> 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<std::string> 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<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
/**
|
||||
* Marks all outputs in each one of the destinations dirty, so their cache is
|
||||
|
Loading…
Reference in New Issue
Block a user