From a63ef3a95c4e4c6685ffbf1f44ebb86b4068ce49 Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Thu, 16 Feb 2017 14:15:58 -0500 Subject: [PATCH] Wallet: add filter for listunspent, update help info --- src/wallet/rpcwallet.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 5303d4e3fc..345295ef2a 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -1398,13 +1398,13 @@ UniValue listreceivedbyaddress(const JSONRPCRequest& request) if (request.fHelp || request.params.size() > 4) throw runtime_error( - "listreceivedbyaddress ( minconf include_empty include_watchonly)\n" + "listreceivedbyaddress ( minconf include_empty include_watchonly, asset)\n" "\nList balances by receiving address.\n" "\nArguments:\n" "1. minconf (numeric, optional, default=1) The minimum number of confirmations before payments are included.\n" "2. include_empty (bool, optional, default=false) Whether to include addresses that haven't received any payments.\n" "3. include_watchonly (bool, optional, default=false) Whether to include watch-only addresses (see 'importaddress').\n" - "4. assetlabel (string, optional) Hex asset id or asset label for balance.\n" + "4. \"asset\" (string, optional, default=bitcoin) The hex asset id or label to filter for. \"*\" is used to list all results.\n" "\nResult:\n" "[\n" " {\n" @@ -2552,9 +2552,9 @@ UniValue listunspent(const JSONRPCRequest& request) if (!EnsureWalletIsAvailable(request.fHelp)) return NullUniValue; - if (request.fHelp || request.params.size() > 4) + if (request.fHelp || request.params.size() > 5) throw runtime_error( - "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] )\n" + "listunspent ( minconf maxconf [\"addresses\",...] [include_unsafe] asset )\n" "\nReturns array of unspent transaction outputs\n" "with between minconf and maxconf (inclusive) confirmations.\n" "Optionally filter to only include txouts paid to specified addresses.\n" @@ -2570,6 +2570,7 @@ UniValue listunspent(const JSONRPCRequest& request) " because they come from unconfirmed untrusted transactions or unconfirmed\n" " replacement transactions (cases where we are less sure that a conflicting\n" " transaction won't be mined).\n" + "5. \"asset\" (string, optional, default=bitcoin) The hex asset id or label to filter for. \"*\" is used to list all results.\n" "\nResult\n" "[ (array of json object)\n" " {\n" @@ -2629,6 +2630,16 @@ UniValue listunspent(const JSONRPCRequest& request) include_unsafe = request.params[3].get_bool(); } + std::string asset = "bitcoin"; + if (request.params.size() > 4 && request.params[4].isStr()) { + asset = request.params[3].get_str(); + } + CAssetID id(uint256S(asset)); + if (asset != "*" && pwalletMain->GetAssetLabelFromID(uint256S(asset)) == "") + id = pwalletMain->GetAssetIDFromLabel(asset); + if (asset != "*" && id == uint256()) + throw JSONRPCError(RPC_WALLET_ERROR, "Unknown or invalid asset id/label"); + UniValue results(UniValue::VARR); vector vecOutputs; assert(pwalletMain != NULL); @@ -2650,6 +2661,10 @@ UniValue listunspent(const JSONRPCRequest& request) if (nValue == -1 || assetid == uint256()) continue; + if (asset != "*" && id != assetid) { + continue; + } + UniValue entry(UniValue::VOBJ); entry.push_back(Pair("txid", out.tx->GetHash().GetHex())); entry.push_back(Pair("vout", out.i));