1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
6 #include "rpc/protocol.h"
9 #include "tinyformat.h"
11 #include "utilstrencodings.h"
21 * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
22 * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
23 * unspecified (HTTP errors and contents of 'error').
25 * 1.0 spec: http://json-rpc.org/wiki/specification
26 * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
29 string JSONRPCRequest(const string& strMethod, const UniValue& params, const UniValue& id)
31 UniValue request(UniValue::VOBJ);
32 request.push_back(Pair("method", strMethod));
33 request.push_back(Pair("params", params));
34 request.push_back(Pair("id", id));
35 return request.write() + "\n";
38 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
40 UniValue reply(UniValue::VOBJ);
42 reply.push_back(Pair("result", NullUniValue));
44 reply.push_back(Pair("result", result));
45 reply.push_back(Pair("error", error));
46 reply.push_back(Pair("id", id));
50 string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
52 UniValue reply = JSONRPCReplyObj(result, error, id);
53 return reply.write() + "\n";
56 UniValue JSONRPCError(int code, const string& message)
58 UniValue error(UniValue::VOBJ);
59 error.push_back(Pair("code", code));
60 error.push_back(Pair("message", message));
64 /** Username used when cookie authentication is in use (arbitrary, only for
65 * recognizability in debugging/logging purposes)
67 static const std::string COOKIEAUTH_USER = "__cookie__";
68 /** Default name for auth cookie file */
69 static const std::string COOKIEAUTH_FILE = ".cookie";
71 boost::filesystem::path GetAuthCookieFile()
73 boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE));
74 if (!path.is_complete()) path = GetDataDir() / path;
78 bool GenerateAuthCookie(std::string *cookie_out)
80 unsigned char rand_pwd[32];
81 GetRandBytes(rand_pwd, 32);
82 std::string cookie = COOKIEAUTH_USER + ":" + EncodeBase64(&rand_pwd[0],32);
84 /** the umask determines what permissions are used to create this file -
85 * these are set to 077 in init.cpp unless overridden with -sysperms.
88 boost::filesystem::path filepath = GetAuthCookieFile();
89 file.open(filepath.string().c_str());
90 if (!file.is_open()) {
91 LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string());
96 LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
103 bool GetAuthCookie(std::string *cookie_out)
107 boost::filesystem::path filepath = GetAuthCookieFile();
108 file.open(filepath.string().c_str());
111 std::getline(file, cookie);
115 *cookie_out = cookie;
119 void DeleteAuthCookie()
122 boost::filesystem::remove(GetAuthCookieFile());
123 } catch (const boost::filesystem::filesystem_error& e) {
124 LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());