PIVX Core  5.6.99
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2015 The Dash developers
4 // Copyright (c) 2015-2021 The PIVX Core developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #include "rpc/protocol.h"
9 
10 #include "random.h"
11 #include "tinyformat.h"
12 #include "util/system.h"
13 #include "utilstrencodings.h"
14 #include "utiltime.h"
15 
26 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
27 {
28  UniValue request(UniValue::VOBJ);
29  request.pushKV("method", strMethod);
30  request.pushKV("params", params);
31  request.pushKV("id", id);
32  return request;
33 }
34 
35 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
36 {
37  UniValue reply(UniValue::VOBJ);
38  if (!error.isNull())
39  reply.pushKV("result", NullUniValue);
40  else
41  reply.pushKV("result", result);
42  reply.pushKV("error", error);
43  reply.pushKV("id", id);
44  return reply;
45 }
46 
47 std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
48 {
49  UniValue reply = JSONRPCReplyObj(result, error, id);
50  return reply.write() + "\n";
51 }
52 
53 UniValue JSONRPCError(int code, const std::string& message)
54 {
56  error.pushKV("code", code);
57  error.pushKV("message", message);
58  return error;
59 }
60 
64 static const std::string COOKIEAUTH_USER = "__cookie__";
66 static const std::string COOKIEAUTH_FILE = ".cookie";
67 
69 {
70  fs::path path(gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE));
71  return AbsPathForConfigVal(path);
72 }
73 
74 bool GenerateAuthCookie(std::string *cookie_out)
75 {
76  const size_t COOKIE_SIZE = 32;
77  unsigned char rand_pwd[COOKIE_SIZE];
78  GetRandBytes(rand_pwd, COOKIE_SIZE);
79  std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
80 
84  fsbridge::ofstream file;
85  fs::path filepath_tmp = GetAuthCookieFile();
86  file.open(filepath_tmp);
87  if (!file.is_open()) {
88  LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
89  return false;
90  }
91  file << cookie;
92  file.close();
93  LogPrintf("Generated RPC authentication cookie %s\n", filepath_tmp.string());
94 
95  if (cookie_out)
96  *cookie_out = cookie;
97  return true;
98 }
99 
100 bool GetAuthCookie(std::string *cookie_out)
101 {
102  fsbridge::ifstream file;
103  std::string cookie;
104  fs::path filepath = GetAuthCookieFile();
105  file.open(filepath);
106  if (!file.is_open())
107  return false;
108  std::getline(file, cookie);
109  file.close();
110 
111  if (cookie_out)
112  *cookie_out = cookie;
113  return true;
114 }
115 
117 {
118  try {
119  fs::remove(GetAuthCookieFile());
120  } catch (const fs::filesystem_error& e) {
121  LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
122  }
123 }
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:449
@ VOBJ
Definition: univalue.h:21
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
fs::ofstream ofstream
Definition: fs.h:93
std::string get_filesystem_error_message(const fs::filesystem_error &e)
Definition: fs.cpp:130
fs::ifstream ifstream
Definition: fs.h:92
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:579
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
Definition: protocol.cpp:100
fs::path GetAuthCookieFile()
Get name of RPC authentication cookie file.
Definition: protocol.cpp:68
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:47
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue &params, const UniValue &id)
JSON-RPC protocol.
Definition: protocol.cpp:26
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:53
void DeleteAuthCookie()
Delete RPC authentication cookie from disk.
Definition: protocol.cpp:116
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
Definition: protocol.cpp:74
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:35
fs::path AbsPathForConfigVal(const fs::path &path, bool net_specific)
Definition: system.cpp:853
ArgsManager gArgs
Definition: system.cpp:89
bool error(const char *fmt, const Args &... args)
Definition: system.h:77
const UniValue NullUniValue
Definition: univalue.cpp:13
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.