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