rippled
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/protocol/KeyType.h>
6#include <xrpl/protocol/PublicKey.h>
7#include <xrpl/protocol/Seed.h>
8#include <xrpl/protocol/tokens.h>
9
10#include <array>
11#include <cstring>
12#include <string>
13
14namespace xrpl {
15
18{
19private:
21
22public:
24
25 SecretKey() = delete;
26 SecretKey(SecretKey const&) = default;
28 operator=(SecretKey const&) = default;
29
30 ~SecretKey();
31
33 SecretKey(Slice const& slice);
34
35 std::uint8_t const*
36 data() const
37 {
38 return buf_;
39 }
40
42 size() const
43 {
44 return sizeof(buf_);
45 }
46
53 to_string() const;
54
56 begin() const noexcept
57 {
58 return buf_;
59 }
60
62 cbegin() const noexcept
63 {
64 return buf_;
65 }
66
68 end() const noexcept
69 {
70 return buf_ + sizeof(buf_);
71 }
72
74 cend() const noexcept
75 {
76 return buf_ + sizeof(buf_);
77 }
78};
79
80inline bool
81operator==(SecretKey const& lhs, SecretKey const& rhs)
82{
83 return lhs.size() == rhs.size() && std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
84}
85
86inline bool
87operator!=(SecretKey const& lhs, SecretKey const& rhs)
88{
89 return !(lhs == rhs);
90}
91
92//------------------------------------------------------------------------------
93
95template <>
97parseBase58(TokenType type, std::string const& s);
98
99inline std::string
101{
102 return encodeBase58Token(type, sk.data(), sk.size());
103}
104
106SecretKey
108
110SecretKey
111generateSecretKey(KeyType type, Seed const& seed);
112
114PublicKey
115derivePublicKey(KeyType type, SecretKey const& sk);
116
126generateKeyPair(KeyType type, Seed const& seed);
127
131
138Buffer
139signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
140
141inline Buffer
142signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
143{
144 return signDigest(derivePublicKey(type, sk), sk, digest);
145}
153Buffer
154sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
155
156inline Buffer
157sign(KeyType type, SecretKey const& sk, Slice const& message)
158{
159 return sign(derivePublicKey(type, sk), sk, message);
160}
163} // namespace xrpl
A secret key.
Definition SecretKey.h:18
const_iterator cend() const noexcept
Definition SecretKey.h:74
std::size_t size() const
Definition SecretKey.h:42
std::uint8_t const * data() const
Definition SecretKey.h:36
std::string to_string() const
Convert the secret key to a hexadecimal string.
Definition SecretKey.cpp:49
const_iterator end() const noexcept
Definition SecretKey.h:68
const_iterator cbegin() const noexcept
Definition SecretKey.h:62
SecretKey & operator=(SecretKey const &)=default
SecretKey()=delete
std::uint8_t buf_[32]
Definition SecretKey.h:20
SecretKey(SecretKey const &)=default
const_iterator begin() const noexcept
Definition SecretKey.h:56
std::uint8_t const * const_iterator
Definition SecretKey.h:23
An immutable linear range of bytes.
Definition Slice.h:26
T memcmp(T... args)
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.
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:137
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:92
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.
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:552
base_uint< 256 > uint256
Definition base_uint.h:526
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:209
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
TokenType
Definition tokens.h:18
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:176