17 #include <boost/algorithm/string.hpp>
18 #include <boost/asio.hpp>
19 #include <boost/asio/ssl.hpp>
20 #include <boost/bind.hpp>
21 #include <boost/filesystem.hpp>
22 #include <boost/foreach.hpp>
23 #include <boost/iostreams/concepts.hpp>
24 #include <boost/iostreams/stream.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include "json/json_spirit_writer_template.h"
29 using namespace boost;
33 Object
CallRPC(
const string& strMethod,
const Array& params)
37 _(
"You must set rpcpassword=<password> in the configuration file:\n%s\n"
38 "If the file does not exist, create it with owner-readable-only file permissions."),
43 asio::io_service io_service;
44 ssl::context context(io_service, ssl::context::sslv23);
45 context.set_options(ssl::context::no_sslv2);
46 asio::ssl::stream<asio::ip::tcp::socket> sslStream(io_service, context);
48 iostreams::stream< SSLIOStreamDevice<asio::ip::tcp> > stream(d);
53 if (fConnected)
break;
57 throw runtime_error(
"couldn't connect to server");
62 map<string, string> mapRequestHeaders;
63 mapRequestHeaders[
"Authorization"] = string(
"Basic ") + strUserPass64;
67 string strPost =
HTTPPost(strRequest, mapRequestHeaders);
68 stream << strPost << std::flush;
75 map<string, string> mapHeaders;
80 throw runtime_error(
"incorrect rpcuser or rpcpassword (authorization failed)");
82 throw runtime_error(
strprintf(
"server returned HTTP error %d", nStatus));
83 else if (strReply.empty())
84 throw runtime_error(
"no response from server");
88 if (!read_string(strReply, valReply))
89 throw runtime_error(
"couldn't parse reply from server");
90 const Object& reply = valReply.get_obj();
92 throw runtime_error(
"expected reply to have result, error and id properties");
107 {
"getaddednodeinfo", 0 },
108 {
"setgenerate", 0 },
109 {
"setgenerate", 1 },
110 {
"getnetworkhashps", 0 },
111 {
"getnetworkhashps", 1 },
112 {
"sendtoaddress", 1 },
114 {
"getreceivedbyaddress", 1 },
115 {
"getreceivedbyaccount", 1 },
116 {
"listreceivedbyaddress", 0 },
117 {
"listreceivedbyaddress", 1 },
118 {
"listreceivedbyaddress", 2 },
119 {
"listreceivedbyaccount", 0 },
120 {
"listreceivedbyaccount", 1 },
121 {
"listreceivedbyaccount", 2 },
124 {
"getblockhash", 0 },
129 {
"listtransactions", 1 },
130 {
"listtransactions", 2 },
131 {
"listtransactions", 3 },
132 {
"listaccounts", 0 },
133 {
"listaccounts", 1 },
134 {
"walletpassphrase", 1 },
135 {
"getblocktemplate", 0 },
136 {
"listsinceblock", 1 },
137 {
"listsinceblock", 2 },
140 {
"addmultisigaddress", 0 },
141 {
"addmultisigaddress", 1 },
142 {
"createmultisig", 0 },
143 {
"createmultisig", 1 },
144 {
"listunspent", 0 },
145 {
"listunspent", 1 },
146 {
"listunspent", 2 },
148 {
"gettransaction", 1 },
149 {
"getrawtransaction", 1 },
150 {
"createrawtransaction", 0 },
151 {
"createrawtransaction", 1 },
152 {
"signrawtransaction", 1 },
153 {
"signrawtransaction", 2 },
154 {
"sendrawtransaction", 1 },
157 {
"lockunspent", 0 },
158 {
"lockunspent", 1 },
159 {
"importprivkey", 2 },
160 {
"importaddress", 2 },
161 {
"verifychain", 0 },
162 {
"verifychain", 1 },
163 {
"keypoolrefill", 0 },
164 {
"getrawmempool", 0 },
170 std::set<std::pair<std::string, int> >
members;
175 bool convert(
const std::string& method,
int idx) {
176 return (members.count(std::make_pair(method, idx)) > 0);
182 const unsigned int n_elem =
183 (
sizeof(vRPCConvertParams) /
sizeof(vRPCConvertParams[0]));
185 for (
unsigned int i = 0; i < n_elem; i++) {
186 members.insert(std::make_pair(vRPCConvertParams[i].methodName,
187 vRPCConvertParams[i].paramIdx));
194 Array
RPCConvertValues(
const std::string &strMethod,
const std::vector<std::string> &strParams)
198 for (
unsigned int idx = 0; idx < strParams.size(); idx++) {
199 const std::string& strVal = strParams[idx];
202 if (!rpcCvtTable.
convert(strMethod, idx)) {
203 params.push_back(strVal);
209 if (!read_string(strVal, jVal))
210 throw runtime_error(
string(
"Error parsing JSON:")+strVal);
211 params.push_back(jVal);
233 throw runtime_error(
"too few parameters");
234 string strMethod = argv[1];
237 std::vector<std::string> strParams(&argv[2], &argv[argc]);
241 Object reply =
CallRPC(strMethod, params);
244 const Value& result = find_value(reply,
"result");
245 const Value& error = find_value(reply,
"error");
247 if (error.type() != null_type)
250 strPrint =
"error: " + write_string(error,
false);
251 int code = find_value(error.get_obj(),
"code").get_int();
257 if (result.type() == null_type)
259 else if (result.type() == str_type)
260 strPrint = result.get_str();
262 strPrint = write_string(result,
true);
265 catch (boost::thread_interrupted) {
268 catch (std::exception& e) {
269 strPrint = string(
"error: ") + e.what();
279 fprintf((nRet == 0 ? stdout : stderr),
"%s\n", strPrint.c_str());
289 strUsage +=
_(
"Options:") +
"\n";
290 strUsage +=
" -? " +
_(
"This help message") +
"\n";
291 strUsage +=
" -conf=<file> " +
_(
"Specify configuration file (default: anoncoin.conf)") +
"\n";
292 strUsage +=
" -datadir=<dir> " +
_(
"Specify data directory") +
"\n";
293 strUsage +=
" -testnet " +
_(
"Use the test network") +
"\n";
294 strUsage +=
" -regtest " +
_(
"Enter regression test mode, which uses a special chain in which blocks can be "
295 "solved instantly. This is intended for regression testing tools and app development.") +
"\n";
297 strUsage +=
_(
"RPC client options:") +
"\n";
300 strUsage +=
" -rpcconnect=<ip> " +
_(
"Send commands to node running on <ip> (default: 127.0.0.1)") +
"\n";
301 strUsage +=
" -rpcport=<port> " +
_(
"Connect to JSON-RPC on <port> (default: 9332 or testnet: 19332)") +
"\n";
302 strUsage +=
" -rpcwait " +
_(
"Wait for RPC server to start") +
"\n";
306 strUsage +=
" -rpcuser=<user> " +
_(
"Username for JSON-RPC connections") +
"\n";
307 strUsage +=
" -rpcpassword=<pw> " +
_(
"Password for JSON-RPC connections") +
"\n";
309 strUsage +=
"\n" +
_(
"SSL options: (see the Bitcoin Wiki for SSL setup instructions)") +
"\n";
310 strUsage +=
" -rpcssl " +
_(
"Use OpenSSL (https) for JSON-RPC connections") +
"\n";
int ReadHTTPStatus(std::basic_istream< char > &stream, int &proto)
std::string HelpMessageCli(bool mainProgram)
Show help message for anoncoin-cli.
void MilliSleep(int64_t n)
std::set< std::pair< std::string, int > > members
Object CallRPC(const string &strMethod, const Array ¶ms)
string EncodeBase64(const unsigned char *pch, size_t len)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
int ReadHTTPMessage(std::basic_istream< char > &stream, map< string, string > &mapHeadersRet, string &strMessageRet, int nProto)
std::string itostr(int n)
void PrintExceptionContinue(std::exception *pex, const char *pszThread)
int CommandLineRPC(int argc, char *argv[])
bool convert(const std::string &method, int idx)
bool IsSwitchChar(char c)
const CChainParams & Params()
Return the currently selected parameters.
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
string HTTPPost(const string &strMsg, const map< string, string > &mapRequestHeaders)
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
string JSONRPCRequest(const string &strMethod, const Array ¶ms, const Value &id)
bool connect(const std::string &server, const std::string &port)
Array RPCConvertValues(const std::string &strMethod, const std::vector< std::string > &strParams)
map< string, string > mapArgs
boost::filesystem::path GetConfigFile()