rippled
Loading...
Searching...
No Matches
PublicKey.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_PUBLICKEY_H_INCLUDED
21#define RIPPLE_PROTOCOL_PUBLICKEY_H_INCLUDED
22
23#include <xrpl/basics/Slice.h>
24#include <xrpl/beast/net/IPEndpoint.h>
25#include <xrpl/protocol/KeyType.h>
26#include <xrpl/protocol/STExchange.h>
27#include <xrpl/protocol/UintTypes.h>
28#include <xrpl/protocol/json_get_or_throw.h>
29#include <xrpl/protocol/tokens.h>
30
31#include <algorithm>
32#include <cstdint>
33#include <cstring>
34#include <optional>
35#include <ostream>
36
37namespace ripple {
38
62{
63protected:
64 // All the constructed public keys are valid, non-empty and contain 33
65 // bytes of data.
66 static constexpr std::size_t size_ = 33;
67 std::uint8_t buf_[size_]; // should be large enough
68
69public:
71
72public:
73 PublicKey() = delete;
74
75 PublicKey(PublicKey const& other);
77 operator=(PublicKey const& other);
78
84 explicit PublicKey(Slice const& slice);
85
86 std::uint8_t const*
87 data() const noexcept
88 {
89 return buf_;
90 }
91
93 size() const noexcept
94 {
95 return size_;
96 }
97
99 begin() const noexcept
100 {
101 return buf_;
102 }
103
105 cbegin() const noexcept
106 {
107 return buf_;
108 }
109
111 end() const noexcept
112 {
113 return buf_ + size_;
114 }
115
117 cend() const noexcept
118 {
119 return buf_ + size_;
120 }
121
122 Slice
123 slice() const noexcept
124 {
125 return {buf_, size_};
126 }
127
128 operator Slice() const noexcept
129 {
130 return slice();
131 }
132};
133
137operator<<(std::ostream& os, PublicKey const& pk);
138
139inline bool
140operator==(PublicKey const& lhs, PublicKey const& rhs)
141{
142 return std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
143}
144
145inline bool
146operator<(PublicKey const& lhs, PublicKey const& rhs)
147{
149 lhs.data(),
150 lhs.data() + lhs.size(),
151 rhs.data(),
152 rhs.data() + rhs.size());
153}
154
155template <class Hasher>
156void
157hash_append(Hasher& h, PublicKey const& pk)
158{
159 h(pk.data(), pk.size());
160}
161
162template <>
164{
165 explicit STExchange() = default;
166
168
169 static void
171 {
172 t.emplace(Slice(u.data(), u.size()));
173 }
174
176 set(SField const& f, PublicKey const& t)
177 {
178 return std::make_unique<STBlob>(f, t.data(), t.size());
179 }
180};
181
182//------------------------------------------------------------------------------
183
184inline std::string
186{
187 return encodeBase58Token(type, pk.data(), pk.size());
188}
189
190template <>
192parseBase58(TokenType type, std::string const& s);
193
195
222ecdsaCanonicality(Slice const& sig);
223
230[[nodiscard]] std::optional<KeyType>
231publicKeyType(Slice const& slice);
232
233[[nodiscard]] inline std::optional<KeyType>
234publicKeyType(PublicKey const& publicKey)
235{
236 return publicKeyType(publicKey.slice());
237}
241[[nodiscard]] bool
243 PublicKey const& publicKey,
244 uint256 const& digest,
245 Slice const& sig,
246 bool mustBeFullyCanonical = true) noexcept;
247
252[[nodiscard]] bool
253verify(
254 PublicKey const& publicKey,
255 Slice const& m,
256 Slice const& sig,
257 bool mustBeFullyCanonical = true) noexcept;
258
260NodeID
261calcNodeID(PublicKey const&);
262
263// VFALCO This belongs in AccountID.h but
264// is here because of header issues
266calcAccountID(PublicKey const& pk);
267
268inline std::string
270 beast::IP::Endpoint const& address,
271 std::optional<PublicKey> const& publicKey = std::nullopt,
272 std::optional<std::string> const& id = std::nullopt)
273{
275 ss << "IP Address: " << address;
276 if (publicKey.has_value())
277 {
278 ss << ", Public Key: " << toBase58(TokenType::NodePublic, *publicKey);
279 }
280 if (id.has_value())
281 {
282 ss << ", Id: " << id.value();
283 }
284 return ss.str();
285}
286} // namespace ripple
287
288//------------------------------------------------------------------------------
289
290namespace Json {
291template <>
293getOrThrow(Json::Value const& v, ripple::SField const& field)
294{
295 using namespace ripple;
296 std::string const b58 = getOrThrow<std::string>(v, field);
297 if (auto pubKeyBlob = strUnHex(b58); publicKeyType(makeSlice(*pubKeyBlob)))
298 {
299 return PublicKey{makeSlice(*pubKeyBlob)};
300 }
301 for (auto const tokenType :
302 {TokenType::NodePublic, TokenType::AccountPublic})
303 {
304 if (auto const pk = parseBase58<PublicKey>(tokenType, b58))
305 return *pk;
306 }
307 Throw<JsonTypeMismatchError>(field.getJsonName(), "PublicKey");
308}
309} // namespace Json
310
311#endif
Represents a JSON value.
Definition json_value.h:149
A public key.
Definition PublicKey.h:62
std::uint8_t const * data() const noexcept
Definition PublicKey.h:87
std::uint8_t const * const_iterator
Definition PublicKey.h:70
std::size_t size() const noexcept
Definition PublicKey.h:93
static constexpr std::size_t size_
Definition PublicKey.h:66
const_iterator begin() const noexcept
Definition PublicKey.h:99
const_iterator cbegin() const noexcept
Definition PublicKey.h:105
const_iterator cend() const noexcept
Definition PublicKey.h:117
const_iterator end() const noexcept
Definition PublicKey.h:111
std::uint8_t buf_[size_]
Definition PublicKey.h:67
Slice slice() const noexcept
Definition PublicKey.h:123
PublicKey & operator=(PublicKey const &other)
Identifies fields.
Definition SField.h:146
std::uint8_t const * data() const
Definition STBlob.h:117
std::size_t size() const
Definition STBlob.h:111
An immutable linear range of bytes.
Definition Slice.h:46
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:25
ripple::AccountID getOrThrow(Json::Value const &v, ripple::SField const &field)
Definition AccountID.h:131
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:48
std::optional< ECDSACanonicality > ecdsaCanonicality(Slice const &sig)
Determines the canonicality of a signature.
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:199
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
TokenType
Definition tokens.h:38
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:194
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:558
base_uint< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition UintTypes.h:59
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition base_uint.h:647
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition tokens.cpp:199
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:156
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:269
bool operator<(Slice const &lhs, Slice const &rhs) noexcept
Definition Slice.h:223
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:585
STL namespace.
T str(T... args)
static void get(std::optional< value_type > &t, STBlob const &u)
Definition PublicKey.h:170
static std::unique_ptr< STBlob > set(SField const &f, PublicKey const &t)
Definition PublicKey.h:176
Convert between serialized type U and C++ type T.
Definition STExchange.h:42