From 6c41028f7c2ec6903c9a5f41d5025e7dca0e3a51 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 7 Sep 2016 16:09:16 -0700 Subject: [PATCH] Add z_listreceivedbyaddress RPC call --- src/rpcclient.cpp | 1 + src/rpcserver.cpp | 1 + src/rpcserver.h | 1 + src/wallet/rpcwallet.cpp | 61 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index a8e4789b6..2841a3793 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -98,6 +98,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "zcbenchmark", 1 }, { "zcbenchmark", 2 }, { "getblocksubsidy", 0}, + { "z_listreceivedbyaddress", 1}, { "z_getbalance", 1}, { "z_gettotalbalance", 0}, { "z_sendmany", 1}, diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 2653311cb..28aa3bc62 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -385,6 +385,7 @@ static const CRPCCommand vRPCCommands[] = { "wallet", "zcrawjoinsplit", &zc_raw_joinsplit, true }, { "wallet", "zcrawreceive", &zc_raw_receive, true }, { "wallet", "zcsamplejoinsplit", &zc_sample_joinsplit, true }, + { "wallet", "z_listreceivedbyaddress",&z_listreceivedbyaddress,false }, { "wallet", "z_getbalance", &z_getbalance, false }, { "wallet", "z_gettotalbalance", &z_gettotalbalance, false }, { "wallet", "z_sendmany", &z_sendmany, false }, diff --git a/src/rpcserver.h b/src/rpcserver.h index ffad60962..6402a7a16 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -256,6 +256,7 @@ extern json_spirit::Value z_getnewaddress(const json_spirit::Array& params, bool extern json_spirit::Value z_listaddresses(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp extern json_spirit::Value z_exportwallet(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp extern json_spirit::Value z_importwallet(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp +extern json_spirit::Value z_listreceivedbyaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp extern json_spirit::Value z_getbalance(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp extern json_spirit::Value z_gettotalbalance(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp extern json_spirit::Value z_sendmany(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 26b2f129b..b74995450 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2886,6 +2886,67 @@ CAmount getBalanceZaddr(std::string address, size_t minDepth = 1) { } +Value z_listreceivedbyaddress(const Array& params, bool fHelp) +{ + if (!EnsureWalletIsAvailable(fHelp)) + return Value::null; + + if (fHelp || params.size()==0 || params.size() >2) + throw runtime_error( + "z_listreceivedbyaddress \"address\" ( minconf )\n" + "\nReturn a list of amounts received by a zaddr belonging to the node’s wallet.\n" + "\nArguments:\n" + "1. \"address\" (string) The private address.\n" + "2. minconf (numeric, optional, default=1) Only include transactions confirmed at least this many times.\n" + "\nResult:\n" + "{\n" + " \"txid\": xxxxx, (string) the transaction id\n" + " \"amount\": xxxxx, (numeric) the amount of value in the note\n" + " \"memo\": xxxxx, (string) hexademical string representation of memo field\n" + "}\n" + ); + + LOCK2(cs_main, pwalletMain->cs_wallet); + + int nMinDepth = 1; + if (params.size() > 1) { + nMinDepth = params[1].get_int(); + } + if (nMinDepth < 0) { + throw JSONRPCError(RPC_INVALID_PARAMETER, "Minimum number of confirmations cannot be less than 0"); + } + + // Check that the from address is valid. + auto fromaddress = params[0].get_str(); + + libzcash::PaymentAddress zaddr; + CZCPaymentAddress address(fromaddress); + try { + zaddr = address.Get(); + } catch (std::runtime_error) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr."); + } + + if (!pwalletMain->HaveSpendingKey(zaddr)) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key not found."); + } + + + Array result; + std::vector entries; + pwalletMain->GetFilteredNotes(entries, fromaddress, nMinDepth, false); + for (CNotePlaintextEntry & entry : entries) { + Object obj; + obj.push_back(Pair("txid",entry.jsop.hash.ToString())); + obj.push_back(Pair("amount", ValueFromAmount(CAmount(entry.plaintext.value)))); + std::string data(entry.plaintext.memo.begin(), entry.plaintext.memo.end()); + obj.push_back(Pair("memo", HexStr(data))); + result.push_back(obj); + } + return result; +} + + Value z_getbalance(const Array& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp))