Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Copyright (c) 2014-2020 The Dash Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <rpc/protocol.h>
8 
9 #include <random.h>
10 #include <tinyformat.h>
11 #include <util.h>
12 #include <utilstrencodings.h>
13 #include <utiltime.h>
14 #include <version.h>
15 
16 #include <fstream>
17 
27 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
28 {
29  UniValue request(UniValue::VOBJ);
30  request.push_back(Pair("method", strMethod));
31  request.push_back(Pair("params", params));
32  request.push_back(Pair("id", id));
33  return request;
34 }
35 
36 UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
37 {
38  UniValue reply(UniValue::VOBJ);
39  if (!error.isNull())
40  reply.push_back(Pair("result", NullUniValue));
41  else
42  reply.push_back(Pair("result", result));
43  reply.push_back(Pair("error", error));
44  reply.push_back(Pair("id", id));
45  return reply;
46 }
47 
48 std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
49 {
50  UniValue reply = JSONRPCReplyObj(result, error, id);
51  return reply.write() + "\n";
52 }
53 
54 UniValue JSONRPCError(int code, const std::string& message)
55 {
57  error.push_back(Pair("code", code));
58  error.push_back(Pair("message", message));
59  return error;
60 }
61 
65 static const std::string COOKIEAUTH_USER = "__cookie__";
67 static const std::string COOKIEAUTH_FILE = ".cookie";
68 
70 static fs::path GetAuthCookieFile(bool temp=false)
71 {
72  std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
73  if (temp) {
74  arg += ".tmp";
75  }
76  return AbsPathForConfigVal(fs::path(arg));
77 }
78 
79 bool GenerateAuthCookie(std::string *cookie_out)
80 {
81  const size_t COOKIE_SIZE = 32;
82  unsigned char rand_pwd[COOKIE_SIZE];
83  GetRandBytes(rand_pwd, COOKIE_SIZE);
84 
85  std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
86 
90  std::ofstream file;
91  fs::path filepath_tmp = GetAuthCookieFile(true);
92  file.open(filepath_tmp.string().c_str());
93  if (!file.is_open()) {
94  LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
95  return false;
96  }
97  file << cookie;
98  file.close();
99 
100  fs::path filepath = GetAuthCookieFile(false);
101  if (!RenameOver(filepath_tmp, filepath)) {
102  LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
103  return false;
104  }
105  LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
106 
107  if (cookie_out)
108  *cookie_out = cookie;
109  return true;
110 }
111 
112 bool GetAuthCookie(std::string *cookie_out)
113 {
114  std::ifstream file;
115  std::string cookie;
116  fs::path filepath = GetAuthCookieFile();
117  file.open(filepath.string().c_str());
118  if (!file.is_open())
119  return false;
120  std::getline(file, cookie);
121  file.close();
122 
123  if (cookie_out)
124  *cookie_out = cookie;
125  return true;
126 }
127 
129 {
130  try {
131  fs::remove(GetAuthCookieFile());
132  } catch (const fs::filesystem_error& e) {
133  LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
134  }
135 }
136 
137 std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue &in, size_t num)
138 {
139  if (!in.isArray()) {
140  throw std::runtime_error("Batch must be an array");
141  }
142  std::vector<UniValue> batch(num);
143  for (size_t i=0; i<in.size(); ++i) {
144  const UniValue &rec = in[i];
145  if (!rec.isObject()) {
146  throw std::runtime_error("Batch member must be object");
147  }
148  size_t id = rec["id"].get_int();
149  if (id >= num) {
150  throw std::runtime_error("Batch member id larger than size");
151  }
152  batch[id] = rec;
153  }
154  return batch;
155 }
bool isObject() const
Definition: univalue.h:85
UniValue JSONRPCRequestObj(const std::string &strMethod, const UniValue &params, const UniValue &id)
JSON-RPC protocol.
Definition: protocol.cpp:27
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.
Definition: protocol.cpp:128
std::string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:48
std::vector< UniValue > JSONRPCProcessBatchReply(const UniValue &in, size_t num)
Parse JSON-RPC batch reply into a vector.
Definition: protocol.cpp:137
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...
Definition: util.cpp:1434
bool GetAuthCookie(std::string *cookie_out)
Read the RPC authentication cookie from disk.
Definition: protocol.cpp:112
bool push_back(const UniValue &val)
Definition: univalue.cpp:110
#define LogPrintf(...)
Definition: util.h:203
static fs::path GetAuthCookieFile(bool temp=false)
Get name of RPC authentication cookie file.
Definition: protocol.cpp:70
int get_int() const
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:185
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:1069
ArgsManager gArgs
Definition: util.cpp:108
bool GenerateAuthCookie(std::string *cookie_out)
Generate a new RPC authentication cookie and write it to disk.
Definition: protocol.cpp:79
static const std::string COOKIEAUTH_USER
Username used when cookie authentication is in use (arbitrary, only for recognizability in debugging/...
Definition: protocol.cpp:65
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:808
static const std::string COOKIEAUTH_FILE
Default name for auth cookie file.
Definition: protocol.cpp:67
const UniValue NullUniValue
Definition: univalue.cpp:15
void GetRandBytes(unsigned char *buf, int num)
Functions to gather random data via the OpenSSL PRNG.
Definition: random.cpp:273
bool error(const char *fmt, const Args &... args)
Definition: util.h:222
UniValue JSONRPCError(int code, const std::string &message)
Definition: protocol.cpp:54
size_t size() const
Definition: univalue.h:69
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:36
bool isArray() const
Definition: univalue.h:84
Released under the MIT license