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.
8 #include "rpcprotocol.h"
18 class CRPCConvertParam
21 std::string methodName; //! method whose params want conversion
22 int paramIdx; //! 0-based idx of param to convert
25 static const CRPCConvertParam vRPCConvertParams[] =
29 { "getaddednodeinfo", 0 },
33 { "getnetworkhashps", 0 },
34 { "getnetworkhashps", 1 },
35 { "sendtoaddress", 1 },
36 { "sendtoaddress", 4 },
38 { "getreceivedbyaddress", 1 },
39 { "getreceivedbyaccount", 1 },
40 { "listreceivedbyaddress", 0 },
41 { "listreceivedbyaddress", 1 },
42 { "listreceivedbyaddress", 2 },
43 { "listreceivedbyaccount", 0 },
44 { "listreceivedbyaccount", 1 },
45 { "listreceivedbyaccount", 2 },
48 { "getblockhash", 0 },
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 },
65 { "addmultisigaddress", 0 },
66 { "addmultisigaddress", 1 },
67 { "createmultisig", 0 },
68 { "createmultisig", 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 },
84 { "gettxoutproof", 0 },
87 { "importprivkey", 2 },
88 { "importaddress", 2 },
91 { "keypoolrefill", 0 },
92 { "getrawmempool", 0 },
94 { "estimatepriority", 0 },
95 { "prioritisetransaction", 1 },
96 { "prioritisetransaction", 2 },
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},
119 { "z_shieldcoinbase", 2},
120 { "z_shieldcoinbase", 3},
121 { "z_getoperationstatus", 0},
122 { "z_getoperationresult", 0},
123 { "z_importkey", 1 },
133 { "z_importkey", 2 },
134 { "z_importviewingkey", 2 },
135 { "z_getpaymentdisclosure", 1},
136 { "z_getpaymentdisclosure", 2}
139 class CRPCConvertTable
142 std::set<std::pair<std::string, int> > members;
147 bool convert(const std::string& method, int idx) {
148 return (members.count(std::make_pair(method, idx)) > 0);
152 CRPCConvertTable::CRPCConvertTable()
154 const unsigned int n_elem =
155 (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
157 for (unsigned int i = 0; i < n_elem; i++) {
158 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
159 vRPCConvertParams[i].paramIdx));
163 static CRPCConvertTable rpcCvtTable;
165 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
166 * as well as objects and arrays.
168 UniValue ParseNonRFCJSONValue(const std::string& strVal)
171 if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
172 !jVal.isArray() || jVal.size()!=1)
173 throw runtime_error(string("Error JSON:")+strVal);
177 /** Convert strings to command-specific RPC representation */
178 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
180 UniValue params(UniValue::VARR);
182 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
183 const std::string& strVal = strParams[idx];
185 if (!rpcCvtTable.convert(strMethod, idx)) {
186 // insert string value directly
187 params.push_back(strVal);
189 // parse string as JSON, insert bool/number/object/etc. value
190 params.push_back(ParseNonRFCJSONValue(strVal));