rippled
Loading...
Searching...
No Matches
PublicKey.h
1#ifndef XRPL_PROTOCOL_PUBLICKEY_H_INCLUDED
2#define XRPL_PROTOCOL_PUBLICKEY_H_INCLUDED
3
4#include <xrpl/basics/Slice.h>
5#include <xrpl/beast/net/IPEndpoint.h>
6#include <xrpl/protocol/KeyType.h>
7#include <xrpl/protocol/STExchange.h>
8#include <xrpl/protocol/UintTypes.h>
9#include <xrpl/protocol/json_get_or_throw.h>
10#include <xrpl/protocol/tokens.h>
11
12#include <algorithm>
13#include <cstdint>
14#include <cstring>
15#include <optional>
16#include <ostream>
17
18namespace ripple {
19
43{
44protected:
45 // All the constructed public keys are valid, non-empty and contain 33
46 // bytes of data.
47 static constexpr std::size_t size_ = 33;
48 std::uint8_t buf_[size_]; // should be large enough
49
50public:
52
53public:
54 PublicKey() = delete;
55
56 PublicKey(PublicKey const& other);
58 operator=(PublicKey const& other);
59
65 explicit PublicKey(Slice const& slice);
66
67 std::uint8_t const*
68 data() const noexcept
69 {
70 return buf_;
71 }
72
74 size() const noexcept
75 {
76 return size_;
77 }
78
80 begin() const noexcept
81 {
82 return buf_;
83 }
84
86 cbegin() const noexcept
87 {
88 return buf_;
89 }
90
92 end() const noexcept
93 {
94 return buf_ + size_;
95 }
96
98 cend() const noexcept
99 {
100 return buf_ + size_;
101 }
102
103 Slice
104 slice() const noexcept
105 {
106 return {buf_, size_};
107 }
108
109 operator Slice() const noexcept
110 {
111 return slice();
112 }
113};
114
118operator<<(std::ostream& os, PublicKey const& pk);
119
120inline bool
121operator==(PublicKey const& lhs, PublicKey const& rhs)
122{
123 return std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
124}
125
126inline bool
127operator<(PublicKey const& lhs, PublicKey const& rhs)
128{
130 lhs.data(),
131 lhs.data() + lhs.size(),
132 rhs.data(),
133 rhs.data() + rhs.size());
134}
135
136template <class Hasher>
137void
138hash_append(Hasher& h, PublicKey const& pk)
139{
140 h(pk.data(), pk.size());
141}
142
143template <>
145{
146 explicit STExchange() = default;
147
149
150 static void
152 {
153 t.emplace(Slice(u.data(), u.size()));
154 }
155
157 set(SField const& f, PublicKey const& t)
158 {
159 return std::make_unique<STBlob>(f, t.data(), t.size());
160 }
161};
162
163//------------------------------------------------------------------------------
164
165inline std::string
167{
168 return encodeBase58Token(type, pk.data(), pk.size());
169}
170
171template <>
173parseBase58(TokenType type, std::string const& s);
174
176
203ecdsaCanonicality(Slice const& sig);
204
211[[nodiscard]] std::optional<KeyType>
212publicKeyType(Slice const& slice);
213
214[[nodiscard]] inline std::optional<KeyType>
215publicKeyType(PublicKey const& publicKey)
216{
217 return publicKeyType(publicKey.slice());
218}
222[[nodiscard]] bool
224 PublicKey const& publicKey,
225 uint256 const& digest,
226 Slice const& sig,
227 bool mustBeFullyCanonical = true) noexcept;
228
233[[nodiscard]] bool
234verify(
235 PublicKey const& publicKey,
236 Slice const& m,
237 Slice const& sig,
238 bool mustBeFullyCanonical = true) noexcept;
239
241NodeID
242calcNodeID(PublicKey const&);
243
244// VFALCO This belongs in AccountID.h but
245// is here because of header issues
247calcAccountID(PublicKey const& pk);
248
249inline std::string
251 beast::IP::Endpoint const& address,
252 std::optional<PublicKey> const& publicKey = std::nullopt,
253 std::optional<std::string> const& id = std::nullopt)
254{
256 ss << "IP Address: " << address;
257 if (publicKey.has_value())
258 {
259 ss << ", Public Key: " << toBase58(TokenType::NodePublic, *publicKey);
260 }
261 if (id.has_value())
262 {
263 ss << ", Id: " << id.value();
264 }
265 return ss.str();
266}
267} // namespace ripple
268
269//------------------------------------------------------------------------------
270
271namespace Json {
272template <>
274getOrThrow(Json::Value const& v, ripple::SField const& field)
275{
276 using namespace ripple;
277 std::string const b58 = getOrThrow<std::string>(v, field);
278 if (auto pubKeyBlob = strUnHex(b58); publicKeyType(makeSlice(*pubKeyBlob)))
279 {
280 return PublicKey{makeSlice(*pubKeyBlob)};
281 }
282 for (auto const tokenType :
283 {TokenType::NodePublic, TokenType::AccountPublic})
284 {
285 if (auto const pk = parseBase58<PublicKey>(tokenType, b58))
286 return *pk;
287 }
288 Throw<JsonTypeMismatchError>(field.getJsonName(), "PublicKey");
289}
290} // namespace Json
291
292#endif
Represents a JSON value.
Definition json_value.h:131
A public key.
Definition PublicKey.h:43
std::uint8_t const * data() const noexcept
Definition PublicKey.h:68
std::uint8_t const * const_iterator
Definition PublicKey.h:51
std::size_t size() const noexcept
Definition PublicKey.h:74
static constexpr std::size_t size_
Definition PublicKey.h:47
const_iterator begin() const noexcept
Definition PublicKey.h:80
const_iterator cbegin() const noexcept
Definition PublicKey.h:86
const_iterator cend() const noexcept
Definition PublicKey.h:98
const_iterator end() const noexcept
Definition PublicKey.h:92
std::uint8_t buf_[size_]
Definition PublicKey.h:48
Slice slice() const noexcept
Definition PublicKey.h:104
PublicKey & operator=(PublicKey const &other)
Identifies fields.
Definition SField.h:127
std::uint8_t const * data() const
Definition STBlob.h:98
std::size_t size() const
Definition STBlob.h:92
An immutable linear range of bytes.
Definition Slice.h:27
T emplace(T... args)
T is_same_v
T lexicographical_compare(T... args)
T memcmp(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:6
ripple::AccountID getOrThrow(Json::Value const &v, ripple::SField const &field)
Definition AccountID.h:112
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:29
std::optional< ECDSACanonicality > ecdsaCanonicality(Slice const &sig)
Determines the canonicality of a signature.
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:180
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
TokenType
Definition tokens.h:19
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a secp256k1 signature on the digest of a message.
ECDSACanonicality
Definition PublicKey.h:175
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
base_uint< 256 > uint256
Definition base_uint.h:539
base_uint< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition UintTypes.h:40
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:628
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition tokens.cpp:181
AccountID calcAccountID(PublicKey const &pk)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:138
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
std::string getFingerprint(beast::IP::Endpoint const &address, std::optional< PublicKey > const &publicKey=std::nullopt, std::optional< std::string > const &id=std::nullopt)
Definition PublicKey.h:250
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:204
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:566
STL namespace.
T str(T... args)
static void get(std::optional< value_type > &t, STBlob const &u)
Definition PublicKey.h:151
static std::unique_ptr< STBlob > set(SField const &f, PublicKey const &t)
Definition PublicKey.h:157
Convert between serialized type U and C++ type T.
Definition STExchange.h:23