xrpld
Loading...
Searching...
No Matches
SecretKey.h
1#pragma once
2
3#include <xrpl/basics/Buffer.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/protocol/KeyType.h>
7#include <xrpl/protocol/PublicKey.h>
8#include <xrpl/protocol/Seed.h>
9#include <xrpl/protocol/tokens.h>
10
11#include <array>
12#include <cstdint>
13#include <cstring>
14#include <optional>
15#include <string>
16#include <utility>
17
18namespace xrpl {
19
24{
25public:
26 static constexpr std::size_t kSize = 32;
27
28private:
30
31public:
33
34 SecretKey() = delete;
35 SecretKey(SecretKey const&) = default;
37 operator=(SecretKey const&) = default;
38
39 bool
40 operator==(SecretKey const&) = delete;
41 bool
42 operator!=(SecretKey const&) = delete;
43
44 ~SecretKey();
45
47 SecretKey(Slice const& slice);
48
49 [[nodiscard]] std::uint8_t const*
50 data() const
51 {
52 return buf_;
53 }
54
55 [[nodiscard]] std::size_t
56 size() const
57 {
58 return sizeof(buf_);
59 }
60
67 [[nodiscard]] std::string
68 toString() const;
69
70 [[nodiscard]] const_iterator
71 begin() const noexcept
72 {
73 return buf_;
74 }
75
76 [[nodiscard]] const_iterator
77 cbegin() const noexcept
78 {
79 return buf_;
80 }
81
82 [[nodiscard]] const_iterator
83 end() const noexcept
84 {
85 return buf_ + sizeof(buf_);
86 }
87
88 [[nodiscard]] const_iterator
89 cend() const noexcept
90 {
91 return buf_ + sizeof(buf_);
92 }
93};
94
95bool
96operator==(SecretKey const& lhs, SecretKey const& rhs) = delete;
97
98bool
99operator!=(SecretKey const& lhs, SecretKey const& rhs) = delete;
100
101//------------------------------------------------------------------------------
102
106template <>
108parseBase58(TokenType type, std::string const& s);
109
110inline std::string
112{
113 return encodeBase58Token(type, sk.data(), sk.size());
114}
115
119SecretKey
121
125SecretKey
126generateSecretKey(KeyType type, Seed const& seed);
127
131PublicKey
132derivePublicKey(KeyType type, SecretKey const& sk);
133
144generateKeyPair(KeyType type, Seed const& seed);
145
151
159Buffer
160signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
161
162inline Buffer
163signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
164{
165 return signDigest(derivePublicKey(type, sk), sk, digest);
166}
167
168
175Buffer
176sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
177
178inline Buffer
179sign(KeyType type, SecretKey const& sk, Slice const& message)
180{
181 return sign(derivePublicKey(type, sk), sk, message);
182}
183
184
185} // namespace xrpl
A secret key.
Definition SecretKey.h:24
const_iterator cend() const noexcept
Definition SecretKey.h:89
bool operator!=(SecretKey const &)=delete
bool operator==(SecretKey const &)=delete
std::size_t size() const
Definition SecretKey.h:56
std::uint8_t const * data() const
Definition SecretKey.h:50
SecretKey(std::array< std::uint8_t, kSize > const &data)
const_iterator end() const noexcept
Definition SecretKey.h:83
const_iterator cbegin() const noexcept
Definition SecretKey.h:77
SecretKey & operator=(SecretKey const &)=default
SecretKey()=delete
std::uint8_t buf_[kSize]
Definition SecretKey.h:29
SecretKey(SecretKey const &)=default
std::string toString() const
Convert the secret key to a hexadecimal string.
Definition SecretKey.cpp:51
const_iterator begin() const noexcept
Definition SecretKey.h:71
std::uint8_t const * const_iterator
Definition SecretKey.h:32
static constexpr std::size_t kSize
Definition SecretKey.h:26
An immutable linear range of bytes.
Definition Slice.h:28
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
KeyType
Definition KeyType.h:8
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:606
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:140
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
bool operator!=(SecretKey const &lhs, SecretKey const &rhs)=delete
Dir::ConstIterator const_iterator
Definition Dir.cpp:16
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
TokenType
Definition tokens.h:19
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition tokens.cpp:181
BaseUInt< 256 > uint256
Definition base_uint.h:580