Code cleanup
[novacoin.git] / src / bitcoinrpc.h
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.
5
6 #ifndef _BITCOINRPC_H_
7 #define _BITCOINRPC_H_ 1
8
9 #include <string>
10 #include <list>
11 #include <map>
12
13 class CBlockIndex;
14
15 #include "json/json_spirit_reader_template.h"
16 #include "json/json_spirit_writer_template.h"
17 #include "json/json_spirit_utils.h"
18
19 #include "util.h"
20 #include "checkpoints.h"
21
22 // HTTP status codes
23 enum HTTPStatusCode
24 {
25     HTTP_OK                    = 200,
26     HTTP_BAD_REQUEST           = 400,
27     HTTP_UNAUTHORIZED          = 401,
28     HTTP_FORBIDDEN             = 403,
29     HTTP_NOT_FOUND             = 404,
30     HTTP_INTERNAL_SERVER_ERROR = 500,
31 };
32
33 // Bitcoin RPC error codes
34 enum RPCErrorCode
35 {
36     // Standard JSON-RPC 2.0 errors
37     RPC_INVALID_REQUEST  = -32600,
38     RPC_METHOD_NOT_FOUND = -32601,
39     RPC_INVALID_PARAMS   = -32602,
40     RPC_INTERNAL_ERROR   = -32603,
41     RPC_PARSE_ERROR      = -32700,
42
43     // General application defined errors
44     RPC_MISC_ERROR                  = -1,  // std::exception thrown in command handling
45     RPC_FORBIDDEN_BY_SAFE_MODE      = -2,  // Server is in safe mode, and command is not allowed in safe mode
46     RPC_TYPE_ERROR                  = -3,  // Unexpected type was passed as parameter
47     RPC_INVALID_ADDRESS_OR_KEY      = -5,  // Invalid address or key
48     RPC_OUT_OF_MEMORY               = -7,  // Ran out of memory during operation
49     RPC_INVALID_PARAMETER           = -8,  // Invalid, missing or duplicate parameter
50     RPC_DATABASE_ERROR              = -20, // Database error
51     RPC_DESERIALIZATION_ERROR       = -22, // Error parsing or validating structure in raw format
52
53     // P2P client errors
54     RPC_CLIENT_NOT_CONNECTED        = -9,  // Bitcoin is not connected
55     RPC_CLIENT_IN_INITIAL_DOWNLOAD  = -10, // Still downloading initial blocks
56
57     // Wallet errors
58     RPC_WALLET_ERROR                = -4,  // Unspecified problem with wallet (key not found etc.)
59     RPC_WALLET_INSUFFICIENT_FUNDS   = -6,  // Not enough funds in wallet or account
60     RPC_WALLET_INVALID_ACCOUNT_NAME = -11, // Invalid account name
61     RPC_WALLET_KEYPOOL_RAN_OUT      = -12, // Keypool ran out, call keypoolrefill first
62     RPC_WALLET_UNLOCK_NEEDED        = -13, // Enter the wallet passphrase with walletpassphrase first
63     RPC_WALLET_PASSPHRASE_INCORRECT = -14, // The wallet passphrase entered was incorrect
64     RPC_WALLET_WRONG_ENC_STATE      = -15, // Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
65     RPC_WALLET_ENCRYPTION_FAILED    = -16, // Failed to encrypt the wallet
66     RPC_WALLET_ALREADY_UNLOCKED     = -17, // Wallet is already unlocked
67 };
68
69 json_spirit::Object JSONRPCError(int code, const std::string& message);
70
71 void ThreadRPCServer(void* parg);
72 int CommandLineRPC(int argc, char *argv[]);
73
74 /** Convert parameter values for RPC call from strings to command-specific JSON objects. */
75 json_spirit::Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams);
76
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
91 typedef json_spirit::Value(*rpcfn_type)(const json_spirit::Array& params, bool fHelp);
92
93 class CRPCCommand
94 {
95 public:
96     std::string name;
97     rpcfn_type actor;
98     bool okSafeMode;
99     bool unlocked;
100 };
101
102 /**
103  * Bitcoin RPC command dispatcher.
104  */
105 class CRPCTable
106 {
107 private:
108     std::map<std::string, const CRPCCommand*> mapCommands;
109 public:
110     CRPCTable();
111     const CRPCCommand* operator[](std::string name) const;
112     std::string help(std::string name) const;
113
114     /**
115      * Execute a method.
116      * @param method   Method to execute
117      * @param params   Array of arguments (JSON objects)
118      * @returns Result of the call.
119      * @throws an exception (json_spirit::Value) when an error happens.
120      */
121     json_spirit::Value execute(const std::string &method, const json_spirit::Array &params) const;
122 };
123
124 extern const CRPCTable tableRPC;
125
126 extern int64_t nWalletUnlockTime;
127 extern int64_t AmountFromValue(const json_spirit::Value& value);
128 extern json_spirit::Value ValueFromAmount(int64_t amount);
129 extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
130
131 extern double GetPoWMHashPS();
132 extern double GetPoSKernelPS();
133
134 extern std::string HexBits(unsigned int nBits);
135 extern std::string HelpRequiringPassphrase();
136 extern void EnsureWalletIsUnlocked();
137
138 //
139 // Utilities: convert hex-encoded Values
140 // (throws error if not hex).
141 //
142 extern uint256 ParseHashV(const json_spirit::Value& v, std::string strName);
143 extern uint256 ParseHashO(const json_spirit::Object& o, std::string strKey);
144 extern std::vector<unsigned char> ParseHexV(const json_spirit::Value& v, std::string strName);
145 extern std::vector<unsigned char> ParseHexO(const json_spirit::Object& o, std::string strKey); 
146
147 extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, bool fHelp); // in rpcnet.cpp
148 extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
149 extern json_spirit::Value dumpwallet(const json_spirit::Array& params, bool fHelp);
150 extern json_spirit::Value importwallet(const json_spirit::Array& params, bool fHelp);
151 extern json_spirit::Value dumpprivkey(const json_spirit::Array& params, bool fHelp); // in rpcdump.cpp
152 extern json_spirit::Value importprivkey(const json_spirit::Array& params, bool fHelp);
153 extern json_spirit::Value importaddress(const json_spirit::Array& params, bool fHelp);
154 extern json_spirit::Value getnettotals(const json_spirit::Array& params, bool fHelp);
155
156 extern json_spirit::Value sendalert(const json_spirit::Array& params, bool fHelp);
157
158 extern json_spirit::Value getsubsidy(const json_spirit::Array& params, bool fHelp);
159 extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
160 extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp);
161 extern json_spirit::Value getworkex(const json_spirit::Array& params, bool fHelp);
162 extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
163 extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
164
165 extern json_spirit::Value getnewaddress(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
166 extern json_spirit::Value getaccountaddress(const json_spirit::Array& params, bool fHelp);
167 extern json_spirit::Value setaccount(const json_spirit::Array& params, bool fHelp);
168 extern json_spirit::Value getaccount(const json_spirit::Array& params, bool fHelp);
169 extern json_spirit::Value getaddressesbyaccount(const json_spirit::Array& params, bool fHelp);
170 extern json_spirit::Value sendtoaddress(const json_spirit::Array& params, bool fHelp);
171 extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
172 extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
173 extern json_spirit::Value getreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
174 extern json_spirit::Value getreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
175 extern json_spirit::Value getbalance(const json_spirit::Array& params, bool fHelp);
176 extern json_spirit::Value movecmd(const json_spirit::Array& params, bool fHelp);
177 extern json_spirit::Value sendfrom(const json_spirit::Array& params, bool fHelp);
178 extern json_spirit::Value sendmany(const json_spirit::Array& params, bool fHelp);
179 extern json_spirit::Value addmultisigaddress(const json_spirit::Array& params, bool fHelp);
180 extern json_spirit::Value addredeemscript(const json_spirit::Array& params, bool fHelp);
181 extern json_spirit::Value listreceivedbyaddress(const json_spirit::Array& params, bool fHelp);
182 extern json_spirit::Value listreceivedbyaccount(const json_spirit::Array& params, bool fHelp);
183 extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp);
184 extern json_spirit::Value listaddressgroupings(const json_spirit::Array& params, bool fHelp);
185 extern json_spirit::Value listaccounts(const json_spirit::Array& params, bool fHelp);
186 extern json_spirit::Value listsinceblock(const json_spirit::Array& params, bool fHelp);
187 extern json_spirit::Value gettransaction(const json_spirit::Array& params, bool fHelp);
188 extern json_spirit::Value backupwallet(const json_spirit::Array& params, bool fHelp);
189 extern json_spirit::Value keypoolrefill(const json_spirit::Array& params, bool fHelp);
190 extern json_spirit::Value walletpassphrase(const json_spirit::Array& params, bool fHelp);
191 extern json_spirit::Value walletpassphrasechange(const json_spirit::Array& params, bool fHelp);
192 extern json_spirit::Value walletlock(const json_spirit::Array& params, bool fHelp);
193 extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool fHelp);
194 extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp);
195 extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp);
196 extern json_spirit::Value reservebalance(const json_spirit::Array& params, bool fHelp);
197 extern json_spirit::Value checkwallet(const json_spirit::Array& params, bool fHelp);
198 extern json_spirit::Value repairwallet(const json_spirit::Array& params, bool fHelp);
199 extern json_spirit::Value resendtx(const json_spirit::Array& params, bool fHelp);
200 extern json_spirit::Value makekeypair(const json_spirit::Array& params, bool fHelp);
201 extern json_spirit::Value mergecoins(const json_spirit::Array& params, bool fHelp);
202
203 extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
204 extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
205 extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
206 extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp);
207 extern json_spirit::Value createmultisig(const json_spirit::Array& params, bool fHelp);
208 extern json_spirit::Value decodescript(const json_spirit::Array& params, bool fHelp);
209 extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp);
210 extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp);
211
212 extern json_spirit::Value getbestblockhash(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp
213 extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp
214 extern json_spirit::Value getdifficulty(const json_spirit::Array& params, bool fHelp);
215 extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp);
216 extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp);
217 extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp);
218 extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp);
219 extern json_spirit::Value getblockbynumber(const json_spirit::Array& params, bool fHelp);
220 extern json_spirit::Value getcheckpoint(const json_spirit::Array& params, bool fHelp);
221
222 #endif