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.
6 #include "rpc/client.h"
7 #include "rpc/protocol.h"
17 class CRPCConvertParam
20 std::string methodName; //! method whose params want conversion
21 int paramIdx; //! 0-based idx of param to convert
24 // dummy for linking - used only in server
25 UniValue RPCCall(const string& strMethod, const UniValue& params, const string credentials, int port, const string host, int timeout)
27 UniValue RPCCallRoot(const string& strMethod, const UniValue& params, int timeout)
28 {return NullUniValue;}
29 bool SetThisChain(UniValue &chainDefinition)
32 static const CRPCConvertParam vRPCConvertParams[] =
36 { "getaddednodeinfo", 0 },
40 { "getnetworkhashps", 0 },
41 { "getnetworkhashps", 1 },
42 { "getnetworksolps", 0 },
43 { "getnetworksolps", 1 },
44 { "sendtoaddress", 1 },
45 { "sendtoaddress", 4 },
47 { "getreceivedbyaddress", 1 },
48 { "getreceivedbyaccount", 1 },
49 { "listreceivedbyaddress", 0 },
50 { "listreceivedbyaddress", 1 },
51 { "listreceivedbyaddress", 2 },
52 { "listreceivedbyaccount", 0 },
53 { "listreceivedbyaccount", 1 },
54 { "listreceivedbyaccount", 2 },
57 { "getblockhash", 0 },
62 { "listtransactions", 1 },
63 { "listtransactions", 2 },
64 { "listtransactions", 3 },
65 { "listaccounts", 0 },
66 { "listaccounts", 1 },
67 { "walletpassphrase", 1 },
68 { "getblocktemplate", 0 },
69 { "listsinceblock", 1 },
70 { "listsinceblock", 2 },
74 { "addmultisigaddress", 0 },
75 { "addmultisigaddress", 1 },
76 { "createmultisig", 0 },
77 { "createmultisig", 1 },
82 { "getblockheader", 1 },
83 { "gettransaction", 1 },
84 { "getrawtransaction", 1 },
85 { "createrawtransaction", 0 },
86 { "createrawtransaction", 1 },
87 { "createrawtransaction", 2 },
88 { "createrawtransaction", 3 },
89 { "signrawtransaction", 1 },
90 { "signrawtransaction", 2 },
91 { "sendrawtransaction", 1 },
92 { "fundrawtransaction", 1 },
95 { "gettxoutproof", 0 },
98 { "importprivkey", 2 },
99 { "importaddress", 2 },
100 { "verifychain", 0 },
101 { "verifychain", 1 },
102 { "keypoolrefill", 0 },
103 { "getrawmempool", 0 },
104 { "estimatefee", 0 },
105 { "estimatepriority", 0 },
106 { "prioritisetransaction", 1 },
107 { "prioritisetransaction", 2 },
110 { "getblockhashes", 0 },
111 { "getblockhashes", 1 },
112 { "getblockhashes", 2 },
113 { "getspentinfo", 0},
114 { "getaddresstxids", 0},
115 { "getaddressbalance", 0},
116 { "getaddressdeltas", 0},
117 { "getaddressutxos", 0},
118 { "getaddressmempool", 0},
119 { "zcrawjoinsplit", 1 },
120 { "zcrawjoinsplit", 2 },
121 { "zcrawjoinsplit", 3 },
122 { "zcrawjoinsplit", 4 },
123 { "zcbenchmark", 1 },
124 { "zcbenchmark", 2 },
125 { "getblocksubsidy", 0},
126 { "z_listaddresses", 0},
127 { "z_listreceivedbyaddress", 1},
128 { "z_listunspent", 0 },
129 { "z_listunspent", 1 },
130 { "z_listunspent", 2 },
131 { "z_listunspent", 3 },
132 { "z_getbalance", 1},
133 { "z_gettotalbalance", 0},
134 { "z_gettotalbalance", 1},
135 { "z_gettotalbalance", 2},
136 { "z_mergetoaddress", 0},
137 { "z_mergetoaddress", 2},
138 { "z_mergetoaddress", 3},
139 { "z_mergetoaddress", 4},
143 { "z_shieldcoinbase", 2},
144 { "z_shieldcoinbase", 3},
145 { "z_getoperationstatus", 0},
146 { "z_getoperationresult", 0},
147 { "z_importkey", 1 },
155 { "z_importkey", 2 },
156 { "z_importviewingkey", 2 },
157 { "z_getpaymentdisclosure", 1},
158 { "z_getpaymentdisclosure", 2},
160 { "assetchainproof", 1},
161 { "crosschainproof", 1},
162 { "getproofroot", 2},
167 { "getdefinedchains", 0},
170 class CRPCConvertTable
173 std::set<std::pair<std::string, int> > members;
178 bool convert(const std::string& method, int idx) {
179 return (members.count(std::make_pair(method, idx)) > 0);
183 CRPCConvertTable::CRPCConvertTable()
185 const unsigned int n_elem =
186 (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
188 for (unsigned int i = 0; i < n_elem; i++) {
189 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
190 vRPCConvertParams[i].paramIdx));
194 static CRPCConvertTable rpcCvtTable;
196 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
197 * as well as objects and arrays.
199 UniValue ParseNonRFCJSONValue(const std::string& strVal)
202 if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
203 !jVal.isArray() || jVal.size()!=1)
204 throw runtime_error(string("Error JSON:")+strVal);
208 /** Convert strings to command-specific RPC representation */
209 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
211 UniValue params(UniValue::VARR);
213 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
214 const std::string& strVal = strParams[idx];
215 if (!rpcCvtTable.convert(strMethod, idx)) {
216 // insert string value directly
217 params.push_back(strVal);
219 // parse string as JSON, insert bool/number/object/etc. value
220 params.push_back(ParseNonRFCJSONValue(strVal));