Bitcoin Core  0.20.99
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <util/system.h>
8 
9 #include <set>
10 #include <stdint.h>
11 
13 {
14 public:
15  std::string methodName;
16  int paramIdx;
17  std::string paramName;
18 };
19 
20 // clang-format off
28 {
29  { "setmocktime", 0, "timestamp" },
30  { "mockscheduler", 0, "delta_time" },
31  { "utxoupdatepsbt", 1, "descriptors" },
32  { "generatetoaddress", 0, "nblocks" },
33  { "generatetoaddress", 2, "maxtries" },
34  { "generatetodescriptor", 0, "num_blocks" },
35  { "generatetodescriptor", 2, "maxtries" },
36  { "generateblock", 1, "transactions" },
37  { "getnetworkhashps", 0, "nblocks" },
38  { "getnetworkhashps", 1, "height" },
39  { "sendtoaddress", 1, "amount" },
40  { "sendtoaddress", 4, "subtractfeefromamount" },
41  { "sendtoaddress", 5 , "replaceable" },
42  { "sendtoaddress", 6 , "conf_target" },
43  { "sendtoaddress", 8, "avoid_reuse" },
44  { "settxfee", 0, "amount" },
45  { "sethdseed", 0, "newkeypool" },
46  { "getreceivedbyaddress", 1, "minconf" },
47  { "getreceivedbylabel", 1, "minconf" },
48  { "listreceivedbyaddress", 0, "minconf" },
49  { "listreceivedbyaddress", 1, "include_empty" },
50  { "listreceivedbyaddress", 2, "include_watchonly" },
51  { "listreceivedbylabel", 0, "minconf" },
52  { "listreceivedbylabel", 1, "include_empty" },
53  { "listreceivedbylabel", 2, "include_watchonly" },
54  { "getbalance", 1, "minconf" },
55  { "getbalance", 2, "include_watchonly" },
56  { "getbalance", 3, "avoid_reuse" },
57  { "getblockhash", 0, "height" },
58  { "waitforblockheight", 0, "height" },
59  { "waitforblockheight", 1, "timeout" },
60  { "waitforblock", 1, "timeout" },
61  { "waitfornewblock", 0, "timeout" },
62  { "listtransactions", 1, "count" },
63  { "listtransactions", 2, "skip" },
64  { "listtransactions", 3, "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  { "deriveaddresses", 1, "range" },
76  { "scantxoutset", 1, "scanobjects" },
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  { "gettransaction", 2, "verbose" },
92  { "getrawtransaction", 1, "verbose" },
93  { "createrawtransaction", 0, "inputs" },
94  { "createrawtransaction", 1, "outputs" },
95  { "createrawtransaction", 2, "locktime" },
96  { "createrawtransaction", 3, "replaceable" },
97  { "decoderawtransaction", 1, "iswitness" },
98  { "signrawtransactionwithkey", 1, "privkeys" },
99  { "signrawtransactionwithkey", 2, "prevtxs" },
100  { "signrawtransactionwithwallet", 1, "prevtxs" },
101  { "sendrawtransaction", 1, "maxfeerate" },
102  { "testmempoolaccept", 0, "rawtxs" },
103  { "testmempoolaccept", 1, "maxfeerate" },
104  { "combinerawtransaction", 0, "txs" },
105  { "fundrawtransaction", 1, "options" },
106  { "fundrawtransaction", 2, "iswitness" },
107  { "walletcreatefundedpsbt", 0, "inputs" },
108  { "walletcreatefundedpsbt", 1, "outputs" },
109  { "walletcreatefundedpsbt", 2, "locktime" },
110  { "walletcreatefundedpsbt", 3, "options" },
111  { "walletcreatefundedpsbt", 4, "bip32derivs" },
112  { "walletprocesspsbt", 1, "sign" },
113  { "walletprocesspsbt", 3, "bip32derivs" },
114  { "createpsbt", 0, "inputs" },
115  { "createpsbt", 1, "outputs" },
116  { "createpsbt", 2, "locktime" },
117  { "createpsbt", 3, "replaceable" },
118  { "combinepsbt", 0, "txs"},
119  { "joinpsbts", 0, "txs"},
120  { "finalizepsbt", 1, "extract"},
121  { "converttopsbt", 1, "permitsigdata"},
122  { "converttopsbt", 2, "iswitness"},
123  { "gettxout", 1, "n" },
124  { "gettxout", 2, "include_mempool" },
125  { "gettxoutproof", 0, "txids" },
126  { "lockunspent", 0, "unlock" },
127  { "lockunspent", 1, "transactions" },
128  { "importprivkey", 2, "rescan" },
129  { "importaddress", 2, "rescan" },
130  { "importaddress", 3, "p2sh" },
131  { "importpubkey", 2, "rescan" },
132  { "importmulti", 0, "requests" },
133  { "importmulti", 1, "options" },
134  { "importdescriptors", 0, "requests" },
135  { "verifychain", 0, "checklevel" },
136  { "verifychain", 1, "nblocks" },
137  { "getblockstats", 0, "hash_or_height" },
138  { "getblockstats", 1, "stats" },
139  { "pruneblockchain", 0, "height" },
140  { "keypoolrefill", 0, "newsize" },
141  { "getrawmempool", 0, "verbose" },
142  { "estimatesmartfee", 0, "conf_target" },
143  { "estimaterawfee", 0, "conf_target" },
144  { "estimaterawfee", 1, "threshold" },
145  { "prioritisetransaction", 1, "dummy" },
146  { "prioritisetransaction", 2, "fee_delta" },
147  { "setban", 2, "bantime" },
148  { "setban", 3, "absolute" },
149  { "setnetworkactive", 0, "state" },
150  { "setwalletflag", 1, "value" },
151  { "getmempoolancestors", 1, "verbose" },
152  { "getmempooldescendants", 1, "verbose" },
153  { "bumpfee", 1, "options" },
154  { "logging", 0, "include" },
155  { "logging", 1, "exclude" },
156  { "disconnectnode", 1, "nodeid" },
157  { "upgradewallet", 0, "version" },
158  // Echo with conversion (For testing only)
159  { "echojson", 0, "arg0" },
160  { "echojson", 1, "arg1" },
161  { "echojson", 2, "arg2" },
162  { "echojson", 3, "arg3" },
163  { "echojson", 4, "arg4" },
164  { "echojson", 5, "arg5" },
165  { "echojson", 6, "arg6" },
166  { "echojson", 7, "arg7" },
167  { "echojson", 8, "arg8" },
168  { "echojson", 9, "arg9" },
169  { "rescanblockchain", 0, "start_height"},
170  { "rescanblockchain", 1, "stop_height"},
171  { "createwallet", 1, "disable_private_keys"},
172  { "createwallet", 2, "blank"},
173  { "createwallet", 4, "avoid_reuse"},
174  { "createwallet", 5, "descriptors"},
175  { "getnodeaddresses", 0, "count"},
176  { "stop", 0, "wait" },
177 };
178 // clang-format on
179 
181 {
182 private:
183  std::set<std::pair<std::string, int>> members;
184  std::set<std::pair<std::string, std::string>> membersByName;
185 
186 public:
188 
189  bool convert(const std::string& method, int idx) {
190  return (members.count(std::make_pair(method, idx)) > 0);
191  }
192  bool convert(const std::string& method, const std::string& name) {
193  return (membersByName.count(std::make_pair(method, name)) > 0);
194  }
195 };
196 
198 {
199  const unsigned int n_elem =
200  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
201 
202  for (unsigned int i = 0; i < n_elem; i++) {
203  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
204  vRPCConvertParams[i].paramIdx));
205  membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
206  vRPCConvertParams[i].paramName));
207  }
208 }
209 
211 
215 UniValue ParseNonRFCJSONValue(const std::string& strVal)
216 {
217  UniValue jVal;
218  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
219  !jVal.isArray() || jVal.size()!=1)
220  throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
221  return jVal[0];
222 }
223 
224 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
225 {
226  UniValue params(UniValue::VARR);
227 
228  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
229  const std::string& strVal = strParams[idx];
230 
231  if (!rpcCvtTable.convert(strMethod, idx)) {
232  // insert string value directly
233  params.push_back(strVal);
234  } else {
235  // parse string as JSON, insert bool/number/object/etc. value
236  params.push_back(ParseNonRFCJSONValue(strVal));
237  }
238  }
239 
240  return params;
241 }
242 
243 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
244 {
245  UniValue params(UniValue::VOBJ);
246 
247  for (const std::string &s: strParams) {
248  size_t pos = s.find('=');
249  if (pos == std::string::npos) {
250  throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
251  }
252 
253  std::string name = s.substr(0, pos);
254  std::string value = s.substr(pos+1);
255 
256  if (!rpcCvtTable.convert(strMethod, name)) {
257  // insert string value directly
258  params.pushKV(name, value);
259  } else {
260  // parse string as JSON, insert bool/number/object/etc. value
261  params.pushKV(name, ParseNonRFCJSONValue(value));
262  }
263  }
264 
265  return params;
266 }
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:210
bool read(const char *raw, size_t len)
std::set< std::pair< std::string, int > > members
Definition: client.cpp:183
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:224
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
UniValue ParseNonRFCJSONValue(const std::string &strVal)
Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) as well as obje...
Definition: client.cpp:215
const char * name
Definition: rest.cpp:41
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:192
std::string paramName
parameter name
Definition: client.cpp:17
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool convert(const std::string &method, int idx)
Definition: client.cpp:189
static const CRPCConvertParam vRPCConvertParams[]
Specify a (method, idx, name) here if the argument is a non-string RPC argument and needs to be conve...
Definition: client.cpp:27
std::string methodName
method whose params want conversion
Definition: client.cpp:15
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:243
size_t size() const
Definition: univalue.h:68
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:184
bool isArray() const
Definition: univalue.h:83
int paramIdx
0-based idx of param to convert
Definition: client.cpp:16