ecoinrpc.h 11 KB

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