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  { "createwallet", 7, "external_signer"},
187  { "loadwallet", 1, "load_on_startup"},
188  { "unloadwallet", 1, "load_on_startup"},
189  { "getnodeaddresses", 0, "count"},
190  { "addpeeraddress", 1, "port"},
191  { "stop", 0, "wait" },
192 };
193 // clang-format on
194 
196 {
197 private:
198  std::set<std::pair<std::string, int>> members;
199  std::set<std::pair<std::string, std::string>> membersByName;
200 
201 public:
203 
204  bool convert(const std::string& method, int idx) {
205  return (members.count(std::make_pair(method, idx)) > 0);
206  }
207  bool convert(const std::string& method, const std::string& name) {
208  return (membersByName.count(std::make_pair(method, name)) > 0);
209  }
210 };
211 
213 {
214  for (const auto& cp : vRPCConvertParams) {
215  members.emplace(cp.methodName, cp.paramIdx);
216  membersByName.emplace(cp.methodName, cp.paramName);
217  }
218 }
219 
221 
225 UniValue ParseNonRFCJSONValue(const std::string& strVal)
226 {
227  UniValue jVal;
228  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
229  !jVal.isArray() || jVal.size()!=1)
230  throw std::runtime_error(std::string("Error parsing JSON: ") + strVal);
231  return jVal[0];
232 }
233 
234 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
235 {
236  UniValue params(UniValue::VARR);
237 
238  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
239  const std::string& strVal = strParams[idx];
240 
241  if (!rpcCvtTable.convert(strMethod, idx)) {
242  // insert string value directly
243  params.push_back(strVal);
244  } else {
245  // parse string as JSON, insert bool/number/object/etc. value
246  params.push_back(ParseNonRFCJSONValue(strVal));
247  }
248  }
249 
250  return params;
251 }
252 
253 UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
254 {
255  UniValue params(UniValue::VOBJ);
256 
257  for (const std::string &s: strParams) {
258  size_t pos = s.find('=');
259  if (pos == std::string::npos) {
260  throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
261  }
262 
263  std::string name = s.substr(0, pos);
264  std::string value = s.substr(pos+1);
265 
266  if (!rpcCvtTable.convert(strMethod, name)) {
267  // insert string value directly
268  params.pushKV(name, value);
269  } else {
270  // parse string as JSON, insert bool/number/object/etc. value
271  params.pushKV(name, ParseNonRFCJSONValue(value));
272  }
273  }
274 
275  return params;
276 }
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:204
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:21
CRPCConvertTable::members
std::set< std::pair< std::string, int > > members
Definition: client.cpp:198
CRPCConvertTable::membersByName
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:199
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:234
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue
Definition: univalue.h:19
CRPCConvertTable
Definition: client.cpp:195
RPCConvertNamedValues
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:253
rpcCvtTable
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:220
UniValue::isArray
bool isArray() const
Definition: univalue.h:83
CRPCConvertParam::paramIdx
int paramIdx
0-based idx of param to convert
Definition: client.cpp:16
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:207
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:68
UniValue::VARR
@ VARR
Definition: univalue.h:21
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:225
client.h
CRPCConvertTable::CRPCConvertTable
CRPCConvertTable()
Definition: client.cpp:212
CRPCConvertParam::paramName
std::string paramName
parameter name
Definition: client.cpp:17