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"
13 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
18 class CRPCConvertParam
21 std::string methodName
; //!< method whose params want conversion
22 int paramIdx
; //!< 0-based idx of param to convert
23 std::string paramName
; //!< parameter name
27 * Specify a (method, idx, name) here if the argument is a non-string RPC
28 * argument and needs to be converted from JSON.
30 * @note Parameter indexes start from 0.
32 static const CRPCConvertParam vRPCConvertParams
[] =
34 { "setmocktime", 0, "timestamp" },
35 { "generate", 0, "nblocks" },
36 { "generate", 1, "maxtries" },
37 { "generatetoaddress", 0, "nblocks" },
38 { "generatetoaddress", 2, "maxtries" },
39 { "getnetworkhashps", 0, "nblocks" },
40 { "getnetworkhashps", 1, "height" },
41 { "sendtoaddress", 1, "amount" },
42 { "sendtoaddress", 4, "subtractfeefromamount" },
43 { "settxfee", 0, "amount" },
44 { "getreceivedbyaddress", 1, "minconf" },
45 { "getreceivedbyaccount", 1, "minconf" },
46 { "listreceivedbyaddress", 0, "minconf" },
47 { "listreceivedbyaddress", 1, "include_empty" },
48 { "listreceivedbyaddress", 2, "include_watchonly" },
49 { "listreceivedbyaccount", 0, "minconf" },
50 { "listreceivedbyaccount", 1, "include_empty" },
51 { "listreceivedbyaccount", 2, "include_watchonly" },
52 { "getbalance", 1, "minconf" },
53 { "getbalance", 2, "include_watchonly" },
54 { "getblockhash", 0, "height" },
55 { "waitforblockheight", 0, "height" },
56 { "waitforblockheight", 1, "timeout" },
57 { "waitforblock", 1, "timeout" },
58 { "waitfornewblock", 0, "timeout" },
59 { "move", 2, "amount" },
60 { "move", 3, "minconf" },
61 { "sendfrom", 2, "amount" },
62 { "sendfrom", 3, "minconf" },
63 { "listtransactions", 1, "count" },
64 { "listtransactions", 2, "skip" },
65 { "listtransactions", 3, "include_watchonly" },
66 { "listaccounts", 0, "minconf" },
67 { "listaccounts", 1, "include_watchonly" },
68 { "walletpassphrase", 1, "timeout" },
69 { "getblocktemplate", 0, "template_request" },
70 { "listsinceblock", 1, "target_confirmations" },
71 { "listsinceblock", 2, "include_watchonly" },
72 { "sendmany", 1, "amounts" },
73 { "sendmany", 2, "minconf" },
74 { "sendmany", 4, "subtractfeefrom" },
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 { "getblock", 1, "verbose" },
83 { "getblockheader", 1, "verbose" },
84 { "gettransaction", 1, "include_watchonly" },
85 { "getrawtransaction", 1, "verbose" },
86 { "createrawtransaction", 0, "transactions" },
87 { "createrawtransaction", 1, "outputs" },
88 { "createrawtransaction", 2, "locktime" },
89 { "signrawtransaction", 1, "prevtxs" },
90 { "signrawtransaction", 2, "privkeys" },
91 { "sendrawtransaction", 1, "allowhighfees" },
92 { "fundrawtransaction", 1, "options" },
93 { "gettxout", 1, "n" },
94 { "gettxout", 2, "include_mempool" },
95 { "gettxoutproof", 0, "txids" },
96 { "lockunspent", 0, "unlock" },
97 { "lockunspent", 1, "transactions" },
98 { "importprivkey", 2, "rescan" },
99 { "importaddress", 2, "rescan" },
100 { "importaddress", 3, "p2sh" },
101 { "importpubkey", 2, "rescan" },
102 { "importmulti", 0, "requests" },
103 { "importmulti", 1, "options" },
104 { "verifychain", 0, "checklevel" },
105 { "verifychain", 1, "nblocks" },
106 { "pruneblockchain", 0, "height" },
107 { "keypoolrefill", 0, "newsize" },
108 { "getrawmempool", 0, "verbose" },
109 { "estimatefee", 0, "nblocks" },
110 { "estimatepriority", 0, "nblocks" },
111 { "estimatesmartfee", 0, "nblocks" },
112 { "estimatesmartpriority", 0, "nblocks" },
113 { "prioritisetransaction", 1, "priority_delta" },
114 { "prioritisetransaction", 2, "fee_delta" },
115 { "setban", 2, "bantime" },
116 { "setban", 3, "absolute" },
117 { "setnetworkactive", 0, "state" },
118 { "getmempoolancestors", 1, "verbose" },
119 { "getmempooldescendants", 1, "verbose" },
120 { "bumpfee", 1, "options" },
121 // Echo with conversion (For testing only)
122 { "echojson", 0, "arg0" },
123 { "echojson", 1, "arg1" },
124 { "echojson", 2, "arg2" },
125 { "echojson", 3, "arg3" },
126 { "echojson", 4, "arg4" },
127 { "echojson", 5, "arg5" },
128 { "echojson", 6, "arg6" },
129 { "echojson", 7, "arg7" },
130 { "echojson", 8, "arg8" },
131 { "echojson", 9, "arg9" },
134 class CRPCConvertTable
137 std::set
<std::pair
<std::string
, int>> members
;
138 std::set
<std::pair
<std::string
, std::string
>> membersByName
;
143 bool convert(const std::string
& method
, int idx
) {
144 return (members
.count(std::make_pair(method
, idx
)) > 0);
146 bool convert(const std::string
& method
, const std::string
& name
) {
147 return (membersByName
.count(std::make_pair(method
, name
)) > 0);
151 CRPCConvertTable::CRPCConvertTable()
153 const unsigned int n_elem
=
154 (sizeof(vRPCConvertParams
) / sizeof(vRPCConvertParams
[0]));
156 for (unsigned int i
= 0; i
< n_elem
; i
++) {
157 members
.insert(std::make_pair(vRPCConvertParams
[i
].methodName
,
158 vRPCConvertParams
[i
].paramIdx
));
159 membersByName
.insert(std::make_pair(vRPCConvertParams
[i
].methodName
,
160 vRPCConvertParams
[i
].paramName
));
164 static CRPCConvertTable rpcCvtTable
;
166 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
167 * as well as objects and arrays.
169 UniValue
ParseNonRFCJSONValue(const std::string
& strVal
)
172 if (!jVal
.read(std::string("[")+strVal
+std::string("]")) ||
173 !jVal
.isArray() || jVal
.size()!=1)
174 throw runtime_error(string("Error parsing JSON:")+strVal
);
178 UniValue
RPCConvertValues(const std::string
&strMethod
, const std::vector
<std::string
> &strParams
)
180 UniValue
params(UniValue::VARR
);
182 for (unsigned int idx
= 0; idx
< strParams
.size(); idx
++) {
183 const std::string
& strVal
= strParams
[idx
];
185 if (!rpcCvtTable
.convert(strMethod
, idx
)) {
186 // insert string value directly
187 params
.push_back(strVal
);
189 // parse string as JSON, insert bool/number/object/etc. value
190 params
.push_back(ParseNonRFCJSONValue(strVal
));
197 UniValue
RPCConvertNamedValues(const std::string
&strMethod
, const std::vector
<std::string
> &strParams
)
199 UniValue
params(UniValue::VOBJ
);
201 for (const std::string
&s
: strParams
) {
202 size_t pos
= s
.find("=");
203 if (pos
== std::string::npos
) {
204 throw(std::runtime_error("No '=' in named argument '"+s
+"', this needs to be present for every argument (even if it is empty)"));
207 std::string name
= s
.substr(0, pos
);
208 std::string value
= s
.substr(pos
+1);
210 if (!rpcCvtTable
.convert(strMethod
, name
)) {
211 // insert string value directly
212 params
.pushKV(name
, value
);
214 // parse string as JSON, insert bool/number/object/etc. value
215 params
.pushKV(name
, ParseNonRFCJSONValue(value
));