1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #define _BITCOINRPC_H_ 1
15 #include "json/json_spirit_reader_template.h"
16 #include "json/json_spirit_writer_template.h"
17 #include "json/json_spirit_utils.h"
25 HTTP_BAD_REQUEST = 400,
26 HTTP_UNAUTHORIZED = 401,
29 HTTP_INTERNAL_SERVER_ERROR = 500,
32 // Bitcoin RPC error codes
35 // Standard JSON-RPC 2.0 errors
36 RPC_INVALID_REQUEST = -32600,
37 RPC_METHOD_NOT_FOUND = -32601,
38 RPC_INVALID_PARAMS = -32602,
39 RPC_INTERNAL_ERROR = -32603,
40 RPC_PARSE_ERROR = -32700,
42 // General application defined errors
43 RPC_MISC_ERROR = -1, // std::exception thrown in command handling
44 RPC_FORBIDDEN_BY_SAFE_MODE = -2, // Server is in safe mode, and command is not allowed in safe mode
45 RPC_TYPE_ERROR = -3, // Unexpected type was passed as parameter
46 RPC_INVALID_ADDRESS_OR_KEY = -5, // Invalid address or key
47 RPC_OUT_OF_MEMORY = -7, // Ran out of memory during operation
48 RPC_INVALID_PARAMETER = -8, // Invalid, missing or duplicate parameter
49 RPC_DATABASE_ERROR = -20, // Database error
50 RPC_DESERIALIZATION_ERROR = -22, // Error parsing or validating structure in raw format
53 RPC_CLIENT_NOT_CONNECTED = -9, // Bitcoin is not connected
54 RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, // Still downloading initial blocks
57 RPC_WALLET_ERROR = -4, // Unspecified problem with wallet (key not found etc.)
58 RPC_WALLET_INSUFFICIENT_FUNDS = -6, // Not enough funds in wallet or account
59 RPC_WALLET_INVALID_ACCOUNT_NAME = -11, // Invalid account name
60 RPC_WALLET_KEYPOOL_RAN_OUT = -12, // Keypool ran out, call keypoolrefill first
61 RPC_WALLET_UNLOCK_NEEDED = -13, // Enter the wallet passphrase with walletpassphrase first
62 RPC_WALLET_PASSPHRASE_INCORRECT = -14, // The wallet passphrase entered was incorrect
63 RPC_WALLET_WRONG_ENC_STATE = -15, // Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
64 RPC_WALLET_ENCRYPTION_FAILED = -16, // Failed to encrypt the wallet
65 RPC_WALLET_ALREADY_UNLOCKED = -17, // Wallet is already unlocked
68 json_spirit::Object JSONRPCError(int code, const std::string& message);
70 void ThreadRPCServer(void* parg);
71 int CommandLineRPC(int argc, char *argv[]);
73 /** Convert parameter values for RPC call from strings to command-specific JSON objects. */
74 json_spirit::Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams);
77 Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
78 the right number of arguments are passed, just that any passed are the correct type.
79 Use like: RPCTypeCheck(params, boost::assign::list_of(str_type)(int_type)(obj_type));
81 void RPCTypeCheck(const json_spirit::Array& params,
82 const std::list<json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
84 Check for expected keys/value types in an Object.
85 Use like: RPCTypeCheck(object, boost::assign::map_list_of("name", str_type)("value", int_type));
87 void RPCTypeCheck(const json_spirit::Object& o,
88 const std::map<std::string, json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
90 typedef json_spirit::Value(*rpcfn_type)(const json_spirit::Array& params, bool fHelp);
102 * Bitcoin RPC command dispatcher.
107 std::map<std::string, const CRPCCommand*> mapCommands;
110 const CRPCCommand* operator[](std::string name) const;
111 std::string help(std::string name) const;
115 * @param method Method to execute
116 * @param params Array of arguments (JSON objects)
117 * @returns Result of the call.
118 * @throws an exception (json_spirit::Value) when an error happens.
120 json_spirit::Value execute(const std::string &method, const json_spirit::Array ¶ms) const;
123 extern const CRPCTable tableRPC;
125 extern int64 nWalletUnlockTime;
126 extern int64 AmountFromValue(const json_spirit::Value& value);
127 extern json_spirit::Value ValueFromAmount(int64 amount);
128 extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
129 extern std::string HexBits(unsigned int nBits);
130 extern std::string HelpRequiringPassphrase();
131 extern void EnsureWalletIsUnlocked();
133 extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp
134 extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
135 extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
136 extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
138 extern json_spirit::Value getgenerate(const json_spirit::Array& params, bool fHelp); // in rpcmining.cpp
139 extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHelp);
140 extern json_spirit::Value gethashespersec(const json_spirit::Array& params, bool fHelp);
141 extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
142 extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp);
143 extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
144 extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
146 extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
147 extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
148 extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp);
149 extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp);
150 extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp);
151 extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp);
152 extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
153 extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
154 extern json_spirit::Value getreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
155 extern json_spirit::Value getreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
156 extern json_spirit::Value getbalance(const json_spirit::Array& params, bool fHelp);
157 extern json_spirit::Value movecmd(const json_spirit::Array& params, bool fHelp);
158 extern json_spirit::Value sendfrom(const json_spirit::Array& params, bool fHelp);
159 extern json_spirit::Value sendmany(const json_spirit::Array& params, bool fHelp);
160 extern json_spirit::Value addmultisigaddress(const json_spirit::Array& params, bool fHelp);
161 extern json_spirit::Value listreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
162 extern json_spirit::Value listreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
163 extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp);
164 extern json_spirit::Value listaddressgroupings(const json_spirit::Array& params, bool fHelp);
165 extern json_spirit::Value listaccounts(const json_spirit::Array& params, bool fHelp);
166 extern json_spirit::Value listsinceblock(const json_spirit::Array& params, bool fHelp);
167 extern json_spirit::Value gettransaction(const json_spirit::Array& params, bool fHelp);
168 extern json_spirit::Value backupwallet(const json_spirit::Array& params, bool fHelp);
169 extern json_spirit::Value keypoolrefill(const json_spirit::Array& params, bool fHelp);
170 extern json_spirit::Value walletpassphrase(const json_spirit::Array& params, bool fHelp);
171 extern json_spirit::Value walletpassphrasechange(const json_spirit::Array& params, bool fHelp);
172 extern json_spirit::Value walletlock(const json_spirit::Array& params, bool fHelp);
173 extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool fHelp);
174 extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
175 extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
177 extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
178 extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
179 extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
180 extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp);
181 extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp);
182 extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp);
184 extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp
185 extern json_spirit::Value getdifficulty(const json_spirit::Array& params, bool fHelp);
186 extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp);
187 extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp);
188 extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp);
189 extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp);