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