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"
15 using namespace json_spirit;
17 class CRPCConvertParam
20 std::string methodName; //! method whose params want conversion
21 int paramIdx; //! 0-based idx of param to convert
24 static const CRPCConvertParam vRPCConvertParams[] =
28 { "getaddednodeinfo", 0 },
32 { "getnetworkhashps", 0 },
33 { "getnetworkhashps", 1 },
34 { "sendtoaddress", 1 },
35 { "sendtoaddress", 4 },
37 { "getreceivedbyaddress", 1 },
38 { "getreceivedbyaccount", 1 },
39 { "listreceivedbyaddress", 0 },
40 { "listreceivedbyaddress", 1 },
41 { "listreceivedbyaddress", 2 },
42 { "listreceivedbyaccount", 0 },
43 { "listreceivedbyaccount", 1 },
44 { "listreceivedbyaccount", 2 },
47 { "getblockhash", 0 },
52 { "listtransactions", 1 },
53 { "listtransactions", 2 },
54 { "listtransactions", 3 },
55 { "listaccounts", 0 },
56 { "listaccounts", 1 },
57 { "walletpassphrase", 1 },
58 { "getblocktemplate", 0 },
59 { "listsinceblock", 1 },
60 { "listsinceblock", 2 },
64 { "addmultisigaddress", 0 },
65 { "addmultisigaddress", 1 },
66 { "createmultisig", 0 },
67 { "createmultisig", 1 },
72 { "gettransaction", 1 },
73 { "getrawtransaction", 1 },
74 { "createrawtransaction", 0 },
75 { "createrawtransaction", 1 },
76 { "signrawtransaction", 1 },
77 { "signrawtransaction", 2 },
78 { "sendrawtransaction", 1 },
81 { "gettxoutproof", 0 },
84 { "importprivkey", 2 },
85 { "importaddress", 2 },
88 { "keypoolrefill", 0 },
89 { "getrawmempool", 0 },
91 { "estimatepriority", 0 },
92 { "prioritisetransaction", 1 },
93 { "prioritisetransaction", 2 },
99 { "getblocksubsidy", 0}
102 class CRPCConvertTable
105 std::set<std::pair<std::string, int> > members;
110 bool convert(const std::string& method, int idx) {
111 return (members.count(std::make_pair(method, idx)) > 0);
115 CRPCConvertTable::CRPCConvertTable()
117 const unsigned int n_elem =
118 (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
120 for (unsigned int i = 0; i < n_elem; i++) {
121 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
122 vRPCConvertParams[i].paramIdx));
126 static CRPCConvertTable rpcCvtTable;
128 /** Convert strings to command-specific RPC representation */
129 Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
133 for (unsigned int idx = 0; idx < strParams.size(); idx++) {
134 const std::string& strVal = strParams[idx];
136 // insert string value directly
137 if (!rpcCvtTable.convert(strMethod, idx)) {
138 params.push_back(strVal);
141 // parse string as JSON, insert bool/number/object/etc. value
144 if (!read_string(strVal, jVal))
145 throw runtime_error(string("Error parsing JSON:")+strVal);
146 params.push_back(jVal);