Raven Core  3.0.0
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "rpc/client.h"
8 #include "rpc/protocol.h"
9 #include "util.h"
10 
11 #include <set>
12 #include <stdint.h>
13 
14 #include <univalue.h>
15 
17 {
18 public:
19  std::string methodName;
20  int paramIdx;
21  std::string paramName;
22 };
23 
30 static const CRPCConvertParam vRPCConvertParams[] =
31 {
32  { "issue", 1, "qty" },
33  { "issue", 4, "units" },
34  { "issue", 5, "reissuable" },
35  { "issue", 6, "has_ipfs" },
36  { "issuerestrictedasset", 1, "qty" },
37  { "issuerestrictedasset", 5, "units" },
38  { "issuerestrictedasset", 6, "reissuable" },
39  { "issuerestrictedasset", 7, "has_ipfs" },
40  { "reissuerestrictedasset", 1, "qty" },
41  { "reissuerestrictedasset", 3, "change_verifier" },
42  { "reissuerestrictedasset", 6, "new_unit" },
43  { "reissuerestrictedasset", 7, "reissuable" },
44  { "issueunique", 1, "asset_tags"},
45  { "issueunique", 2, "ipfs_hashes"},
46  { "transfer", 1, "qty"},
47  { "transfer", 4, "expire_time"},
48  { "transferfromaddress", 2, "qty"},
49  { "transferfromaddress", 5, "expire_time"},
50  { "transferfromaddresses", 1, "from_addresses"},
51  { "transferfromaddresses", 2, "qty"},
52  { "transferfromaddresses", 5, "expire_time"},
53  { "transferqualifier", 2, "qty"},
54  { "transferqualifier", 5, "expire_time"},
55  { "reissue", 1, "qty"},
56  { "reissue", 4, "reissuable"},
57  { "reissue", 5, "new_unit"},
58  { "listmyassets", 1, "verbose" },
59  { "listmyassets", 2, "count" },
60  { "listmyassets", 3, "start"},
61  { "listmyassets", 4, "confs"},
62  { "listassets", 1, "verbose" },
63  { "listassets", 2, "count" },
64  { "listassets", 3, "start" },
65  { "setmocktime", 0, "timestamp" },
66  { "generate", 0, "nblocks" },
67  { "generate", 1, "maxtries" },
68  { "setgenerate", 0, "generate" },
69  { "setgenerate", 1, "genproclimit" },
70  { "generatetoaddress", 0, "nblocks" },
71  { "generatetoaddress", 2, "maxtries" },
72  { "getnetworkhashps", 0, "nblocks" },
73  { "getnetworkhashps", 1, "height" },
74  { "sendtoaddress", 1, "amount" },
75  { "sendtoaddress", 4, "subtractfeefromamount" },
76 // { "sendtoaddress", 5 , "replaceable" },
77  { "sendtoaddress", 5 , "conf_target" },
78  { "settxfee", 0, "amount" },
79  { "getreceivedbyaddress", 1, "minconf" },
80  { "getreceivedbyaccount", 1, "minconf" },
81  { "listreceivedbyaddress", 0, "minconf" },
82  { "listreceivedbyaddress", 1, "include_empty" },
83  { "listreceivedbyaddress", 2, "include_watchonly" },
84  { "listreceivedbyaccount", 0, "minconf" },
85  { "listreceivedbyaccount", 1, "include_empty" },
86  { "listreceivedbyaccount", 2, "include_watchonly" },
87  { "getbalance", 1, "minconf" },
88  { "getbalance", 2, "include_watchonly" },
89  { "getblockhash", 0, "height" },
90  { "waitforblockheight", 0, "height" },
91  { "waitforblockheight", 1, "timeout" },
92  { "waitforblock", 1, "timeout" },
93  { "waitfornewblock", 0, "timeout" },
94  { "move", 2, "amount" },
95  { "move", 3, "minconf" },
96  { "sendfrom", 2, "amount" },
97  { "sendfrom", 3, "minconf" },
98  { "listtransactions", 1, "count" },
99  { "listtransactions", 2, "skip" },
100  { "listtransactions", 3, "include_watchonly" },
101  { "listaccounts", 0, "minconf" },
102  { "listaccounts", 1, "include_watchonly" },
103  { "walletpassphrase", 1, "timeout" },
104  { "getblocktemplate", 0, "template_request" },
105  { "listsinceblock", 1, "target_confirmations" },
106  { "listsinceblock", 2, "include_watchonly" },
107  { "listsinceblock", 3, "include_removed" },
108  { "sendmany", 1, "amounts" },
109  { "sendmany", 2, "minconf" },
110  { "sendmany", 4, "subtractfeefrom" },
111 // { "sendmany", 5 , "replaceable" },
112  { "sendmany", 5 , "conf_target" },
113  { "addmultisigaddress", 0, "nrequired" },
114  { "addmultisigaddress", 1, "keys" },
115  { "createmultisig", 0, "nrequired" },
116  { "createmultisig", 1, "keys" },
117  { "listunspent", 0, "minconf" },
118  { "listunspent", 1, "maxconf" },
119  { "listunspent", 2, "addresses" },
120  { "listunspent", 3, "include_unsafe" },
121  { "listunspent", 4, "query_options" },
122  { "getblock", 1, "verbosity" },
123  { "getblock", 1, "verbose" },
124  { "getblockheader", 1, "verbose" },
125  { "getchaintxstats", 0, "nblocks" },
126  { "gettransaction", 1, "include_watchonly" },
127  { "getrawtransaction", 1, "verbose" },
128  { "createrawtransaction", 0, "inputs" },
129  { "createrawtransaction", 1, "outputs" },
130  { "createrawtransaction", 2, "locktime" },
131 // { "createrawtransaction", 3, "replaceable" },
132  { "signrawtransaction", 1, "prevtxs" },
133  { "signrawtransaction", 2, "privkeys" },
134  { "sendrawtransaction", 1, "allowhighfees" },
135  { "combinerawtransaction", 0, "txs" },
136  { "fundrawtransaction", 1, "options" },
137  { "gettxout", 1, "n" },
138  { "gettxout", 2, "include_mempool" },
139  { "gettxoutproof", 0, "txids" },
140  { "lockunspent", 0, "unlock" },
141  { "lockunspent", 1, "transactions" },
142  { "importprivkey", 2, "rescan" },
143  { "importaddress", 2, "rescan" },
144  { "importaddress", 3, "p2sh" },
145  { "importpubkey", 2, "rescan" },
146  { "importmulti", 0, "requests" },
147  { "importmulti", 1, "options" },
148  { "verifychain", 0, "checklevel" },
149  { "verifychain", 1, "nblocks" },
150  { "pruneblockchain", 0, "height" },
151  { "keypoolrefill", 0, "newsize" },
152  { "getrawmempool", 0, "verbose" },
153  { "estimatefee", 0, "nblocks" },
154  { "estimatesmartfee", 0, "conf_target" },
155  { "estimaterawfee", 0, "conf_target" },
156  { "estimaterawfee", 1, "threshold" },
157  { "prioritisetransaction", 1, "dummy" },
158  { "prioritisetransaction", 2, "fee_delta" },
159  { "setban", 2, "bantime" },
160  { "setban", 3, "absolute" },
161  { "setnetworkactive", 0, "state" },
162  { "getmempoolancestors", 1, "verbose" },
163  { "getmempooldescendants", 1, "verbose" },
164  { "getblockhashes", 0 , "high"},
165  { "getblockhashes", 1, "low"},
166  { "getblockhashes", 2, "options" },
167  { "getspentinfo", 0, "txid_index"},
168  { "getaddresstxids", 0, "addresses"},
169  { "getaddresstxids", 1, "includeAssets"},
170  { "getaddressbalance", 0, "addresses"},
171  { "getaddressbalance", 1, "includeAssets"},
172  { "getaddressdeltas", 0, "addresses"},
173  { "getaddressutxos", 0, "addresses"},
174  { "getaddressmempool", 0, "addresses"},
175  { "getaddressmempool", 1, "includeAssets"},
176  { "bumpfee", 1, "options" },
177  { "logging", 0, "include" },
178  { "logging", 1, "exclude" },
179  { "disconnectnode", 1, "nodeid" },
180  // Echo with conversion (For testing only)
181  { "echojson", 0, "arg0" },
182  { "echojson", 1, "arg1" },
183  { "echojson", 2, "arg2" },
184  { "echojson", 3, "arg3" },
185  { "echojson", 4, "arg4" },
186  { "echojson", 5, "arg5" },
187  { "echojson", 6, "arg6" },
188  { "echojson", 7, "arg7" },
189  { "echojson", 8, "arg8" },
190  { "echojson", 9, "arg9" },
191  { "rescanblockchain", 0, "start_height"},
192  { "rescanblockchain", 1, "stop_height"},
193  { "listaddressesbyasset", 1, "totalonly"},
194  { "listaddressesbyasset", 2, "count"},
195  { "listaddressesbyasset", 3, "start"},
196  { "listassetbalancesbyaddress", 1, "totalonly"},
197  { "listassetbalancesbyaddress", 2, "count"},
198  { "listassetbalancesbyaddress", 3, "start"},
199  { "sendmessage", 2, "expire_time"},
200 };
201 
203 {
204 private:
205  std::set<std::pair<std::string, int>> members;
206  std::set<std::pair<std::string, std::string>> membersByName;
207 
208 public:
210 
211  bool convert(const std::string& method, int idx) {
212  return (members.count(std::make_pair(method, idx)) > 0);
213  }
214  bool convert(const std::string& method, const std::string& name) {
215  return (membersByName.count(std::make_pair(method, name)) > 0);
216  }
217 };
218 
220 {
221  const unsigned int n_elem =
222  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
223 
224  for (unsigned int i = 0; i < n_elem; i++) {
225  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
226  vRPCConvertParams[i].paramIdx));
227  membersByName.insert(std::make_pair(vRPCConvertParams[i].methodName,
228  vRPCConvertParams[i].paramName));
229  }
230 }
231 
232 static CRPCConvertTable rpcCvtTable;
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 }
bool read(const char *raw, size_t len)
std::set< std::pair< std::string, int > > members
Definition: client.cpp:205
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert positional arguments to command-specific RPC representation.
Definition: client.cpp:246
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
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
bool convert(const std::string &method, const std::string &name)
Definition: client.cpp:214
std::string paramName
parameter name
Definition: client.cpp:21
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:135
bool convert(const std::string &method, int idx)
Definition: client.cpp:211
std::string methodName
method whose params want conversion
Definition: client.cpp:19
UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert named arguments to command-specific RPC representation.
Definition: client.cpp:265
size_t size() const
Definition: univalue.h:70
std::set< std::pair< std::string, std::string > > membersByName
Definition: client.cpp:206
bool isArray() const
Definition: univalue.h:85
int paramIdx
0-based idx of param to convert
Definition: client.cpp:20