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