1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 "rpc/client.h"
7 #include "rpc/protocol.h"
8 #include "util.h"
9 
10 #include <set>
11 #include <stdint.h>
12 
13 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
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     { "generate", 0 },
31     { "generate", 1 },
32     { "generatetoaddress", 0 },
33     { "generatetoaddress", 2 },
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     { "createrawtransaction", 2 },
80     { "signrawtransaction", 1 },
81     { "signrawtransaction", 2 },
82     { "sendrawtransaction", 1 },
83     { "fundrawtransaction", 1 },
84     { "gettxout", 1 },
85     { "gettxout", 2 },
86     { "gettxoutproof", 0 },
87     { "lockunspent", 0 },
88     { "lockunspent", 1 },
89     { "importprivkey", 2 },
90     { "importaddress", 2 },
91     { "importaddress", 3 },
92     { "importpubkey", 2 },
93     { "verifychain", 0 },
94     { "verifychain", 1 },
95     { "keypoolrefill", 0 },
96     { "getrawmempool", 0 },
97     { "estimatefee", 0 },
98     { "estimatepriority", 0 },
99     { "estimatesmartfee", 0 },
100     { "estimatesmartpriority", 0 },
101     { "prioritisetransaction", 1 },
102     { "prioritisetransaction", 2 },
103     { "setban", 2 },
104     { "setban", 3 },
105     { "getmempoolancestors", 1 },
106     { "getmempooldescendants", 1 },
107 };
108 
109 class CRPCConvertTable
110 {
111 private:
112     std::set<std::pair<std::string, int> > members;
113 
114 public:
115     CRPCConvertTable();
116 
convert(const std::string & method,int idx)117     bool convert(const std::string& method, int idx) {
118         return (members.count(std::make_pair(method, idx)) > 0);
119     }
120 };
121 
CRPCConvertTable()122 CRPCConvertTable::CRPCConvertTable()
123 {
124     const unsigned int n_elem =
125         (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
126 
127     for (unsigned int i = 0; i < n_elem; i++) {
128         members.insert(std::make_pair(vRPCConvertParams[i].methodName,
129                                       vRPCConvertParams[i].paramIdx));
130     }
131 }
132 
133 static CRPCConvertTable rpcCvtTable;
134 
135 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
136  * as well as objects and arrays.
137  */
ParseNonRFCJSONValue(const std::string & strVal)138 UniValue ParseNonRFCJSONValue(const std::string& strVal)
139 {
140     UniValue jVal;
141     if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
142         !jVal.isArray() || jVal.size()!=1)
143         throw runtime_error(string("Error parsing JSON:")+strVal);
144     return jVal[0];
145 }
146 
147 /** Convert strings to command-specific RPC representation */
RPCConvertValues(const std::string & strMethod,const std::vector<std::string> & strParams)148 UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
149 {
150     UniValue params(UniValue::VARR);
151 
152     for (unsigned int idx = 0; idx < strParams.size(); idx++) {
153         const std::string& strVal = strParams[idx];
154 
155         if (!rpcCvtTable.convert(strMethod, idx)) {
156             // insert string value directly
157             params.push_back(strVal);
158         } else {
159             // parse string as JSON, insert bool/number/object/etc. value
160             params.push_back(ParseNonRFCJSONValue(strVal));
161         }
162     }
163 
164     return params;
165 }
166