Dash Core  0.12.2.1
P2P Digital Currency
client.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2017 The Dash 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 <boost/algorithm/string/case_conv.hpp> // for to_lower()
15 #include <univalue.h>
16 
17 using namespace std;
18 
20 {
21 public:
22  std::string methodName;
23  int paramIdx;
24 };
26 {
27  { "stop", 0 },
28  { "setmocktime", 0 },
29  { "getaddednodeinfo", 0 },
30  { "setgenerate", 0 },
31  { "setgenerate", 1 },
32  { "generate", 0 },
33  { "getnetworkhashps", 0 },
34  { "getnetworkhashps", 1 },
35  { "sendtoaddress", 1 },
36  { "sendtoaddress", 4 },
37  { "sendtoaddress", 5 },
38  { "sendtoaddress", 6 },
39  { "instantsendtoaddress", 1 },
40  { "instantsendtoaddress", 4 },
41  { "settxfee", 0 },
42  { "getreceivedbyaddress", 1 },
43  { "getreceivedbyaddress", 2 },
44  { "getreceivedbyaccount", 1 },
45  { "getreceivedbyaccount", 2 },
46  { "listreceivedbyaddress", 0 },
47  { "listreceivedbyaddress", 1 },
48  { "listreceivedbyaddress", 2 },
49  { "listreceivedbyaddress", 3 },
50  { "listreceivedbyaccount", 0 },
51  { "listreceivedbyaccount", 1 },
52  { "listreceivedbyaccount", 2 },
53  { "listreceivedbyaccount", 3 },
54  { "getbalance", 1 },
55  { "getbalance", 2 },
56  { "getbalance", 3 },
57  { "getchaintips", 0 },
58  { "getchaintips", 1 },
59  { "getblockhash", 0 },
60  { "getsuperblockbudget", 0 },
61  { "move", 2 },
62  { "move", 3 },
63  { "sendfrom", 2 },
64  { "sendfrom", 3 },
65  { "sendfrom", 4 },
66  { "listtransactions", 1 },
67  { "listtransactions", 2 },
68  { "listtransactions", 3 },
69  { "listaccounts", 0 },
70  { "listaccounts", 1 },
71  { "listaccounts", 2 },
72  { "walletpassphrase", 1 },
73  { "walletpassphrase", 2 },
74  { "getblocktemplate", 0 },
75  { "listsinceblock", 1 },
76  { "listsinceblock", 2 },
77  { "sendmany", 1 },
78  { "sendmany", 2 },
79  { "sendmany", 3 },
80  { "sendmany", 5 },
81  { "sendmany", 6 },
82  { "sendmany", 7 },
83  { "addmultisigaddress", 0 },
84  { "addmultisigaddress", 1 },
85  { "createmultisig", 0 },
86  { "createmultisig", 1 },
87  { "listunspent", 0 },
88  { "listunspent", 1 },
89  { "listunspent", 2 },
90  { "getblock", 1 },
91  { "getblockheader", 1 },
92  { "getblockheaders", 1 },
93  { "getblockheaders", 2 },
94  { "gettransaction", 1 },
95  { "getrawtransaction", 1 },
96  { "createrawtransaction", 0 },
97  { "createrawtransaction", 1 },
98  { "createrawtransaction", 2 },
99  { "signrawtransaction", 1 },
100  { "signrawtransaction", 2 },
101  { "sendrawtransaction", 1 },
102  { "sendrawtransaction", 2 },
103  { "fundrawtransaction", 1 },
104  { "gettxout", 1 },
105  { "gettxout", 2 },
106  { "gettxoutproof", 0 },
107  { "lockunspent", 0 },
108  { "lockunspent", 1 },
109  { "importprivkey", 2 },
110  { "importelectrumwallet", 1 },
111  { "importaddress", 2 },
112  { "importaddress", 3 },
113  { "importpubkey", 2 },
114  { "verifychain", 0 },
115  { "verifychain", 1 },
116  { "keypoolrefill", 0 },
117  { "getrawmempool", 0 },
118  { "estimatefee", 0 },
119  { "estimatepriority", 0 },
120  { "estimatesmartfee", 0 },
121  { "estimatesmartpriority", 0 },
122  { "prioritisetransaction", 1 },
123  { "prioritisetransaction", 2 },
124  { "setban", 2 },
125  { "setban", 3 },
126  { "setnetworkactive", 0 },
127  { "spork", 1 },
128  { "voteraw", 1 },
129  { "voteraw", 5 },
130  { "getblockhashes", 0 },
131  { "getblockhashes", 1 },
132  { "getspentinfo", 0},
133  { "getaddresstxids", 0},
134  { "getaddressbalance", 0},
135  { "getaddressdeltas", 0},
136  { "getaddressutxos", 0},
137  { "getaddressmempool", 0},
138 };
139 
141 {
142 private:
143  std::set<std::pair<std::string, int> > members;
144 
145 public:
147 
148  bool convert(const std::string& method, int idx) {
149  return (members.count(std::make_pair(method, idx)) > 0);
150  }
151 };
152 
154 {
155  const unsigned int n_elem =
156  (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
157 
158  for (unsigned int i = 0; i < n_elem; i++) {
159  members.insert(std::make_pair(vRPCConvertParams[i].methodName,
160  vRPCConvertParams[i].paramIdx));
161  }
162 }
163 
165 
169 UniValue ParseNonRFCJSONValue(const std::string& strVal)
170 {
171  UniValue jVal;
172  if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
173  !jVal.isArray() || jVal.size()!=1)
174  throw runtime_error(string("Error parsing JSON:")+strVal);
175  return jVal[0];
176 }
177 
179 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
180 {
181  UniValue params(UniValue::VARR);
182 
183  for (unsigned int idx = 0; idx < strParams.size(); idx++) {
184  const std::string& strVal = strParams[idx];
185 
186  if (!rpcCvtTable.convert(strMethod, idx)) {
187  // insert string value directly
188  params.push_back(strVal);
189  } else {
190  // parse string as JSON, insert bool/number/object/etc. value
191  params.push_back(ParseNonRFCJSONValue(strVal));
192  }
193  }
194 
195  return params;
196 }
197 
std::set< std::pair< std::string, int > > members
Definition: client.cpp:143
std::string methodName
Definition: client.cpp:22
static CRPCConvertTable rpcCvtTable
Definition: client.cpp:164
bool push_back(const UniValue &val)
Definition: univalue.cpp:176
int paramIdx
method whose params want conversion
Definition: client.cpp:23
UniValue RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
Definition: client.cpp:179
UniValue ParseNonRFCJSONValue(const std::string &strVal)
Definition: client.cpp:169
bool isArray() const
Definition: univalue.h:83
static const CRPCConvertParam vRPCConvertParams[]
Definition: client.cpp:25
bool read(const char *raw)
bool convert(const std::string &method, int idx)
Definition: client.cpp:148
size_t size() const
Definition: univalue.h:69