Bitcoin Core 29.99.0
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1// Copyright (c) 2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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 <common/args.h>
7#include <rpc/client.h>
8#include <tinyformat.h>
9
10#include <cstdint>
11#include <set>
12#include <string>
13#include <string_view>
14
16{
17public:
18 std::string methodName;
20 std::string paramName;
21};
22
23// clang-format off
31{
32 { "setmocktime", 0, "timestamp" },
33 { "mockscheduler", 0, "delta_time" },
34 { "utxoupdatepsbt", 1, "descriptors" },
35 { "generatetoaddress", 0, "nblocks" },
36 { "generatetoaddress", 2, "maxtries" },
37 { "generatetodescriptor", 0, "num_blocks" },
38 { "generatetodescriptor", 2, "maxtries" },
39 { "generateblock", 1, "transactions" },
40 { "generateblock", 2, "submit" },
41 { "getnetworkhashps", 0, "nblocks" },
42 { "getnetworkhashps", 1, "height" },
43 { "sendtoaddress", 1, "amount" },
44 { "sendtoaddress", 4, "subtractfeefromamount" },
45 { "sendtoaddress", 5 , "replaceable" },
46 { "sendtoaddress", 6 , "conf_target" },
47 { "sendtoaddress", 8, "avoid_reuse" },
48 { "sendtoaddress", 9, "fee_rate"},
49 { "sendtoaddress", 10, "verbose"},
50 { "settxfee", 0, "amount" },
51 { "getreceivedbyaddress", 1, "minconf" },
52 { "getreceivedbyaddress", 2, "include_immature_coinbase" },
53 { "getreceivedbylabel", 1, "minconf" },
54 { "getreceivedbylabel", 2, "include_immature_coinbase" },
55 { "listreceivedbyaddress", 0, "minconf" },
56 { "listreceivedbyaddress", 1, "include_empty" },
57 { "listreceivedbyaddress", 2, "include_watchonly" },
58 { "listreceivedbyaddress", 4, "include_immature_coinbase" },
59 { "listreceivedbylabel", 0, "minconf" },
60 { "listreceivedbylabel", 1, "include_empty" },
61 { "listreceivedbylabel", 2, "include_watchonly" },
62 { "listreceivedbylabel", 3, "include_immature_coinbase" },
63 { "getbalance", 1, "minconf" },
64 { "getbalance", 2, "include_watchonly" },
65 { "getbalance", 3, "avoid_reuse" },
66 { "getblockfrompeer", 1, "peer_id" },
67 { "getblockhash", 0, "height" },
68 { "waitforblockheight", 0, "height" },
69 { "waitforblockheight", 1, "timeout" },
70 { "waitforblock", 1, "timeout" },
71 { "waitfornewblock", 0, "timeout" },
72 { "listtransactions", 1, "count" },
73 { "listtransactions", 2, "skip" },
74 { "listtransactions", 3, "include_watchonly" },
75 { "walletpassphrase", 1, "timeout" },
76 { "getblocktemplate", 0, "template_request" },
77 { "listsinceblock", 1, "target_confirmations" },
78 { "listsinceblock", 2, "include_watchonly" },
79 { "listsinceblock", 3, "include_removed" },
80 { "listsinceblock", 4, "include_change" },
81 { "sendmany", 1, "amounts" },
82 { "sendmany", 2, "minconf" },
83 { "sendmany", 4, "subtractfeefrom" },
84 { "sendmany", 5 , "replaceable" },
85 { "sendmany", 6 , "conf_target" },
86 { "sendmany", 8, "fee_rate"},
87 { "sendmany", 9, "verbose" },
88 { "deriveaddresses", 1, "range" },
89 { "scanblocks", 1, "scanobjects" },
90 { "scanblocks", 2, "start_height" },
91 { "scanblocks", 3, "stop_height" },
92 { "scanblocks", 5, "options" },
93 { "scanblocks", 5, "filter_false_positives" },
94 { "getdescriptoractivity", 0, "blockhashes" },
95 { "getdescriptoractivity", 1, "scanobjects" },
96 { "getdescriptoractivity", 2, "include_mempool" },
97 { "scantxoutset", 1, "scanobjects" },
98 { "createmultisig", 0, "nrequired" },
99 { "createmultisig", 1, "keys" },
100 { "listunspent", 0, "minconf" },
101 { "listunspent", 1, "maxconf" },
102 { "listunspent", 2, "addresses" },
103 { "listunspent", 3, "include_unsafe" },
104 { "listunspent", 4, "query_options" },
105 { "listunspent", 4, "minimumAmount" },
106 { "listunspent", 4, "maximumAmount" },
107 { "listunspent", 4, "maximumCount" },
108 { "listunspent", 4, "minimumSumAmount" },
109 { "listunspent", 4, "include_immature_coinbase" },
110 { "getblock", 1, "verbosity" },
111 { "getblock", 1, "verbose" },
112 { "getblockheader", 1, "verbose" },
113 { "getchaintxstats", 0, "nblocks" },
114 { "gettransaction", 1, "include_watchonly" },
115 { "gettransaction", 2, "verbose" },
116 { "getrawtransaction", 1, "verbosity" },
117 { "getrawtransaction", 1, "verbose" },
118 { "createrawtransaction", 0, "inputs" },
119 { "createrawtransaction", 1, "outputs" },
120 { "createrawtransaction", 2, "locktime" },
121 { "createrawtransaction", 3, "replaceable" },
122 { "createrawtransaction", 4, "version" },
123 { "decoderawtransaction", 1, "iswitness" },
124 { "signrawtransactionwithkey", 1, "privkeys" },
125 { "signrawtransactionwithkey", 2, "prevtxs" },
126 { "signrawtransactionwithwallet", 1, "prevtxs" },
127 { "sendrawtransaction", 1, "maxfeerate" },
128 { "sendrawtransaction", 2, "maxburnamount" },
129 { "testmempoolaccept", 0, "rawtxs" },
130 { "testmempoolaccept", 1, "maxfeerate" },
131 { "submitpackage", 0, "package" },
132 { "submitpackage", 1, "maxfeerate" },
133 { "submitpackage", 2, "maxburnamount" },
134 { "combinerawtransaction", 0, "txs" },
135 { "fundrawtransaction", 1, "options" },
136 { "fundrawtransaction", 1, "add_inputs"},
137 { "fundrawtransaction", 1, "include_unsafe"},
138 { "fundrawtransaction", 1, "minconf"},
139 { "fundrawtransaction", 1, "maxconf"},
140 { "fundrawtransaction", 1, "changePosition"},
141 { "fundrawtransaction", 1, "includeWatching"},
142 { "fundrawtransaction", 1, "lockUnspents"},
143 { "fundrawtransaction", 1, "fee_rate"},
144 { "fundrawtransaction", 1, "feeRate"},
145 { "fundrawtransaction", 1, "subtractFeeFromOutputs"},
146 { "fundrawtransaction", 1, "input_weights"},
147 { "fundrawtransaction", 1, "conf_target"},
148 { "fundrawtransaction", 1, "replaceable"},
149 { "fundrawtransaction", 1, "solving_data"},
150 { "fundrawtransaction", 1, "max_tx_weight"},
151 { "fundrawtransaction", 2, "iswitness" },
152 { "walletcreatefundedpsbt", 0, "inputs" },
153 { "walletcreatefundedpsbt", 1, "outputs" },
154 { "walletcreatefundedpsbt", 2, "locktime" },
155 { "walletcreatefundedpsbt", 3, "options" },
156 { "walletcreatefundedpsbt", 3, "add_inputs"},
157 { "walletcreatefundedpsbt", 3, "include_unsafe"},
158 { "walletcreatefundedpsbt", 3, "minconf"},
159 { "walletcreatefundedpsbt", 3, "maxconf"},
160 { "walletcreatefundedpsbt", 3, "changePosition"},
161 { "walletcreatefundedpsbt", 3, "includeWatching"},
162 { "walletcreatefundedpsbt", 3, "lockUnspents"},
163 { "walletcreatefundedpsbt", 3, "fee_rate"},
164 { "walletcreatefundedpsbt", 3, "feeRate"},
165 { "walletcreatefundedpsbt", 3, "subtractFeeFromOutputs"},
166 { "walletcreatefundedpsbt", 3, "conf_target"},
167 { "walletcreatefundedpsbt", 3, "replaceable"},
168 { "walletcreatefundedpsbt", 3, "solving_data"},
169 { "walletcreatefundedpsbt", 3, "max_tx_weight"},
170 { "walletcreatefundedpsbt", 4, "bip32derivs" },
171 { "walletcreatefundedpsbt", 5, "version" },
172 { "walletprocesspsbt", 1, "sign" },
173 { "walletprocesspsbt", 3, "bip32derivs" },
174 { "walletprocesspsbt", 4, "finalize" },
175 { "descriptorprocesspsbt", 1, "descriptors"},
176 { "descriptorprocesspsbt", 3, "bip32derivs" },
177 { "descriptorprocesspsbt", 4, "finalize" },
178 { "createpsbt", 0, "inputs" },
179 { "createpsbt", 1, "outputs" },
180 { "createpsbt", 2, "locktime" },
181 { "createpsbt", 3, "replaceable" },
182 { "createpsbt", 4, "version" },
183 { "combinepsbt", 0, "txs"},
184 { "joinpsbts", 0, "txs"},
185 { "finalizepsbt", 1, "extract"},
186 { "converttopsbt", 1, "permitsigdata"},
187 { "converttopsbt", 2, "iswitness"},
188 { "gettxout", 1, "n" },
189 { "gettxout", 2, "include_mempool" },
190 { "gettxoutproof", 0, "txids" },
191 { "gettxoutsetinfo", 1, "hash_or_height" },
192 { "gettxoutsetinfo", 2, "use_index"},
193 { "dumptxoutset", 2, "options" },
194 { "dumptxoutset", 2, "rollback" },
195 { "lockunspent", 0, "unlock" },
196 { "lockunspent", 1, "transactions" },
197 { "lockunspent", 2, "persistent" },
198 { "send", 0, "outputs" },
199 { "send", 1, "conf_target" },
200 { "send", 3, "fee_rate"},
201 { "send", 4, "options" },
202 { "send", 4, "add_inputs"},
203 { "send", 4, "include_unsafe"},
204 { "send", 4, "minconf"},
205 { "send", 4, "maxconf"},
206 { "send", 4, "add_to_wallet"},
207 { "send", 4, "change_position"},
208 { "send", 4, "fee_rate"},
209 { "send", 4, "include_watching"},
210 { "send", 4, "inputs"},
211 { "send", 4, "locktime"},
212 { "send", 4, "lock_unspents"},
213 { "send", 4, "psbt"},
214 { "send", 4, "subtract_fee_from_outputs"},
215 { "send", 4, "conf_target"},
216 { "send", 4, "replaceable"},
217 { "send", 4, "solving_data"},
218 { "send", 4, "max_tx_weight"},
219 { "send", 5, "version"},
220 { "sendall", 0, "recipients" },
221 { "sendall", 1, "conf_target" },
222 { "sendall", 3, "fee_rate"},
223 { "sendall", 4, "options" },
224 { "sendall", 4, "add_to_wallet"},
225 { "sendall", 4, "fee_rate"},
226 { "sendall", 4, "include_watching"},
227 { "sendall", 4, "inputs"},
228 { "sendall", 4, "locktime"},
229 { "sendall", 4, "lock_unspents"},
230 { "sendall", 4, "psbt"},
231 { "sendall", 4, "send_max"},
232 { "sendall", 4, "minconf"},
233 { "sendall", 4, "maxconf"},
234 { "sendall", 4, "conf_target"},
235 { "sendall", 4, "replaceable"},
236 { "sendall", 4, "solving_data"},
237 { "sendall", 4, "version"},
238 { "simulaterawtransaction", 0, "rawtxs" },
239 { "simulaterawtransaction", 1, "options" },
240 { "simulaterawtransaction", 1, "include_watchonly"},
241 { "importmempool", 1, "options" },
242 { "importmempool", 1, "apply_fee_delta_priority" },
243 { "importmempool", 1, "use_current_time" },
244 { "importmempool", 1, "apply_unbroadcast_set" },
245 { "importdescriptors", 0, "requests" },
246 { "listdescriptors", 0, "private" },
247 { "verifychain", 0, "checklevel" },
248 { "verifychain", 1, "nblocks" },
249 { "getblockstats", 0, "hash_or_height" },
250 { "getblockstats", 1, "stats" },
251 { "pruneblockchain", 0, "height" },
252 { "keypoolrefill", 0, "newsize" },
253 { "getrawmempool", 0, "verbose" },
254 { "getrawmempool", 1, "mempool_sequence" },
255 { "getorphantxs", 0, "verbosity" },
256 { "estimatesmartfee", 0, "conf_target" },
257 { "estimaterawfee", 0, "conf_target" },
258 { "estimaterawfee", 1, "threshold" },
259 { "prioritisetransaction", 1, "dummy" },
260 { "prioritisetransaction", 2, "fee_delta" },
261 { "setban", 2, "bantime" },
262 { "setban", 3, "absolute" },
263 { "setnetworkactive", 0, "state" },
264 { "setwalletflag", 1, "value" },
265 { "getmempoolancestors", 1, "verbose" },
266 { "getmempooldescendants", 1, "verbose" },
267 { "gettxspendingprevout", 0, "outputs" },
268 { "bumpfee", 1, "options" },
269 { "bumpfee", 1, "conf_target"},
270 { "bumpfee", 1, "fee_rate"},
271 { "bumpfee", 1, "replaceable"},
272 { "bumpfee", 1, "outputs"},
273 { "bumpfee", 1, "original_change_index"},
274 { "psbtbumpfee", 1, "options" },
275 { "psbtbumpfee", 1, "conf_target"},
276 { "psbtbumpfee", 1, "fee_rate"},
277 { "psbtbumpfee", 1, "replaceable"},
278 { "psbtbumpfee", 1, "outputs"},
279 { "psbtbumpfee", 1, "original_change_index"},
280 { "logging", 0, "include" },
281 { "logging", 1, "exclude" },
282 { "disconnectnode", 1, "nodeid" },
283 { "gethdkeys", 0, "active_only" },
284 { "gethdkeys", 0, "options" },
285 { "gethdkeys", 0, "private" },
286 { "createwalletdescriptor", 1, "options" },
287 { "createwalletdescriptor", 1, "internal" },
288 // Echo with conversion (For testing only)
289 { "echojson", 0, "arg0" },
290 { "echojson", 1, "arg1" },
291 { "echojson", 2, "arg2" },
292 { "echojson", 3, "arg3" },
293 { "echojson", 4, "arg4" },
294 { "echojson", 5, "arg5" },
295 { "echojson", 6, "arg6" },
296 { "echojson", 7, "arg7" },
297 { "echojson", 8, "arg8" },
298 { "echojson", 9, "arg9" },
299 { "rescanblockchain", 0, "start_height"},
300 { "rescanblockchain", 1, "stop_height"},
301 { "createwallet", 1, "disable_private_keys"},
302 { "createwallet", 2, "blank"},
303 { "createwallet", 4, "avoid_reuse"},
304 { "createwallet", 5, "descriptors"},
305 { "createwallet", 6, "load_on_startup"},
306 { "createwallet", 7, "external_signer"},
307 { "restorewallet", 2, "load_on_startup"},
308 { "loadwallet", 1, "load_on_startup"},
309 { "unloadwallet", 1, "load_on_startup"},
310 { "getnodeaddresses", 0, "count"},
311 { "addpeeraddress", 1, "port"},
312 { "addpeeraddress", 2, "tried"},
313 { "sendmsgtopeer", 0, "peer_id" },
314 { "stop", 0, "wait" },
315 { "addnode", 2, "v2transport" },
316 { "addconnection", 2, "v2transport" },
317};
318// clang-format on
319
321static UniValue Parse(std::string_view raw)
322{
323 UniValue parsed;
324 if (!parsed.read(raw)) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw));
325 return parsed;
326}
327
329{
330private:
331 std::set<std::pair<std::string, int>> members;
332 std::set<std::pair<std::string, std::string>> membersByName;
333
334public:
336
338 UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, int param_idx)
339 {
340 return members.count({method, param_idx}) > 0 ? Parse(arg_value) : arg_value;
341 }
342
344 UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, const std::string& param_name)
345 {
346 return membersByName.count({method, param_name}) > 0 ? Parse(arg_value) : arg_value;
347 }
348};
349
351{
352 for (const auto& cp : vRPCConvertParams) {
353 members.emplace(cp.methodName, cp.paramIdx);
354 membersByName.emplace(cp.methodName, cp.paramName);
355 }
356}
357
359
360UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
361{
362 UniValue params(UniValue::VARR);
363
364 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
365 std::string_view value{strParams[idx]};
366 params.push_back(rpcCvtTable.ArgToUniValue(value, strMethod, idx));
367 }
368
369 return params;
370}
371
372UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
373{
374 UniValue params(UniValue::VOBJ);
375 UniValue positional_args{UniValue::VARR};
376
377 for (std::string_view s: strParams) {
378 size_t pos = s.find('=');
379 if (pos == std::string::npos) {
380 positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size()));
381 continue;
382 }
383
384 std::string name{s.substr(0, pos)};
385 std::string_view value{s.substr(pos+1)};
386
387 // Intentionally overwrite earlier named values with later ones as a
388 // convenience for scripts and command line users that want to merge
389 // options.
390 params.pushKV(name, rpcCvtTable.ArgToUniValue(value, strMethod, name));
391 }
392
393 if (!positional_args.empty()) {
394 // Use pushKVEnd instead of pushKV to avoid overwriting an explicit
395 // "args" value with an implicit one. Let the RPC server handle the
396 // request as given.
397 params.pushKVEnd("args", std::move(positional_args));
398 }
399
400 return params;
401}
std::string methodName
method whose params want conversion
Definition: client.cpp:18
int paramIdx
0-based idx of param to convert
Definition: client.cpp:19
std::string paramName
parameter name
Definition: client.cpp:20
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:332
UniValue ArgToUniValue(std::string_view arg_value, const std::string &method, int param_idx)
Return arg_value as UniValue, and first parse it if it is a non-string parameter.
Definition: client.cpp:338
std::set< std::pair< std::string, int > > members
Definition: client.cpp:331
UniValue ArgToUniValue(std::string_view arg_value, const std::string &method, const std::string &param_name)
Return arg_value as UniValue, and first parse it if it is a non-string parameter.
Definition: client.cpp:344
void push_back(UniValue val)
Definition: univalue.cpp:104
@ VOBJ
Definition: univalue.h:24
@ VARR
Definition: univalue.h:24
void pushKVEnd(std::string key, UniValue val)
Definition: univalue.cpp:118
bool read(std::string_view raw)
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:360
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:321
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:372
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:358
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:30
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
const char * name
Definition: rest.cpp:50