Bitcoin Core  24.1.0
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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  { "sendtoaddress", 9, "fee_rate"},
45  { "sendtoaddress", 10, "verbose"},
46  { "settxfee", 0, "amount" },
47  { "sethdseed", 0, "newkeypool" },
48  { "getreceivedbyaddress", 1, "minconf" },
49  { "getreceivedbyaddress", 2, "include_immature_coinbase" },
50  { "getreceivedbylabel", 1, "minconf" },
51  { "getreceivedbylabel", 2, "include_immature_coinbase" },
52  { "listreceivedbyaddress", 0, "minconf" },
53  { "listreceivedbyaddress", 1, "include_empty" },
54  { "listreceivedbyaddress", 2, "include_watchonly" },
55  { "listreceivedbyaddress", 4, "include_immature_coinbase" },
56  { "listreceivedbylabel", 0, "minconf" },
57  { "listreceivedbylabel", 1, "include_empty" },
58  { "listreceivedbylabel", 2, "include_watchonly" },
59  { "listreceivedbylabel", 3, "include_immature_coinbase" },
60  { "getbalance", 1, "minconf" },
61  { "getbalance", 2, "include_watchonly" },
62  { "getbalance", 3, "avoid_reuse" },
63  { "getblockfrompeer", 1, "peer_id" },
64  { "getblockhash", 0, "height" },
65  { "waitforblockheight", 0, "height" },
66  { "waitforblockheight", 1, "timeout" },
67  { "waitforblock", 1, "timeout" },
68  { "waitfornewblock", 0, "timeout" },
69  { "listtransactions", 1, "count" },
70  { "listtransactions", 2, "skip" },
71  { "listtransactions", 3, "include_watchonly" },
72  { "walletpassphrase", 1, "timeout" },
73  { "getblocktemplate", 0, "template_request" },
74  { "listsinceblock", 1, "target_confirmations" },
75  { "listsinceblock", 2, "include_watchonly" },
76  { "listsinceblock", 3, "include_removed" },
77  { "listsinceblock", 4, "include_change" },
78  { "sendmany", 1, "amounts" },
79  { "sendmany", 2, "minconf" },
80  { "sendmany", 4, "subtractfeefrom" },
81  { "sendmany", 5 , "replaceable" },
82  { "sendmany", 6 , "conf_target" },
83  { "sendmany", 8, "fee_rate"},
84  { "sendmany", 9, "verbose" },
85  { "deriveaddresses", 1, "range" },
86  { "scantxoutset", 1, "scanobjects" },
87  { "addmultisigaddress", 0, "nrequired" },
88  { "addmultisigaddress", 1, "keys" },
89  { "createmultisig", 0, "nrequired" },
90  { "createmultisig", 1, "keys" },
91  { "listunspent", 0, "minconf" },
92  { "listunspent", 1, "maxconf" },
93  { "listunspent", 2, "addresses" },
94  { "listunspent", 3, "include_unsafe" },
95  { "listunspent", 4, "query_options" },
96  { "getblock", 1, "verbosity" },
97  { "getblock", 1, "verbose" },
98  { "getblockheader", 1, "verbose" },
99  { "getchaintxstats", 0, "nblocks" },
100  { "gettransaction", 1, "include_watchonly" },
101  { "gettransaction", 2, "verbose" },
102  { "getrawtransaction", 1, "verbose" },
103  { "createrawtransaction", 0, "inputs" },
104  { "createrawtransaction", 1, "outputs" },
105  { "createrawtransaction", 2, "locktime" },
106  { "createrawtransaction", 3, "replaceable" },
107  { "decoderawtransaction", 1, "iswitness" },
108  { "signrawtransactionwithkey", 1, "privkeys" },
109  { "signrawtransactionwithkey", 2, "prevtxs" },
110  { "signrawtransactionwithwallet", 1, "prevtxs" },
111  { "sendrawtransaction", 1, "maxfeerate" },
112  { "testmempoolaccept", 0, "rawtxs" },
113  { "testmempoolaccept", 1, "maxfeerate" },
114  { "submitpackage", 0, "package" },
115  { "combinerawtransaction", 0, "txs" },
116  { "fundrawtransaction", 1, "options" },
117  { "fundrawtransaction", 2, "iswitness" },
118  { "walletcreatefundedpsbt", 0, "inputs" },
119  { "walletcreatefundedpsbt", 1, "outputs" },
120  { "walletcreatefundedpsbt", 2, "locktime" },
121  { "walletcreatefundedpsbt", 3, "options" },
122  { "walletcreatefundedpsbt", 4, "bip32derivs" },
123  { "walletprocesspsbt", 1, "sign" },
124  { "walletprocesspsbt", 3, "bip32derivs" },
125  { "walletprocesspsbt", 4, "finalize" },
126  { "createpsbt", 0, "inputs" },
127  { "createpsbt", 1, "outputs" },
128  { "createpsbt", 2, "locktime" },
129  { "createpsbt", 3, "replaceable" },
130  { "combinepsbt", 0, "txs"},
131  { "joinpsbts", 0, "txs"},
132  { "finalizepsbt", 1, "extract"},
133  { "converttopsbt", 1, "permitsigdata"},
134  { "converttopsbt", 2, "iswitness"},
135  { "gettxout", 1, "n" },
136  { "gettxout", 2, "include_mempool" },
137  { "gettxoutproof", 0, "txids" },
138  { "gettxoutsetinfo", 1, "hash_or_height" },
139  { "gettxoutsetinfo", 2, "use_index"},
140  { "lockunspent", 0, "unlock" },
141  { "lockunspent", 1, "transactions" },
142  { "lockunspent", 2, "persistent" },
143  { "send", 0, "outputs" },
144  { "send", 1, "conf_target" },
145  { "send", 3, "fee_rate"},
146  { "send", 4, "options" },
147  { "sendall", 0, "recipients" },
148  { "sendall", 1, "conf_target" },
149  { "sendall", 3, "fee_rate"},
150  { "sendall", 4, "options" },
151  { "simulaterawtransaction", 0, "rawtxs" },
152  { "simulaterawtransaction", 1, "options" },
153  { "importprivkey", 2, "rescan" },
154  { "importaddress", 2, "rescan" },
155  { "importaddress", 3, "p2sh" },
156  { "importpubkey", 2, "rescan" },
157  { "importmulti", 0, "requests" },
158  { "importmulti", 1, "options" },
159  { "importdescriptors", 0, "requests" },
160  { "listdescriptors", 0, "private" },
161  { "verifychain", 0, "checklevel" },
162  { "verifychain", 1, "nblocks" },
163  { "getblockstats", 0, "hash_or_height" },
164  { "getblockstats", 1, "stats" },
165  { "pruneblockchain", 0, "height" },
166  { "keypoolrefill", 0, "newsize" },
167  { "getrawmempool", 0, "verbose" },
168  { "getrawmempool", 1, "mempool_sequence" },
169  { "estimatesmartfee", 0, "conf_target" },
170  { "estimaterawfee", 0, "conf_target" },
171  { "estimaterawfee", 1, "threshold" },
172  { "prioritisetransaction", 1, "dummy" },
173  { "prioritisetransaction", 2, "fee_delta" },
174  { "setban", 2, "bantime" },
175  { "setban", 3, "absolute" },
176  { "setnetworkactive", 0, "state" },
177  { "setwalletflag", 1, "value" },
178  { "getmempoolancestors", 1, "verbose" },
179  { "getmempooldescendants", 1, "verbose" },
180  { "gettxspendingprevout", 0, "outputs" },
181  { "bumpfee", 1, "options" },
182  { "psbtbumpfee", 1, "options" },
183  { "logging", 0, "include" },
184  { "logging", 1, "exclude" },
185  { "disconnectnode", 1, "nodeid" },
186  { "upgradewallet", 0, "version" },
187  // Echo with conversion (For testing only)
188  { "echojson", 0, "arg0" },
189  { "echojson", 1, "arg1" },
190  { "echojson", 2, "arg2" },
191  { "echojson", 3, "arg3" },
192  { "echojson", 4, "arg4" },
193  { "echojson", 5, "arg5" },
194  { "echojson", 6, "arg6" },
195  { "echojson", 7, "arg7" },
196  { "echojson", 8, "arg8" },
197  { "echojson", 9, "arg9" },
198  { "rescanblockchain", 0, "start_height"},
199  { "rescanblockchain", 1, "stop_height"},
200  { "createwallet", 1, "disable_private_keys"},
201  { "createwallet", 2, "blank"},
202  { "createwallet", 4, "avoid_reuse"},
203  { "createwallet", 5, "descriptors"},
204  { "createwallet", 6, "load_on_startup"},
205  { "createwallet", 7, "external_signer"},
206  { "restorewallet", 2, "load_on_startup"},
207  { "loadwallet", 1, "load_on_startup"},
208  { "unloadwallet", 1, "load_on_startup"},
209  { "getnodeaddresses", 0, "count"},
210  { "addpeeraddress", 1, "port"},
211  { "addpeeraddress", 2, "tried"},
212  { "stop", 0, "wait" },
213 };
214 // clang-format on
215 
217 {
218 private:
219  std::set<std::pair<std::string, int>> members;
220  std::set<std::pair<std::string, std::string>> membersByName;
221 
222 public:
224 
225  bool convert(const std::string& method, int idx) {
226  return (members.count(std::make_pair(method, idx)) > 0);
227  }
228  bool convert(const std::string& method, const std::string& name) {
229  return (membersByName.count(std::make_pair(method, name)) > 0);
230  }
231 };
232 
234 {
235  for (const auto& cp : vRPCConvertParams) {
236  members.emplace(cp.methodName, cp.paramIdx);
237  membersByName.emplace(cp.methodName, cp.paramName);
238  }
239 }
240 
242 
246 UniValue ParseNonRFCJSONValue(const std::string& strVal)
247 {
248  UniValue jVal;
249  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
250  !jVal.isArray() || jVal.size()!=1)
251  throw std::runtime_error(std::string("Error parsing JSON: ") + strVal);
252  return jVal[0];
253 }
254 
255 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
256 {
257  UniValue params(UniValue::VARR);
258 
259  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
260  const std::string& strVal = strParams[idx];
261 
262  if (!rpcCvtTable.convert(strMethod, idx)) {
263  // insert string value directly
264  params.push_back(strVal);
265  } else {
266  // parse string as JSON, insert bool/number/object/etc. value
267  params.push_back(ParseNonRFCJSONValue(strVal));
268  }
269  }
270 
271  return params;
272 }
273 
274 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
275 {
276  UniValue params(UniValue::VOBJ);
277 
278  for (const std::string &s: strParams) {
279  size_t pos = s.find('=');
280  if (pos == std::string::npos) {
281  throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
282  }
283 
284  std::string name = s.substr(0, pos);
285  std::string value = s.substr(pos+1);
286 
287  if (!rpcCvtTable.convert(strMethod, name)) {
288  // insert string value directly
289  params.pushKV(name, value);
290  } else {
291  // parse string as JSON, insert bool/number/object/etc. value
292  params.pushKV(name, ParseNonRFCJSONValue(value));
293  }
294  }
295 
296  return params;
297 }
void push_back(UniValue val)
Definition: univalue.cpp:104
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:241
bool read(const char *raw, size_t len)
std::set< std::pair< std::string, int > > members
Definition: client.cpp:219
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:255
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:246
const char * name
Definition: rest.cpp:46
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:228
std::string paramName
parameter name
Definition: client.cpp:17
bool convert(const std::string &method, int idx)
Definition: client.cpp:225
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
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:274
size_t size() const
Definition: univalue.h:65
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:220
bool isArray() const
Definition: univalue.h:80
int paramIdx
0-based idx of param to convert
Definition: client.cpp:16