From 275f9310c52befad93a6d915050de1d0d668cc91 Mon Sep 17 00:00:00 2001 From: random-zebra Date: Mon, 13 Dec 2021 10:18:01 +0100 Subject: [PATCH] [RPC] listunspent: pre-filter coins by destination in AvailableCoins --- src/wallet/rpcwallet.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 2f8e6ee15ae53..55d0b136119e5 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3760,6 +3760,8 @@ UniValue listunspent(const JSONRPCRequest& request) nMaxDepth = request.params[1].get_int(); } + CWallet::AvailableCoinsFilter coinFilter; + std::set destinations; if (request.params.size() > 2) { RPCTypeCheckArgument(request.params[2], UniValue::VARR); @@ -3773,6 +3775,7 @@ UniValue listunspent(const JSONRPCRequest& request) throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + input.get_str()); destinations.insert(dest); } + coinFilter.onlyFilteredDest = &destinations; } // List watch only utxo @@ -3784,7 +3787,6 @@ UniValue listunspent(const JSONRPCRequest& request) nWatchonlyConfig = 1; } - CWallet::AvailableCoinsFilter coinFilter; if (request.params.size() > 4) { const UniValue& options = request.params[4].get_obj(); @@ -3828,19 +3830,13 @@ UniValue listunspent(const JSONRPCRequest& request) if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth) continue; - CTxDestination address; - const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey; - bool fValidAddress = ExtractDestination(scriptPubKey, address); - - if (!destinations.empty() && (!fValidAddress || !destinations.count(address))) { - continue; - } - UniValue entry(UniValue::VOBJ); entry.pushKV("txid", out.tx->GetHash().GetHex()); entry.pushKV("vout", out.i); entry.pushKV("generated", out.tx->IsCoinStake() || out.tx->IsCoinBase()); - if (fValidAddress) { + CTxDestination address; + const CScript& scriptPubKey = out.tx->tx->vout[out.i].scriptPubKey; + if (ExtractDestination(scriptPubKey, address)) { entry.pushKV("address", EncodeDestination(address)); if (pwallet->HasAddressBook(address)) { entry.pushKV("label", pwallet->GetNameForAddressBookEntry(address));