1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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.
5 
6 #include <rpc/client.h>
7 #include <rpc/protocol.h>
8 #include <util/system.h>
9 
10 #include <set>
11 #include <stdint.h>
12 
13 class CRPCConvertParam
14 {
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
19 };
20 
21 // clang-format off
22 /**
23  * Specify a (method, idx, name) here if the argument is a non-string RPC
24  * argument and needs to be converted from JSON.
25  *
26  * @note Parameter indexes start from 0.
27  */
28 static const CRPCConvertParam vRPCConvertParams[] =
29 {
30     { "setmocktime", 0, "timestamp" },
31     { "generate", 0, "nblocks" },
32     { "generate", 1, "maxtries" },
33     { "generatetoaddress", 0, "nblocks" },
34     { "generatetoaddress", 2, "maxtries" },
35     { "getnetworkhashps", 0, "nblocks" },
36     { "getnetworkhashps", 1, "height" },
37     { "sendtoaddress", 1, "amount" },
38     { "sendtoaddress", 4, "subtractfeefromamount" },
39     { "sendtoaddress", 5 , "replaceable" },
40     { "sendtoaddress", 6 , "conf_target" },
41     { "settxfee", 0, "amount" },
42     { "sethdseed", 0, "newkeypool" },
43     { "getreceivedbyaddress", 1, "minconf" },
44     { "getreceivedbylabel", 1, "minconf" },
45     { "listreceivedbyaddress", 0, "minconf" },
46     { "listreceivedbyaddress", 1, "include_empty" },
47     { "listreceivedbyaddress", 2, "include_watchonly" },
48     { "listreceivedbylabel", 0, "minconf" },
49     { "listreceivedbylabel", 1, "include_empty" },
50     { "listreceivedbylabel", 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     { "listtransactions", 1, "count" },
59     { "listtransactions", 2, "skip" },
60     { "listtransactions", 3, "include_watchonly" },
61     { "walletpassphrase", 1, "timeout" },
62     { "getblocktemplate", 0, "template_request" },
63     { "listsinceblock", 1, "target_confirmations" },
64     { "listsinceblock", 2, "include_watchonly" },
65     { "listsinceblock", 3, "include_removed" },
66     { "sendmany", 1, "amounts" },
67     { "sendmany", 2, "minconf" },
68     { "sendmany", 4, "subtractfeefrom" },
69     { "sendmany", 5 , "replaceable" },
70     { "sendmany", 6 , "conf_target" },
71     { "deriveaddresses", 1, "range" },
72     { "scantxoutset", 1, "scanobjects" },
73     { "addmultisigaddress", 0, "nrequired" },
74     { "addmultisigaddress", 1, "keys" },
75     { "createmultisig", 0, "nrequired" },
76     { "createmultisig", 1, "keys" },
77     { "listunspent", 0, "minconf" },
78     { "listunspent", 1, "maxconf" },
79     { "listunspent", 2, "addresses" },
80     { "listunspent", 3, "include_unsafe" },
81     { "listunspent", 4, "query_options" },
82     { "getblock", 1, "verbosity" },
83     { "getblock", 1, "verbose" },
84     { "getblockheader", 1, "verbose" },
85     { "getchaintxstats", 0, "nblocks" },
86     { "gettransaction", 1, "include_watchonly" },
87     { "getrawtransaction", 1, "verbose" },
88     { "createrawtransaction", 0, "inputs" },
89     { "createrawtransaction", 1, "outputs" },
90     { "createrawtransaction", 2, "locktime" },
91     { "createrawtransaction", 3, "replaceable" },
92     { "decoderawtransaction", 1, "iswitness" },
93     { "signrawtransactionwithkey", 1, "privkeys" },
94     { "signrawtransactionwithkey", 2, "prevtxs" },
95     { "signrawtransactionwithwallet", 1, "prevtxs" },
96     { "sendrawtransaction", 1, "allowhighfees" },
97     { "testmempoolaccept", 0, "rawtxs" },
98     { "testmempoolaccept", 1, "allowhighfees" },
99     { "combinerawtransaction", 0, "txs" },
100     { "fundrawtransaction", 1, "options" },
101     { "fundrawtransaction", 2, "iswitness" },
102     { "walletcreatefundedpsbt", 0, "inputs" },
103     { "walletcreatefundedpsbt", 1, "outputs" },
104     { "walletcreatefundedpsbt", 2, "locktime" },
105     { "walletcreatefundedpsbt", 3, "options" },
106     { "walletcreatefundedpsbt", 4, "bip32derivs" },
107     { "walletprocesspsbt", 1, "sign" },
108     { "walletprocesspsbt", 3, "bip32derivs" },
109     { "createpsbt", 0, "inputs" },
110     { "createpsbt", 1, "outputs" },
111     { "createpsbt", 2, "locktime" },
112     { "createpsbt", 3, "replaceable" },
113     { "combinepsbt", 0, "txs"},
114     { "joinpsbts", 0, "txs"},
115     { "finalizepsbt", 1, "extract"},
116     { "converttopsbt", 1, "permitsigdata"},
117     { "converttopsbt", 2, "iswitness"},
118     { "gettxout", 1, "n" },
119     { "gettxout", 2, "include_mempool" },
120     { "gettxoutproof", 0, "txids" },
121     { "lockunspent", 0, "unlock" },
122     { "lockunspent", 1, "transactions" },
123     { "importprivkey", 2, "rescan" },
124     { "importaddress", 2, "rescan" },
125     { "importaddress", 3, "p2sh" },
126     { "importpubkey", 2, "rescan" },
127     { "importmulti", 0, "requests" },
128     { "importmulti", 1, "options" },
129     { "verifychain", 0, "checklevel" },
130     { "verifychain", 1, "nblocks" },
131     { "getblockstats", 0, "hash_or_height" },
132     { "getblockstats", 1, "stats" },
133     { "pruneblockchain", 0, "height" },
134     { "keypoolrefill", 0, "newsize" },
135     { "getrawmempool", 0, "verbose" },
136     { "estimatesmartfee", 0, "conf_target" },
137     { "estimaterawfee", 0, "conf_target" },
138     { "estimaterawfee", 1, "threshold" },
139     { "prioritisetransaction", 1, "dummy" },
140     { "prioritisetransaction", 2, "fee_delta" },
141     { "setban", 2, "bantime" },
142     { "setban", 3, "absolute" },
143     { "setnetworkactive", 0, "state" },
144     { "getmempoolancestors", 1, "verbose" },
145     { "getmempooldescendants", 1, "verbose" },
146     { "bumpfee", 1, "options" },
147     { "logging", 0, "include" },
148     { "logging", 1, "exclude" },
149     { "disconnectnode", 1, "nodeid" },
150     // Echo with conversion (For testing only)
151     { "echojson", 0, "arg0" },
152     { "echojson", 1, "arg1" },
153     { "echojson", 2, "arg2" },
154     { "echojson", 3, "arg3" },
155     { "echojson", 4, "arg4" },
156     { "echojson", 5, "arg5" },
157     { "echojson", 6, "arg6" },
158     { "echojson", 7, "arg7" },
159     { "echojson", 8, "arg8" },
160     { "echojson", 9, "arg9" },
161     { "rescanblockchain", 0, "start_height"},
162     { "rescanblockchain", 1, "stop_height"},
163     { "createwallet", 1, "disable_private_keys"},
164     { "createwallet", 2, "blank"},
165     { "getnodeaddresses", 0, "count"},
166     { "stop", 0, "wait" },
167 };
168 // clang-format on
169 
170 class CRPCConvertTable
171 {
172 private:
173     std::set<std::pair<std::string, int>> members;
174     std::set<std::pair<std::string, std::string>> membersByName;
175 
176 public:
177     CRPCConvertTable();
178 
convert(const std::string & method,int idx)179     bool convert(const std::string& method, int idx) {
180         return (members.count(std::make_pair(method, idx)) > 0);
181     }
convert(const std::string & method,const std::string & name)182     bool convert(const std::string& method, const std::string& name) {
183         return (membersByName.count(std::make_pair(method, name)) > 0);
184     }
185 };
186 
CRPCConvertTable()187 CRPCConvertTable::CRPCConvertTable()
188 {
189     const unsigned int n_elem =
190         (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
191 
192     for (unsigned int i = 0; i < n_elem; i++) {
193         members.insert(std::make_pair(vRPCConvertParams[i].methodName,
194                                       vRPCConvertParams[i].paramIdx));
195         membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
196                                             vRPCConvertParams[i].paramName));
197     }
198 }
199 
200 static CRPCConvertTable rpcCvtTable;
201 
202 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
203  * as well as objects and arrays.
204  */
ParseNonRFCJSONValue(const std::string & strVal)205 UniValue ParseNonRFCJSONValue(const std::string& strVal)
206 {
207     UniValue jVal;
208     if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
209         !jVal.isArray() || jVal.size()!=1)
210         throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
211     return jVal[0];
212 }
213 
RPCConvertValues(const std::string & strMethod,const std::vector<std::string> & strParams)214 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
215 {
216     UniValue params(UniValue::VARR);
217 
218     for (unsigned int idx = 0; idx < strParams.size(); idx++) {
219         const std::string& strVal = strParams[idx];
220 
221         if (!rpcCvtTable.convert(strMethod, idx)) {
222             // insert string value directly
223             params.push_back(strVal);
224         } else {
225             // parse string as JSON, insert bool/number/object/etc. value
226             params.push_back(ParseNonRFCJSONValue(strVal));
227         }
228     }
229 
230     return params;
231 }
232 
RPCConvertNamedValues(const std::string & strMethod,const std::vector<std::string> & strParams)233 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
234 {
235     UniValue params(UniValue::VOBJ);
236 
237     for (const std::string &s: strParams) {
238         size_t pos = s.find('=');
239         if (pos == std::string::npos) {
240             throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
241         }
242 
243         std::string name = s.substr(0, pos);
244         std::string value = s.substr(pos+1);
245 
246         if (!rpcCvtTable.convert(strMethod, name)) {
247             // insert string value directly
248             params.pushKV(name, value);
249         } else {
250             // parse string as JSON, insert bool/number/object/etc. value
251             params.pushKV(name, ParseNonRFCJSONValue(value));
252         }
253     }
254 
255     return params;
256 }
257