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