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/protocol/KeyType.h>
25#include <xrpl/protocol/STExchange.h>
26#include <xrpl/protocol/UintTypes.h>
27#include <xrpl/protocol/json_get_or_throw.h>
28#include <xrpl/protocol/tokens.h>
29
30#include <algorithm>
31#include <cstdint>
32#include <cstring>
33#include <optional>
34#include <ostream>
35
36namespace ripple {
37
61{
62protected:
63 // All the constructed public keys are valid, non-empty and contain 33
64 // bytes of data.
65 static constexpr std::size_t size_ = 33;
66 std::uint8_t buf_[size_]; // should be large enough
67
68public:
70
71public:
72 PublicKey() = delete;
73
74 PublicKey(PublicKey const& other);
76 operator=(PublicKey const& other);
77
83 explicit PublicKey(Slice const& slice);
84
85 std::uint8_t const*
86 data() const noexcept
87 {
88 return buf_;
89 }
90
92 size() const noexcept
93 {
94 return size_;
95 }
96
98 begin() const noexcept
99 {
100 return buf_;
101 }
102
104 cbegin() const noexcept
105 {
106 return buf_;
107 }
108
110 end() const noexcept
111 {
112 return buf_ + size_;
113 }
114
116 cend() const noexcept
117 {
118 return buf_ + size_;
119 }
120
121 Slice
122 slice() const noexcept
123 {
124 return {buf_, size_};
125 }
126
127 operator Slice() const noexcept
128 {
129 return slice();
130 }
131};
132
136operator<<(std::ostream& os, PublicKey const& pk);
137
138inline bool
139operator==(PublicKey const& lhs, PublicKey const& rhs)
140{
141 return std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
142}
143
144inline bool
145operator<(PublicKey const& lhs, PublicKey const& rhs)
146{
148 lhs.data(),
149 lhs.data() + lhs.size(),
150 rhs.data(),
151 rhs.data() + rhs.size());
152}
153
154template <class Hasher>
155void
156hash_append(Hasher& h, PublicKey const& pk)
157{
158 h(pk.data(), pk.size());
159}
160
161template <>
163{
164 explicit STExchange() = default;
165
167
168 static void
170 {
171 t.emplace(Slice(u.data(), u.size()));
172 }
173
175 set(SField const& f, PublicKey const& t)
176 {
177 return std::make_unique<STBlob>(f, t.data(), t.size());
178 }
179};
180
181//------------------------------------------------------------------------------
182
183inline std::string
185{
186 return encodeBase58Token(type, pk.data(), pk.size());
187}
188
189template <>
191parseBase58(TokenType type, std::string const& s);
192
194
221ecdsaCanonicality(Slice const& sig);
222
229[[nodiscard]] std::optional<KeyType>
230publicKeyType(Slice const& slice);
231
232[[nodiscard]] inline std::optional<KeyType>
233publicKeyType(PublicKey const& publicKey)
234{
235 return publicKeyType(publicKey.slice());
236}
240[[nodiscard]] bool
242 PublicKey const& publicKey,
243 uint256 const& digest,
244 Slice const& sig,
245 bool mustBeFullyCanonical = true) noexcept;
246
251[[nodiscard]] bool
252verify(
253 PublicKey const& publicKey,
254 Slice const& m,
255 Slice const& sig,
256 bool mustBeFullyCanonical = true) noexcept;
257
259NodeID
260calcNodeID(PublicKey const&);
261
262// VFALCO This belongs in AccountID.h but
263// is here because of header issues
265calcAccountID(PublicKey const& pk);
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 :
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:149
A public key.
Definition: PublicKey.h:61
std::uint8_t const * data() const noexcept
Definition: PublicKey.h:86
std::uint8_t const * const_iterator
Definition: PublicKey.h:69
std::size_t size() const noexcept
Definition: PublicKey.h:92
static constexpr std::size_t size_
Definition: PublicKey.h:65
const_iterator begin() const noexcept
Definition: PublicKey.h:98
const_iterator cbegin() const noexcept
Definition: PublicKey.h:104
const_iterator cend() const noexcept
Definition: PublicKey.h:116
const_iterator end() const noexcept
Definition: PublicKey.h:110
std::uint8_t buf_[size_]
Definition: PublicKey.h:66
Slice slice() const noexcept
Definition: PublicKey.h:122
PublicKey & operator=(PublicKey const &other)
Definition: PublicKey.cpp:210
Identifies fields.
Definition: SField.h:143
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 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.
Definition: PublicKey.cpp:129
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:199
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:114
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.
Definition: PublicKey.cpp:238
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
ECDSACanonicality
Definition: PublicKey.h:193
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:124
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
Definition: PublicKey.cpp:288
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:637
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)
Definition: AccountID.cpp:168
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:223
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:156
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:244
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:319
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
static void get(std::optional< value_type > &t, STBlob const &u)
Definition: PublicKey.h:169
static std::unique_ptr< STBlob > set(SField const &f, PublicKey const &t)
Definition: PublicKey.h:175
Convert between serialized type U and C++ type T.
Definition: STExchange.h:42