27 request.
pushKV(
"method", strMethod);
28 request.
pushKV(
"params", params);
39 reply.
pushKV(
"result", result);
48 return reply.
write() +
"\n";
54 error.pushKV(
"code", code);
55 error.pushKV(
"message", message);
62 static const std::string COOKIEAUTH_USER =
"__cookie__";
64 static const std::string COOKIEAUTH_FILE =
".cookie";
67 static fs::path GetAuthCookieFile(
bool temp=
false)
69 std::string arg =
gArgs.
GetArg(
"-rpccookiefile", COOKIEAUTH_FILE);
78 const size_t COOKIE_SIZE = 32;
79 unsigned char rand_pwd[COOKIE_SIZE];
81 std::string cookie = COOKIEAUTH_USER +
":" +
HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
87 fs::path filepath_tmp = GetAuthCookieFile(
true);
88 file.open(filepath_tmp);
89 if (!file.is_open()) {
90 LogPrintf(
"Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
96 fs::path filepath = GetAuthCookieFile(
false);
98 LogPrintf(
"Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
101 LogPrintf(
"Generated RPC authentication cookie %s\n", filepath.string());
104 *cookie_out = cookie;
112 fs::path filepath = GetAuthCookieFile();
116 std::getline(file, cookie);
120 *cookie_out = cookie;
127 fs::remove(GetAuthCookieFile());
128 }
catch (
const fs::filesystem_error& e) {
136 throw std::runtime_error(
"Batch must be an array");
138 std::vector<UniValue> batch(num);
139 for (
size_t i=0; i<in.
size(); ++i) {
142 throw std::runtime_error(
"Batch member must be object");
144 size_t id = rec[
"id"].
get_int();
146 throw std::runtime_error(
"Batch member id larger than size");
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue ¶ms, const UniValue &id)
JSON-RPC protocol.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
std::vector< UniValue > JSONRPCProcessBatchReply(const UniValue &in, size_t num)
Parse JSON-RPC batch reply into a vector.
fs::path AbsPathForConfigVal(const fs::path &path, bool net_specific)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool pushKV(const std::string &key, const UniValue &val)
bool RenameOver(fs::path src, fs::path dest)
std::string get_filesystem_error_message(const fs::filesystem_error &e)
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
const UniValue NullUniValue
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
bool error(const char *fmt, const Args &... args)
UniValue JSONRPCError(int code, const std::string &message)
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)