]> Git Repo - VerusCoin.git/blob - src/rpcclient.cpp
Merge pull request #557 from jl777/kolo-assets-new
[VerusCoin.git] / src / rpcclient.cpp
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 "rpcclient.h"
7
8 #include "rpcprotocol.h"
9 #include "util.h"
10
11 #include <set>
12 #include <stdint.h>
13
14 #include <univalue.h>
15
16 using namespace std;
17
18 class CRPCConvertParam
19 {
20 public:
21     std::string methodName;            //! method whose params want conversion
22     int paramIdx;                      //! 0-based idx of param to convert
23 };
24
25 static const CRPCConvertParam vRPCConvertParams[] =
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     { "settxfee", 0 },
38     { "getreceivedbyaddress", 1 },
39     { "getreceivedbyaccount", 1 },
40     { "listreceivedbyaddress", 0 },
41     { "listreceivedbyaddress", 1 },
42     { "listreceivedbyaddress", 2 },
43     { "listreceivedbyaccount", 0 },
44     { "listreceivedbyaccount", 1 },
45     { "listreceivedbyaccount", 2 },
46     { "getbalance", 1 },
47     { "getbalance", 2 },
48     { "getblockhash", 0 },
49     { "move", 2 },
50     { "move", 3 },
51     { "sendfrom", 2 },
52     { "sendfrom", 3 },
53     { "listtransactions", 1 },
54     { "listtransactions", 2 },
55     { "listtransactions", 3 },
56     { "listaccounts", 0 },
57     { "listaccounts", 1 },
58     { "walletpassphrase", 1 },
59     { "getblocktemplate", 0 },
60     { "listsinceblock", 1 },
61     { "listsinceblock", 2 },
62     { "sendmany", 1 },
63     { "sendmany", 2 },
64     { "sendmany", 4 },
65     { "addmultisigaddress", 0 },
66     { "addmultisigaddress", 1 },
67     { "createmultisig", 0 },
68     { "createmultisig", 1 },
69     { "listunspent", 0 },
70     { "listunspent", 1 },
71     { "listunspent", 2 },
72     { "getblock", 1 },
73     { "getblockheader", 1 },
74     { "gettransaction", 1 },
75     { "getrawtransaction", 1 },
76     { "createrawtransaction", 0 },
77     { "createrawtransaction", 1 },
78     { "signrawtransaction", 1 },
79     { "signrawtransaction", 2 },
80     { "sendrawtransaction", 1 },
81     { "fundrawtransaction", 1 },
82     { "gettxout", 1 },
83     { "gettxout", 2 },
84     { "gettxoutproof", 0 },
85     { "lockunspent", 0 },
86     { "lockunspent", 1 },
87     { "importprivkey", 2 },
88     { "importaddress", 2 },
89     { "verifychain", 0 },
90     { "verifychain", 1 },
91     { "keypoolrefill", 0 },
92     { "getrawmempool", 0 },
93     { "estimatefee", 0 },
94     { "estimatepriority", 0 },
95     { "prioritisetransaction", 1 },
96     { "prioritisetransaction", 2 },
97     { "setban", 2 },
98     { "setban", 3 },
99     { "zcrawjoinsplit", 1 },
100     { "zcrawjoinsplit", 2 },
101     { "zcrawjoinsplit", 3 },
102     { "zcrawjoinsplit", 4 },
103     { "zcbenchmark", 1 },
104     { "zcbenchmark", 2 },
105     { "getblocksubsidy", 0},
106     { "z_listaddresses", 0},
107     { "z_listreceivedbyaddress", 1},
108     { "z_getbalance", 1},
109     { "z_gettotalbalance", 0},
110     { "z_gettotalbalance", 1},
111     { "z_gettotalbalance", 2},
112     { "z_mergetoaddress", 0},
113     { "z_mergetoaddress", 2},
114     { "z_mergetoaddress", 3},
115     { "z_mergetoaddress", 4},
116     { "z_sendmany", 1},
117     { "z_sendmany", 2},
118     { "z_sendmany", 3},
119     { "z_shieldcoinbase", 2},
120     { "z_shieldcoinbase", 3},
121     { "z_getoperationstatus", 0},
122     { "z_getoperationresult", 0},
123     { "z_importkey", 1 },
124     { "paxprice", 4 },
125     { "paxprices", 3 },
126     { "paxpending", 0 },
127     { "notaries", 2 },
128     { "height_MoM", 1 },
129     { "txMoMproof", 1 },
130     { "minerids", 1 },
131     { "kvsearch", 1 },
132     { "kvupdate", 4 },
133     { "z_importkey", 2 },
134     { "z_importviewingkey", 2 },
135     { "z_getpaymentdisclosure", 1},
136     { "z_getpaymentdisclosure", 2}
137 };
138
139 class CRPCConvertTable
140 {
141 private:
142     std::set<std::pair<std::string, int> > members;
143
144 public:
145     CRPCConvertTable();
146
147     bool convert(const std::string& method, int idx) {
148         return (members.count(std::make_pair(method, idx)) > 0);
149     }
150 };
151
152 CRPCConvertTable::CRPCConvertTable()
153 {
154     const unsigned int n_elem =
155         (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
156
157     for (unsigned int i = 0; i < n_elem; i++) {
158         members.insert(std::make_pair(vRPCConvertParams[i].methodName,
159                                       vRPCConvertParams[i].paramIdx));
160     }
161 }
162
163 static CRPCConvertTable rpcCvtTable;
164
165 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
166  * as well as objects and arrays.
167  */
168 UniValue ParseNonRFCJSONValue(const std::string& strVal)
169 {
170     UniValue jVal;
171     if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
172         !jVal.isArray() || jVal.size()!=1)
173         throw runtime_error(string("Error JSON:")+strVal);
174     return jVal[0];
175 }
176
177 /** Convert strings to command-specific RPC representation */
178 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
179 {
180     UniValue params(UniValue::VARR);
181
182     for (unsigned int idx = 0; idx < strParams.size(); idx++) {
183         const std::string& strVal = strParams[idx];
184
185         if (!rpcCvtTable.convert(strMethod, idx)) {
186             // insert string value directly
187             params.push_back(strVal);
188         } else {
189             // parse string as JSON, insert bool/number/object/etc. value
190             params.push_back(ParseNonRFCJSONValue(strVal));
191         }
192     }
193
194     return params;
195 }
196
This page took 0.035036 seconds and 4 git commands to generate.