Bitcoin ABC  0.22.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  {"getblockhash", 0, "height"},
53  {"waitforblockheight", 0, "height"},
54  {"waitforblockheight", 1, "timeout"},
55  {"waitforblock", 1, "timeout"},
56  {"waitfornewblock", 0, "timeout"},
57  {"listtransactions", 1, "count"},
58  {"listtransactions", 2, "skip"},
59  {"listtransactions", 3, "include_watchonly"},
60  {"walletpassphrase", 1, "timeout"},
61  {"getblocktemplate", 0, "template_request"},
62  {"listsinceblock", 1, "target_confirmations"},
63  {"listsinceblock", 2, "include_watchonly"},
64  {"listsinceblock", 3, "include_removed"},
65  {"sendmany", 1, "amounts"},
66  {"sendmany", 2, "minconf"},
67  {"sendmany", 4, "subtractfeefrom"},
68  {"deriveaddresses", 1, "range"},
69  {"scantxoutset", 1, "scanobjects"},
70  {"addmultisigaddress", 0, "nrequired"},
71  {"addmultisigaddress", 1, "keys"},
72  {"createmultisig", 0, "nrequired"},
73  {"createmultisig", 1, "keys"},
74  {"listunspent", 0, "minconf"},
75  {"listunspent", 1, "maxconf"},
76  {"listunspent", 2, "addresses"},
77  {"listunspent", 3, "include_unsafe"},
78  {"listunspent", 4, "query_options"},
79  {"getblock", 1, "verbosity"},
80  {"getblock", 1, "verbose"},
81  {"getblockheader", 1, "verbose"},
82  {"getchaintxstats", 0, "nblocks"},
83  {"gettransaction", 1, "include_watchonly"},
84  {"gettransaction", 2, "verbose"},
85  {"getrawtransaction", 1, "verbose"},
86  {"createrawtransaction", 0, "inputs"},
87  {"createrawtransaction", 1, "outputs"},
88  {"createrawtransaction", 2, "locktime"},
89  {"signrawtransactionwithkey", 1, "privkeys"},
90  {"signrawtransactionwithkey", 2, "prevtxs"},
91  {"signrawtransactionwithwallet", 1, "prevtxs"},
92  {"sendrawtransaction", 1, "maxfeerate"},
93  {"testmempoolaccept", 0, "rawtxs"},
94  {"testmempoolaccept", 1, "maxfeerate"},
95  {"combinerawtransaction", 0, "txs"},
96  {"fundrawtransaction", 1, "options"},
97  {"walletcreatefundedpsbt", 0, "inputs"},
98  {"walletcreatefundedpsbt", 1, "outputs"},
99  {"walletcreatefundedpsbt", 2, "locktime"},
100  {"walletcreatefundedpsbt", 3, "options"},
101  {"walletcreatefundedpsbt", 4, "bip32derivs"},
102  {"walletprocesspsbt", 1, "sign"},
103  {"walletprocesspsbt", 3, "bip32derivs"},
104  {"createpsbt", 0, "inputs"},
105  {"createpsbt", 1, "outputs"},
106  {"createpsbt", 2, "locktime"},
107  {"combinepsbt", 0, "txs"},
108  {"joinpsbts", 0, "txs"},
109  {"finalizepsbt", 1, "extract"},
110  {"converttopsbt", 1, "permitsigdata"},
111  {"gettxout", 1, "n"},
112  {"gettxout", 2, "include_mempool"},
113  {"gettxoutproof", 0, "txids"},
114  {"lockunspent", 0, "unlock"},
115  {"lockunspent", 1, "transactions"},
116  {"importprivkey", 2, "rescan"},
117  {"importaddress", 2, "rescan"},
118  {"importaddress", 3, "p2sh"},
119  {"importpubkey", 2, "rescan"},
120  {"importmulti", 0, "requests"},
121  {"importmulti", 1, "options"},
122  {"importdescriptors", 0, "requests"},
123  {"verifychain", 0, "checklevel"},
124  {"verifychain", 1, "nblocks"},
125  {"getblockstats", 0, "hash_or_height"},
126  {"getblockstats", 1, "stats"},
127  {"pruneblockchain", 0, "height"},
128  {"keypoolrefill", 0, "newsize"},
129  {"getrawmempool", 0, "verbose"},
130  {"estimatefee", 0, "nblocks"},
131  {"prioritisetransaction", 1, "dummy"},
132  {"prioritisetransaction", 2, "fee_delta"},
133  {"setban", 2, "bantime"},
134  {"setban", 3, "absolute"},
135  {"setnetworkactive", 0, "state"},
136  {"setwalletflag", 1, "value"},
137  {"getmempoolancestors", 1, "verbose"},
138  {"getmempooldescendants", 1, "verbose"},
139  {"disconnectnode", 1, "nodeid"},
140  {"logging", 0, "include"},
141  {"logging", 1, "exclude"},
142  {"upgradewallet", 0, "version"},
143  // Echo with conversion (For testing only)
144  {"echojson", 0, "arg0"},
145  {"echojson", 1, "arg1"},
146  {"echojson", 2, "arg2"},
147  {"echojson", 3, "arg3"},
148  {"echojson", 4, "arg4"},
149  {"echojson", 5, "arg5"},
150  {"echojson", 6, "arg6"},
151  {"echojson", 7, "arg7"},
152  {"echojson", 8, "arg8"},
153  {"echojson", 9, "arg9"},
154  {"rescanblockchain", 0, "start_height"},
155  {"rescanblockchain", 1, "stop_height"},
156  {"createwallet", 1, "disable_private_keys"},
157  {"createwallet", 2, "blank"},
158  {"createwallet", 4, "avoid_reuse"},
159  {"createwallet", 5, "descriptors"},
160  {"getnodeaddresses", 0, "count"},
161  {"stop", 0, "wait"},
162  // Avalanche
163  {"addavalanchenode", 0, "nodeid"},
164  {"buildavalancheproof", 0, "sequence"},
165  {"buildavalancheproof", 1, "expiration"},
166  {"buildavalancheproof", 3, "stakes"},
167  // ABC specific RPC
168  {"setexcessiveblock", 0, "blockSize"},
169 };
170 
172 private:
173  std::set<std::pair<std::string, int>> members;
174  std::set<std::pair<std::string, std::string>> membersByName;
175 
176 public:
178 
179  bool convert(const std::string &method, int idx) {
180  return (members.count(std::make_pair(method, idx)) > 0);
181  }
182  bool convert(const std::string &method, const std::string &name) {
183  return (membersByName.count(std::make_pair(method, name)) > 0);
184  }
185 };
186 
188  const unsigned int n_elem =
189  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
190 
191  for (unsigned int i = 0; i < n_elem; i++) {
192  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
193  vRPCConvertParams[i].paramIdx));
194  membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
195  vRPCConvertParams[i].paramName));
196  }
197 }
198 
200 
205 UniValue ParseNonRFCJSONValue(const std::string &strVal) {
206  UniValue jVal;
207  if (!jVal.read(std::string("[") + strVal + std::string("]")) ||
208  !jVal.isArray() || jVal.size() != 1) {
209  throw std::runtime_error(std::string("Error parsing JSON:") + strVal);
210  }
211  return jVal[0];
212 }
213 
214 UniValue RPCConvertValues(const std::string &strMethod,
215  const std::vector<std::string> &strParams) {
216  UniValue params(UniValue::VARR);
217 
218  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
219  const std::string &strVal = strParams[idx];
220 
221  if (!rpcCvtTable.convert(strMethod, idx)) {
222  // insert string value directly
223  params.push_back(strVal);
224  } else {
225  // parse string as JSON, insert bool/number/object/etc. value
226  params.push_back(ParseNonRFCJSONValue(strVal));
227  }
228  }
229 
230  return params;
231 }
232 
233 UniValue RPCConvertNamedValues(const std::string &strMethod,
234  const std::vector<std::string> &strParams) {
235  UniValue params(UniValue::VOBJ);
236 
237  for (const std::string &s : strParams) {
238  size_t pos = s.find('=');
239  if (pos == std::string::npos) {
240  throw(std::runtime_error("No '=' in named argument '" + s +
241  "', this needs to be present for every "
242  "argument (even if it is empty)"));
243  }
244 
245  std::string name = s.substr(0, pos);
246  std::string value = s.substr(pos + 1);
247 
248  if (!rpcCvtTable.convert(strMethod, name)) {
249  // insert string value directly
250  params.pushKV(name, value);
251  } else {
252  // parse string as JSON, insert bool/number/object/etc. value
253  params.pushKV(name, ParseNonRFCJSONValue(value));
254  }
255  }
256 
257  return params;
258 }
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:199
bool read(const char *raw, size_t len)
std::set< std::pair< std::string, int > > members
Definition: client.cpp:173
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:214
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
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:205
const char * name
Definition: rest.cpp:43
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:182
std::string paramName
parameter name
Definition: client.cpp:16
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool convert(const std::string &method, int idx)
Definition: client.cpp:179
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
std::string methodName
method whose params want conversion
Definition: client.cpp:14
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:233
size_t size() const
Definition: univalue.h:80
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:174
bool isArray() const
Definition: univalue.h:95
int paramIdx
0-based idx of param to convert
Definition: client.cpp:15