rippled
Loading...
Searching...
No Matches
PublicKey.h
1#pragma once
2
3#include <xrpl/basics/Slice.h>
4#include <xrpl/beast/net/IPEndpoint.h>
5#include <xrpl/protocol/KeyType.h>
6#include <xrpl/protocol/STExchange.h>
7#include <xrpl/protocol/UintTypes.h>
8#include <xrpl/protocol/json_get_or_throw.h>
9#include <xrpl/protocol/tokens.h>
10
11#include <algorithm>
12#include <cstdint>
13#include <cstring>
14#include <optional>
15#include <ostream>
16
17namespace xrpl {
18
42{
43protected:
44 // All the constructed public keys are valid, non-empty and contain 33
45 // bytes of data.
46 static constexpr std::size_t size_ = 33;
47 std::uint8_t buf_[size_]; // should be large enough
48
49public:
51
52public:
53 PublicKey() = delete;
54
55 PublicKey(PublicKey const& other);
57 operator=(PublicKey const& other);
58
64 explicit PublicKey(Slice const& slice);
65
66 std::uint8_t const*
67 data() const noexcept
68 {
69 return buf_;
70 }
71
73 size() const noexcept
74 {
75 return size_;
76 }
77
79 begin() const noexcept
80 {
81 return buf_;
82 }
83
85 cbegin() const noexcept
86 {
87 return buf_;
88 }
89
91 end() const noexcept
92 {
93 return buf_ + size_;
94 }
95
97 cend() const noexcept
98 {
99 return buf_ + size_;
100 }
101
102 Slice
103 slice() const noexcept
104 {
105 return {buf_, size_};
106 }
107
108 operator Slice() const noexcept
109 {
110 return slice();
111 }
112};
113
117operator<<(std::ostream& os, PublicKey const& pk);
118
119inline bool
120operator==(PublicKey const& lhs, PublicKey const& rhs)
121{
122 return std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
123}
124
125inline bool
126operator<(PublicKey const& lhs, PublicKey const& rhs)
127{
128 return std::lexicographical_compare(lhs.data(), lhs.data() + lhs.size(), rhs.data(), rhs.data() + rhs.size());
129}
130
131template <class Hasher>
132void
133hash_append(Hasher& h, PublicKey const& pk)
134{
135 h(pk.data(), pk.size());
136}
137
138template <>
140{
141 explicit STExchange() = default;
142
144
145 static void
147 {
148 t.emplace(Slice(u.data(), u.size()));
149 }
150
152 set(SField const& f, PublicKey const& t)
153 {
154 return std::make_unique<STBlob>(f, t.data(), t.size());
155 }
156};
157
158//------------------------------------------------------------------------------
159
160inline std::string
162{
163 return encodeBase58Token(type, pk.data(), pk.size());
164}
165
166template <>
168parseBase58(TokenType type, std::string const& s);
169
171
198ecdsaCanonicality(Slice const& sig);
199
206[[nodiscard]] std::optional<KeyType>
207publicKeyType(Slice const& slice);
208
209[[nodiscard]] inline std::optional<KeyType>
210publicKeyType(PublicKey const& publicKey)
211{
212 return publicKeyType(publicKey.slice());
213}
217[[nodiscard]] bool
219 PublicKey const& publicKey,
220 uint256 const& digest,
221 Slice const& sig,
222 bool mustBeFullyCanonical = true) noexcept;
223
228[[nodiscard]] bool
229verify(PublicKey const& publicKey, Slice const& m, Slice const& sig) noexcept;
230
232NodeID
233calcNodeID(PublicKey const&);
234
235// VFALCO This belongs in AccountID.h but
236// is here because of header issues
238calcAccountID(PublicKey const& pk);
239
240inline std::string
242 beast::IP::Endpoint const& address,
243 std::optional<PublicKey> const& publicKey = std::nullopt,
244 std::optional<std::string> const& id = std::nullopt)
245{
247 ss << "IP Address: " << address;
248 if (publicKey.has_value())
249 {
250 ss << ", Public Key: " << toBase58(TokenType::NodePublic, *publicKey);
251 }
252 if (id.has_value())
253 {
254 ss << ", Id: " << id.value();
255 }
256 return ss.str();
257}
258} // namespace xrpl
259
260//------------------------------------------------------------------------------
261
262namespace Json {
263template <>
264inline xrpl::PublicKey
265getOrThrow(Json::Value const& v, xrpl::SField const& field)
266{
267 using namespace xrpl;
268 std::string const b58 = getOrThrow<std::string>(v, field);
269 if (auto pubKeyBlob = strUnHex(b58); publicKeyType(makeSlice(*pubKeyBlob)))
270 {
271 return PublicKey{makeSlice(*pubKeyBlob)};
272 }
273 for (auto const tokenType : {TokenType::NodePublic, TokenType::AccountPublic})
274 {
275 if (auto const pk = parseBase58<PublicKey>(tokenType, b58))
276 return *pk;
277 }
278 Throw<JsonTypeMismatchError>(field.getJsonName(), "PublicKey");
279}
280} // namespace Json
Represents a JSON value.
Definition json_value.h:130
A public key.
Definition PublicKey.h:42
const_iterator cbegin() const noexcept
Definition PublicKey.h:85
const_iterator begin() const noexcept
Definition PublicKey.h:79
std::uint8_t const * data() const noexcept
Definition PublicKey.h:67
std::size_t size() const noexcept
Definition PublicKey.h:73
const_iterator end() const noexcept
Definition PublicKey.h:91
std::uint8_t buf_[size_]
Definition PublicKey.h:47
std::uint8_t const * const_iterator
Definition PublicKey.h:50
Slice slice() const noexcept
Definition PublicKey.h:103
PublicKey()=delete
const_iterator cend() const noexcept
Definition PublicKey.h:97
PublicKey & operator=(PublicKey const &other)
static constexpr std::size_t size_
Definition PublicKey.h:46
Identifies fields.
Definition SField.h:126
std::size_t size() const
Definition STBlob.h:88
std::uint8_t const * data() const
Definition STBlob.h:94
An immutable linear range of bytes.
Definition Slice.h:26
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:5
xrpl::AccountID getOrThrow(Json::Value const &v, xrpl::SField const &field)
Definition AccountID.h:111
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:198
base_uint< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition UintTypes.h:39
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:137
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.
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) noexcept
Verify a signature on a message.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
std::optional< ECDSACanonicality > ecdsaCanonicality(Slice const &sig)
Determines the canonicality of a signature.
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:612
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:552
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
base_uint< 256 > uint256
Definition base_uint.h:526
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:241
AccountID calcAccountID(PublicKey const &pk)
TokenType
Definition tokens.h:18
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:174
ECDSACanonicality
Definition PublicKey.h:170
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition tokens.cpp:176
T str(T... args)
static std::unique_ptr< STBlob > set(SField const &f, PublicKey const &t)
Definition PublicKey.h:152
static void get(std::optional< value_type > &t, STBlob const &u)
Definition PublicKey.h:146
Convert between serialized type U and C++ type T.
Definition STExchange.h:22