rippled
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 <ripple/basics/Slice.h>
24 #include <ripple/protocol/KeyType.h>
25 #include <ripple/protocol/STExchange.h>
26 #include <ripple/protocol/UintTypes.h>
27 #include <ripple/protocol/json_get_or_throw.h>
28 #include <ripple/protocol/tokens.h>
29 
30 #include <algorithm>
31 #include <cstdint>
32 #include <cstring>
33 #include <optional>
34 #include <ostream>
35 #include <utility>
36 
37 namespace ripple {
38 
61 class PublicKey
62 {
63 protected:
65  std::uint8_t buf_[33]; // should be large enough
66 
67 public:
68  using const_iterator = std::uint8_t const*;
69 
70  PublicKey() = default;
71  PublicKey(PublicKey const& other);
72  PublicKey&
73  operator=(PublicKey const& other);
74 
80  explicit PublicKey(Slice const& slice);
81 
82  std::uint8_t const*
83  data() const noexcept
84  {
85  return buf_;
86  }
87 
89  size() const noexcept
90  {
91  return size_;
92  }
93 
95  begin() const noexcept
96  {
97  return buf_;
98  }
99 
101  cbegin() const noexcept
102  {
103  return buf_;
104  }
105 
107  end() const noexcept
108  {
109  return buf_ + size_;
110  }
111 
113  cend() const noexcept
114  {
115  return buf_ + size_;
116  }
117 
118  bool
119  empty() const noexcept
120  {
121  return size_ == 0;
122  }
123 
124  Slice
125  slice() const noexcept
126  {
127  return {buf_, size_};
128  }
129 
130  operator Slice() const noexcept
131  {
132  return slice();
133  }
134 };
135 
139 operator<<(std::ostream& os, PublicKey const& pk);
140 
141 inline bool
142 operator==(PublicKey const& lhs, PublicKey const& rhs)
143 {
144  return lhs.size() == rhs.size() &&
145  std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
146 }
147 
148 inline bool
149 operator<(PublicKey const& lhs, PublicKey const& rhs)
150 {
152  lhs.data(),
153  lhs.data() + lhs.size(),
154  rhs.data(),
155  rhs.data() + rhs.size());
156 }
157 
158 template <class Hasher>
159 void
160 hash_append(Hasher& h, PublicKey const& pk)
161 {
162  h(pk.data(), pk.size());
163 }
164 
165 template <>
167 {
168  explicit STExchange() = default;
169 
171 
172  static void
174  {
175  t.emplace(Slice(u.data(), u.size()));
176  }
177 
179  set(SField const& f, PublicKey const& t)
180  {
181  return std::make_unique<STBlob>(f, t.data(), t.size());
182  }
183 };
184 
185 //------------------------------------------------------------------------------
186 
187 inline std::string
188 toBase58(TokenType type, PublicKey const& pk)
189 {
190  return encodeBase58Token(type, pk.data(), pk.size());
191 }
192 
193 template <>
195 parseBase58(TokenType type, std::string const& s);
196 
198 
225 ecdsaCanonicality(Slice const& sig);
226 
233 [[nodiscard]] std::optional<KeyType>
234 publicKeyType(Slice const& slice);
235 
236 [[nodiscard]] inline std::optional<KeyType>
237 publicKeyType(PublicKey const& publicKey)
238 {
239  return publicKeyType(publicKey.slice());
240 }
244 [[nodiscard]] bool
246  PublicKey const& publicKey,
247  uint256 const& digest,
248  Slice const& sig,
249  bool mustBeFullyCanonical = true) noexcept;
250 
255 [[nodiscard]] bool
256 verify(
257  PublicKey const& publicKey,
258  Slice const& m,
259  Slice const& sig,
260  bool mustBeFullyCanonical = true) noexcept;
261 
263 NodeID
264 calcNodeID(PublicKey const&);
265 
266 // VFALCO This belongs in AccountID.h but
267 // is here because of header issues
268 AccountID
269 calcAccountID(PublicKey const& pk);
270 
271 } // namespace ripple
272 
273 //------------------------------------------------------------------------------
274 
275 namespace Json {
276 template <>
277 inline ripple::PublicKey
278 getOrThrow(Json::Value const& v, ripple::SField const& field)
279 {
280  using namespace ripple;
281  std::string const b58 = getOrThrow<std::string>(v, field);
282  if (auto pubKeyBlob = strUnHex(b58); publicKeyType(makeSlice(*pubKeyBlob)))
283  {
284  return PublicKey{makeSlice(*pubKeyBlob)};
285  }
286  for (auto const tokenType :
288  {
289  if (auto const pk = parseBase58<PublicKey>(tokenType, b58))
290  return *pk;
291  }
292  Throw<JsonTypeMismatchError>(field.getJsonName(), "PublicKey");
293 }
294 } // namespace Json
295 
296 #endif
ripple::PublicKey::data
std::uint8_t const * data() const noexcept
Definition: PublicKey.h:83
ripple::makeSlice
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:241
ripple::PublicKey::PublicKey
PublicKey()=default
ripple::Dir::const_iterator
Definition: Directory.h:49
std::string
STL class.
utility
ripple::calcNodeID
NodeID calcNodeID(PublicKey const &pk)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:303
ripple::NodeID
base_uint< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition: UintTypes.h:59
cstring
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::verify
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical) noexcept
Verify a signature on a message.
Definition: PublicKey.cpp:272
ripple::PublicKey::empty
bool empty() const noexcept
Definition: PublicKey.h:119
ripple::PublicKey::cend
const_iterator cend() const noexcept
Definition: PublicKey.h:113
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::ECDSACanonicality
ECDSACanonicality
Definition: PublicKey.h:197
std::optional::emplace
T emplace(T... args)
ripple::PublicKey::slice
Slice slice() const noexcept
Definition: PublicKey.h:125
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:303
ripple::ECDSACanonicality::fullyCanonical
@ fullyCanonical
algorithm
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:47
ripple::publicKeyType
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
ripple::STExchange< STBlob, PublicKey >::set
static std::unique_ptr< STBlob > set(SField const &f, PublicKey const &t)
Definition: PublicKey.h:179
Json
JSON (JavaScript Object Notation).
Definition: DeliverMax.h:28
ripple::PublicKey::const_iterator
std::uint8_t const * const_iterator
Definition: PublicKey.h:68
ripple::PublicKey
A public key.
Definition: PublicKey.h:61
ripple::TokenType
TokenType
Definition: tokens.h:29
ripple::operator<
bool operator<(CanonicalTXSet::Key const &lhs, CanonicalTXSet::Key const &rhs)
Definition: CanonicalTXSet.cpp:25
ripple::PublicKey::size
std::size_t size() const noexcept
Definition: PublicKey.h:89
std::ostream
STL class.
ripple::PublicKey::size_
std::size_t size_
Definition: PublicKey.h:64
std::lexicographical_compare
T lexicographical_compare(T... args)
ripple::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:158
ripple::ecdsaCanonicality
std::optional< ECDSACanonicality > ecdsaCanonicality(Slice const &sig)
Determine whether a signature is canonical.
Definition: PublicKey.cpp:114
ripple::STExchange< STBlob, PublicKey >::get
static void get(std::optional< value_type > &t, STBlob const &u)
Definition: PublicKey.h:173
ripple::STExchange
Convert between serialized type U and C++ type T.
Definition: STExchange.h:41
cstdint
std::uint8_t
ripple::PublicKey::operator=
PublicKey & operator=(PublicKey const &other)
Definition: PublicKey.cpp:192
ripple::STBlob::size
std::size_t size() const
Definition: STBlob.h:110
ripple::ECDSACanonicality::canonical
@ canonical
beast::field
field_t< CharT, Traits, Allocator > field(std::basic_string< CharT, Traits, Allocator > const &text, int width=8, int pad=0, bool right=false)
Definition: iosformat.h:162
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::encodeBase58Token
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition: tokens.cpp:199
ripple::SField
Identifies fields.
Definition: SField.h:139
ripple::verifyDigest
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical) noexcept
Verify a secp256k1 signature on the digest of a message.
Definition: PublicKey.cpp:222
ripple::PublicKey::begin
const_iterator begin() const noexcept
Definition: PublicKey.h:95
ripple::TokenType::AccountPublic
@ AccountPublic
ripple::TokenType::NodePublic
@ NodePublic
optional
ripple::PublicKey::buf_
std::uint8_t buf_[33]
Definition: PublicKey.h:65
std::size_t
ripple::PublicKey::end
const_iterator end() const noexcept
Definition: PublicKey.h:107
std::memcmp
T memcmp(T... args)
ostream
ripple::hash_append
void hash_append(Hasher &h, ValidatorBlobInfo const &blobInfo)
Definition: ValidatorList.h:897
ripple::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:114
std::unique_ptr
STL class.
ripple::PublicKey::cbegin
const_iterator cbegin() const noexcept
Definition: PublicKey.h:101
ripple::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition: StringUtilities.h:52
ripple::STBlob
Definition: STBlob.h:33
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STBlob::data
std::uint8_t const * data() const
Definition: STBlob.h:116