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"
15 class CRPCConvertParam
18 std::string methodName
; //!< method whose params want conversion
19 int paramIdx
; //!< 0-based idx of param to convert
20 std::string paramName
; //!< parameter name
24 * Specify a (method, idx, name) here if the argument is a non-string RPC
25 * argument and needs to be converted from JSON.
27 * @note Parameter indexes start from 0.
29 static const CRPCConvertParam vRPCConvertParams
[] =
31 { "setmocktime", 0, "timestamp" },
32 { "generate", 0, "nblocks" },
33 { "generate", 1, "maxtries" },
34 { "generatetoaddress", 0, "nblocks" },
35 { "generatetoaddress", 2, "maxtries" },
36 { "getnetworkhashps", 0, "nblocks" },
37 { "getnetworkhashps", 1, "height" },
38 { "sendtoaddress", 1, "amount" },
39 { "sendtoaddress", 4, "subtractfeefromamount" },
40 { "sendtoaddress", 5 , "replaceable" },
41 { "sendtoaddress", 6 , "conf_target" },
42 { "settxfee", 0, "amount" },
43 { "getreceivedbyaddress", 1, "minconf" },
44 { "getreceivedbyaccount", 1, "minconf" },
45 { "listreceivedbyaddress", 0, "minconf" },
46 { "listreceivedbyaddress", 1, "include_empty" },
47 { "listreceivedbyaddress", 2, "include_watchonly" },
48 { "listreceivedbyaccount", 0, "minconf" },
49 { "listreceivedbyaccount", 1, "include_empty" },
50 { "listreceivedbyaccount", 2, "include_watchonly" },
51 { "getbalance", 1, "minconf" },
52 { "getbalance", 2, "include_watchonly" },
53 { "getblockhash", 0, "height" },
54 { "waitforblockheight", 0, "height" },
55 { "waitforblockheight", 1, "timeout" },
56 { "waitforblock", 1, "timeout" },
57 { "waitfornewblock", 0, "timeout" },
58 { "move", 2, "amount" },
59 { "move", 3, "minconf" },
60 { "sendfrom", 2, "amount" },
61 { "sendfrom", 3, "minconf" },
62 { "listtransactions", 1, "count" },
63 { "listtransactions", 2, "skip" },
64 { "listtransactions", 3, "include_watchonly" },
65 { "listaccounts", 0, "minconf" },
66 { "listaccounts", 1, "include_watchonly" },
67 { "walletpassphrase", 1, "timeout" },
68 { "getblocktemplate", 0, "template_request" },
69 { "listsinceblock", 1, "target_confirmations" },
70 { "listsinceblock", 2, "include_watchonly" },
71 { "listsinceblock", 3, "include_removed" },
72 { "sendmany", 1, "amounts" },
73 { "sendmany", 2, "minconf" },
74 { "sendmany", 4, "subtractfeefrom" },
75 { "sendmany", 5 , "replaceable" },
76 { "sendmany", 6 , "conf_target" },
77 { "addmultisigaddress", 0, "nrequired" },
78 { "addmultisigaddress", 1, "keys" },
79 { "createmultisig", 0, "nrequired" },
80 { "createmultisig", 1, "keys" },
81 { "listunspent", 0, "minconf" },
82 { "listunspent", 1, "maxconf" },
83 { "listunspent", 2, "addresses" },
84 { "listunspent", 3, "include_unsafe" },
85 { "listunspent", 4, "query_options" },
86 { "getblock", 1, "verbosity" },
87 { "getblock", 1, "verbose" },
88 { "getblockheader", 1, "verbose" },
89 { "getchaintxstats", 0, "nblocks" },
90 { "gettransaction", 1, "include_watchonly" },
91 { "getrawtransaction", 1, "verbose" },
92 { "createrawtransaction", 0, "inputs" },
93 { "createrawtransaction", 1, "outputs" },
94 { "createrawtransaction", 2, "locktime" },
95 { "createrawtransaction", 3, "replaceable" },
96 { "signrawtransaction", 1, "prevtxs" },
97 { "signrawtransaction", 2, "privkeys" },
98 { "sendrawtransaction", 1, "allowhighfees" },
99 { "combinerawtransaction", 0, "txs" },
100 { "fundrawtransaction", 1, "options" },
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, "nblocks" },
119 { "estimaterawfee", 0, "nblocks" },
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 // Echo with conversion (For testing only)
133 { "echojson", 0, "arg0" },
134 { "echojson", 1, "arg1" },
135 { "echojson", 2, "arg2" },
136 { "echojson", 3, "arg3" },
137 { "echojson", 4, "arg4" },
138 { "echojson", 5, "arg5" },
139 { "echojson", 6, "arg6" },
140 { "echojson", 7, "arg7" },
141 { "echojson", 8, "arg8" },
142 { "echojson", 9, "arg9" },
145 class CRPCConvertTable
148 std::set
<std::pair
<std::string
, int>> members
;
149 std::set
<std::pair
<std::string
, std::string
>> membersByName
;
154 bool convert(const std::string
& method
, int idx
) {
155 return (members
.count(std::make_pair(method
, idx
)) > 0);
157 bool convert(const std::string
& method
, const std::string
& name
) {
158 return (membersByName
.count(std::make_pair(method
, name
)) > 0);
162 CRPCConvertTable::CRPCConvertTable()
164 const unsigned int n_elem
=
165 (sizeof(vRPCConvertParams
) / sizeof(vRPCConvertParams
[0]));
167 for (unsigned int i
= 0; i
< n_elem
; i
++) {
168 members
.insert(std::make_pair(vRPCConvertParams
[i
].methodName
,
169 vRPCConvertParams
[i
].paramIdx
));
170 membersByName
.insert(std::make_pair(vRPCConvertParams
[i
].methodName
,
171 vRPCConvertParams
[i
].paramName
));
175 static CRPCConvertTable rpcCvtTable
;
177 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
178 * as well as objects and arrays.
180 UniValue
ParseNonRFCJSONValue(const std::string
& strVal
)
183 if (!jVal
.read(std::string("[")+strVal
+std::string("]")) ||
184 !jVal
.isArray() || jVal
.size()!=1)
185 throw std::runtime_error(std::string("Error parsing JSON:")+strVal
);
189 UniValue
RPCConvertValues(const std::string
&strMethod
, const std::vector
<std::string
> &strParams
)
191 UniValue
params(UniValue::VARR
);
193 for (unsigned int idx
= 0; idx
< strParams
.size(); idx
++) {
194 const std::string
& strVal
= strParams
[idx
];
196 if (!rpcCvtTable
.convert(strMethod
, idx
)) {
197 // insert string value directly
198 params
.push_back(strVal
);
200 // parse string as JSON, insert bool/number/object/etc. value
201 params
.push_back(ParseNonRFCJSONValue(strVal
));
208 UniValue
RPCConvertNamedValues(const std::string
&strMethod
, const std::vector
<std::string
> &strParams
)
210 UniValue
params(UniValue::VOBJ
);
212 for (const std::string
&s
: strParams
) {
213 size_t pos
= s
.find("=");
214 if (pos
== std::string::npos
) {
215 throw(std::runtime_error("No '=' in named argument '"+s
+"', this needs to be present for every argument (even if it is empty)"));
218 std::string name
= s
.substr(0, pos
);
219 std::string value
= s
.substr(pos
+1);
221 if (!rpcCvtTable
.convert(strMethod
, name
)) {
222 // insert string value directly
223 params
.pushKV(name
, value
);
225 // parse string as JSON, insert bool/number/object/etc. value
226 params
.pushKV(name
, ParseNonRFCJSONValue(value
));