Dash Core  0.12.2.1
P2P Digital Currency
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-2017 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 <stdint.h>
17 #include <fstream>
18 
19 using namespace std;
20 
30 string JSONRPCRequest(const string& strMethod, const UniValue& params, const UniValue& id)
31 {
32  UniValue request(UniValue::VOBJ);
33  request.push_back(Pair("method", strMethod));
34  request.push_back(Pair("params", params));
35  request.push_back(Pair("id", id));
36  return request.write() + "\n";
37 }
38 
40 {
41  UniValue reply(UniValue::VOBJ);
42  if (!error.isNull())
43  reply.push_back(Pair("result", NullUniValue));
44  else
45  reply.push_back(Pair("result", result));
46  reply.push_back(Pair("error", error));
47  reply.push_back(Pair("id", id));
48  return reply;
49 }
50 
51 string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
52 {
53  UniValue reply = JSONRPCReplyObj(result, error, id);
54  return reply.write() + "\n";
55 }
56 
57 UniValue JSONRPCError(int code, const string& message)
58 {
60  error.push_back(Pair("code", code));
61  error.push_back(Pair("message", message));
62  return error;
63 }
64 
68 static const std::string COOKIEAUTH_USER = "__cookie__";
70 static const std::string COOKIEAUTH_FILE = ".cookie";
71 
72 boost::filesystem::path GetAuthCookieFile()
73 {
74  boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE));
75  if (!path.is_complete()) path = GetDataDir() / path;
76  return path;
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  boost::filesystem::path filepath = GetAuthCookieFile();
92  file.open(filepath.string().c_str());
93  if (!file.is_open()) {
94  LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string());
95  return false;
96  }
97  file << cookie;
98  file.close();
99  LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
100 
101  if (cookie_out)
102  *cookie_out = cookie;
103  return true;
104 }
105 
106 bool GetAuthCookie(std::string *cookie_out)
107 {
108  std::ifstream file;
109  std::string cookie;
110  boost::filesystem::path filepath = GetAuthCookieFile();
111  file.open(filepath.string().c_str());
112  if (!file.is_open())
113  return false;
114  std::getline(file, cookie);
115  file.close();
116 
117  if (cookie_out)
118  *cookie_out = cookie;
119  return true;
120 }
121 
123 {
124  try {
125  boost::filesystem::remove(GetAuthCookieFile());
126  } catch (const boost::filesystem::filesystem_error& e) {
127  LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
128  }
129 }
130 
const boost::filesystem::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:547
boost::filesystem::path GetAuthCookieFile()
Definition: protocol.cpp:72
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
void DeleteAuthCookie()
Definition: protocol.cpp:122
string JSONRPCRequest(const string &strMethod, const UniValue &params, const UniValue &id)
Definition: protocol.cpp:30
bool push_back(const UniValue &val)
Definition: univalue.cpp:176
bool GetAuthCookie(std::string *cookie_out)
Definition: protocol.cpp:106
#define LogPrintf(...)
Definition: util.h:98
static bool error(const char *format)
Definition: util.h:131
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:166
bool GenerateAuthCookie(std::string *cookie_out)
Definition: protocol.cpp:79
static const std::string COOKIEAUTH_USER
Definition: protocol.cpp:68
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
static const std::string COOKIEAUTH_FILE
Definition: protocol.cpp:70
const UniValue NullUniValue
Definition: univalue.cpp:78
void GetRandBytes(unsigned char *buf, int num)
Definition: random.cpp:86
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Definition: util.cpp:441
string JSONRPCReply(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:51
UniValue JSONRPCReplyObj(const UniValue &result, const UniValue &error, const UniValue &id)
Definition: protocol.cpp:39
UniValue JSONRPCError(int code, const string &message)
Definition: protocol.cpp:57
result
Definition: rpcuser.py:37