diff --git a/src/wallet/rpc/transactions.cpp b/src/wallet/rpc/transactions.cpp index d8535694453..1db66bebfde 100644 --- a/src/wallet/rpc/transactions.cpp +++ b/src/wallet/rpc/transactions.cpp @@ -139,12 +139,17 @@ static UniValue ListReceived(const CWallet& wallet, const UniValue& params, cons UniValue ret(UniValue::VARR); std::map label_tally; - const auto& func = [&](const CTxDestination& address, const std::string& label, bool is_change, const std::optional& purpose) { + const auto& func = [&](const CTxDestination& address, const std::string& label, bool is_change, + const std::optional& purpose) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) { if (is_change) return; // no change addresses + // Entries in mapTally are only ever added for wallet.IsMine() addresses (see the tally + // loop above), so it's only addresses missing from mapTally that need the IsMine() check. auto it = mapTally.find(address); - if (it == mapTally.end() && !fIncludeEmpty) - return; + if (it == mapTally.end()) { + if (!fIncludeEmpty) return; + if (!wallet.IsMine(address)) return; // exclude addresses not owned by the wallet (e.g. "send" purpose) + } CAmount nAmount = 0; int nConf = std::numeric_limits::max(); diff --git a/test/functional/wallet_listreceivedby.py b/test/functional/wallet_listreceivedby.py index a1339da3463..f7a08ebd4ad 100755 --- a/test/functional/wallet_listreceivedby.py +++ b/test/functional/wallet_listreceivedby.py @@ -101,6 +101,17 @@ class ReceivedByTest(BitcoinTestFramework): res = self.nodes[1].listreceivedbyaddress(0, True, True, other_addr) assert_equal(len(res), 0) + self.log.info("listreceivedbyaddress and listreceivedbylabel exclude not owned addresses") + # setlabel assigns a "send" purpose when the wallet doesn't own the address. + send_label = "external-address" + external_addr = self.nodes[0].getnewaddress(send_label) + self.nodes[1].setlabel(external_addr, send_label) + assert_equal(self.nodes[1].getaddressinfo(external_addr)["ismine"], False) + assert_array_result(self.nodes[1].listreceivedbyaddress(minconf=0, include_empty=True), + {"address": external_addr}, {}, True) + assert_array_result(self.nodes[1].listreceivedbylabel(minconf=0, include_empty=True), + {"label": send_label}, {}, True) + self.log.info("getreceivedbyaddress Test") # Send from node 0 to 1