Bitcoin ABC  0.28.12
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  {"createwallettransaction", 1, "amount"},
174  // Avalanche
175  {"addavalanchenode", 0, "nodeid"},
176  {"buildavalancheproof", 0, "sequence"},
177  {"buildavalancheproof", 1, "expiration"},
178  {"buildavalancheproof", 3, "stakes"},
179  {"getremoteproofs", 0, "nodeid"},
180  {"getstakingreward", 1, "recompute"},
181  {"setstakingreward", 2, "append"},
182 };
183 
185 private:
186  std::set<std::pair<std::string, int>> members;
187  std::set<std::pair<std::string, std::string>> membersByName;
188 
189 public:
191 
192  bool convert(const std::string &method, int idx) {
193  return (members.count(std::make_pair(method, idx)) > 0);
194  }
195  bool convert(const std::string &method, const std::string &name) {
196  return (membersByName.count(std::make_pair(method, name)) > 0);
197  }
198 };
199 
201  for (const auto &cp : vRPCConvertParams) {
202  members.emplace(cp.methodName, cp.paramIdx);
203  membersByName.emplace(cp.methodName, cp.paramName);
204  }
205 }
206 
208 
213 UniValue ParseNonRFCJSONValue(const std::string &strVal) {
214  UniValue jVal;
215  if (!jVal.read(std::string("[") + strVal + std::string("]")) ||
216  !jVal.isArray() || jVal.size() != 1) {
217  throw std::runtime_error(std::string("Error parsing JSON: ") + strVal);
218  }
219  return jVal[0];
220 }
221 
222 UniValue RPCConvertValues(const std::string &strMethod,
223  const std::vector<std::string> &strParams) {
224  UniValue params(UniValue::VARR);
225 
226  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
227  const std::string &strVal = strParams[idx];
228 
229  if (!rpcCvtTable.convert(strMethod, idx)) {
230  // insert string value directly
231  params.push_back(strVal);
232  } else {
233  // parse string as JSON, insert bool/number/object/etc. value
234  params.push_back(ParseNonRFCJSONValue(strVal));
235  }
236  }
237 
238  return params;
239 }
240 
241 UniValue RPCConvertNamedValues(const std::string &strMethod,
242  const std::vector<std::string> &strParams) {
243  UniValue params(UniValue::VOBJ);
244 
245  for (const std::string &s : strParams) {
246  size_t pos = s.find('=');
247  if (pos == std::string::npos) {
248  throw(std::runtime_error("No '=' in named argument '" + s +
249  "', this needs to be present for every "
250  "argument (even if it is empty)"));
251  }
252 
253  std::string name = s.substr(0, pos);
254  std::string value = s.substr(pos + 1);
255 
256  if (!rpcCvtTable.convert(strMethod, name)) {
257  // insert string value directly
258  params.pushKV(name, value);
259  } else {
260  // parse string as JSON, insert bool/number/object/etc. value
261  params.pushKV(name, ParseNonRFCJSONValue(value));
262  }
263  }
264 
265  return params;
266 }
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:192
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:195
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:187
std::set< std::pair< std::string, int > > members
Definition: client.cpp:186
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:213
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:222
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:241
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:207
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:48