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-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{
14public:
15 std::string methodName;
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 { "gettxoutsetinfo", 1, "hash_or_height" },
131 { "gettxoutsetinfo", 2, "use_index"},
132 { "lockunspent", 0, "unlock" },
133 { "lockunspent", 1, "transactions" },
134 { "lockunspent", 2, "persistent" },
135 { "send", 0, "outputs" },
136 { "send", 1, "conf_target" },
137 { "send", 3, "fee_rate"},
138 { "send", 4, "options" },
139 { "importprivkey", 2, "rescan" },
140 { "importaddress", 2, "rescan" },
141 { "importaddress", 3, "p2sh" },
142 { "importpubkey", 2, "rescan" },
143 { "importmulti", 0, "requests" },
144 { "importmulti", 1, "options" },
145 { "importdescriptors", 0, "requests" },
146 { "listdescriptors", 0, "private" },
147 { "verifychain", 0, "checklevel" },
148 { "verifychain", 1, "nblocks" },
149 { "getblockstats", 0, "hash_or_height" },
150 { "getblockstats", 1, "stats" },
151 { "pruneblockchain", 0, "height" },
152 { "keypoolrefill", 0, "newsize" },
153 { "getrawmempool", 0, "verbose" },
154 { "getrawmempool", 1, "mempool_sequence" },
155 { "estimatesmartfee", 0, "conf_target" },
156 { "estimaterawfee", 0, "conf_target" },
157 { "estimaterawfee", 1, "threshold" },
158 { "prioritisetransaction", 1, "dummy" },
159 { "prioritisetransaction", 2, "fee_delta" },
160 { "setban", 2, "bantime" },
161 { "setban", 3, "absolute" },
162 { "setnetworkactive", 0, "state" },
163 { "setwalletflag", 1, "value" },
164 { "getmempoolancestors", 1, "verbose" },
165 { "getmempooldescendants", 1, "verbose" },
166 { "bumpfee", 1, "options" },
167 { "psbtbumpfee", 1, "options" },
168 { "logging", 0, "include" },
169 { "logging", 1, "exclude" },
170 { "disconnectnode", 1, "nodeid" },
171 { "upgradewallet", 0, "version" },
172 // Echo with conversion (For testing only)
173 { "echojson", 0, "arg0" },
174 { "echojson", 1, "arg1" },
175 { "echojson", 2, "arg2" },
176 { "echojson", 3, "arg3" },
177 { "echojson", 4, "arg4" },
178 { "echojson", 5, "arg5" },
179 { "echojson", 6, "arg6" },
180 { "echojson", 7, "arg7" },
181 { "echojson", 8, "arg8" },
182 { "echojson", 9, "arg9" },
183 { "rescanblockchain", 0, "start_height"},
184 { "rescanblockchain", 1, "stop_height"},
185 { "createwallet", 1, "disable_private_keys"},
186 { "createwallet", 2, "blank"},
187 { "createwallet", 4, "avoid_reuse"},
188 { "createwallet", 5, "descriptors"},
189 { "createwallet", 6, "load_on_startup"},
190 { "createwallet", 7, "external_signer"},
191 { "restorewallet", 2, "load_on_startup"},
192 { "loadwallet", 1, "load_on_startup"},
193 { "unloadwallet", 1, "load_on_startup"},
194 { "getnodeaddresses", 0, "count"},
195 { "addpeeraddress", 1, "port"},
196 { "addpeeraddress", 2, "tried"},
197 { "stop", 0, "wait" },
198};
199// clang-format on
200
202{
203private:
204 std::set<std::pair<std::string, int>> members;
205 std::set<std::pair<std::string, std::string>> membersByName;
206
207public:
209
210 bool convert(const std::string& method, int idx) {
211 return (members.count(std::make_pair(method, idx)) > 0);
212 }
213 bool convert(const std::string& method, const std::string& name) {
214 return (membersByName.count(std::make_pair(method, name)) > 0);
215 }
216};
217
219{
220 for (const auto& cp : vRPCConvertParams) {
221 members.emplace(cp.methodName, cp.paramIdx);
222 membersByName.emplace(cp.methodName, cp.paramName);
223 }
224}
225
227
231UniValue ParseNonRFCJSONValue(const std::string& strVal)
232{
233 UniValue jVal;
234 if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
235 !jVal.isArray() || jVal.size()!=1)
236 throw std::runtime_error(std::string("Error parsing JSON: ") + strVal);
237 return jVal[0];
238}
239
240UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
241{
242 UniValue params(UniValue::VARR);
243
244 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
245 const std::string& strVal = strParams[idx];
246
247 if (!rpcCvtTable.convert(strMethod, idx)) {
248 // insert string value directly
249 params.push_back(strVal);
250 } else {
251 // parse string as JSON, insert bool/number/object/etc. value
252 params.push_back(ParseNonRFCJSONValue(strVal));
253 }
254 }
255
256 return params;
257}
258
259UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<std::string> &strParams)
260{
261 UniValue params(UniValue::VOBJ);
262
263 for (const std::string &s: strParams) {
264 size_t pos = s.find('=');
265 if (pos == std::string::npos) {
266 throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)"));
267 }
268
269 std::string name = s.substr(0, pos);
270 std::string value = s.substr(pos+1);
271
272 if (!rpcCvtTable.convert(strMethod, name)) {
273 // insert string value directly
274 params.pushKV(name, value);
275 } else {
276 // parse string as JSON, insert bool/number/object/etc. value
277 params.pushKV(name, ParseNonRFCJSONValue(value));
278 }
279 }
280
281 return params;
282}
std::string methodName
method whose params want conversion
Definition: client.cpp:15
int paramIdx
0-based idx of param to convert
Definition: client.cpp:16
std::string paramName
parameter name
Definition: client.cpp:17
bool convert(const std::string &method, int idx)
Definition: client.cpp:210
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:213
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:205
std::set< std::pair< std::string, int > > members
Definition: client.cpp:204
bool isArray() const
Definition: univalue.h:81
@ VOBJ
Definition: univalue.h:19
@ VARR
Definition: univalue.h:19
size_t size() const
Definition: univalue.h:66
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
bool read(const char *raw, size_t len)
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:231
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:240
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:259
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:226
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
const char * name
Definition: rest.cpp:43