Bitcoin ABC  0.26.3
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 #include <util/system.h>
8 
9 #include <cstdint>
10 #include <set>
11 
13 public:
14  std::string methodName;
15  int paramIdx;
16  std::string paramName;
17 };
18 
26  {"setmocktime", 0, "timestamp"},
27  {"mockscheduler", 0, "delta_time"},
28  {"utxoupdatepsbt", 1, "descriptors"},
29  {"generatetoaddress", 0, "nblocks"},
30  {"generatetoaddress", 2, "maxtries"},
31  {"generatetodescriptor", 0, "num_blocks"},
32  {"generatetodescriptor", 2, "maxtries"},
33  {"generateblock", 1, "transactions"},
34  {"getnetworkhashps", 0, "nblocks"},
35  {"getnetworkhashps", 1, "height"},
36  {"sendtoaddress", 1, "amount"},
37  {"sendtoaddress", 4, "subtractfeefromamount"},
38  {"sendtoaddress", 5, "avoid_reuse"},
39  {"settxfee", 0, "amount"},
40  {"sethdseed", 0, "newkeypool"},
41  {"getreceivedbyaddress", 1, "minconf"},
42  {"getreceivedbylabel", 1, "minconf"},
43  {"listreceivedbyaddress", 0, "minconf"},
44  {"listreceivedbyaddress", 1, "include_empty"},
45  {"listreceivedbyaddress", 2, "include_watchonly"},
46  {"listreceivedbylabel", 0, "minconf"},
47  {"listreceivedbylabel", 1, "include_empty"},
48  {"listreceivedbylabel", 2, "include_watchonly"},
49  {"getbalance", 1, "minconf"},
50  {"getbalance", 2, "include_watchonly"},
51  {"getbalance", 3, "avoid_reuse"},
52  {"getblockfrompeer", 1, "peer_id"},
53  {"getblockhash", 0, "height"},
54  {"waitforblockheight", 0, "height"},
55  {"waitforblockheight", 1, "timeout"},
56  {"waitforblock", 1, "timeout"},
57  {"waitfornewblock", 0, "timeout"},
58  {"listtransactions", 1, "count"},
59  {"listtransactions", 2, "skip"},
60  {"listtransactions", 3, "include_watchonly"},
61  {"walletpassphrase", 1, "timeout"},
62  {"getblocktemplate", 0, "template_request"},
63  {"listsinceblock", 1, "target_confirmations"},
64  {"listsinceblock", 2, "include_watchonly"},
65  {"listsinceblock", 3, "include_removed"},
66  {"sendmany", 1, "amounts"},
67  {"sendmany", 2, "minconf"},
68  {"sendmany", 4, "subtractfeefrom"},
69  {"deriveaddresses", 1, "range"},
70  {"scantxoutset", 1, "scanobjects"},
71  {"addmultisigaddress", 0, "nrequired"},
72  {"addmultisigaddress", 1, "keys"},
73  {"createmultisig", 0, "nrequired"},
74  {"createmultisig", 1, "keys"},
75  {"listunspent", 0, "minconf"},
76  {"listunspent", 1, "maxconf"},
77  {"listunspent", 2, "addresses"},
78  {"listunspent", 3, "include_unsafe"},
79  {"listunspent", 4, "query_options"},
80  {"getblock", 1, "verbosity"},
81  {"getblock", 1, "verbose"},
82  {"getblockheader", 1, "verbose"},
83  {"getchaintxstats", 0, "nblocks"},
84  {"gettransaction", 1, "include_watchonly"},
85  {"gettransaction", 2, "verbose"},
86  {"getrawtransaction", 1, "verbose"},
87  {"createrawtransaction", 0, "inputs"},
88  {"createrawtransaction", 1, "outputs"},
89  {"createrawtransaction", 2, "locktime"},
90  {"signrawtransactionwithkey", 1, "privkeys"},
91  {"signrawtransactionwithkey", 2, "prevtxs"},
92  {"signrawtransactionwithwallet", 1, "prevtxs"},
93  {"sendrawtransaction", 1, "maxfeerate"},
94  {"testmempoolaccept", 0, "rawtxs"},
95  {"testmempoolaccept", 1, "maxfeerate"},
96  {"combinerawtransaction", 0, "txs"},
97  {"fundrawtransaction", 1, "options"},
98  {"walletcreatefundedpsbt", 0, "inputs"},
99  {"walletcreatefundedpsbt", 1, "outputs"},
100  {"walletcreatefundedpsbt", 2, "locktime"},
101  {"walletcreatefundedpsbt", 3, "options"},
102  {"walletcreatefundedpsbt", 4, "bip32derivs"},
103  {"walletprocesspsbt", 1, "sign"},
104  {"walletprocesspsbt", 3, "bip32derivs"},
105  {"createpsbt", 0, "inputs"},
106  {"createpsbt", 1, "outputs"},
107  {"createpsbt", 2, "locktime"},
108  {"combinepsbt", 0, "txs"},
109  {"joinpsbts", 0, "txs"},
110  {"finalizepsbt", 1, "extract"},
111  {"converttopsbt", 1, "permitsigdata"},
112  {"gettxout", 1, "n"},
113  {"gettxout", 2, "include_mempool"},
114  {"gettxoutproof", 0, "txids"},
115  {"gettxoutsetinfo", 1, "hash_or_height"},
116  {"gettxoutsetinfo", 2, "use_index"},
117  {"lockunspent", 0, "unlock"},
118  {"lockunspent", 1, "transactions"},
119  {"send", 0, "outputs"},
120  {"send", 1, "options"},
121  {"importprivkey", 2, "rescan"},
122  {"importaddress", 2, "rescan"},
123  {"importaddress", 3, "p2sh"},
124  {"importpubkey", 2, "rescan"},
125  {"importmulti", 0, "requests"},
126  {"importmulti", 1, "options"},
127  {"importdescriptors", 0, "requests"},
128  {"verifychain", 0, "checklevel"},
129  {"verifychain", 1, "nblocks"},
130  {"getblockstats", 0, "hash_or_height"},
131  {"getblockstats", 1, "stats"},
132  {"pruneblockchain", 0, "height"},
133  {"keypoolrefill", 0, "newsize"},
134  {"getrawmempool", 0, "verbose"},
135  {"getrawmempool", 1, "mempool_sequence"},
136  {"prioritisetransaction", 1, "dummy"},
137  {"prioritisetransaction", 2, "fee_delta"},
138  {"setban", 2, "bantime"},
139  {"setban", 3, "absolute"},
140  {"setnetworkactive", 0, "state"},
141  {"setwalletflag", 1, "value"},
142  {"getmempoolancestors", 1, "verbose"},
143  {"getmempooldescendants", 1, "verbose"},
144  {"disconnectnode", 1, "nodeid"},
145  {"logging", 0, "include"},
146  {"logging", 1, "exclude"},
147  {"upgradewallet", 0, "version"},
148  // Echo with conversion (For testing only)
149  {"echojson", 0, "arg0"},
150  {"echojson", 1, "arg1"},
151  {"echojson", 2, "arg2"},
152  {"echojson", 3, "arg3"},
153  {"echojson", 4, "arg4"},
154  {"echojson", 5, "arg5"},
155  {"echojson", 6, "arg6"},
156  {"echojson", 7, "arg7"},
157  {"echojson", 8, "arg8"},
158  {"echojson", 9, "arg9"},
159  {"rescanblockchain", 0, "start_height"},
160  {"rescanblockchain", 1, "stop_height"},
161  {"createwallet", 1, "disable_private_keys"},
162  {"createwallet", 2, "blank"},
163  {"createwallet", 4, "avoid_reuse"},
164  {"createwallet", 5, "descriptors"},
165  {"createwallet", 6, "load_on_startup"},
166  {"restorewallet", 2, "load_on_startup"},
167  {"loadwallet", 1, "load_on_startup"},
168  {"unloadwallet", 1, "load_on_startup"},
169  {"getnodeaddresses", 0, "count"},
170  {"addpeeraddress", 1, "port"},
171  {"addpeeraddress", 2, "tried"},
172  {"stop", 0, "wait"},
173  // Avalanche
174  {"addavalanchenode", 0, "nodeid"},
175  {"buildavalancheproof", 0, "sequence"},
176  {"buildavalancheproof", 1, "expiration"},
177  {"buildavalancheproof", 3, "stakes"},
178 };
179 
181 private:
182  std::set<std::pair<std::string, int>> members;
183  std::set<std::pair<std::string, std::string>> membersByName;
184 
185 public:
187 
188  bool convert(const std::string &method, int idx) {
189  return (members.count(std::make_pair(method, idx)) > 0);
190  }
191  bool convert(const std::string &method, const std::string &name) {
192  return (membersByName.count(std::make_pair(method, name)) > 0);
193  }
194 };
195 
197  for (const auto &cp : vRPCConvertParams) {
198  members.emplace(cp.methodName, cp.paramIdx);
199  membersByName.emplace(cp.methodName, cp.paramName);
200  }
201 }
202 
204 
209 UniValue ParseNonRFCJSONValue(const std::string &strVal) {
210  UniValue jVal;
211  if (!jVal.read(std::string("[") + strVal + std::string("]")) ||
212  !jVal.isArray() || jVal.size() != 1) {
213  throw std::runtime_error(std::string("Error parsing JSON: ") + strVal);
214  }
215  return jVal[0];
216 }
217 
218 UniValue RPCConvertValues(const std::string &strMethod,
219  const std::vector<std::string> &strParams) {
220  UniValue params(UniValue::VARR);
221 
222  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
223  const std::string &strVal = strParams[idx];
224 
225  if (!rpcCvtTable.convert(strMethod, idx)) {
226  // insert string value directly
227  params.push_back(strVal);
228  } else {
229  // parse string as JSON, insert bool/number/object/etc. value
230  params.push_back(ParseNonRFCJSONValue(strVal));
231  }
232  }
233 
234  return params;
235 }
236 
237 UniValue RPCConvertNamedValues(const std::string &strMethod,
238  const std::vector<std::string> &strParams) {
239  UniValue params(UniValue::VOBJ);
240 
241  for (const std::string &s : strParams) {
242  size_t pos = s.find('=');
243  if (pos == std::string::npos) {
244  throw(std::runtime_error("No '=' in named argument '" + s +
245  "', this needs to be present for every "
246  "argument (even if it is empty)"));
247  }
248 
249  std::string name = s.substr(0, pos);
250  std::string value = s.substr(pos + 1);
251 
252  if (!rpcCvtTable.convert(strMethod, name)) {
253  // insert string value directly
254  params.pushKV(name, value);
255  } else {
256  // parse string as JSON, insert bool/number/object/etc. value
257  params.pushKV(name, ParseNonRFCJSONValue(value));
258  }
259  }
260 
261  return params;
262 }
std::string methodName
method whose params want conversion
Definition: client.cpp:14
int paramIdx
0-based idx of param to convert
Definition: client.cpp:15
std::string paramName
parameter name
Definition: client.cpp:16
bool convert(const std::string &method, int idx)
Definition: client.cpp:188
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:191
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:183
std::set< std::pair< std::string, int > > members
Definition: client.cpp:182
bool isArray() const
Definition: univalue.h:95
@ VOBJ
Definition: univalue.h:27
@ VARR
Definition: univalue.h:27
size_t size() const
Definition: univalue.h:80
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool read(const char *raw, size_t len)
UniValue ParseNonRFCJSONValue(const std::string &strVal)
Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) as well as obje...
Definition: client.cpp:209
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:218
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:237
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:203
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:25
const char * name
Definition: rest.cpp:49