31 request.
push_back(Pair(
"method", strMethod));
32 request.
push_back(Pair(
"params", params));
52 return reply.
write() +
"\n";
59 error.
push_back(Pair(
"message", message));
66 static const std::string COOKIEAUTH_USER =
"__cookie__";
68 static const std::string COOKIEAUTH_FILE =
".cookie";
71 static fs::path GetAuthCookieFile(
bool temp=
false)
73 std::string arg =
gArgs.
GetArg(
"-rpccookiefile", COOKIEAUTH_FILE);
78 if (!path.is_complete()) path =
GetDataDir() / path;
84 const size_t COOKIE_SIZE = 32;
85 unsigned char rand_pwd[COOKIE_SIZE];
87 std::string cookie = COOKIEAUTH_USER +
":" +
HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
93 fs::path filepath_tmp = GetAuthCookieFile(
true);
94 file.open(filepath_tmp.string().c_str());
95 if (!file.is_open()) {
96 LogPrintf(
"Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
102 fs::path filepath = GetAuthCookieFile(
false);
104 LogPrintf(
"Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
107 LogPrintf(
"Generated RPC authentication cookie %s\n", filepath.string());
110 *cookie_out = cookie;
118 fs::path filepath = GetAuthCookieFile();
119 file.open(filepath.string().c_str());
122 std::getline(file, cookie);
126 *cookie_out = cookie;
133 fs::remove(GetAuthCookieFile());
134 }
catch (
const fs::filesystem_error& e) {
135 LogPrintf(
"%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
142 throw std::runtime_error(
"Batch must be an array");
144 std::vector<UniValue> batch(num);
145 for (
size_t i=0; i<in.
size(); ++i) {
148 throw std::runtime_error(
"Batch member must be object");
150 size_t id = rec[
"id"].
get_int();
152 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)
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
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.
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
bool push_back(const UniValue &val)
bool RenameOver(fs::path src, fs::path dest)
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)
const fs::path & GetDataDir(bool fNetSpecific)
UniValue JSONRPCError(int code, const std::string &message)
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)