client.cpp
1 // Copyright (c) 2010 Satoshi Nakamoto 2 // Copyright (c) 2009-2022 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 <set> 11 #include <stdint.h> 12 #include <string> 13 #include <string_view> 14 15 class CRPCConvertParam 16 { 17 public: 18 std::string methodName; //!< method whose params want conversion 19 int paramIdx; //!< 0-based idx of param to convert 20 std::string paramName; //!< parameter name 21 }; 22 23 // clang-format off 24 /** 25 * Specify a (method, idx, name) here if the argument is a non-string RPC 26 * argument and needs to be converted from JSON. 27 * 28 * @note Parameter indexes start from 0. 29 */ 30 static const CRPCConvertParam vRPCConvertParams[] = 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 { "sethdseed", 0, "newkeypool" }, 52 { "getreceivedbyaddress", 1, "minconf" }, 53 { "getreceivedbyaddress", 2, "include_immature_coinbase" }, 54 { "getreceivedbylabel", 1, "minconf" }, 55 { "getreceivedbylabel", 2, "include_immature_coinbase" }, 56 { "listreceivedbyaddress", 0, "minconf" }, 57 { "listreceivedbyaddress", 1, "include_empty" }, 58 { "listreceivedbyaddress", 2, "include_watchonly" }, 59 { "listreceivedbyaddress", 4, "include_immature_coinbase" }, 60 { "listreceivedbylabel", 0, "minconf" }, 61 { "listreceivedbylabel", 1, "include_empty" }, 62 { "listreceivedbylabel", 2, "include_watchonly" }, 63 { "listreceivedbylabel", 3, "include_immature_coinbase" }, 64 { "getbalance", 1, "minconf" }, 65 { "getbalance", 2, "include_watchonly" }, 66 { "getbalance", 3, "avoid_reuse" }, 67 { "getblockfrompeer", 1, "peer_id" }, 68 { "getblockhash", 0, "height" }, 69 { "waitforblockheight", 0, "height" }, 70 { "waitforblockheight", 1, "timeout" }, 71 { "waitforblock", 1, "timeout" }, 72 { "waitfornewblock", 0, "timeout" }, 73 { "listtransactions", 1, "count" }, 74 { "listtransactions", 2, "skip" }, 75 { "listtransactions", 3, "include_watchonly" }, 76 { "walletpassphrase", 1, "timeout" }, 77 { "getblocktemplate", 0, "template_request" }, 78 { "listsinceblock", 1, "target_confirmations" }, 79 { "listsinceblock", 2, "include_watchonly" }, 80 { "listsinceblock", 3, "include_removed" }, 81 { "listsinceblock", 4, "include_change" }, 82 { "sendmany", 1, "amounts" }, 83 { "sendmany", 2, "minconf" }, 84 { "sendmany", 4, "subtractfeefrom" }, 85 { "sendmany", 5 , "replaceable" }, 86 { "sendmany", 6 , "conf_target" }, 87 { "sendmany", 8, "fee_rate"}, 88 { "sendmany", 9, "verbose" }, 89 { "deriveaddresses", 1, "range" }, 90 { "scanblocks", 1, "scanobjects" }, 91 { "scanblocks", 2, "start_height" }, 92 { "scanblocks", 3, "stop_height" }, 93 { "scanblocks", 5, "options" }, 94 { "scanblocks", 5, "filter_false_positives" }, 95 { "getdescriptoractivity", 0, "blockhashes" }, 96 { "getdescriptoractivity", 1, "scanobjects" }, 97 { "getdescriptoractivity", 2, "include_mempool" }, 98 { "scantxoutset", 1, "scanobjects" }, 99 { "addmultisigaddress", 0, "nrequired" }, 100 { "addmultisigaddress", 1, "keys" }, 101 { "createmultisig", 0, "nrequired" }, 102 { "createmultisig", 1, "keys" }, 103 { "listunspent", 0, "minconf" }, 104 { "listunspent", 1, "maxconf" }, 105 { "listunspent", 2, "addresses" }, 106 { "listunspent", 3, "include_unsafe" }, 107 { "listunspent", 4, "query_options" }, 108 { "listunspent", 4, "minimumAmount" }, 109 { "listunspent", 4, "maximumAmount" }, 110 { "listunspent", 4, "maximumCount" }, 111 { "listunspent", 4, "minimumSumAmount" }, 112 { "listunspent", 4, "include_immature_coinbase" }, 113 { "getblock", 1, "verbosity" }, 114 { "getblock", 1, "verbose" }, 115 { "getblockheader", 1, "verbose" }, 116 { "getchaintxstats", 0, "nblocks" }, 117 { "gettransaction", 1, "include_watchonly" }, 118 { "gettransaction", 2, "verbose" }, 119 { "getrawtransaction", 1, "verbosity" }, 120 { "getrawtransaction", 1, "verbose" }, 121 { "createrawtransaction", 0, "inputs" }, 122 { "createrawtransaction", 1, "outputs" }, 123 { "createrawtransaction", 2, "locktime" }, 124 { "createrawtransaction", 3, "replaceable" }, 125 { "decoderawtransaction", 1, "iswitness" }, 126 { "signrawtransactionwithkey", 1, "privkeys" }, 127 { "signrawtransactionwithkey", 2, "prevtxs" }, 128 { "signrawtransactionwithwallet", 1, "prevtxs" }, 129 { "sendrawtransaction", 1, "maxfeerate" }, 130 { "sendrawtransaction", 2, "maxburnamount" }, 131 { "testmempoolaccept", 0, "rawtxs" }, 132 { "testmempoolaccept", 1, "maxfeerate" }, 133 { "submitpackage", 0, "package" }, 134 { "submitpackage", 1, "maxfeerate" }, 135 { "submitpackage", 2, "maxburnamount" }, 136 { "combinerawtransaction", 0, "txs" }, 137 { "fundrawtransaction", 1, "options" }, 138 { "fundrawtransaction", 1, "add_inputs"}, 139 { "fundrawtransaction", 1, "include_unsafe"}, 140 { "fundrawtransaction", 1, "minconf"}, 141 { "fundrawtransaction", 1, "maxconf"}, 142 { "fundrawtransaction", 1, "changePosition"}, 143 { "fundrawtransaction", 1, "includeWatching"}, 144 { "fundrawtransaction", 1, "lockUnspents"}, 145 { "fundrawtransaction", 1, "fee_rate"}, 146 { "fundrawtransaction", 1, "feeRate"}, 147 { "fundrawtransaction", 1, "subtractFeeFromOutputs"}, 148 { "fundrawtransaction", 1, "input_weights"}, 149 { "fundrawtransaction", 1, "conf_target"}, 150 { "fundrawtransaction", 1, "replaceable"}, 151 { "fundrawtransaction", 1, "solving_data"}, 152 { "fundrawtransaction", 1, "max_tx_weight"}, 153 { "fundrawtransaction", 2, "iswitness" }, 154 { "walletcreatefundedpsbt", 0, "inputs" }, 155 { "walletcreatefundedpsbt", 1, "outputs" }, 156 { "walletcreatefundedpsbt", 2, "locktime" }, 157 { "walletcreatefundedpsbt", 3, "options" }, 158 { "walletcreatefundedpsbt", 3, "add_inputs"}, 159 { "walletcreatefundedpsbt", 3, "include_unsafe"}, 160 { "walletcreatefundedpsbt", 3, "minconf"}, 161 { "walletcreatefundedpsbt", 3, "maxconf"}, 162 { "walletcreatefundedpsbt", 3, "changePosition"}, 163 { "walletcreatefundedpsbt", 3, "includeWatching"}, 164 { "walletcreatefundedpsbt", 3, "lockUnspents"}, 165 { "walletcreatefundedpsbt", 3, "fee_rate"}, 166 { "walletcreatefundedpsbt", 3, "feeRate"}, 167 { "walletcreatefundedpsbt", 3, "subtractFeeFromOutputs"}, 168 { "walletcreatefundedpsbt", 3, "conf_target"}, 169 { "walletcreatefundedpsbt", 3, "replaceable"}, 170 { "walletcreatefundedpsbt", 3, "solving_data"}, 171 { "walletcreatefundedpsbt", 3, "max_tx_weight"}, 172 { "walletcreatefundedpsbt", 4, "bip32derivs" }, 173 { "walletprocesspsbt", 1, "sign" }, 174 { "walletprocesspsbt", 3, "bip32derivs" }, 175 { "walletprocesspsbt", 4, "finalize" }, 176 { "descriptorprocesspsbt", 1, "descriptors"}, 177 { "descriptorprocesspsbt", 3, "bip32derivs" }, 178 { "descriptorprocesspsbt", 4, "finalize" }, 179 { "createpsbt", 0, "inputs" }, 180 { "createpsbt", 1, "outputs" }, 181 { "createpsbt", 2, "locktime" }, 182 { "createpsbt", 3, "replaceable" }, 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 { "sendall", 0, "recipients" }, 220 { "sendall", 1, "conf_target" }, 221 { "sendall", 3, "fee_rate"}, 222 { "sendall", 4, "options" }, 223 { "sendall", 4, "add_to_wallet"}, 224 { "sendall", 4, "fee_rate"}, 225 { "sendall", 4, "include_watching"}, 226 { "sendall", 4, "inputs"}, 227 { "sendall", 4, "locktime"}, 228 { "sendall", 4, "lock_unspents"}, 229 { "sendall", 4, "psbt"}, 230 { "sendall", 4, "send_max"}, 231 { "sendall", 4, "minconf"}, 232 { "sendall", 4, "maxconf"}, 233 { "sendall", 4, "conf_target"}, 234 { "sendall", 4, "replaceable"}, 235 { "sendall", 4, "solving_data"}, 236 { "simulaterawtransaction", 0, "rawtxs" }, 237 { "simulaterawtransaction", 1, "options" }, 238 { "simulaterawtransaction", 1, "include_watchonly"}, 239 { "importprivkey", 2, "rescan" }, 240 { "importaddress", 2, "rescan" }, 241 { "importaddress", 3, "p2sh" }, 242 { "importpubkey", 2, "rescan" }, 243 { "importmempool", 1, "options" }, 244 { "importmempool", 1, "apply_fee_delta_priority" }, 245 { "importmempool", 1, "use_current_time" }, 246 { "importmempool", 1, "apply_unbroadcast_set" }, 247 { "importmulti", 0, "requests" }, 248 { "importmulti", 1, "options" }, 249 { "importmulti", 1, "rescan" }, 250 { "importdescriptors", 0, "requests" }, 251 { "listdescriptors", 0, "private" }, 252 { "verifychain", 0, "checklevel" }, 253 { "verifychain", 1, "nblocks" }, 254 { "getblockstats", 0, "hash_or_height" }, 255 { "getblockstats", 1, "stats" }, 256 { "pruneblockchain", 0, "height" }, 257 { "keypoolrefill", 0, "newsize" }, 258 { "getrawmempool", 0, "verbose" }, 259 { "getrawmempool", 1, "mempool_sequence" }, 260 { "getorphantxs", 0, "verbosity" }, 261 { "estimatesmartfee", 0, "conf_target" }, 262 { "estimaterawfee", 0, "conf_target" }, 263 { "estimaterawfee", 1, "threshold" }, 264 { "prioritisetransaction", 1, "dummy" }, 265 { "prioritisetransaction", 2, "fee_delta" }, 266 { "setban", 2, "bantime" }, 267 { "setban", 3, "absolute" }, 268 { "setnetworkactive", 0, "state" }, 269 { "setwalletflag", 1, "value" }, 270 { "getmempoolancestors", 1, "verbose" }, 271 { "getmempooldescendants", 1, "verbose" }, 272 { "gettxspendingprevout", 0, "outputs" }, 273 { "bumpfee", 1, "options" }, 274 { "bumpfee", 1, "conf_target"}, 275 { "bumpfee", 1, "fee_rate"}, 276 { "bumpfee", 1, "replaceable"}, 277 { "bumpfee", 1, "outputs"}, 278 { "bumpfee", 1, "original_change_index"}, 279 { "psbtbumpfee", 1, "options" }, 280 { "psbtbumpfee", 1, "conf_target"}, 281 { "psbtbumpfee", 1, "fee_rate"}, 282 { "psbtbumpfee", 1, "replaceable"}, 283 { "psbtbumpfee", 1, "outputs"}, 284 { "psbtbumpfee", 1, "original_change_index"}, 285 { "logging", 0, "include" }, 286 { "logging", 1, "exclude" }, 287 { "disconnectnode", 1, "nodeid" }, 288 { "upgradewallet", 0, "version" }, 289 { "gethdkeys", 0, "active_only" }, 290 { "gethdkeys", 0, "options" }, 291 { "gethdkeys", 0, "private" }, 292 { "createwalletdescriptor", 1, "options" }, 293 { "createwalletdescriptor", 1, "internal" }, 294 // Echo with conversion (For testing only) 295 { "echojson", 0, "arg0" }, 296 { "echojson", 1, "arg1" }, 297 { "echojson", 2, "arg2" }, 298 { "echojson", 3, "arg3" }, 299 { "echojson", 4, "arg4" }, 300 { "echojson", 5, "arg5" }, 301 { "echojson", 6, "arg6" }, 302 { "echojson", 7, "arg7" }, 303 { "echojson", 8, "arg8" }, 304 { "echojson", 9, "arg9" }, 305 { "rescanblockchain", 0, "start_height"}, 306 { "rescanblockchain", 1, "stop_height"}, 307 { "createwallet", 1, "disable_private_keys"}, 308 { "createwallet", 2, "blank"}, 309 { "createwallet", 4, "avoid_reuse"}, 310 { "createwallet", 5, "descriptors"}, 311 { "createwallet", 6, "load_on_startup"}, 312 { "createwallet", 7, "external_signer"}, 313 { "restorewallet", 2, "load_on_startup"}, 314 { "loadwallet", 1, "load_on_startup"}, 315 { "unloadwallet", 1, "load_on_startup"}, 316 { "getnodeaddresses", 0, "count"}, 317 { "addpeeraddress", 1, "port"}, 318 { "addpeeraddress", 2, "tried"}, 319 { "sendmsgtopeer", 0, "peer_id" }, 320 { "stop", 0, "wait" }, 321 { "addnode", 2, "v2transport" }, 322 { "addconnection", 2, "v2transport" }, 323 }; 324 // clang-format on 325 326 /** Parse string to UniValue or throw runtime_error if string contains invalid JSON */ 327 static UniValue Parse(std::string_view raw) 328 { 329 UniValue parsed; 330 if (!parsed.read(raw)) throw std::runtime_error(tfm::format("Error parsing JSON: %s", raw)); 331 return parsed; 332 } 333 334 class CRPCConvertTable 335 { 336 private: 337 std::set<std::pair<std::string, int>> members; 338 std::set<std::pair<std::string, std::string>> membersByName; 339 340 public: 341 CRPCConvertTable(); 342 343 /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */ 344 UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, int param_idx) 345 { 346 return members.count({method, param_idx}) > 0 ? Parse(arg_value) : arg_value; 347 } 348 349 /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */ 350 UniValue ArgToUniValue(std::string_view arg_value, const std::string& method, const std::string& param_name) 351 { 352 return membersByName.count({method, param_name}) > 0 ? Parse(arg_value) : arg_value; 353 } 354 }; 355 356 CRPCConvertTable::CRPCConvertTable() 357 { 358 for (const auto& cp : vRPCConvertParams) { 359 members.emplace(cp.methodName, cp.paramIdx); 360 membersByName.emplace(cp.methodName, cp.paramName); 361 } 362 } 363 364 static CRPCConvertTable rpcCvtTable; 365 366 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams) 367 { 368 UniValue params(UniValue::VARR); 369 370 for (unsigned int idx = 0; idx < strParams.size(); idx++) { 371 std::string_view value{strParams[idx]}; 372 params.push_back(rpcCvtTable.ArgToUniValue(value, strMethod, idx)); 373 } 374 375 return params; 376 } 377 378 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams) 379 { 380 UniValue params(UniValue::VOBJ); 381 UniValue positional_args{UniValue::VARR}; 382 383 for (std::string_view s: strParams) { 384 size_t pos = s.find('='); 385 if (pos == std::string::npos) { 386 positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size())); 387 continue; 388 } 389 390 std::string name{s.substr(0, pos)}; 391 std::string_view value{s.substr(pos+1)}; 392 393 // Intentionally overwrite earlier named values with later ones as a 394 // convenience for scripts and command line users that want to merge 395 // options. 396 params.pushKV(name, rpcCvtTable.ArgToUniValue(value, strMethod, name)); 397 } 398 399 if (!positional_args.empty()) { 400 // Use pushKVEnd instead of pushKV to avoid overwriting an explicit 401 // "args" value with an implicit one. Let the RPC server handle the 402 // request as given. 403 params.pushKVEnd("args", std::move(positional_args)); 404 } 405 406 return params; 407 }