Merge #11667: Add scripts to dumpwallet RPC
[bitcoinplatinum.git] / src / rpc / client.cpp
blobe641493e1eaeba7c430b1f60f77d2855d84b8fff
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include <rpc/client.h>
7 #include <rpc/protocol.h>
8 #include <util.h>
10 #include <set>
11 #include <stdint.h>
13 class CRPCConvertParam
15 public:
16 std::string methodName; //!< method whose params want conversion
17 int paramIdx; //!< 0-based idx of param to convert
18 std::string paramName; //!< parameter name
21 /**
22 * Specify a (method, idx, name) here if the argument is a non-string RPC
23 * argument and needs to be converted from JSON.
25 * @note Parameter indexes start from 0.
27 static const CRPCConvertParam vRPCConvertParams[] =
29 { "setmocktime", 0, "timestamp" },
30 { "generate", 0, "nblocks" },
31 { "generate", 1, "maxtries" },
32 { "generatetoaddress", 0, "nblocks" },
33 { "generatetoaddress", 2, "maxtries" },
34 { "getnetworkhashps", 0, "nblocks" },
35 { "getnetworkhashps", 1, "height" },
36 { "sendtoaddress", 1, "amount" },
37 { "sendtoaddress", 4, "subtractfeefromamount" },
38 { "sendtoaddress", 5 , "replaceable" },
39 { "sendtoaddress", 6 , "conf_target" },
40 { "settxfee", 0, "amount" },
41 { "getreceivedbyaddress", 1, "minconf" },
42 { "getreceivedbyaccount", 1, "minconf" },
43 { "listreceivedbyaddress", 0, "minconf" },
44 { "listreceivedbyaddress", 1, "include_empty" },
45 { "listreceivedbyaddress", 2, "include_watchonly" },
46 { "listreceivedbyaccount", 0, "minconf" },
47 { "listreceivedbyaccount", 1, "include_empty" },
48 { "listreceivedbyaccount", 2, "include_watchonly" },
49 { "getbalance", 1, "minconf" },
50 { "getbalance", 2, "include_watchonly" },
51 { "getblockhash", 0, "height" },
52 { "waitforblockheight", 0, "height" },
53 { "waitforblockheight", 1, "timeout" },
54 { "waitforblock", 1, "timeout" },
55 { "waitfornewblock", 0, "timeout" },
56 { "move", 2, "amount" },
57 { "move", 3, "minconf" },
58 { "sendfrom", 2, "amount" },
59 { "sendfrom", 3, "minconf" },
60 { "listtransactions", 1, "count" },
61 { "listtransactions", 2, "skip" },
62 { "listtransactions", 3, "include_watchonly" },
63 { "listaccounts", 0, "minconf" },
64 { "listaccounts", 1, "include_watchonly" },
65 { "walletpassphrase", 1, "timeout" },
66 { "getblocktemplate", 0, "template_request" },
67 { "listsinceblock", 1, "target_confirmations" },
68 { "listsinceblock", 2, "include_watchonly" },
69 { "listsinceblock", 3, "include_removed" },
70 { "sendmany", 1, "amounts" },
71 { "sendmany", 2, "minconf" },
72 { "sendmany", 4, "subtractfeefrom" },
73 { "sendmany", 5 , "replaceable" },
74 { "sendmany", 6 , "conf_target" },
75 { "addmultisigaddress", 0, "nrequired" },
76 { "addmultisigaddress", 1, "keys" },
77 { "createmultisig", 0, "nrequired" },
78 { "createmultisig", 1, "keys" },
79 { "listunspent", 0, "minconf" },
80 { "listunspent", 1, "maxconf" },
81 { "listunspent", 2, "addresses" },
82 { "listunspent", 3, "include_unsafe" },
83 { "listunspent", 4, "query_options" },
84 { "getblock", 1, "verbosity" },
85 { "getblock", 1, "verbose" },
86 { "getblockheader", 1, "verbose" },
87 { "getchaintxstats", 0, "nblocks" },
88 { "gettransaction", 1, "include_watchonly" },
89 { "getrawtransaction", 1, "verbose" },
90 { "createrawtransaction", 0, "inputs" },
91 { "createrawtransaction", 1, "outputs" },
92 { "createrawtransaction", 2, "locktime" },
93 { "createrawtransaction", 3, "replaceable" },
94 { "decoderawtransaction", 1, "iswitness" },
95 { "signrawtransaction", 1, "prevtxs" },
96 { "signrawtransaction", 2, "privkeys" },
97 { "sendrawtransaction", 1, "allowhighfees" },
98 { "combinerawtransaction", 0, "txs" },
99 { "fundrawtransaction", 1, "options" },
100 { "fundrawtransaction", 2, "iswitness" },
101 { "gettxout", 1, "n" },
102 { "gettxout", 2, "include_mempool" },
103 { "gettxoutproof", 0, "txids" },
104 { "lockunspent", 0, "unlock" },
105 { "lockunspent", 1, "transactions" },
106 { "importprivkey", 2, "rescan" },
107 { "importaddress", 2, "rescan" },
108 { "importaddress", 3, "p2sh" },
109 { "importpubkey", 2, "rescan" },
110 { "importmulti", 0, "requests" },
111 { "importmulti", 1, "options" },
112 { "verifychain", 0, "checklevel" },
113 { "verifychain", 1, "nblocks" },
114 { "pruneblockchain", 0, "height" },
115 { "keypoolrefill", 0, "newsize" },
116 { "getrawmempool", 0, "verbose" },
117 { "estimatefee", 0, "nblocks" },
118 { "estimatesmartfee", 0, "conf_target" },
119 { "estimaterawfee", 0, "conf_target" },
120 { "estimaterawfee", 1, "threshold" },
121 { "prioritisetransaction", 1, "dummy" },
122 { "prioritisetransaction", 2, "fee_delta" },
123 { "setban", 2, "bantime" },
124 { "setban", 3, "absolute" },
125 { "setnetworkactive", 0, "state" },
126 { "getmempoolancestors", 1, "verbose" },
127 { "getmempooldescendants", 1, "verbose" },
128 { "bumpfee", 1, "options" },
129 { "logging", 0, "include" },
130 { "logging", 1, "exclude" },
131 { "disconnectnode", 1, "nodeid" },
132 { "addwitnessaddress", 1, "p2sh" },
133 // Echo with conversion (For testing only)
134 { "echojson", 0, "arg0" },
135 { "echojson", 1, "arg1" },
136 { "echojson", 2, "arg2" },
137 { "echojson", 3, "arg3" },
138 { "echojson", 4, "arg4" },
139 { "echojson", 5, "arg5" },
140 { "echojson", 6, "arg6" },
141 { "echojson", 7, "arg7" },
142 { "echojson", 8, "arg8" },
143 { "echojson", 9, "arg9" },
144 { "rescanblockchain", 0, "start_height"},
145 { "rescanblockchain", 1, "stop_height"},
148 class CRPCConvertTable
150 private:
151 std::set<std::pair<std::string, int>> members;
152 std::set<std::pair<std::string, std::string>> membersByName;
154 public:
155 CRPCConvertTable();
157 bool convert(const std::string& method, int idx) {
158 return (members.count(std::make_pair(method, idx)) > 0);
160 bool convert(const std::string& method, const std::string& name) {
161 return (membersByName.count(std::make_pair(method, name)) > 0);
165 CRPCConvertTable::CRPCConvertTable()
167 const unsigned int n_elem =
168 (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
170 for (unsigned int i = 0; i < n_elem; i++) {
171 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
172 vRPCConvertParams[i].paramIdx));
173 membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
174 vRPCConvertParams[i].paramName));
178 static CRPCConvertTable rpcCvtTable;
180 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
181 * as well as objects and arrays.
183 UniValue ParseNonRFCJSONValue(const std::string& strVal)
185 UniValue jVal;
186 if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
187 !jVal.isArray() || jVal.size()!=1)
188 throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
189 return jVal[0];
192 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
194 UniValue params(UniValue::VARR);
196 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
197 const std::string& strVal = strParams[idx];
199 if (!rpcCvtTable.convert(strMethod, idx)) {
200 // insert string value directly
201 params.push_back(strVal);
202 } else {
203 // parse string as JSON, insert bool/number/object/etc. value
204 params.push_back(ParseNonRFCJSONValue(strVal));
208 return params;
211 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
213 UniValue params(UniValue::VOBJ);
215 for (const std::string &s: strParams) {
216 size_t pos = s.find("=");
217 if (pos == std::string::npos) {
218 throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
221 std::string name = s.substr(0, pos);
222 std::string value = s.substr(pos+1);
224 if (!rpcCvtTable.convert(strMethod, name)) {
225 // insert string value directly
226 params.pushKV(name, value);
227 } else {
228 // parse string as JSON, insert bool/number/object/etc. value
229 params.pushKV(name, ParseNonRFCJSONValue(value));
233 return params;