client.cpp raw

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