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{
19public:
20 static constexpr std::size_t size_ = 32;
21
22private:
24
25public:
27
28 SecretKey() = delete;
29 SecretKey(SecretKey const&) = default;
31 operator=(SecretKey const&) = default;
32
33 bool
34 operator==(SecretKey const&) = delete;
35 bool
36 operator!=(SecretKey const&) = delete;
37
38 ~SecretKey();
39
41 SecretKey(Slice const& slice);
42
43 std::uint8_t const*
44 data() const
45 {
46 return buf_;
47 }
48
50 size() const
51 {
52 return sizeof(buf_);
53 }
54
61 to_string() const;
62
64 begin() const noexcept
65 {
66 return buf_;
67 }
68
70 cbegin() const noexcept
71 {
72 return buf_;
73 }
74
76 end() const noexcept
77 {
78 return buf_ + sizeof(buf_);
79 }
80
82 cend() const noexcept
83 {
84 return buf_ + sizeof(buf_);
85 }
86};
87
88inline bool
89operator==(SecretKey const& lhs, SecretKey const& rhs) = delete;
90
91inline bool
92operator!=(SecretKey const& lhs, SecretKey const& rhs) = delete;
93
94//------------------------------------------------------------------------------
95
97template <>
99parseBase58(TokenType type, std::string const& s);
100
101inline std::string
103{
104 return encodeBase58Token(type, sk.data(), sk.size());
105}
106
108SecretKey
110
112SecretKey
113generateSecretKey(KeyType type, Seed const& seed);
114
116PublicKey
117derivePublicKey(KeyType type, SecretKey const& sk);
118
128generateKeyPair(KeyType type, Seed const& seed);
129
133
140Buffer
141signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
142
143inline Buffer
144signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
145{
146 return signDigest(derivePublicKey(type, sk), sk, digest);
147}
155Buffer
156sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
157
158inline Buffer
159sign(KeyType type, SecretKey const& sk, Slice const& message)
160{
161 return sign(derivePublicKey(type, sk), sk, message);
162}
165} // namespace xrpl
A secret key.
Definition SecretKey.h:18
SecretKey(std::array< std::uint8_t, size_ > const &data)
const_iterator cend() const noexcept
Definition SecretKey.h:82
bool operator!=(SecretKey const &)=delete
bool operator==(SecretKey const &)=delete
std::size_t size() const
Definition SecretKey.h:50
std::uint8_t const * data() const
Definition SecretKey.h:44
static constexpr std::size_t size_
Definition SecretKey.h:20
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:76
const_iterator cbegin() const noexcept
Definition SecretKey.h:70
SecretKey & operator=(SecretKey const &)=default
std::uint8_t buf_[size_]
Definition SecretKey.h:23
SecretKey()=delete
SecretKey(SecretKey const &)=default
const_iterator begin() const noexcept
Definition SecretKey.h:64
std::uint8_t const * const_iterator
Definition SecretKey.h:26
An immutable linear range of bytes.
Definition Slice.h:26
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:138
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:557
base_uint< 256 > uint256
Definition base_uint.h:531
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:177