PRCYCoin  2.0.0.7rc1
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2015 The Dash developers
4 // Copyright (c) 2015-2018 The PIVX developers
5 // Copyright (c) 2018-2020 The DAPS Project developers
6 // Distributed under the MIT software license, see the accompanying
7 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 
9 #include "rpc/client.h"
10 
11 #include "rpc/protocol.h"
12 #include "guiinterface.h"
13 #include "util.h"
14 
15 #include <set>
16 #include <stdint.h>
17 
18 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
19 #include <univalue.h>
20 
21 
23 {
24 public:
25  std::string methodName;
26  int paramIdx;
27 };
28 static const CRPCConvertParam vRPCConvertParams[] =
29  {
30  {"stop", 0},
31  {"setmocktime", 0},
32  {"getaddednodeinfo", 0},
33  {"setgenerate", 0},
34  {"setgenerate", 1},
35  {"generate", 0},
36  {"generatepoa", 0},
37  {"generatepoa", 1},
38  {"getnetworkhashps", 0},
39  {"getnetworkhashps", 1},
40  {"rescan", 0},
41  {"rescanwallettransactions", 0},
42  {"sendtostealthaddress", 1},
43  {"sendtostealthaddress", 2},
44  {"sendalltostealthaddress", 1},
45  {"settxfee", 0},
46  {"getreceivedbyaddress", 1},
47  {"getreceivedbyaccount", 1},
48  {"listreceivedbyaddress", 0},
49  {"listreceivedbyaddress", 1},
50  {"listreceivedbyaddress", 2},
51  {"listreceivedbyaccount", 0},
52  {"listreceivedbyaccount", 1},
53  {"listreceivedbyaccount", 2},
54  {"getbalance", 1},
55  {"getbalances", 0},
56  {"getbalance", 2},
57  {"getblockhash", 0},
58  { "waitforblockheight", 0 },
59  { "waitforblockheight", 1 },
60  { "waitforblock", 1 },
61  { "waitforblock", 2 },
62  { "waitfornewblock", 0 },
63  { "waitfornewblock", 1 },
64  {"setmaxreorgdepth", 0},
65  {"resyncfrom", 0},
66  {"setdecoyconfirmation", 0},
67  {"getrawtransactionbyblockheight", 0},
68  {"move", 2},
69  {"move", 3},
70  {"sendfrom", 2},
71  {"sendfrom", 3},
72  {"listtransactions", 1},
73  {"listtransactions", 2},
74  {"listtransactions", 3},
75  {"listtransactionsbypaymentid", 0},
76  {"listtransactionsbypaymentid", 1},
77  {"listtransactionsbypaymentid", 2},
78  {"listtransactionsbypaymentid", 3},
79  {"listaccounts", 0},
80  {"listaccounts", 1},
81  {"unlockwallet", 1},
82  {"unlockwallet", 2},
83  {"getblocktemplate", 0},
84  {"getpoablocktemplate", 0},
85  {"setminingnbits", 0},
86  {"setminingnbits", 1},
87  {"generateintegratedaddress", 0},
88  {"generateintegratedaddress", 1},
89  {"listsinceblock", 1},
90  {"listsinceblock", 2},
91  {"sendmany", 1},
92  {"sendmany", 2},
93  {"addmultisigaddress", 0},
94  {"addmultisigaddress", 1},
95  {"createmultisig", 0},
96  {"createmultisig", 1},
97  {"listunspent", 0},
98  {"listunspent", 1},
99  {"listunspent", 2},
100  {"logging", 0},
101  {"logging", 1},
102  {"getblock", 1},
103  {"getblockheader", 1},
104  {"getblockindexstats", 0},
105  {"getblockindexstats", 1},
106  {"getblockindexstats", 2},
107  {"gettransaction", 1},
108  {"getrawtransaction", 1},
109  {"createrawtransaction", 0},
110  {"createrawtransaction", 1},
111  {"createrawtransaction", 2},
112  {"signrawtransaction", 1},
113  {"signrawtransaction", 2},
114  {"sendrawtransaction", 1},
115  {"gettxout", 1},
116  {"gettxout", 2},
117  {"getunspentcount", 0},
118  {"getunspentcount", 1},
119  {"getunspentcount", 2},
120  {"lockunspent", 0},
121  {"lockunspent", 1},
122  {"importprivkey", 2},
123  {"importaddress", 2},
124  {"verifychain", 0},
125  {"verifychain", 1},
126  {"keypoolrefill", 0},
127  {"getrawmempool", 0},
128  {"estimatefee", 0},
129  {"getfeeinfo", 0},
130  {"estimatepriority", 0},
131  {"prioritisetransaction", 1},
132  {"prioritisetransaction", 2},
133  {"setban", 2},
134  {"setban", 3},
135  {"preparebudget", 2},
136  {"preparebudget", 3},
137  {"preparebudget", 5},
138  {"submitbudget", 2},
139  {"submitbudget", 3},
140  {"submitbudget", 5},
141  {"submitbudget", 7},
142  // disabled until removal of the legacy 'masternode' command
143  //{"startmasternode", 1},
144  {"mnvoteraw", 1},
145  {"mnvoteraw", 4},
146  {"reservebalance", 0},
147  {"reservebalance", 1},
148  {"setstakesplitthreshold", 0},
149  {"autocombinedust", 0},
150  {"autocombinedust", 1}
151  };
152 
154 {
155 private:
156  std::set<std::pair<std::string, int> > members;
157 
158 public:
160 
161  bool convert(const std::string& method, int idx)
162  {
163  return (members.count(std::make_pair(method, idx)) > 0);
164  }
165 };
166 
168 {
169  const unsigned int n_elem =
170  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
171 
172  for (unsigned int i = 0; i < n_elem; i++) {
173  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
174  vRPCConvertParams[i].paramIdx));
175  }
176 }
177 
178 static CRPCConvertTable rpcCvtTable;
179 
183 UniValue ParseNonRFCJSONValue(const std::string& strVal)
184 {
185  UniValue jVal;
186  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
187  !jVal.isArray() || jVal.size()!=1)
188  throw std::runtime_error(std::string("Error parsing JSON:")+strVal);
189  return jVal[0];
190 }
191 
193 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
194 {
195  UniValue params(UniValue::VARR);
196 
197  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
198  const std::string& strVal = strParams[idx];
199 
200  if (!rpcCvtTable.convert(strMethod, idx)) {
201  // insert string value directly
202  params.push_back(strVal);
203  } else {
204  // parse string as JSON, insert bool/number/object/etc. value
205  params.push_back(ParseNonRFCJSONValue(strVal));
206  }
207  }
208 
209  return params;
210 }
CRPCConvertParam::methodName
std::string methodName
Definition: client.cpp:25
CRPCConvertTable::convert
bool convert(const std::string &method, int idx)
Definition: client.cpp:161
CRPCConvertTable::members
std::set< std::pair< std::string, int > > members
Definition: client.cpp:156
protocol.h
UniValue::read
bool read(const char *raw, size_t len)
Definition: univalue_read.cpp:253
RPCConvertValues
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Convert strings to command-specific RPC representation.
Definition: client.cpp:193
guiinterface.h
UniValue
Definition: univalue.h:19
CRPCConvertTable
Definition: client.cpp:153
univalue.h
UniValue::isArray
bool isArray() const
Definition: univalue.h:83
CRPCConvertParam::paramIdx
int paramIdx
method whose params want conversion
Definition: client.cpp:26
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:173
CRPCConvertParam
Definition: client.cpp:22
UniValue::size
size_t size() const
Definition: univalue.h:69
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:183
client.h
CRPCConvertTable::CRPCConvertTable
CRPCConvertTable()
Definition: client.cpp:167