Line data Source code
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 <boost/algorithm/string/case_conv.hpp> // for to_lower()
15 : #include <univalue.h>
16 :
17 : using namespace std;
18 :
19 13912 : class CRPCConvertParam
20 : {
21 : public:
22 : std::string methodName; //! method whose params want conversion
23 : int paramIdx; //! 0-based idx of param to convert
24 : };
25 :
26 282 : static const CRPCConvertParam vRPCConvertParams[] =
27 : {
28 : { "stop", 0 },
29 : { "setmocktime", 0 },
30 : { "getaddednodeinfo", 0 },
31 : { "setgenerate", 0 },
32 : { "setgenerate", 1 },
33 : { "generate", 0 },
34 : { "getnetworkhashps", 0 },
35 : { "getnetworkhashps", 1 },
36 : { "sendtoaddress", 1 },
37 : { "sendtoaddress", 4 },
38 : { "settxfee", 0 },
39 : { "getreceivedbyaddress", 1 },
40 : { "getreceivedbyaccount", 1 },
41 : { "listreceivedbyaddress", 0 },
42 : { "listreceivedbyaddress", 1 },
43 : { "listreceivedbyaddress", 2 },
44 : { "listreceivedbyaccount", 0 },
45 : { "listreceivedbyaccount", 1 },
46 : { "listreceivedbyaccount", 2 },
47 : { "getbalance", 1 },
48 : { "getbalance", 2 },
49 : { "getblockhash", 0 },
50 : { "move", 2 },
51 : { "move", 3 },
52 : { "sendfrom", 2 },
53 : { "sendfrom", 3 },
54 : { "listtransactions", 1 },
55 : { "listtransactions", 2 },
56 : { "listtransactions", 3 },
57 : { "listaccounts", 0 },
58 : { "listaccounts", 1 },
59 : { "walletpassphrase", 1 },
60 : { "getblocktemplate", 0 },
61 : { "listsinceblock", 1 },
62 : { "listsinceblock", 2 },
63 : { "sendmany", 1 },
64 : { "sendmany", 2 },
65 : { "sendmany", 4 },
66 : { "addmultisigaddress", 0 },
67 : { "addmultisigaddress", 1 },
68 : { "createmultisig", 0 },
69 : { "createmultisig", 1 },
70 : { "listunspent", 0 },
71 : { "listunspent", 1 },
72 : { "listunspent", 2 },
73 : { "getblock", 1 },
74 : { "getblockheader", 1 },
75 : { "gettransaction", 1 },
76 : { "getrawtransaction", 1 },
77 : { "createrawtransaction", 0 },
78 : { "createrawtransaction", 1 },
79 : { "signrawtransaction", 1 },
80 : { "signrawtransaction", 2 },
81 : { "sendrawtransaction", 1 },
82 : { "fundrawtransaction", 1 },
83 : { "gettxout", 1 },
84 : { "gettxout", 2 },
85 : { "gettxoutproof", 0 },
86 : { "lockunspent", 0 },
87 : { "lockunspent", 1 },
88 : { "importprivkey", 2 },
89 : { "importaddress", 2 },
90 : { "importaddress", 3 },
91 : { "importpubkey", 2 },
92 : { "verifychain", 0 },
93 : { "verifychain", 1 },
94 : { "keypoolrefill", 0 },
95 : { "getrawmempool", 0 },
96 : { "estimatefee", 0 },
97 : { "estimatepriority", 0 },
98 : { "prioritisetransaction", 1 },
99 : { "prioritisetransaction", 2 },
100 : { "setban", 2 },
101 : { "setban", 3 },
102 7050 : };
103 :
104 188 : class CRPCConvertTable
105 : {
106 : private:
107 : std::set<std::pair<std::string, int> > members;
108 :
109 : public:
110 : CRPCConvertTable();
111 :
112 160 : bool convert(const std::string& method, int idx) {
113 800 : return (members.count(std::make_pair(method, idx)) > 0);
114 : }
115 : };
116 :
117 94 : CRPCConvertTable::CRPCConvertTable()
118 : {
119 : const unsigned int n_elem =
120 94 : (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
121 :
122 7050 : for (unsigned int i = 0; i < n_elem; i++) {
123 : members.insert(std::make_pair(vRPCConvertParams[i].methodName,
124 27824 : vRPCConvertParams[i].paramIdx));
125 : }
126 94 : }
127 :
128 94 : static CRPCConvertTable rpcCvtTable;
129 :
130 : /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
131 : * as well as objects and arrays.
132 : */
133 85 : UniValue ParseNonRFCJSONValue(const std::string& strVal)
134 : {
135 : UniValue jVal;
136 680 : if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
137 235 : !jVal.isArray() || jVal.size()!=1)
138 80 : throw runtime_error(string("Error parsing JSON:")+strVal);
139 85 : return jVal[0];
140 : }
141 :
142 : /** Convert strings to command-specific RPC representation */
143 209 : UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
144 : {
145 627 : UniValue params(UniValue::VARR);
146 :
147 712 : for (unsigned int idx = 0; idx < strParams.size(); idx++) {
148 320 : const std::string& strVal = strParams[idx];
149 :
150 160 : if (!rpcCvtTable.convert(strMethod, idx)) {
151 : // insert string value directly
152 86 : params.push_back(strVal);
153 : } else {
154 : // parse string as JSON, insert bool/number/object/etc. value
155 74 : params.push_back(ParseNonRFCJSONValue(strVal));
156 : }
157 : }
158 :
159 196 : return params;
160 282 : }
161 :
|