]>
Commit | Line | Data |
---|---|---|
8bd66202 | 1 | // Copyright (c) 2010 Satoshi Nakamoto |
88216419 | 2 | // Copyright (c) 2009-2012 The Bitcoin developers |
8bd66202 | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
8bd66202 | 5 | |
e46704dd PW |
6 | #ifndef _BITCOINRPC_H_ |
7 | #define _BITCOINRPC_H_ 1 | |
8 | ||
9 | #include <string> | |
899d373b | 10 | #include <list> |
e46704dd PW |
11 | #include <map> |
12 | ||
c625ae04 JG |
13 | class CBlockIndex; |
14 | ||
e46704dd PW |
15 | #include "json/json_spirit_reader_template.h" |
16 | #include "json/json_spirit_writer_template.h" | |
17 | #include "json/json_spirit_utils.h" | |
18 | ||
b4b7ed19 GA |
19 | #include "util.h" |
20 | ||
285746d3 WL |
21 | // HTTP status codes |
22 | enum HTTPStatusCode | |
23 | { | |
24 | HTTP_OK = 200, | |
25 | HTTP_BAD_REQUEST = 400, | |
26 | HTTP_UNAUTHORIZED = 401, | |
27 | HTTP_FORBIDDEN = 403, | |
28 | HTTP_NOT_FOUND = 404, | |
29 | HTTP_INTERNAL_SERVER_ERROR = 500, | |
30 | }; | |
31 | ||
738835d7 WL |
32 | // Bitcoin RPC error codes |
33 | enum RPCErrorCode | |
34 | { | |
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, | |
41 | ||
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 | |
51 | ||
52 | // P2P client errors | |
53 | RPC_CLIENT_NOT_CONNECTED = -9, // Bitcoin is not connected | |
54 | RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, // Still downloading initial blocks | |
55 | ||
56 | // Wallet errors | |
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 | |
66 | }; | |
67 | ||
480d44bd JG |
68 | json_spirit::Object JSONRPCError(int code, const std::string& message); |
69 | ||
8bd66202 GA |
70 | void ThreadRPCServer(void* parg); |
71 | int CommandLineRPC(int argc, char *argv[]); | |
e46704dd | 72 | |
460c51fd WL |
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); | |
75 | ||
899d373b GA |
76 | /* |
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)); | |
80 | */ | |
81 | void RPCTypeCheck(const json_spirit::Array& params, | |
cc6dfd1f | 82 | const std::list<json_spirit::Value_type>& typesExpected, bool fAllowNull=false); |
899d373b GA |
83 | /* |
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)); | |
86 | */ | |
87 | void RPCTypeCheck(const json_spirit::Object& o, | |
cc6dfd1f | 88 | const std::map<std::string, json_spirit::Value_type>& typesExpected, bool fAllowNull=false); |
899d373b | 89 | |
e46704dd PW |
90 | typedef json_spirit::Value(*rpcfn_type)(const json_spirit::Array& params, bool fHelp); |
91 | ||
92 | class CRPCCommand | |
93 | { | |
94 | public: | |
95 | std::string name; | |
96 | rpcfn_type actor; | |
97 | bool okSafeMode; | |
0e1d3551 | 98 | bool unlocked; |
e46704dd PW |
99 | }; |
100 | ||
460c51fd WL |
101 | /** |
102 | * Bitcoin RPC command dispatcher. | |
103 | */ | |
e46704dd PW |
104 | class CRPCTable |
105 | { | |
106 | private: | |
107 | std::map<std::string, const CRPCCommand*> mapCommands; | |
108 | public: | |
109 | CRPCTable(); | |
110 | const CRPCCommand* operator[](std::string name) const; | |
111 | std::string help(std::string name) const; | |
460c51fd WL |
112 | |
113 | /** | |
114 | * Execute a method. | |
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. | |
119 | */ | |
120 | json_spirit::Value execute(const std::string &method, const json_spirit::Array ¶ms) const; | |
e46704dd PW |
121 | }; |
122 | ||
123 | extern const CRPCTable tableRPC; | |
124 | ||
e3bc5698 JG |
125 | extern int64 nWalletUnlockTime; |
126 | extern int64 AmountFromValue(const json_spirit::Value& value); | |
127 | extern json_spirit::Value ValueFromAmount(int64 amount); | |
c625ae04 JG |
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(); | |
e3bc5698 JG |
132 | |
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); | |
137 | ||
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); | |
145 | ||
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); | |
34226be7 | 161 | extern json_spirit::Value createmultisig(const json_spirit::Array& params, bool fHelp); |
e3bc5698 JG |
162 | extern json_spirit::Value listreceivedbyaddress(const json_spirit::Array& params, bool fHelp); |
163 | extern json_spirit::Value listreceivedbyaccount(const json_spirit::Array& params, bool fHelp); | |
164 | extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp); | |
22dfd735 | 165 | extern json_spirit::Value listaddressgroupings(const json_spirit::Array& params, bool fHelp); |
e3bc5698 JG |
166 | extern json_spirit::Value listaccounts(const json_spirit::Array& params, bool fHelp); |
167 | extern json_spirit::Value listsinceblock(const json_spirit::Array& params, bool fHelp); | |
168 | extern json_spirit::Value gettransaction(const json_spirit::Array& params, bool fHelp); | |
169 | extern json_spirit::Value backupwallet(const json_spirit::Array& params, bool fHelp); | |
170 | extern json_spirit::Value keypoolrefill(const json_spirit::Array& params, bool fHelp); | |
171 | extern json_spirit::Value walletpassphrase(const json_spirit::Array& params, bool fHelp); | |
172 | extern json_spirit::Value walletpassphrasechange(const json_spirit::Array& params, bool fHelp); | |
173 | extern json_spirit::Value walletlock(const json_spirit::Array& params, bool fHelp); | |
174 | extern json_spirit::Value encryptwallet(const json_spirit::Array& params, bool fHelp); | |
175 | extern json_spirit::Value validateaddress(const json_spirit::Array& params, bool fHelp); | |
c625ae04 | 176 | extern json_spirit::Value getinfo(const json_spirit::Array& params, bool fHelp); |
e3bc5698 JG |
177 | |
178 | extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp | |
179 | extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp); | |
fdbb537d JG |
180 | extern json_spirit::Value lockunspent(const json_spirit::Array& params, bool fHelp); |
181 | extern json_spirit::Value listlockunspent(const json_spirit::Array& params, bool fHelp); | |
e3bc5698 JG |
182 | extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp); |
183 | extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp); | |
184 | extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp); | |
185 | extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp); | |
186 | ||
c625ae04 JG |
187 | extern json_spirit::Value getblockcount(const json_spirit::Array& params, bool fHelp); // in rpcblockchain.cpp |
188 | extern json_spirit::Value getdifficulty(const json_spirit::Array& params, bool fHelp); | |
189 | extern json_spirit::Value settxfee(const json_spirit::Array& params, bool fHelp); | |
190 | extern json_spirit::Value getrawmempool(const json_spirit::Array& params, bool fHelp); | |
191 | extern json_spirit::Value getblockhash(const json_spirit::Array& params, bool fHelp); | |
192 | extern json_spirit::Value getblock(const json_spirit::Array& params, bool fHelp); | |
beeb5761 PW |
193 | extern json_spirit::Value gettxoutsetinfo(const json_spirit::Array& params, bool fHelp); |
194 | extern json_spirit::Value gettxout(const json_spirit::Array& params, bool fHelp); | |
c625ae04 | 195 | |
e46704dd | 196 | #endif |