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/tokens.h>
27 #include <ripple/protocol/UintTypes.h>
28 #include <boost/optional.hpp>
29 #include <algorithm>
30 #include <cstdint>
31 #include <cstring>
32 #include <ostream>
33 #include <utility>
34 
35 namespace ripple {
36 
59 class PublicKey
60 {
61 protected:
63  std::uint8_t buf_[33]; // should be large enough
64 
65 public:
66  using const_iterator = std::uint8_t const*;
67 
68  PublicKey() = default;
69  PublicKey (PublicKey const& other);
70  PublicKey& operator= (PublicKey const& other);
71 
77  explicit
78  PublicKey (Slice const& slice);
79 
80  std::uint8_t const*
81  data() const noexcept
82  {
83  return buf_;
84  }
85 
87  size() const noexcept
88  {
89  return size_;
90  }
91 
93  begin() const noexcept
94  {
95  return buf_;
96  }
97 
99  cbegin() const noexcept
100  {
101  return buf_;
102  }
103 
105  end() const noexcept
106  {
107  return buf_ + size_;
108  }
109 
111  cend() const noexcept
112  {
113  return buf_ + size_;
114  }
115 
116  bool
117  empty() const noexcept
118  {
119  return size_ == 0;
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 
137 operator<<(std::ostream& os, PublicKey const& pk);
138 
139 inline
140 bool
142  PublicKey const& rhs)
143 {
144  return lhs.size() == rhs.size() &&
145  std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0;
146 }
147 
148 inline
149 bool
151  PublicKey const& rhs)
152 {
154  lhs.data(), lhs.data() + lhs.size(),
155  rhs.data(), rhs.data() + rhs.size());
156 }
157 
158 template <class Hasher>
159 void
160 hash_append (Hasher& h,
161  PublicKey const& pk)
162 {
163  h(pk.data(), pk.size());
164 }
165 
166 template<>
168 {
169  explicit STExchange() = default;
170 
172 
173  static
174  void
175  get (boost::optional<value_type>& t,
176  STBlob const& u)
177  {
178  t.emplace (Slice(u.data(), u.size()));
179  }
180 
181  static
183  set (SField const& f, PublicKey const& t)
184  {
185  return std::make_unique<STBlob>(
186  f, t.data(), t.size());
187  }
188 };
189 
190 //------------------------------------------------------------------------------
191 
192 inline
194 toBase58 (TokenType type, PublicKey const& pk)
195 {
196  return base58EncodeToken(
197  type, pk.data(), pk.size());
198 }
199 
200 template<>
201 boost::optional<PublicKey>
202 parseBase58 (TokenType type, std::string const& s);
203 
205 {
206  canonical,
208 };
209 
235 boost::optional<ECDSACanonicality>
236 ecdsaCanonicality (Slice const& sig);
237 
244 boost::optional<KeyType>
245 publicKeyType (Slice const& slice);
246 
247 inline
248 boost::optional<KeyType>
249 publicKeyType (PublicKey const& publicKey)
250 {
251  return publicKeyType (publicKey.slice());
252 }
256 bool
257 verifyDigest (PublicKey const& publicKey,
258  uint256 const& digest,
259  Slice const& sig,
260  bool mustBeFullyCanonical = true);
261 
266 bool
267 verify (PublicKey const& publicKey,
268  Slice const& m,
269  Slice const& sig,
270  bool mustBeFullyCanonical = true);
271 
273 NodeID
274 calcNodeID (PublicKey const&);
275 
276 // VFALCO This belongs in AccountID.h but
277 // is here because of header issues
278 AccountID
279 calcAccountID (PublicKey const& pk);
280 
281 } // ripple
282 
283 #endif
ripple::PublicKey::data
std::uint8_t const * data() const noexcept
Definition: PublicKey.h:81
ripple::STBlob::data
std::uint8_t const * data() const
Definition: STBlob.h:83
ripple::PublicKey::PublicKey
PublicKey()=default
ripple::Dir::const_iterator
Definition: Directory.h:49
std::string
STL class.
ripple::base58EncodeToken
std::string base58EncodeToken(TokenType type, void const *token, std::size_t size)
Definition: tokens.cpp:182
utility
ripple::calcNodeID
NodeID calcNodeID(PublicKey const &pk)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:307
ripple::NodeID
base_uint< 160, detail::NodeIDTag > NodeID
NodeID is a 160-bit hash representing one node.
Definition: UintTypes.h:59
cstring
ripple::publicKeyType
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
ripple::STExchange< STBlob, PublicKey >::get
static void get(boost::optional< value_type > &t, STBlob const &u)
Definition: PublicKey.h:175
ripple::PublicKey::empty
bool empty() const noexcept
Definition: PublicKey.h:117
ripple::PublicKey::cend
const_iterator cend() const noexcept
Definition: PublicKey.h:111
ripple::AccountID
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:48
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
ripple::ECDSACanonicality
ECDSACanonicality
Definition: PublicKey.h:204
ripple::PublicKey::slice
Slice slice() const noexcept
Definition: PublicKey.h:123
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:237
ripple::ECDSACanonicality::fullyCanonical
@ fullyCanonical
algorithm
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:148
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:44
ripple::verify
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical)
Verify a signature on a message.
Definition: PublicKey.cpp:277
ripple::STExchange< STBlob, PublicKey >::set
static std::unique_ptr< STBlob > set(SField const &f, PublicKey const &t)
Definition: PublicKey.h:183
ripple::PublicKey::const_iterator
std::uint8_t const * const_iterator
Definition: PublicKey.h:66
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::TokenType
TokenType
Definition: tokens.h:29
ripple::PublicKey::size
std::size_t size() const noexcept
Definition: PublicKey.h:87
std::ostream
STL class.
ripple::PublicKey::size_
std::size_t size_
Definition: PublicKey.h:62
std::lexicographical_compare
T lexicographical_compare(T... args)
ripple::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:136
ripple::STExchange
Convert between serialized type U and C++ type T.
Definition: STExchange.h:41
cstdint
ripple::ecdsaCanonicality
boost::optional< ECDSACanonicality > ecdsaCanonicality(Slice const &sig)
Determine whether a signature is canonical.
Definition: PublicKey.cpp:116
std::uint8_t
ripple::PublicKey::operator=
PublicKey & operator=(PublicKey const &other)
Definition: PublicKey.cpp:196
ripple::verifyDigest
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical)
Verify a secp256k1 signature on the digest of a message.
Definition: PublicKey.cpp:222
ripple::STBlob::size
std::size_t size() const
Definition: STBlob.h:77
ripple::ECDSACanonicality::canonical
@ canonical
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::PublicKey::begin
const_iterator begin() const noexcept
Definition: PublicKey.h:93
ripple::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:151
ripple::PublicKey::buf_
std::uint8_t buf_[33]
Definition: PublicKey.h:63
std::size_t
ripple::PublicKey::end
const_iterator end() const noexcept
Definition: PublicKey.h:105
std::memcmp
T memcmp(T... args)
ripple::operator<
bool operator<(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:456
ostream
std::unique_ptr
STL class.
ripple::PublicKey::cbegin
const_iterator cbegin() const noexcept
Definition: PublicKey.h:99
ripple::STBlob
Definition: STBlob.h:33
ripple::parseBase58
boost::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:36