]> Git Repo - VerusCoin.git/blame - src/rpc/protocol.cpp
Don't expect finalization in accepted notarizatoin for submission
[VerusCoin.git] / src / rpc / protocol.cpp
CommitLineData
fb78cc23 1// Copyright (c) 2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
72fb3d29 3// Distributed under the MIT software license, see the accompanying
fb78cc23
WL
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
4519a766 6#include "rpc/protocol.h"
fb78cc23 7
e957192c 8#include "random.h"
ad49c256 9#include "tinyformat.h"
611116d4 10#include "util.h"
ad49c256
WL
11#include "utilstrencodings.h"
12#include "utiltime.h"
6e5fd003 13#include "version.h"
fb78cc23
WL
14
15#include <stdint.h>
e957192c 16#include <fstream>
fb78cc23 17
fb78cc23 18using namespace std;
fb78cc23 19
72fb3d29
MF
20/**
21 * JSON-RPC protocol. Bitcoin speaks version 1.0 for maximum compatibility,
22 * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
23 * unspecified (HTTP errors and contents of 'error').
c246c1ea 24 *
72fb3d29
MF
25 * 1.0 spec: http://json-rpc.org/wiki/specification
26 * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
72fb3d29 27 */
fb78cc23 28
d014114d 29string JSONRPCRequest(const string& strMethod, const UniValue& params, const UniValue& id)
fb78cc23 30{
851f58f9 31 UniValue request(UniValue::VOBJ);
fb78cc23
WL
32 request.push_back(Pair("method", strMethod));
33 request.push_back(Pair("params", params));
34 request.push_back(Pair("id", id));
ed21d5bd 35 return request.write() + "\n";
fb78cc23
WL
36}
37
d014114d 38UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
fb78cc23 39{
851f58f9 40 UniValue reply(UniValue::VOBJ);
ed21d5bd
JG
41 if (!error.isNull())
42 reply.push_back(Pair("result", NullUniValue));
fb78cc23
WL
43 else
44 reply.push_back(Pair("result", result));
45 reply.push_back(Pair("error", error));
46 reply.push_back(Pair("id", id));
47 return reply;
48}
49
d014114d 50string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
fb78cc23 51{
851f58f9 52 UniValue reply = JSONRPCReplyObj(result, error, id);
ed21d5bd 53 return reply.write() + "\n";
fb78cc23
WL
54}
55
851f58f9 56UniValue JSONRPCError(int code, const string& message)
fb78cc23 57{
9756b7bd 58 UniValue error(UniValue::VOBJ);
fb78cc23
WL
59 error.push_back(Pair("code", code));
60 error.push_back(Pair("message", message));
61 return error;
62}
e957192c
WL
63
64/** Username used when cookie authentication is in use (arbitrary, only for
65 * recognizability in debugging/logging purposes)
66 */
67static const std::string COOKIEAUTH_USER = "__cookie__";
68/** Default name for auth cookie file */
69static const std::string COOKIEAUTH_FILE = ".cookie";
70
71boost::filesystem::path GetAuthCookieFile()
72{
73 boost::filesystem::path path(GetArg("-rpccookiefile", COOKIEAUTH_FILE));
74 if (!path.is_complete()) path = GetDataDir() / path;
75 return path;
76}
77
78bool GenerateAuthCookie(std::string *cookie_out)
79{
80 unsigned char rand_pwd[32];
81 GetRandBytes(rand_pwd, 32);
82 std::string cookie = COOKIEAUTH_USER + ":" + EncodeBase64(&rand_pwd[0],32);
83
84 /** the umask determines what permissions are used to create this file -
85 * these are set to 077 in init.cpp unless overridden with -sysperms.
86 */
87 std::ofstream file;
88 boost::filesystem::path filepath = GetAuthCookieFile();
89 file.open(filepath.string().c_str());
90 if (!file.is_open()) {
91 LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath.string());
92 return false;
93 }
94 file << cookie;
95 file.close();
96 LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
97
98 if (cookie_out)
99 *cookie_out = cookie;
100 return true;
101}
102
103bool GetAuthCookie(std::string *cookie_out)
104{
105 std::ifstream file;
106 std::string cookie;
107 boost::filesystem::path filepath = GetAuthCookieFile();
108 file.open(filepath.string().c_str());
109 if (!file.is_open())
110 return false;
111 std::getline(file, cookie);
112 file.close();
113
114 if (cookie_out)
115 *cookie_out = cookie;
116 return true;
117}
118
119void DeleteAuthCookie()
120{
121 try {
122 boost::filesystem::remove(GetAuthCookieFile());
123 } catch (const boost::filesystem::filesystem_error& e) {
124 LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
125 }
126}
127
This page took 0.199394 seconds and 4 git commands to generate.