Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/rpc/request.h
Line
Count
Source
1
// Copyright (c) 2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core 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 BITCOIN_RPC_REQUEST_H
7
#define BITCOIN_RPC_REQUEST_H
8
9
#include <univalue.h>
10
#include <util/fs.h>
11
12
#include <any>
13
#include <cstdint>
14
#include <optional>
15
#include <string>
16
#include <vector>
17
18
enum class JSONRPCVersion {
19
    V1_LEGACY,
20
    V2
21
};
22
23
/** JSON-RPC 2.0 request, only used in bitcoin-cli **/
24
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
25
UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version);
26
UniValue JSONRPCError(int code, const std::string& message);
27
28
enum class AuthCookieResult : uint8_t {
29
    Disabled, // -norpccookiefile
30
    Error,
31
    Ok,
32
};
33
34
/**
35
 * Generate a new RPC authentication cookie and write it to disk
36
 * @param[in] cookie_perms Filesystem permissions to use for the cookie file.
37
 * @param[out] user Generated username, only set if `OK` is returned.
38
 * @param[out] pass Generated password, only set if `OK` is returned.
39
 * @retval AuthCookieResult::Disabled Authentication via cookie is disabled.
40
 * @retval AuthCookieResult::Error Error occurred, auth data could not be saved to disk.
41
 * @retval AuthCookieResult::Ok Auth data was generated, saved to disk and in `user` and `pass`.
42
 */
43
AuthCookieResult GenerateAuthCookie(const std::optional<fs::perms>& cookie_perms,
44
                                            std::string& user,
45
                                            std::string& pass);
46
47
/** Read the RPC authentication cookie from disk */
48
AuthCookieResult GetAuthCookie(std::string& cookie_out);
49
/** Delete RPC authentication cookie from disk */
50
void DeleteAuthCookie();
51
/** Parse JSON-RPC batch reply into a vector */
52
std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in);
53
54
class JSONRPCRequest
55
{
56
public:
57
    std::optional<UniValue> id = UniValue::VNULL;
58
    std::string strMethod;
59
    UniValue params;
60
    enum Mode { EXECUTE, GET_HELP, GET_ARGS } mode = EXECUTE;
61
    std::string URI;
62
    std::string authUser;
63
    std::string peerAddr;
64
    std::any context;
65
    JSONRPCVersion m_json_version = JSONRPCVersion::V1_LEGACY;
66
67
    void parse(const UniValue& valRequest);
68
200k
    [[nodiscard]] bool IsNotification() const { return !id.has_value() && m_json_version == JSONRPCVersion::V2; };
69
};
70
71
#endif // BITCOIN_RPC_REQUEST_H