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