You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitcoinrpc.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (c) 2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 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.
  5. #ifndef _BITCOINRPC_H_
  6. #define _BITCOINRPC_H_ 1
  7. #include "uint256.h"
  8. #include <list>
  9. #include <map>
  10. #include <stdint.h>
  11. #include <string>
  12. #include "json/json_spirit_reader_template.h"
  13. #include "json/json_spirit_utils.h"
  14. #include "json/json_spirit_writer_template.h"
  15. class CBlockIndex;
  16. class CReserveKey;
  17. // HTTP status codes
  18. enum HTTPStatusCode
  19. {
  20. HTTP_OK = 200,
  21. HTTP_BAD_REQUEST = 400,
  22. HTTP_UNAUTHORIZED = 401,
  23. HTTP_FORBIDDEN = 403,
  24. HTTP_NOT_FOUND = 404,
  25. HTTP_INTERNAL_SERVER_ERROR = 500,
  26. };
  27. // Bitcoin RPC error codes
  28. enum RPCErrorCode
  29. {
  30. // Standard JSON-RPC 2.0 errors
  31. RPC_INVALID_REQUEST = -32600,
  32. RPC_METHOD_NOT_FOUND = -32601,
  33. RPC_INVALID_PARAMS = -32602,
  34. RPC_INTERNAL_ERROR = -32603,
  35. RPC_PARSE_ERROR = -32700,
  36. // General application defined errors
  37. RPC_MISC_ERROR = -1, // std::exception thrown in command handling
  38. RPC_FORBIDDEN_BY_SAFE_MODE = -2, // Server is in safe mode, and command is not allowed in safe mode
  39. RPC_TYPE_ERROR = -3, // Unexpected type was passed as parameter
  40. RPC_INVALID_ADDRESS_OR_KEY = -5, // Invalid address or key
  41. RPC_OUT_OF_MEMORY = -7, // Ran out of memory during operation
  42. RPC_INVALID_PARAMETER = -8, // Invalid, missing or duplicate parameter
  43. RPC_DATABASE_ERROR = -20, // Database error
  44. RPC_DESERIALIZATION_ERROR = -22, // Error parsing or validating structure in raw format
  45. RPC_SERVER_NOT_STARTED = -18, // RPC server was not started (StartRPCThreads() not called)
  46. // P2P client errors
  47. RPC_CLIENT_NOT_CONNECTED = -9, // Bitcoin is not connected
  48. RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, // Still downloading initial blocks
  49. RPC_CLIENT_NODE_ALREADY_ADDED = -23, // Node is already added
  50. RPC_CLIENT_NODE_NOT_ADDED = -24, // Node has not been added before
  51. // Wallet errors
  52. RPC_WALLET_ERROR = -4, // Unspecified problem with wallet (key not found etc.)
  53. RPC_WALLET_INSUFFICIENT_FUNDS = -6, // Not enough funds in wallet or account
  54. RPC_WALLET_INVALID_ACCOUNT_NAME = -11, // Invalid account name
  55. RPC_WALLET_KEYPOOL_RAN_OUT = -12, // Keypool ran out, call keypoolrefill first
  56. RPC_WALLET_UNLOCK_NEEDED = -13, // Enter the wallet passphrase with walletpassphrase first
  57. RPC_WALLET_PASSPHRASE_INCORRECT = -14, // The wallet passphrase entered was incorrect
  58. RPC_WALLET_WRONG_ENC_STATE = -15, // Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
  59. RPC_WALLET_ENCRYPTION_FAILED = -16, // Failed to encrypt the wallet
  60. RPC_WALLET_ALREADY_UNLOCKED = -17, // Wallet is already unlocked
  61. };
  62. json_spirit::Object JSONRPCError(int code, const std::string& message);
  63. void StartRPCThreads();
  64. void StopRPCThreads();
  65. int CommandLineRPC(int argc, char *argv[]);
  66. /** Convert parameter values for RPC call from strings to command-specific JSON objects. */
  67. json_spirit::Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams);
  68. /*
  69. Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
  70. the right number of arguments are passed, just that any passed are the correct type.
  71. Use like: RPCTypeCheck(params, boost::assign::list_of(str_type)(int_type)(obj_type));
  72. */
  73. void RPCTypeCheck(const json_spirit::Array& params,
  74. const std::list<json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
  75. /*
  76. Check for expected keys/value types in an Object.
  77. Use like: RPCTypeCheck(object, boost::assign::map_list_of("name", str_type)("value", int_type));
  78. */
  79. void RPCTypeCheck(const json_spirit::Object& o,
  80. const std::map<std::string, json_spirit::Value_type>& typesExpected, bool fAllowNull=false);
  81. /*
  82. Run func nSeconds from now. Uses boost deadline timers.
  83. Overrides previous timer <name> (if any).
  84. */
  85. void RPCRunLater(const std::string& name, boost::function<void(void)> func, int64_t nSeconds);
  86. typedef json_spirit::Value(*rpcfn_type)(const json_spirit::Array& params, bool fHelp);
  87. class CRPCCommand
  88. {
  89. public:
  90. std::string name;
  91. rpcfn_type actor;
  92. bool okSafeMode;
  93. bool threadSafe;
  94. bool reqWallet;
  95. };
  96. /**
  97. * Bitcoin RPC command dispatcher.
  98. */
  99. class CRPCTable
  100. {
  101. private:
  102. std::map<std::string, const CRPCCommand*> mapCommands;
  103. public:
  104. CRPCTable();
  105. const CRPCCommand* operator[](std::string name) const;
  106. std::string help(std::string name) const;
  107. /**
  108. * Execute a method.
  109. * @param method Method to execute
  110. * @param params Array of arguments (JSON objects)
  111. * @returns Result of the call.
  112. * @throws an exception (json_spirit::Value) when an error happens.
  113. */
  114. json_spirit::Value execute(const std::string &method, const json_spirit::Array &params) const;
  115. };
  116. extern const CRPCTable tableRPC;
  117. //
  118. // Utilities: convert hex-encoded Values
  119. // (throws error if not hex).
  120. //
  121. extern uint256 ParseHashV(const json_spirit::Value& v, std::string strName);
  122. extern uint256 ParseHashO(const json_spirit::Object& o, std::string strKey);
  123. extern std::vector<unsigned char> ParseHexV(const json_spirit::Value& v, std::string strName);
  124. extern std::vector<unsigned char> ParseHexO(const json_spirit::Object& o, std::string strKey);
  125. extern void InitRPCMining();
  126. extern void ShutdownRPCMining();
  127. extern int64_t nWalletUnlockTime;
  128. extern int64_t AmountFromValue(const json_spirit::Value& value);
  129. extern json_spirit::Value ValueFromAmount(int64_t amount);
  130. extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
  131. extern std::string HexBits(unsigned int nBits);
  132. extern std::string HelpRequiringPassphrase();
  133. extern void EnsureWalletIsUnlocked();
  134. extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp
  135. extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
  136. extern json_spirit::Value ping(const json_spirit::Array& params, bool fHelp);
  137. extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
  138. extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
  139. extern json_spirit::Value getnettotals(const json_spirit::Array& params, bool fHelp);
  140. extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
  141. extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
  142. extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp);
  143. extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fHelp);
  144. extern json_spirit::Value getgenerate(const json_spirit::Array& params, bool fHelp); // in rpcmining.cpp
  145. extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHelp);
  146. extern json_spirit::Value getnetworkhashps(const json_spirit::Array& params, bool fHelp);
  147. extern json_spirit::Value gethashespersec(const json_spirit::Array& params, bool fHelp);
  148. extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
  149. extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp);
  150. extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
  151. extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
  152. extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
  153. extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
  154. extern json_spirit::Value getrawchangeaddress(const json_spirit::Array& params, bool fHelp);
  155. extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp);
  156. extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp);
  157. extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp);
  158. extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp);
  159. extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
  160. extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
  161. extern json_spirit::Value getreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
  162. extern json_spirit::Value getreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
  163. extern json_spirit::Value getbalance(const json_spirit::Array& params, bool fHelp);
  164. extern json_spirit::Value movecmd(const json_spirit::Array& params, bool fHelp);
  165. extern json_spirit::Value sendfrom(const json_spirit::Array& params, bool fHelp);
  166. extern json_spirit::Value sendmany(const json_spirit::Array& params, bool fHelp);
  167. extern json_spirit::Value addmultisigaddress(const json_spirit::Array& params, bool fHelp);
  168. extern json_spirit::Value createmultisig(const json_spirit::Array& params, bool fHelp);
  169. extern json_spirit::Value listreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
  170. extern json_spirit::Value listreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
  171. extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp);
  172. extern json_spirit::Value listaddressgroupings(const json_spirit::Array& params, bool fHelp);
  173. extern json_spirit::Value listaccounts(const json_spirit::Array& params, bool fHelp);
  174. extern json_spirit::Value listsinceblock(const json_spirit::Array& params, bool fHelp);
  175. extern json_spirit::Value gettransaction(const json_spirit::Array& params, bool fHelp);
  176. extern json_spirit::Value backupwallet(const json_spirit::Array& params, bool fHelp);
  177. extern json_spirit::Value keypoolrefill(const json_spirit::Array& params, bool fHelp);
  178. extern json_spirit::Value walletpassphrase(const json_spirit::Array& params, bool fHelp);
  179. extern json_spirit::Value walletpassphrasechange(const json_spirit::Array& params, bool fHelp);
  180. extern json_spirit::Value walletlock(const json_spirit::Array& params, bool fHelp);
  181. extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool fHelp);
  182. extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
  183. extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
  184. extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
  185. extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
  186. extern json_spirit::Value lockunspent(const json_spirit::Array& params, bool fHelp);
  187. extern json_spirit::Value listlockunspent(const json_spirit::Array& params, bool fHelp);
  188. extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
  189. extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp);
  190. extern json_spirit::Value decodescript(const json_spirit::Array& params, bool fHelp);
  191. extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp);
  192. extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp);
  193. extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp
  194. extern json_spirit::Value getbestblockhash(const json_spirit::Array& params, bool fHelp);
  195. extern json_spirit::Value getdifficulty(const json_spirit::Array& params, bool fHelp);
  196. extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp);
  197. extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp);
  198. extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp);
  199. extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp);
  200. extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp);
  201. extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp);
  202. extern json_spirit::Value verifychain(const json_spirit::Array& params, bool fHelp);
  203. #endif