2013-11-20 05:18:57 -08:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-11-19 18:19:29 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-20 05:18:57 -08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "rpcclient.h"
|
|
|
|
|
|
|
|
#include "rpcprotocol.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-09-14 03:43:56 -07:00
|
|
|
#include <set>
|
2013-11-20 05:18:57 -08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-05-10 05:49:18 -07:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
|
2015-05-18 05:02:18 -07:00
|
|
|
#include "univalue/univalue.h"
|
2015-05-10 05:49:18 -07:00
|
|
|
|
2013-11-20 05:18:57 -08:00
|
|
|
using namespace std;
|
|
|
|
|
2014-06-26 19:12:36 -07:00
|
|
|
class CRPCConvertParam
|
2013-11-20 05:18:57 -08:00
|
|
|
{
|
2014-06-26 19:12:36 -07:00
|
|
|
public:
|
2014-11-19 18:19:29 -08:00
|
|
|
std::string methodName; //! method whose params want conversion
|
|
|
|
int paramIdx; //! 0-based idx of param to convert
|
2014-06-26 19:12:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const CRPCConvertParam vRPCConvertParams[] =
|
|
|
|
{
|
|
|
|
{ "stop", 0 },
|
2014-11-12 15:59:41 -08:00
|
|
|
{ "setmocktime", 0 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "getaddednodeinfo", 0 },
|
|
|
|
{ "setgenerate", 0 },
|
|
|
|
{ "setgenerate", 1 },
|
2015-03-31 20:28:28 -07:00
|
|
|
{ "generate", 0 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "getnetworkhashps", 0 },
|
|
|
|
{ "getnetworkhashps", 1 },
|
|
|
|
{ "sendtoaddress", 1 },
|
2014-07-23 05:34:36 -07:00
|
|
|
{ "sendtoaddress", 4 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "settxfee", 0 },
|
|
|
|
{ "getreceivedbyaddress", 1 },
|
|
|
|
{ "getreceivedbyaccount", 1 },
|
|
|
|
{ "listreceivedbyaddress", 0 },
|
|
|
|
{ "listreceivedbyaddress", 1 },
|
2014-05-31 07:57:26 -07:00
|
|
|
{ "listreceivedbyaddress", 2 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "listreceivedbyaccount", 0 },
|
|
|
|
{ "listreceivedbyaccount", 1 },
|
2014-05-31 07:57:26 -07:00
|
|
|
{ "listreceivedbyaccount", 2 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "getbalance", 1 },
|
2014-04-08 06:23:50 -07:00
|
|
|
{ "getbalance", 2 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "getblockhash", 0 },
|
|
|
|
{ "move", 2 },
|
|
|
|
{ "move", 3 },
|
|
|
|
{ "sendfrom", 2 },
|
|
|
|
{ "sendfrom", 3 },
|
|
|
|
{ "listtransactions", 1 },
|
|
|
|
{ "listtransactions", 2 },
|
2014-04-08 08:51:04 -07:00
|
|
|
{ "listtransactions", 3 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "listaccounts", 0 },
|
2014-04-08 07:13:15 -07:00
|
|
|
{ "listaccounts", 1 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "walletpassphrase", 1 },
|
|
|
|
{ "getblocktemplate", 0 },
|
|
|
|
{ "listsinceblock", 1 },
|
2014-04-08 08:51:04 -07:00
|
|
|
{ "listsinceblock", 2 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "sendmany", 1 },
|
|
|
|
{ "sendmany", 2 },
|
2014-07-23 05:34:36 -07:00
|
|
|
{ "sendmany", 4 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "addmultisigaddress", 0 },
|
|
|
|
{ "addmultisigaddress", 1 },
|
|
|
|
{ "createmultisig", 0 },
|
|
|
|
{ "createmultisig", 1 },
|
|
|
|
{ "listunspent", 0 },
|
|
|
|
{ "listunspent", 1 },
|
|
|
|
{ "listunspent", 2 },
|
|
|
|
{ "getblock", 1 },
|
2015-06-05 12:37:17 -07:00
|
|
|
{ "getblockheader", 1 },
|
2014-09-07 05:38:42 -07:00
|
|
|
{ "gettransaction", 1 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "getrawtransaction", 1 },
|
|
|
|
{ "createrawtransaction", 0 },
|
|
|
|
{ "createrawtransaction", 1 },
|
|
|
|
{ "signrawtransaction", 1 },
|
|
|
|
{ "signrawtransaction", 2 },
|
|
|
|
{ "sendrawtransaction", 1 },
|
2015-04-24 18:27:30 -07:00
|
|
|
{ "fundrawtransaction", 1 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "gettxout", 1 },
|
|
|
|
{ "gettxout", 2 },
|
2014-11-01 16:01:48 -07:00
|
|
|
{ "gettxoutproof", 0 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "lockunspent", 0 },
|
|
|
|
{ "lockunspent", 1 },
|
|
|
|
{ "importprivkey", 2 },
|
2013-07-25 16:06:01 -07:00
|
|
|
{ "importaddress", 2 },
|
2015-06-11 00:57:50 -07:00
|
|
|
{ "importaddress", 3 },
|
2015-07-09 22:47:36 -07:00
|
|
|
{ "importpubkey", 2 },
|
2014-06-26 19:12:36 -07:00
|
|
|
{ "verifychain", 0 },
|
|
|
|
{ "verifychain", 1 },
|
|
|
|
{ "keypoolrefill", 0 },
|
|
|
|
{ "getrawmempool", 0 },
|
|
|
|
{ "estimatefee", 0 },
|
|
|
|
{ "estimatepriority", 0 },
|
2014-07-14 17:11:55 -07:00
|
|
|
{ "prioritisetransaction", 1 },
|
|
|
|
{ "prioritisetransaction", 2 },
|
2015-05-19 01:07:46 -07:00
|
|
|
{ "setban", 2 },
|
2015-06-12 09:31:47 -07:00
|
|
|
{ "setban", 3 },
|
2014-06-26 19:12:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class CRPCConvertTable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::set<std::pair<std::string, int> > members;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CRPCConvertTable();
|
|
|
|
|
|
|
|
bool convert(const std::string& method, int idx) {
|
|
|
|
return (members.count(std::make_pair(method, idx)) > 0);
|
2013-11-20 05:18:57 -08:00
|
|
|
}
|
2014-06-26 19:12:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
CRPCConvertTable::CRPCConvertTable()
|
|
|
|
{
|
|
|
|
const unsigned int n_elem =
|
|
|
|
(sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < n_elem; i++) {
|
|
|
|
members.insert(std::make_pair(vRPCConvertParams[i].methodName,
|
|
|
|
vRPCConvertParams[i].paramIdx));
|
2013-11-20 05:18:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 19:12:36 -07:00
|
|
|
static CRPCConvertTable rpcCvtTable;
|
|
|
|
|
2015-06-04 01:31:22 -07:00
|
|
|
/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null)
|
|
|
|
* as well as objects and arrays.
|
|
|
|
*/
|
|
|
|
UniValue ParseNonRFCJSONValue(const std::string& strVal)
|
|
|
|
{
|
|
|
|
UniValue jVal;
|
|
|
|
if (!jVal.read(std::string("[")+strVal+std::string("]")) ||
|
|
|
|
!jVal.isArray() || jVal.size()!=1)
|
|
|
|
throw runtime_error(string("Error parsing JSON:")+strVal);
|
|
|
|
return jVal[0];
|
|
|
|
}
|
|
|
|
|
2014-11-19 18:19:29 -08:00
|
|
|
/** Convert strings to command-specific RPC representation */
|
2015-05-13 12:29:19 -07:00
|
|
|
UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)
|
2013-11-20 05:18:57 -08:00
|
|
|
{
|
2015-05-10 04:35:44 -07:00
|
|
|
UniValue params(UniValue::VARR);
|
2014-06-26 19:12:36 -07:00
|
|
|
|
|
|
|
for (unsigned int idx = 0; idx < strParams.size(); idx++) {
|
|
|
|
const std::string& strVal = strParams[idx];
|
|
|
|
|
|
|
|
if (!rpcCvtTable.convert(strMethod, idx)) {
|
2015-06-04 01:31:22 -07:00
|
|
|
// insert string value directly
|
2014-06-26 19:12:36 -07:00
|
|
|
params.push_back(strVal);
|
2015-06-04 01:31:22 -07:00
|
|
|
} else {
|
|
|
|
// parse string as JSON, insert bool/number/object/etc. value
|
|
|
|
params.push_back(ParseNonRFCJSONValue(strVal));
|
2014-06-26 19:12:36 -07:00
|
|
|
}
|
|
|
|
}
|
2013-11-20 05:18:57 -08:00
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|