aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
authorfurszy <matiasfurszyfer@protonmail.com>2022-06-11 11:06:22 -0300
committerfurszy <matiasfurszyfer@protonmail.com>2022-06-21 10:23:20 -0300
commit09649bc95d5f2855a54a8cf02e65215a3b333c92 (patch)
tree5fb9700427f6d7816f78327048832e2d3fe24deb /src/wallet
parent192eb1e61c3c43baec7f32c498ab0ce0656a58f7 (diff)
refactor: implement general 'ListAddrBookAddresses' for addressbook destinations lookup
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/rpc/coins.cpp6
-rw-r--r--src/wallet/wallet.cpp15
-rw-r--r--src/wallet/wallet.h13
3 files changed, 22 insertions, 12 deletions
diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp
index 2649fa586c..6050ad7b4b 100644
--- a/src/wallet/rpc/coins.cpp
+++ b/src/wallet/rpc/coins.cpp
@@ -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
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 910562e669..8ca8ef0a19 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -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;
}
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index 7da601c3b7..970d3e2e75 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -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