diff --git a/src/rpc/client.h b/src/rpc/client.h --- a/src/rpc/client.h +++ b/src/rpc/client.h @@ -9,14 +9,17 @@ #include /** Convert positional arguments to command-specific RPC representation */ -UniValue RPCConvertValues(const std::string& strMethod, const std::vector& strParams); +UniValue RPCConvertValues(const std::string &strMethod, + const std::vector &strParams); /** Convert named arguments to command-specific RPC representation */ -UniValue RPCConvertNamedValues(const std::string& strMethod, const std::vector& strParams); +UniValue RPCConvertNamedValues(const std::string &strMethod, + const std::vector &strParams); -/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) +/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, + * false, null) * as well as objects and arrays. */ -UniValue ParseNonRFCJSONValue(const std::string& strVal); +UniValue ParseNonRFCJSONValue(const std::string &strVal); #endif // BITCOIN_RPCCLIENT_H diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -7,16 +7,15 @@ #include "rpc/protocol.h" #include "util.h" +#include #include -#include #include // for to_lower() #include using namespace std; -class CRPCConvertParam -{ +class CRPCConvertParam { public: std::string methodName; //!< method whose params want conversion int paramIdx; //!< 0-based idx of param to convert @@ -29,109 +28,107 @@ * * @note Parameter indexes start from 0. */ -static const CRPCConvertParam vRPCConvertParams[] = -{ - { "setmocktime", 0, "timestamp" }, - { "generate", 0, "nblocks" }, - { "generate", 1, "maxtries" }, - { "generatetoaddress", 0, "nblocks" }, - { "generatetoaddress", 2, "maxtries" }, - { "getnetworkhashps", 0, "nblocks" }, - { "getnetworkhashps", 1, "height" }, - { "sendtoaddress", 1, "amount" }, - { "sendtoaddress", 4, "subtractfeefromamount" }, - { "settxfee", 0, "amount" }, - { "getreceivedbyaddress", 1, "minconf" }, - { "getreceivedbyaccount", 1, "minconf" }, - { "listreceivedbyaddress", 0, "minconf" }, - { "listreceivedbyaddress", 1, "include_empty" }, - { "listreceivedbyaddress", 2, "include_watchonly" }, - { "listreceivedbyaccount", 0, "minconf" }, - { "listreceivedbyaccount", 1, "include_empty" }, - { "listreceivedbyaccount", 2, "include_watchonly" }, - { "getbalance", 1, "minconf" }, - { "getbalance", 2, "include_watchonly" }, - { "getblockhash", 0, "height" }, - { "waitforblockheight", 0, "height" }, - { "waitforblockheight", 1, "timeout" }, - { "waitforblock", 1, "timeout" }, - { "waitfornewblock", 0, "timeout" }, - { "move", 2, "amount" }, - { "move", 3, "minconf" }, - { "sendfrom", 2, "amount" }, - { "sendfrom", 3, "minconf" }, - { "listtransactions", 1, "count" }, - { "listtransactions", 2, "skip" }, - { "listtransactions", 3, "include_watchonly" }, - { "listaccounts", 0, "minconf" }, - { "listaccounts", 1, "include_watchonly" }, - { "walletpassphrase", 1, "timeout" }, - { "getblocktemplate", 0, "template_request" }, - { "listsinceblock", 1, "target_confirmations" }, - { "listsinceblock", 2, "include_watchonly" }, - { "sendmany", 1, "amounts" }, - { "sendmany", 2, "minconf" }, - { "sendmany", 4, "subtractfeefrom" }, - { "addmultisigaddress", 0, "nrequired" }, - { "addmultisigaddress", 1, "keys" }, - { "createmultisig", 0, "nrequired" }, - { "createmultisig", 1, "keys" }, - { "listunspent", 0, "minconf" }, - { "listunspent", 1, "maxconf" }, - { "listunspent", 2, "addresses" }, - { "getblock", 1, "verbose" }, - { "getblockheader", 1, "verbose" }, - { "gettransaction", 1, "include_watchonly" }, - { "getrawtransaction", 1, "verbose" }, - { "createrawtransaction", 0, "inputs" }, - { "createrawtransaction", 1, "outputs" }, - { "createrawtransaction", 2, "locktime" }, - { "signrawtransaction", 1, "prevtxs" }, - { "signrawtransaction", 2, "privkeys" }, - { "sendrawtransaction", 1, "allowhighfees" }, - { "fundrawtransaction", 1, "options" }, - { "gettxout", 1, "n" }, - { "gettxout", 2, "include_mempool" }, - { "gettxoutproof", 0, "txids" }, - { "lockunspent", 0, "unlock" }, - { "lockunspent", 1, "transactions" }, - { "importprivkey", 2, "rescan" }, - { "importaddress", 2, "rescan" }, - { "importaddress", 3, "p2sh" }, - { "importpubkey", 2, "rescan" }, - { "importmulti", 0, "requests" }, - { "importmulti", 1, "options" }, - { "verifychain", 0, "checklevel" }, - { "verifychain", 1, "nblocks" }, - { "pruneblockchain", 0, "height" }, - { "keypoolrefill", 0, "newsize" }, - { "getrawmempool", 0, "verbose" }, - { "estimatefee", 0, "nblocks" }, - { "estimatepriority", 0, "nblocks" }, - { "estimatesmartfee", 0, "nblocks" }, - { "estimatesmartpriority", 0, "nblocks" }, - { "prioritisetransaction", 1, "priority_delta" }, - { "prioritisetransaction", 2, "fee_delta" }, - { "setban", 2, "bantime" }, - { "setban", 3, "absolute" }, - { "setnetworkactive", 0, "state" }, - { "getmempoolancestors", 1, "verbose" }, - { "getmempooldescendants", 1, "verbose" }, +static const CRPCConvertParam vRPCConvertParams[] = { + {"setmocktime", 0, "timestamp"}, + {"generate", 0, "nblocks"}, + {"generate", 1, "maxtries"}, + {"generatetoaddress", 0, "nblocks"}, + {"generatetoaddress", 2, "maxtries"}, + {"getnetworkhashps", 0, "nblocks"}, + {"getnetworkhashps", 1, "height"}, + {"sendtoaddress", 1, "amount"}, + {"sendtoaddress", 4, "subtractfeefromamount"}, + {"settxfee", 0, "amount"}, + {"getreceivedbyaddress", 1, "minconf"}, + {"getreceivedbyaccount", 1, "minconf"}, + {"listreceivedbyaddress", 0, "minconf"}, + {"listreceivedbyaddress", 1, "include_empty"}, + {"listreceivedbyaddress", 2, "include_watchonly"}, + {"listreceivedbyaccount", 0, "minconf"}, + {"listreceivedbyaccount", 1, "include_empty"}, + {"listreceivedbyaccount", 2, "include_watchonly"}, + {"getbalance", 1, "minconf"}, + {"getbalance", 2, "include_watchonly"}, + {"getblockhash", 0, "height"}, + {"waitforblockheight", 0, "height"}, + {"waitforblockheight", 1, "timeout"}, + {"waitforblock", 1, "timeout"}, + {"waitfornewblock", 0, "timeout"}, + {"move", 2, "amount"}, + {"move", 3, "minconf"}, + {"sendfrom", 2, "amount"}, + {"sendfrom", 3, "minconf"}, + {"listtransactions", 1, "count"}, + {"listtransactions", 2, "skip"}, + {"listtransactions", 3, "include_watchonly"}, + {"listaccounts", 0, "minconf"}, + {"listaccounts", 1, "include_watchonly"}, + {"walletpassphrase", 1, "timeout"}, + {"getblocktemplate", 0, "template_request"}, + {"listsinceblock", 1, "target_confirmations"}, + {"listsinceblock", 2, "include_watchonly"}, + {"sendmany", 1, "amounts"}, + {"sendmany", 2, "minconf"}, + {"sendmany", 4, "subtractfeefrom"}, + {"addmultisigaddress", 0, "nrequired"}, + {"addmultisigaddress", 1, "keys"}, + {"createmultisig", 0, "nrequired"}, + {"createmultisig", 1, "keys"}, + {"listunspent", 0, "minconf"}, + {"listunspent", 1, "maxconf"}, + {"listunspent", 2, "addresses"}, + {"getblock", 1, "verbose"}, + {"getblockheader", 1, "verbose"}, + {"gettransaction", 1, "include_watchonly"}, + {"getrawtransaction", 1, "verbose"}, + {"createrawtransaction", 0, "inputs"}, + {"createrawtransaction", 1, "outputs"}, + {"createrawtransaction", 2, "locktime"}, + {"signrawtransaction", 1, "prevtxs"}, + {"signrawtransaction", 2, "privkeys"}, + {"sendrawtransaction", 1, "allowhighfees"}, + {"fundrawtransaction", 1, "options"}, + {"gettxout", 1, "n"}, + {"gettxout", 2, "include_mempool"}, + {"gettxoutproof", 0, "txids"}, + {"lockunspent", 0, "unlock"}, + {"lockunspent", 1, "transactions"}, + {"importprivkey", 2, "rescan"}, + {"importaddress", 2, "rescan"}, + {"importaddress", 3, "p2sh"}, + {"importpubkey", 2, "rescan"}, + {"importmulti", 0, "requests"}, + {"importmulti", 1, "options"}, + {"verifychain", 0, "checklevel"}, + {"verifychain", 1, "nblocks"}, + {"pruneblockchain", 0, "height"}, + {"keypoolrefill", 0, "newsize"}, + {"getrawmempool", 0, "verbose"}, + {"estimatefee", 0, "nblocks"}, + {"estimatepriority", 0, "nblocks"}, + {"estimatesmartfee", 0, "nblocks"}, + {"estimatesmartpriority", 0, "nblocks"}, + {"prioritisetransaction", 1, "priority_delta"}, + {"prioritisetransaction", 2, "fee_delta"}, + {"setban", 2, "bantime"}, + {"setban", 3, "absolute"}, + {"setnetworkactive", 0, "state"}, + {"getmempoolancestors", 1, "verbose"}, + {"getmempooldescendants", 1, "verbose"}, // Echo with conversion (For testing only) - { "echojson", 0, "arg0" }, - { "echojson", 1, "arg1" }, - { "echojson", 2, "arg2" }, - { "echojson", 3, "arg3" }, - { "echojson", 4, "arg4" }, - { "echojson", 5, "arg5" }, - { "echojson", 6, "arg6" }, - { "echojson", 7, "arg7" }, - { "echojson", 8, "arg8" }, - { "echojson", 9, "arg9" }, + {"echojson", 0, "arg0"}, + {"echojson", 1, "arg1"}, + {"echojson", 2, "arg2"}, + {"echojson", 3, "arg3"}, + {"echojson", 4, "arg4"}, + {"echojson", 5, "arg5"}, + {"echojson", 6, "arg6"}, + {"echojson", 7, "arg7"}, + {"echojson", 8, "arg8"}, + {"echojson", 9, "arg9"}, }; -class CRPCConvertTable -{ +class CRPCConvertTable { private: std::set> members; std::set> membersByName; @@ -139,16 +136,15 @@ public: CRPCConvertTable(); - bool convert(const std::string& method, int idx) { + bool convert(const std::string &method, int idx) { return (members.count(std::make_pair(method, idx)) > 0); } - bool convert(const std::string& method, const std::string& name) { + bool convert(const std::string &method, const std::string &name) { return (membersByName.count(std::make_pair(method, name)) > 0); } }; -CRPCConvertTable::CRPCConvertTable() -{ +CRPCConvertTable::CRPCConvertTable() { const unsigned int n_elem = (sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0])); @@ -162,24 +158,24 @@ static CRPCConvertTable rpcCvtTable; -/** Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, false, null) - * as well as objects and arrays. +/** + * Non-RFC4627 JSON parser, accepts internal values (such as numbers, true, + * false, null) as well as objects and arrays. */ -UniValue ParseNonRFCJSONValue(const std::string& strVal) -{ +UniValue ParseNonRFCJSONValue(const std::string &strVal) { UniValue jVal; - if (!jVal.read(std::string("[")+strVal+std::string("]")) || - !jVal.isArray() || jVal.size()!=1) - throw runtime_error(string("Error parsing JSON:")+strVal); + if (!jVal.read(std::string("[") + strVal + std::string("]")) || + !jVal.isArray() || jVal.size() != 1) + throw runtime_error(string("Error parsing JSON:") + strVal); return jVal[0]; } -UniValue RPCConvertValues(const std::string &strMethod, const std::vector &strParams) -{ +UniValue RPCConvertValues(const std::string &strMethod, + const std::vector &strParams) { UniValue params(UniValue::VARR); for (unsigned int idx = 0; idx < strParams.size(); idx++) { - const std::string& strVal = strParams[idx]; + const std::string &strVal = strParams[idx]; if (!rpcCvtTable.convert(strMethod, idx)) { // insert string value directly @@ -193,18 +189,20 @@ return params; } -UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector &strParams) -{ +UniValue RPCConvertNamedValues(const std::string &strMethod, + const std::vector &strParams) { UniValue params(UniValue::VOBJ); - for (const std::string &s: strParams) { + for (const std::string &s : strParams) { size_t pos = s.find("="); if (pos == std::string::npos) { - throw(std::runtime_error("No '=' in named argument '"+s+"', this needs to be present for every argument (even if it is empty)")); + throw(std::runtime_error("No '=' in named argument '" + s + + "', this needs to be present for every " + "argument (even if it is empty)")); } std::string name = s.substr(0, pos); - std::string value = s.substr(pos+1); + std::string value = s.substr(pos + 1); if (!rpcCvtTable.convert(strMethod, name)) { // insert string value directly diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -6,81 +6,111 @@ #ifndef BITCOIN_RPCPROTOCOL_H #define BITCOIN_RPCPROTOCOL_H +#include +#include #include #include -#include #include -#include #include //! HTTP status codes -enum HTTPStatusCode -{ - HTTP_OK = 200, - HTTP_BAD_REQUEST = 400, - HTTP_UNAUTHORIZED = 401, - HTTP_FORBIDDEN = 403, - HTTP_NOT_FOUND = 404, - HTTP_BAD_METHOD = 405, +enum HTTPStatusCode { + HTTP_OK = 200, + HTTP_BAD_REQUEST = 400, + HTTP_UNAUTHORIZED = 401, + HTTP_FORBIDDEN = 403, + HTTP_NOT_FOUND = 404, + HTTP_BAD_METHOD = 405, HTTP_INTERNAL_SERVER_ERROR = 500, - HTTP_SERVICE_UNAVAILABLE = 503, + HTTP_SERVICE_UNAVAILABLE = 503, }; //! Bitcoin RPC error codes -enum RPCErrorCode -{ +enum RPCErrorCode { //! Standard JSON-RPC 2.0 errors - RPC_INVALID_REQUEST = -32600, + RPC_INVALID_REQUEST = -32600, RPC_METHOD_NOT_FOUND = -32601, - RPC_INVALID_PARAMS = -32602, - RPC_INTERNAL_ERROR = -32603, - RPC_PARSE_ERROR = -32700, + RPC_INVALID_PARAMS = -32602, + RPC_INTERNAL_ERROR = -32603, + RPC_PARSE_ERROR = -32700, //! General application defined errors - RPC_MISC_ERROR = -1, //!< std::exception thrown in command handling - RPC_FORBIDDEN_BY_SAFE_MODE = -2, //!< Server is in safe mode, and command is not allowed in safe mode - RPC_TYPE_ERROR = -3, //!< Unexpected type was passed as parameter - RPC_INVALID_ADDRESS_OR_KEY = -5, //!< Invalid address or key - RPC_OUT_OF_MEMORY = -7, //!< Ran out of memory during operation - RPC_INVALID_PARAMETER = -8, //!< Invalid, missing or duplicate parameter - RPC_DATABASE_ERROR = -20, //!< Database error - RPC_DESERIALIZATION_ERROR = -22, //!< Error parsing or validating structure in raw format - RPC_VERIFY_ERROR = -25, //!< General error during transaction or block submission - RPC_VERIFY_REJECTED = -26, //!< Transaction or block was rejected by network rules - RPC_VERIFY_ALREADY_IN_CHAIN = -27, //!< Transaction already in chain - RPC_IN_WARMUP = -28, //!< Client still warming up + //!< std::exception thrown in command handling + RPC_MISC_ERROR = -1, + //!< Server is in safe mode, and command is not allowed in safe mode + RPC_FORBIDDEN_BY_SAFE_MODE = -2, + //!< Unexpected type was passed as parameter + RPC_TYPE_ERROR = -3, + //!< Invalid address or key + RPC_INVALID_ADDRESS_OR_KEY = -5, + //!< Ran out of memory during operation + RPC_OUT_OF_MEMORY = -7, + //!< Invalid, missing or duplicate parameter + RPC_INVALID_PARAMETER = -8, + //!< Database error + RPC_DATABASE_ERROR = -20, + //!< Error parsing or validating structure in raw format + RPC_DESERIALIZATION_ERROR = -22, + //!< General error during transaction or block submission + RPC_VERIFY_ERROR = -25, + //!< Transaction or block was rejected by network rules + RPC_VERIFY_REJECTED = -26, + //!< Transaction already in chain + RPC_VERIFY_ALREADY_IN_CHAIN = -27, + //!< Client still warming up + RPC_IN_WARMUP = -28, //! Aliases for backward compatibility - RPC_TRANSACTION_ERROR = RPC_VERIFY_ERROR, - RPC_TRANSACTION_REJECTED = RPC_VERIFY_REJECTED, - RPC_TRANSACTION_ALREADY_IN_CHAIN= RPC_VERIFY_ALREADY_IN_CHAIN, + RPC_TRANSACTION_ERROR = RPC_VERIFY_ERROR, + RPC_TRANSACTION_REJECTED = RPC_VERIFY_REJECTED, + RPC_TRANSACTION_ALREADY_IN_CHAIN = RPC_VERIFY_ALREADY_IN_CHAIN, //! P2P client errors - RPC_CLIENT_NOT_CONNECTED = -9, //!< Bitcoin is not connected - RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, //!< Still downloading initial blocks - RPC_CLIENT_NODE_ALREADY_ADDED = -23, //!< Node is already added - RPC_CLIENT_NODE_NOT_ADDED = -24, //!< Node has not been added before - RPC_CLIENT_NODE_NOT_CONNECTED = -29, //!< Node to disconnect not found in connected nodes - RPC_CLIENT_INVALID_IP_OR_SUBNET = -30, //!< Invalid IP/Subnet - RPC_CLIENT_P2P_DISABLED = -31, //!< No valid connection manager instance found + //!< Bitcoin is not connected + RPC_CLIENT_NOT_CONNECTED = -9, + //!< Still downloading initial blocks + RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, + //!< Node is already added + RPC_CLIENT_NODE_ALREADY_ADDED = -23, + //!< Node has not been added before + RPC_CLIENT_NODE_NOT_ADDED = -24, + //!< Node to disconnect not found in connected nodes + RPC_CLIENT_NODE_NOT_CONNECTED = -29, + //!< Invalid IP/Subnet + RPC_CLIENT_INVALID_IP_OR_SUBNET = -30, + //!< No valid connection manager instance found + RPC_CLIENT_P2P_DISABLED = -31, //! Wallet errors - RPC_WALLET_ERROR = -4, //!< Unspecified problem with wallet (key not found etc.) - RPC_WALLET_INSUFFICIENT_FUNDS = -6, //!< Not enough funds in wallet or account - RPC_WALLET_INVALID_ACCOUNT_NAME = -11, //!< Invalid account name - RPC_WALLET_KEYPOOL_RAN_OUT = -12, //!< Keypool ran out, call keypoolrefill first - RPC_WALLET_UNLOCK_NEEDED = -13, //!< Enter the wallet passphrase with walletpassphrase first - RPC_WALLET_PASSPHRASE_INCORRECT = -14, //!< The wallet passphrase entered was incorrect - RPC_WALLET_WRONG_ENC_STATE = -15, //!< Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.) - RPC_WALLET_ENCRYPTION_FAILED = -16, //!< Failed to encrypt the wallet - RPC_WALLET_ALREADY_UNLOCKED = -17, //!< Wallet is already unlocked + //!< Unspecified problem with wallet (key not found etc.) + RPC_WALLET_ERROR = -4, + //!< Not enough funds in wallet or account + RPC_WALLET_INSUFFICIENT_FUNDS = -6, + //!< Invalid account name + RPC_WALLET_INVALID_ACCOUNT_NAME = -11, + //!< Keypool ran out, call keypoolrefill first + RPC_WALLET_KEYPOOL_RAN_OUT = -12, + //!< Enter the wallet passphrase with walletpassphrase first + RPC_WALLET_UNLOCK_NEEDED = -13, + //!< The wallet passphrase entered was incorrect + RPC_WALLET_PASSPHRASE_INCORRECT = -14, + //!< Command given in wrong wallet encryption state (encrypting an encrypted + //! wallet etc.) + RPC_WALLET_WRONG_ENC_STATE = -15, + //!< Failed to encrypt the wallet + RPC_WALLET_ENCRYPTION_FAILED = -16, + //!< Wallet is already unlocked + RPC_WALLET_ALREADY_UNLOCKED = -17, }; -UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id); -UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id); -std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id); -UniValue JSONRPCError(int code, const std::string& message); +UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue ¶ms, + const UniValue &id); +UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, + const UniValue &id); +std::string JSONRPCReply(const UniValue &result, const UniValue &error, + const UniValue &id); +UniValue JSONRPCError(int code, const std::string &message); /** Get name of RPC authentication cookie file */ boost::filesystem::path GetAuthCookieFile(); diff --git a/src/rpc/protocol.cpp b/src/rpc/protocol.cpp --- a/src/rpc/protocol.cpp +++ b/src/rpc/protocol.cpp @@ -12,22 +12,22 @@ #include "utiltime.h" #include "version.h" -#include +#include #include using namespace std; /** - * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility, - * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were + * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility, but + * uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were * unspecified (HTTP errors and contents of 'error'). - * + * * 1.0 spec: http://json-rpc.org/wiki/specification * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html */ -UniValue JSONRPCRequestObj(const string& strMethod, const UniValue& params, const UniValue& id) -{ +UniValue JSONRPCRequestObj(const string &strMethod, const UniValue ¶ms, + const UniValue &id) { UniValue request(UniValue::VOBJ); request.push_back(Pair("method", strMethod)); request.push_back(Pair("params", params)); @@ -35,8 +35,8 @@ return request; } -UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id) -{ +UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, + const UniValue &id) { UniValue reply(UniValue::VOBJ); if (!error.isNull()) reply.push_back(Pair("result", NullUniValue)); @@ -47,14 +47,13 @@ return reply; } -string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id) -{ +string JSONRPCReply(const UniValue &result, const UniValue &error, + const UniValue &id) { UniValue reply = JSONRPCReplyObj(result, error, id); return reply.write() + "\n"; } -UniValue JSONRPCError(int code, const string& message) -{ +UniValue JSONRPCError(int code, const string &message) { UniValue error(UniValue::VOBJ); error.push_back(Pair("code", code)); error.push_back(Pair("message", message)); @@ -68,19 +67,18 @@ /** Default name for auth cookie file */ static const std::string COOKIEAUTH_FILE = ".cookie"; -boost::filesystem::path GetAuthCookieFile() -{ +boost::filesystem::path GetAuthCookieFile() { boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE)); if (!path.is_complete()) path = GetDataDir() / path; return path; } -bool GenerateAuthCookie(std::string *cookie_out) -{ +bool GenerateAuthCookie(std::string *cookie_out) { const size_t COOKIE_SIZE = 32; unsigned char rand_pwd[COOKIE_SIZE]; GetRandBytes(rand_pwd, COOKIE_SIZE); - std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE); + std::string cookie = + COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd + COOKIE_SIZE); /** the umask determines what permissions are used to create this file - * these are set to 077 in init.cpp unless overridden with -sysperms. @@ -89,40 +87,36 @@ boost::filesystem::path filepath = GetAuthCookieFile(); file.open(filepath.string().c_str()); if (!file.is_open()) { - LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string()); + LogPrintf("Unable to open cookie authentication file %s for writing\n", + filepath.string()); return false; } file << cookie; file.close(); LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); - if (cookie_out) - *cookie_out = cookie; + if (cookie_out) *cookie_out = cookie; return true; } -bool GetAuthCookie(std::string *cookie_out) -{ +bool GetAuthCookie(std::string *cookie_out) { std::ifstream file; std::string cookie; boost::filesystem::path filepath = GetAuthCookieFile(); file.open(filepath.string().c_str()); - if (!file.is_open()) - return false; + if (!file.is_open()) return false; std::getline(file, cookie); file.close(); - if (cookie_out) - *cookie_out = cookie; + if (cookie_out) *cookie_out = cookie; return true; } -void DeleteAuthCookie() -{ +void DeleteAuthCookie() { try { boost::filesystem::remove(GetAuthCookieFile()); - } catch (const boost::filesystem::filesystem_error& e) { - LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what()); + } catch (const boost::filesystem::filesystem_error &e) { + LogPrintf("%s: Unable to remove random auth cookie file: %s\n", + __func__, e.what()); } } -