Branch data Line data Source code
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 : :
15 : : //! Specify whether parameter should be parsed by bitcoin-cli as a JSON value,
16 : : //! or passed unchanged as a string, or a combination of both.
17 : : enum ParamFormat { JSON, STRING, JSON_OR_STRING };
18 : :
19 : : class CRPCConvertParam
20 : : {
21 : : public:
22 : : std::string methodName; //!< method whose params want conversion
23 : : int paramIdx; //!< 0-based idx of param to convert
24 : : std::string paramName; //!< parameter name
25 : : ParamFormat format{ParamFormat::JSON}; //!< parameter format
26 : : };
27 : :
28 : : // clang-format off
29 : : /**
30 : : * Specify a (method, idx, name, format) here if the argument is a non-string RPC
31 : : * argument and needs to be converted from JSON, or if it is a string argument
32 : : * passed to a method that accepts '=' characters in any string arguments.
33 : : *
34 : : * JSON parameters need to be listed here to make bitcoin-cli parse command line
35 : : * arguments as JSON, instead of passing them as raw strings. `JSON` and
36 : : * `JSON_OR_STRING` formats both make `bitcoin-cli` attempt to parse the
37 : : * argument as JSON. But if parsing fails, the former triggers an error while
38 : : * the latter falls back to passing the argument as a raw string. This is
39 : : * useful for arguments like hash_or_height, allowing invocations such as
40 : : * `bitcoin-cli getblockstats <hash>` without needing to quote the hash string
41 : : * as JSON (`'"<hash>"'`).
42 : : *
43 : : * String parameters that may contain an '=' character (e.g. base64 strings,
44 : : * filenames, or labels) need to be listed here with format `ParamFormat::STRING`
45 : : * to make bitcoin-cli treat them as positional parameters when `-named` is used.
46 : : * This prevents `bitcoin-cli` from splitting strings like "my=wallet" into a named
47 : : * argument "my" and value "wallet" when the whole string is intended to be a
48 : : * single positional argument. And if one string parameter is listed for a method,
49 : : * other string parameters for that method need to be listed as well so bitcoin-cli
50 : : * does not make the opposite mistake and pass other arguments by position instead of
51 : : * name because it does not recognize their names. See \ref RPCConvertNamedValues
52 : : * for more information on how named and positional arguments are distinguished with
53 : : * -named.
54 : : *
55 : : * @note Parameter indexes start from 0.
56 : : */
57 : : static const CRPCConvertParam vRPCConvertParams[] =
58 : : {
59 : : { "setmocktime", 0, "timestamp" },
60 : : { "mockscheduler", 0, "delta_time" },
61 : : { "utxoupdatepsbt", 0, "psbt", ParamFormat::STRING },
62 : : { "utxoupdatepsbt", 1, "descriptors" },
63 : : { "generatetoaddress", 0, "nblocks" },
64 : : { "generatetoaddress", 2, "maxtries" },
65 : : { "generatetodescriptor", 0, "num_blocks" },
66 : : { "generatetodescriptor", 2, "maxtries" },
67 : : { "generateblock", 1, "transactions" },
68 : : { "generateblock", 2, "submit" },
69 : : { "getnetworkhashps", 0, "nblocks" },
70 : : { "getnetworkhashps", 1, "height" },
71 : : { "sendtoaddress", 0, "address", ParamFormat::STRING },
72 : : { "sendtoaddress", 1, "amount" },
73 : : { "sendtoaddress", 2, "comment", ParamFormat::STRING },
74 : : { "sendtoaddress", 3, "comment_to", ParamFormat::STRING },
75 : : { "sendtoaddress", 4, "subtractfeefromamount" },
76 : : { "sendtoaddress", 5 , "replaceable" },
77 : : { "sendtoaddress", 6 , "conf_target" },
78 : : { "sendtoaddress", 7, "estimate_mode", ParamFormat::STRING },
79 : : { "sendtoaddress", 8, "avoid_reuse" },
80 : : { "sendtoaddress", 9, "fee_rate"},
81 : : { "sendtoaddress", 10, "verbose"},
82 : : { "settxfee", 0, "amount" },
83 : : { "getreceivedbyaddress", 1, "minconf" },
84 : : { "getreceivedbyaddress", 2, "include_immature_coinbase" },
85 : : { "getreceivedbylabel", 0, "label", ParamFormat::STRING },
86 : : { "getreceivedbylabel", 1, "minconf" },
87 : : { "getreceivedbylabel", 2, "include_immature_coinbase" },
88 : : { "listreceivedbyaddress", 0, "minconf" },
89 : : { "listreceivedbyaddress", 1, "include_empty" },
90 : : { "listreceivedbyaddress", 2, "include_watchonly" },
91 : : { "listreceivedbyaddress", 4, "include_immature_coinbase" },
92 : : { "listreceivedbylabel", 0, "minconf" },
93 : : { "listreceivedbylabel", 1, "include_empty" },
94 : : { "listreceivedbylabel", 2, "include_watchonly" },
95 : : { "listreceivedbylabel", 3, "include_immature_coinbase" },
96 : : { "getbalance", 1, "minconf" },
97 : : { "getbalance", 2, "include_watchonly" },
98 : : { "getbalance", 3, "avoid_reuse" },
99 : : { "getblockfrompeer", 1, "peer_id" },
100 : : { "getblockhash", 0, "height" },
101 : : { "waitforblockheight", 0, "height" },
102 : : { "waitforblockheight", 1, "timeout" },
103 : : { "waitforblock", 1, "timeout" },
104 : : { "waitfornewblock", 0, "timeout" },
105 : : { "listtransactions", 0, "label", ParamFormat::STRING },
106 : : { "listtransactions", 1, "count" },
107 : : { "listtransactions", 2, "skip" },
108 : : { "listtransactions", 3, "include_watchonly" },
109 : : { "walletpassphrase", 0, "passphrase", ParamFormat::STRING },
110 : : { "walletpassphrase", 1, "timeout" },
111 : : { "getblocktemplate", 0, "template_request" },
112 : : { "listsinceblock", 0, "blockhash", ParamFormat::STRING },
113 : : { "listsinceblock", 1, "target_confirmations" },
114 : : { "listsinceblock", 2, "include_watchonly" },
115 : : { "listsinceblock", 3, "include_removed" },
116 : : { "listsinceblock", 4, "include_change" },
117 : : { "listsinceblock", 5, "label", ParamFormat::STRING },
118 : : { "sendmany", 0, "dummy", ParamFormat::STRING },
119 : : { "sendmany", 1, "amounts" },
120 : : { "sendmany", 2, "minconf" },
121 : : { "sendmany", 3, "comment", ParamFormat::STRING },
122 : : { "sendmany", 4, "subtractfeefrom" },
123 : : { "sendmany", 5 , "replaceable" },
124 : : { "sendmany", 6 , "conf_target" },
125 : : { "sendmany", 7, "estimate_mode", ParamFormat::STRING },
126 : : { "sendmany", 8, "fee_rate"},
127 : : { "sendmany", 9, "verbose" },
128 : : { "deriveaddresses", 1, "range" },
129 : : { "scanblocks", 1, "scanobjects" },
130 : : { "scanblocks", 2, "start_height" },
131 : : { "scanblocks", 3, "stop_height" },
132 : : { "scanblocks", 5, "options" },
133 : : { "scanblocks", 5, "filter_false_positives" },
134 : : { "getdescriptoractivity", 0, "blockhashes" },
135 : : { "getdescriptoractivity", 1, "scanobjects" },
136 : : { "getdescriptoractivity", 2, "include_mempool" },
137 : : { "scantxoutset", 1, "scanobjects" },
138 : : { "createmultisig", 0, "nrequired" },
139 : : { "createmultisig", 1, "keys" },
140 : : { "listunspent", 0, "minconf" },
141 : : { "listunspent", 1, "maxconf" },
142 : : { "listunspent", 2, "addresses" },
143 : : { "listunspent", 3, "include_unsafe" },
144 : : { "listunspent", 4, "query_options" },
145 : : { "listunspent", 4, "minimumAmount" },
146 : : { "listunspent", 4, "maximumAmount" },
147 : : { "listunspent", 4, "maximumCount" },
148 : : { "listunspent", 4, "minimumSumAmount" },
149 : : { "listunspent", 4, "include_immature_coinbase" },
150 : : { "getblock", 1, "verbosity" },
151 : : { "getblock", 1, "verbose" },
152 : : { "getblockheader", 1, "verbose" },
153 : : { "getchaintxstats", 0, "nblocks" },
154 : : { "gettransaction", 1, "include_watchonly" },
155 : : { "gettransaction", 2, "verbose" },
156 : : { "getrawtransaction", 1, "verbosity" },
157 : : { "getrawtransaction", 1, "verbose" },
158 : : { "createrawtransaction", 0, "inputs" },
159 : : { "createrawtransaction", 1, "outputs" },
160 : : { "createrawtransaction", 2, "locktime" },
161 : : { "createrawtransaction", 3, "replaceable" },
162 : : { "createrawtransaction", 4, "version" },
163 : : { "decoderawtransaction", 1, "iswitness" },
164 : : { "signrawtransactionwithkey", 1, "privkeys" },
165 : : { "signrawtransactionwithkey", 2, "prevtxs" },
166 : : { "signrawtransactionwithwallet", 1, "prevtxs" },
167 : : { "sendrawtransaction", 1, "maxfeerate" },
168 : : { "sendrawtransaction", 2, "maxburnamount" },
169 : : { "testmempoolaccept", 0, "rawtxs" },
170 : : { "testmempoolaccept", 1, "maxfeerate" },
171 : : { "submitpackage", 0, "package" },
172 : : { "submitpackage", 1, "maxfeerate" },
173 : : { "submitpackage", 2, "maxburnamount" },
174 : : { "combinerawtransaction", 0, "txs" },
175 : : { "fundrawtransaction", 1, "options" },
176 : : { "fundrawtransaction", 1, "add_inputs"},
177 : : { "fundrawtransaction", 1, "include_unsafe"},
178 : : { "fundrawtransaction", 1, "minconf"},
179 : : { "fundrawtransaction", 1, "maxconf"},
180 : : { "fundrawtransaction", 1, "changePosition"},
181 : : { "fundrawtransaction", 1, "includeWatching"},
182 : : { "fundrawtransaction", 1, "lockUnspents"},
183 : : { "fundrawtransaction", 1, "fee_rate"},
184 : : { "fundrawtransaction", 1, "feeRate"},
185 : : { "fundrawtransaction", 1, "subtractFeeFromOutputs"},
186 : : { "fundrawtransaction", 1, "input_weights"},
187 : : { "fundrawtransaction", 1, "conf_target"},
188 : : { "fundrawtransaction", 1, "replaceable"},
189 : : { "fundrawtransaction", 1, "solving_data"},
190 : : { "fundrawtransaction", 1, "max_tx_weight"},
191 : : { "fundrawtransaction", 2, "iswitness" },
192 : : { "walletcreatefundedpsbt", 0, "inputs" },
193 : : { "walletcreatefundedpsbt", 1, "outputs" },
194 : : { "walletcreatefundedpsbt", 2, "locktime" },
195 : : { "walletcreatefundedpsbt", 3, "options" },
196 : : { "walletcreatefundedpsbt", 3, "add_inputs"},
197 : : { "walletcreatefundedpsbt", 3, "include_unsafe"},
198 : : { "walletcreatefundedpsbt", 3, "minconf"},
199 : : { "walletcreatefundedpsbt", 3, "maxconf"},
200 : : { "walletcreatefundedpsbt", 3, "changePosition"},
201 : : { "walletcreatefundedpsbt", 3, "includeWatching"},
202 : : { "walletcreatefundedpsbt", 3, "lockUnspents"},
203 : : { "walletcreatefundedpsbt", 3, "fee_rate"},
204 : : { "walletcreatefundedpsbt", 3, "feeRate"},
205 : : { "walletcreatefundedpsbt", 3, "subtractFeeFromOutputs"},
206 : : { "walletcreatefundedpsbt", 3, "conf_target"},
207 : : { "walletcreatefundedpsbt", 3, "replaceable"},
208 : : { "walletcreatefundedpsbt", 3, "solving_data"},
209 : : { "walletcreatefundedpsbt", 3, "max_tx_weight"},
210 : : { "walletcreatefundedpsbt", 4, "bip32derivs" },
211 : : { "walletcreatefundedpsbt", 5, "version" },
212 : : { "walletprocesspsbt", 0, "psbt", ParamFormat::STRING },
213 : : { "walletprocesspsbt", 1, "sign" },
214 : : { "walletprocesspsbt", 2, "sighashtype", ParamFormat::STRING },
215 : : { "walletprocesspsbt", 3, "bip32derivs" },
216 : : { "walletprocesspsbt", 4, "finalize" },
217 : : { "descriptorprocesspsbt", 0, "psbt", ParamFormat::STRING },
218 : : { "descriptorprocesspsbt", 1, "descriptors"},
219 : : { "descriptorprocesspsbt", 2, "sighashtype", ParamFormat::STRING },
220 : : { "descriptorprocesspsbt", 3, "bip32derivs" },
221 : : { "descriptorprocesspsbt", 4, "finalize" },
222 : : { "createpsbt", 0, "inputs" },
223 : : { "createpsbt", 1, "outputs" },
224 : : { "createpsbt", 2, "locktime" },
225 : : { "createpsbt", 3, "replaceable" },
226 : : { "createpsbt", 4, "version" },
227 : : { "combinepsbt", 0, "txs"},
228 : : { "joinpsbts", 0, "txs"},
229 : : { "finalizepsbt", 0, "psbt", ParamFormat::STRING },
230 : : { "finalizepsbt", 1, "extract"},
231 : : { "converttopsbt", 1, "permitsigdata"},
232 : : { "converttopsbt", 2, "iswitness"},
233 : : { "gettxout", 1, "n" },
234 : : { "gettxout", 2, "include_mempool" },
235 : : { "gettxoutproof", 0, "txids" },
236 : : { "gettxoutsetinfo", 1, "hash_or_height", ParamFormat::JSON_OR_STRING },
237 : : { "gettxoutsetinfo", 2, "use_index"},
238 : : { "dumptxoutset", 0, "path", ParamFormat::STRING },
239 : : { "dumptxoutset", 1, "type", ParamFormat::STRING },
240 : : { "dumptxoutset", 2, "options" },
241 : : { "dumptxoutset", 2, "rollback", ParamFormat::JSON_OR_STRING },
242 : : { "lockunspent", 0, "unlock" },
243 : : { "lockunspent", 1, "transactions" },
244 : : { "lockunspent", 2, "persistent" },
245 : : { "send", 0, "outputs" },
246 : : { "send", 1, "conf_target" },
247 : : { "send", 3, "fee_rate"},
248 : : { "send", 4, "options" },
249 : : { "send", 4, "add_inputs"},
250 : : { "send", 4, "include_unsafe"},
251 : : { "send", 4, "minconf"},
252 : : { "send", 4, "maxconf"},
253 : : { "send", 4, "add_to_wallet"},
254 : : { "send", 4, "change_position"},
255 : : { "send", 4, "fee_rate"},
256 : : { "send", 4, "include_watching"},
257 : : { "send", 4, "inputs"},
258 : : { "send", 4, "locktime"},
259 : : { "send", 4, "lock_unspents"},
260 : : { "send", 4, "psbt"},
261 : : { "send", 4, "subtract_fee_from_outputs"},
262 : : { "send", 4, "conf_target"},
263 : : { "send", 4, "replaceable"},
264 : : { "send", 4, "solving_data"},
265 : : { "send", 4, "max_tx_weight"},
266 : : { "send", 5, "version"},
267 : : { "sendall", 0, "recipients" },
268 : : { "sendall", 1, "conf_target" },
269 : : { "sendall", 3, "fee_rate"},
270 : : { "sendall", 4, "options" },
271 : : { "sendall", 4, "add_to_wallet"},
272 : : { "sendall", 4, "fee_rate"},
273 : : { "sendall", 4, "include_watching"},
274 : : { "sendall", 4, "inputs"},
275 : : { "sendall", 4, "locktime"},
276 : : { "sendall", 4, "lock_unspents"},
277 : : { "sendall", 4, "psbt"},
278 : : { "sendall", 4, "send_max"},
279 : : { "sendall", 4, "minconf"},
280 : : { "sendall", 4, "maxconf"},
281 : : { "sendall", 4, "conf_target"},
282 : : { "sendall", 4, "replaceable"},
283 : : { "sendall", 4, "solving_data"},
284 : : { "sendall", 4, "version"},
285 : : { "simulaterawtransaction", 0, "rawtxs" },
286 : : { "simulaterawtransaction", 1, "options" },
287 : : { "simulaterawtransaction", 1, "include_watchonly"},
288 : : { "importmempool", 0, "filepath", ParamFormat::STRING },
289 : : { "importmempool", 1, "options" },
290 : : { "importmempool", 1, "apply_fee_delta_priority" },
291 : : { "importmempool", 1, "use_current_time" },
292 : : { "importmempool", 1, "apply_unbroadcast_set" },
293 : : { "importdescriptors", 0, "requests" },
294 : : { "listdescriptors", 0, "private" },
295 : : { "verifychain", 0, "checklevel" },
296 : : { "verifychain", 1, "nblocks" },
297 : : { "getblockstats", 0, "hash_or_height", ParamFormat::JSON_OR_STRING },
298 : : { "getblockstats", 1, "stats" },
299 : : { "pruneblockchain", 0, "height" },
300 : : { "keypoolrefill", 0, "newsize" },
301 : : { "getrawmempool", 0, "verbose" },
302 : : { "getrawmempool", 1, "mempool_sequence" },
303 : : { "getorphantxs", 0, "verbosity" },
304 : : { "estimatesmartfee", 0, "conf_target" },
305 : : { "estimaterawfee", 0, "conf_target" },
306 : : { "estimaterawfee", 1, "threshold" },
307 : : { "prioritisetransaction", 1, "dummy" },
308 : : { "prioritisetransaction", 2, "fee_delta" },
309 : : { "setban", 2, "bantime" },
310 : : { "setban", 3, "absolute" },
311 : : { "setnetworkactive", 0, "state" },
312 : : { "setwalletflag", 1, "value" },
313 : : { "getmempoolancestors", 1, "verbose" },
314 : : { "getmempooldescendants", 1, "verbose" },
315 : : { "gettxspendingprevout", 0, "outputs" },
316 : : { "bumpfee", 1, "options" },
317 : : { "bumpfee", 1, "conf_target"},
318 : : { "bumpfee", 1, "fee_rate"},
319 : : { "bumpfee", 1, "replaceable"},
320 : : { "bumpfee", 1, "outputs"},
321 : : { "bumpfee", 1, "original_change_index"},
322 : : { "psbtbumpfee", 1, "options" },
323 : : { "psbtbumpfee", 1, "conf_target"},
324 : : { "psbtbumpfee", 1, "fee_rate"},
325 : : { "psbtbumpfee", 1, "replaceable"},
326 : : { "psbtbumpfee", 1, "outputs"},
327 : : { "psbtbumpfee", 1, "original_change_index"},
328 : : { "logging", 0, "include" },
329 : : { "logging", 1, "exclude" },
330 : : { "disconnectnode", 1, "nodeid" },
331 : : { "gethdkeys", 0, "active_only" },
332 : : { "gethdkeys", 0, "options" },
333 : : { "gethdkeys", 0, "private" },
334 : : { "createwalletdescriptor", 1, "options" },
335 : : { "createwalletdescriptor", 1, "internal" },
336 : : // Echo with conversion (For testing only)
337 : : { "echojson", 0, "arg0" },
338 : : { "echojson", 1, "arg1" },
339 : : { "echojson", 2, "arg2" },
340 : : { "echojson", 3, "arg3" },
341 : : { "echojson", 4, "arg4" },
342 : : { "echojson", 5, "arg5" },
343 : : { "echojson", 6, "arg6" },
344 : : { "echojson", 7, "arg7" },
345 : : { "echojson", 8, "arg8" },
346 : : { "echojson", 9, "arg9" },
347 : : { "rescanblockchain", 0, "start_height"},
348 : : { "rescanblockchain", 1, "stop_height"},
349 : : { "createwallet", 0, "wallet_name", ParamFormat::STRING },
350 : : { "createwallet", 1, "disable_private_keys"},
351 : : { "createwallet", 2, "blank"},
352 : : { "createwallet", 3, "passphrase", ParamFormat::STRING },
353 : : { "createwallet", 4, "avoid_reuse"},
354 : : { "createwallet", 5, "descriptors"},
355 : : { "createwallet", 6, "load_on_startup"},
356 : : { "createwallet", 7, "external_signer"},
357 : : { "restorewallet", 0, "wallet_name", ParamFormat::STRING },
358 : : { "restorewallet", 1, "backup_file", ParamFormat::STRING },
359 : : { "restorewallet", 2, "load_on_startup"},
360 : : { "loadwallet", 0, "filename", ParamFormat::STRING },
361 : : { "loadwallet", 1, "load_on_startup"},
362 : : { "unloadwallet", 0, "wallet_name", ParamFormat::STRING },
363 : : { "unloadwallet", 1, "load_on_startup"},
364 : : { "getnodeaddresses", 0, "count"},
365 : : { "addpeeraddress", 1, "port"},
366 : : { "addpeeraddress", 2, "tried"},
367 : : { "sendmsgtopeer", 0, "peer_id" },
368 : : { "stop", 0, "wait" },
369 : : { "addnode", 2, "v2transport" },
370 : : { "addconnection", 2, "v2transport" },
371 : : { "decodepsbt", 0, "psbt", ParamFormat::STRING },
372 : : { "analyzepsbt", 0, "psbt", ParamFormat::STRING},
373 : : { "verifymessage", 1, "signature", ParamFormat::STRING },
374 : : { "verifymessage", 2, "message", ParamFormat::STRING },
375 : : { "getnewaddress", 0, "label", ParamFormat::STRING },
376 : : { "getnewaddress", 1, "address_type", ParamFormat::STRING },
377 : : { "backupwallet", 0, "destination", ParamFormat::STRING },
378 : : { "echoipc", 0, "arg", ParamFormat::STRING },
379 : : { "encryptwallet", 0, "passphrase", ParamFormat::STRING },
380 : : { "getaddressesbylabel", 0, "label", ParamFormat::STRING },
381 : : { "loadtxoutset", 0, "path", ParamFormat::STRING },
382 : : { "migratewallet", 0, "wallet_name", ParamFormat::STRING },
383 : : { "migratewallet", 1, "passphrase", ParamFormat::STRING },
384 : : { "setlabel", 1, "label", ParamFormat::STRING },
385 : : { "signmessage", 1, "message", ParamFormat::STRING },
386 : : { "signmessagewithprivkey", 1, "message", ParamFormat::STRING },
387 : : { "walletpassphrasechange", 0, "oldpassphrase", ParamFormat::STRING },
388 : : { "walletpassphrasechange", 1, "newpassphrase", ParamFormat::STRING },
389 : : };
390 : : // clang-format on
391 : :
392 : : /** Parse string to UniValue or throw runtime_error if string contains invalid JSON */
393 : 746 : static UniValue Parse(std::string_view raw, ParamFormat format = ParamFormat::JSON)
394 : : {
395 [ + - ]: 746 : UniValue parsed;
396 [ + - + + ]: 746 : if (!parsed.read(raw)) {
397 [ + - + - : 14 : if (format != ParamFormat::JSON_OR_STRING) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw));
+ - ]
398 [ # # # # ]: 0 : return UniValue(std::string(raw));
399 : : }
400 : 739 : return parsed;
401 : 746 : }
402 : :
403 : : namespace rpc_convert
404 : : {
405 : 681 : const CRPCConvertParam* FromPosition(std::string_view method, size_t pos)
406 : : {
407 : 192680 : auto it = std::ranges::find_if(vRPCConvertParams, [&](const auto& p) {
408 [ - + + + : 191999 : return p.methodName == method && p.paramIdx == static_cast<int>(pos);
+ + ]
409 : : });
410 : :
411 [ + + ]: 681 : return it == std::end(vRPCConvertParams) ? nullptr : &*it;
412 : : }
413 : :
414 : 997 : const CRPCConvertParam* FromName(std::string_view method, std::string_view name)
415 : : {
416 : 145041 : auto it = std::ranges::find_if(vRPCConvertParams, [&](const auto& p) {
417 [ - + + + : 144044 : return p.methodName == method && p.paramName == name;
- + + + ]
418 : : });
419 : :
420 [ + + ]: 997 : return it == std::end(vRPCConvertParams) ? nullptr : &*it;
421 : : }
422 : : } // namespace rpc_convert
423 : :
424 : 1341 : static UniValue ParseParam(const CRPCConvertParam* param, std::string_view raw)
425 : : {
426 : : // Only parse parameters which have the JSON or JSON_OR_STRING format; otherwise, treat them as strings.
427 [ + + + + : 1936 : return (param && (param->format == ParamFormat::JSON || param->format == ParamFormat::JSON_OR_STRING)) ? Parse(raw, param->format) : UniValue(std::string(raw));
- + + - ]
428 : : }
429 : :
430 : : /**
431 : : * Convert command lines arguments to params object when -named is disabled.
432 : : */
433 : 480 : UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
434 : : {
435 : 480 : UniValue params(UniValue::VARR);
436 : :
437 [ - + + + ]: 816 : for (std::string_view s : strParams) {
438 [ - + - + : 343 : params.push_back(ParseParam(rpc_convert::FromPosition(strMethod, params.size()), s));
+ - + + +
- ]
439 : : }
440 : :
441 : 473 : return params;
442 : 7 : }
443 : :
444 : : /**
445 : : * Convert command line arguments to params object when -named is enabled.
446 : : *
447 : : * The -named syntax accepts named arguments in NAME=VALUE format, as well as
448 : : * positional arguments without names. The syntax is inherently ambiguous if
449 : : * names are omitted and values contain '=', so a heuristic is used to
450 : : * disambiguate:
451 : : *
452 : : * - Arguments that do not contain '=' are treated as positional parameters.
453 : : *
454 : : * - Arguments that do contain '=' are assumed to be named parameters in
455 : : * NAME=VALUE format except for two special cases:
456 : : *
457 : : * 1. The case where NAME is not a known parameter name, and the next
458 : : * positional parameter requires a JSON value, and the argument parses as
459 : : * JSON. E.g. ["list", "with", "="].
460 : : *
461 : : * 2. The case where NAME is not a known parameter name and the next
462 : : * positional parameter requires a string value. E.g. "my=wallet".
463 : : *
464 : : * For example, the command `bitcoin-cli -named createwallet "my=wallet"`,
465 : : * the parser initially sees "my=wallet" and attempts to process it as a
466 : : * parameter named "my". When it finds that "my" is not a valid named parameter
467 : : * parameter for this method, it falls back to checking the rule for the
468 : : * next available positional parameter (index 0). Because it finds the rule
469 : : * that this parameter is a ParamFormat::STRING, it correctly treats the entire
470 : : * "my=wallet" as a single positional string, successfully creating a
471 : : * wallet with that literal name.
472 : : */
473 : 655 : UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
474 : : {
475 : 655 : UniValue params(UniValue::VOBJ);
476 : 655 : UniValue positional_args{UniValue::VARR};
477 : :
478 [ - + + + ]: 1663 : for (std::string_view s: strParams) {
479 : 1008 : size_t pos = s.find('=');
480 [ + + ]: 1008 : if (pos == std::string_view::npos) {
481 [ - + - + : 11 : positional_args.push_back(ParseParam(rpc_convert::FromPosition(strMethod, positional_args.size()), s));
+ - + - +
- ]
482 : 11 : continue;
483 : : }
484 : :
485 [ + - + - ]: 997 : std::string name{s.substr(0, pos)};
486 [ + - ]: 997 : std::string_view value{s.substr(pos+1)};
487 : :
488 [ - + - + : 997 : const CRPCConvertParam* named_param{rpc_convert::FromName(strMethod, name)};
+ - ]
489 [ + + ]: 997 : if (!named_param) {
490 [ - + - + : 327 : const CRPCConvertParam* positional_param = rpc_convert::FromPosition(strMethod, positional_args.size());
+ - ]
491 [ + + ]: 327 : UniValue parsed_value;
492 [ + + + + : 327 : if (positional_param && positional_param->format == ParamFormat::JSON && parsed_value.read(s)) {
+ - + + ]
493 [ + - ]: 3 : positional_args.push_back(std::move(parsed_value));
494 : 3 : continue;
495 [ + + + + ]: 324 : } else if (positional_param && positional_param->format == ParamFormat::STRING) {
496 [ + - + - ]: 7 : positional_args.push_back(s);
497 : 7 : continue;
498 : : }
499 : 327 : }
500 : :
501 : : // Intentionally overwrite earlier named values with later ones as a
502 : : // convenience for scripts and command line users that want to merge
503 : : // options.
504 [ + - + - ]: 2961 : params.pushKV(name, ParseParam(named_param, value));
505 : 997 : }
506 : :
507 [ - + + + ]: 655 : if (!positional_args.empty()) {
508 : : // Use pushKVEnd instead of pushKV to avoid overwriting an explicit
509 : : // "args" value with an implicit one. Let the RPC server handle the
510 : : // request as given.
511 [ + - + - ]: 28 : params.pushKVEnd("args", std::move(positional_args));
512 : : }
513 : :
514 : 655 : return params;
515 : 655 : }
|