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