request.h raw
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Limenka developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef LIMENKA_RPC_REQUEST_H
7 #define LIMENKA_RPC_REQUEST_H
8
9 #include <any>
10 #include <optional>
11 #include <string>
12 #include <utility>
13
14 #include <univalue.h>
15 #include <util/fs.h>
16
17 enum class JSONRPCVersion {
18 V1_LEGACY,
19 V2
20 };
21
22 /** JSON-RPC 2.0 request, only used in limenka-cli **/
23 UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
24 UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version);
25 UniValue JSONRPCError(int code, const std::string& message);
26
27 /** Generate a new RPC authentication cookie and write it to disk */
28 bool GenerateAuthCookie(std::string* cookie_out, const std::pair<std::optional<fs::perms>, bool>& cookie_perms);
29 /** Read the RPC authentication cookie from disk */
30 bool GetAuthCookie(std::string *cookie_out);
31 /** Delete RPC authentication cookie from disk */
32 void DeleteAuthCookie();
33 /** Parse JSON-RPC batch reply into a vector */
34 std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in);
35
36 class JSONRPCRequest
37 {
38 public:
39 std::optional<UniValue> id = UniValue::VNULL;
40 std::string strMethod;
41 UniValue params;
42 enum Mode { EXECUTE, GET_HELP, GET_ARGS } mode = EXECUTE;
43 std::string URI;
44 std::string authUser;
45 std::string m_wallet_restriction{"-"};
46 std::string peerAddr;
47 std::any context;
48 JSONRPCVersion m_json_version = JSONRPCVersion::V1_LEGACY;
49
50 void parse(const UniValue& valRequest);
51 [[nodiscard]] bool IsNotification() const { return !id.has_value() && m_json_version == JSONRPCVersion::V2; };
52 };
53
54 #endif // LIMENKA_RPC_REQUEST_H
55