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"
14 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
15 #include "univalue/univalue.h"
19 class CRPCConvertParam
22 std::string methodName
; //! method whose params want conversion
23 int paramIdx
; //! 0-based idx of param to convert
26 static const CRPCConvertParam vRPCConvertParams
[] =
30 { "getaddednodeinfo", 0 },
34 { "getnetworkhashps", 0 },
35 { "getnetworkhashps", 1 },
36 { "sendtoaddress", 1 },
37 { "sendtoaddress", 4 },
39 { "getreceivedbyaddress", 1 },
40 { "getreceivedbyaccount", 1 },
41 { "listreceivedbyaddress", 0 },
42 { "listreceivedbyaddress", 1 },
43 { "listreceivedbyaddress", 2 },
44 { "listreceivedbyaccount", 0 },
45 { "listreceivedbyaccount", 1 },
46 { "listreceivedbyaccount", 2 },
49 { "getblockhash", 0 },
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 },
66 { "addmultisigaddress", 0 },
67 { "addmultisigaddress", 1 },
68 { "createmultisig", 0 },
69 { "createmultisig", 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 },
85 { "gettxoutproof", 0 },
88 { "importprivkey", 2 },
89 { "importaddress", 2 },
92 { "keypoolrefill", 0 },
93 { "getrawmempool", 0 },
95 { "estimatepriority", 0 },
96 { "prioritisetransaction", 1 },
97 { "prioritisetransaction", 2 },
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 /** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
129 * as well as objects and arrays.
131 UniValue
ParseNonRFCJSONValue(const std::string
& strVal
)
134 if (!jVal
.read(std::string("[")+strVal
+std::string("]")) ||
135 !jVal
.isArray() || jVal
.size()!=1)
136 throw runtime_error(string("Error parsing JSON:")+strVal
);
140 /** Convert strings to command-specific RPC representation */
141 UniValue
RPCConvertValues(const std::string
&strMethod
, const std::vector
<std::string
> &strParams
)
143 UniValue
params(UniValue::VARR
);
145 for (unsigned int idx
= 0; idx
< strParams
.size(); idx
++) {
146 const std::string
& strVal
= strParams
[idx
];
148 if (!rpcCvtTable
.convert(strMethod
, idx
)) {
149 // insert string value directly
150 params
.push_back(strVal
);
152 // parse string as JSON, insert bool/number/object/etc. value
153 params
.push_back(ParseNonRFCJSONValue(strVal
));