Bitcoin ABC  0.24.7
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  {"send", 0, "outputs"},
117  {"send", 1, "options"},
118  {"importprivkey", 2, "rescan"},
119  {"importaddress", 2, "rescan"},
120  {"importaddress", 3, "p2sh"},
121  {"importpubkey", 2, "rescan"},
122  {"importmulti", 0, "requests"},
123  {"importmulti", 1, "options"},
124  {"importdescriptors", 0, "requests"},
125  {"verifychain", 0, "checklevel"},
126  {"verifychain", 1, "nblocks"},
127  {"getblockstats", 0, "hash_or_height"},
128  {"getblockstats", 1, "stats"},
129  {"pruneblockchain", 0, "height"},
130  {"keypoolrefill", 0, "newsize"},
131  {"getrawmempool", 0, "verbose"},
132  {"getrawmempool", 1, "mempool_sequence"},
133  {"prioritisetransaction", 1, "dummy"},
134  {"prioritisetransaction", 2, "fee_delta"},
135  {"setban", 2, "bantime"},
136  {"setban", 3, "absolute"},
137  {"setnetworkactive", 0, "state"},
138  {"setwalletflag", 1, "value"},
139  {"getmempoolancestors", 1, "verbose"},
140  {"getmempooldescendants", 1, "verbose"},
141  {"disconnectnode", 1, "nodeid"},
142  {"logging", 0, "include"},
143  {"logging", 1, "exclude"},
144  {"upgradewallet", 0, "version"},
145  // Echo with conversion (For testing only)
146  {"echojson", 0, "arg0"},
147  {"echojson", 1, "arg1"},
148  {"echojson", 2, "arg2"},
149  {"echojson", 3, "arg3"},
150  {"echojson", 4, "arg4"},
151  {"echojson", 5, "arg5"},
152  {"echojson", 6, "arg6"},
153  {"echojson", 7, "arg7"},
154  {"echojson", 8, "arg8"},
155  {"echojson", 9, "arg9"},
156  {"rescanblockchain", 0, "start_height"},
157  {"rescanblockchain", 1, "stop_height"},
158  {"createwallet", 1, "disable_private_keys"},
159  {"createwallet", 2, "blank"},
160  {"createwallet", 4, "avoid_reuse"},
161  {"createwallet", 5, "descriptors"},
162  {"createwallet", 6, "load_on_startup"},
163  {"loadwallet", 1, "load_on_startup"},
164  {"unloadwallet", 1, "load_on_startup"},
165  {"getnodeaddresses", 0, "count"},
166  {"addpeeraddress", 1, "port"},
167  {"stop", 0, "wait"},
168  // Avalanche
169  {"addavalanchenode", 0, "nodeid"},
170  {"buildavalancheproof", 0, "sequence"},
171  {"buildavalancheproof", 1, "expiration"},
172  {"buildavalancheproof", 3, "stakes"},
173 };
174 
176 private:
177  std::set<std::pair<std::string, int>> members;
178  std::set<std::pair<std::string, std::string>> membersByName;
179 
180 public:
182 
183  bool convert(const std::string &method, int idx) {
184  return (members.count(std::make_pair(method, idx)) > 0);
185  }
186  bool convert(const std::string &method, const std::string &name) {
187  return (membersByName.count(std::make_pair(method, name)) > 0);
188  }
189 };
190 
192  const unsigned int n_elem =
193  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
194 
195  for (unsigned int i = 0; i < n_elem; i++) {
196  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
197  vRPCConvertParams[i].paramIdx));
198  membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
199  vRPCConvertParams[i].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 }
CRPCConvertParam::methodName
std::string methodName
method whose params want conversion
Definition: client.cpp:14
CRPCConvertTable::convert
bool convert(const std::string &method, int idx)
Definition: client.cpp:183
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:27
CRPCConvertTable::members
std::set< std::pair< std::string, int > > members
Definition: client.cpp:177
CRPCConvertTable::membersByName
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:178
UniValue::read
bool read(const char *raw, size_t len)
Definition: univalue_read.cpp:259
RPCConvertValues
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::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue
Definition: univalue.h:23
CRPCConvertTable
Definition: client.cpp:175
RPCConvertNamedValues
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:237
rpcCvtTable
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:203
UniValue::isArray
bool isArray() const
Definition: univalue.h:95
CRPCConvertParam::paramIdx
int paramIdx
0-based idx of param to convert
Definition: client.cpp:15
name
const char * name
Definition: rest.cpp:43
system.h
CRPCConvertTable::convert
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:186
vRPCConvertParams
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
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
CRPCConvertParam
Definition: client.cpp:12
UniValue::size
size_t size() const
Definition: univalue.h:80
UniValue::VARR
@ VARR
Definition: univalue.h:27
ParseNonRFCJSONValue
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
client.h
CRPCConvertTable::CRPCConvertTable
CRPCConvertTable()
Definition: client.cpp:191
CRPCConvertParam::paramName
std::string paramName
parameter name
Definition: client.cpp:16