mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Simplify RipplePublicKey:
This implements the bare minimum necessary to store a 33 byte public key and use it in ordered containers. It is an efficient and well defined alternative to RippleAddress when the caller only needs a node public key.
This commit is contained in:
@@ -492,7 +492,7 @@ ConnectAttempt::onReadBody (error_code ec,
|
|||||||
"Cluster name: " << name;
|
"Cluster name: " << name;
|
||||||
|
|
||||||
auto const result = overlay_.peerFinder().activate (
|
auto const result = overlay_.peerFinder().activate (
|
||||||
slot_, RipplePublicKey(publicKey), cluster);
|
slot_, publicKey.toPublicKey(), cluster);
|
||||||
if (result != PeerFinder::Result::success)
|
if (result != PeerFinder::Result::success)
|
||||||
return fail("Outbound slots full");
|
return fail("Outbound slots full");
|
||||||
|
|
||||||
@@ -605,7 +605,7 @@ ConnectAttempt::processResponse (beast::http::message const& m,
|
|||||||
"Cluster name: " << name;
|
"Cluster name: " << name;
|
||||||
|
|
||||||
auto const result = overlay_.peerFinder().activate (slot_,
|
auto const result = overlay_.peerFinder().activate (slot_,
|
||||||
RipplePublicKey(publicKey), clusterNode);
|
publicKey.toPublicKey(), clusterNode);
|
||||||
if (result != PeerFinder::Result::success)
|
if (result != PeerFinder::Result::success)
|
||||||
return fail("Outbound slots full");
|
return fail("Outbound slots full");
|
||||||
|
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
|
|||||||
publicKey, name);
|
publicKey, name);
|
||||||
|
|
||||||
auto const result = m_peerFinder->activate (slot,
|
auto const result = m_peerFinder->activate (slot,
|
||||||
RipplePublicKey(publicKey), cluster);
|
publicKey.toPublicKey(), cluster);
|
||||||
if (result != PeerFinder::Result::success)
|
if (result != PeerFinder::Result::success)
|
||||||
{
|
{
|
||||||
if (journal.trace) journal.trace <<
|
if (journal.trace) journal.trace <<
|
||||||
@@ -409,7 +409,7 @@ OverlayImpl::add_active (std::shared_ptr<PeerImp> const& peer)
|
|||||||
journal_.debug <<
|
journal_.debug <<
|
||||||
"activated " << peer->getRemoteAddress() <<
|
"activated " << peer->getRemoteAddress() <<
|
||||||
" (" << peer->id() <<
|
" (" << peer->id() <<
|
||||||
":" << RipplePublicKey(peer->getNodePublic()) << ")";
|
":" << peer->getNodePublic().toPublicKey() << ")";
|
||||||
|
|
||||||
// As we are not on the strand, run() must be called
|
// As we are not on the strand, run() must be called
|
||||||
// while holding the lock, otherwise new I/O can be
|
// while holding the lock, otherwise new I/O can be
|
||||||
@@ -569,7 +569,7 @@ OverlayImpl::activate (std::shared_ptr<PeerImp> const& peer)
|
|||||||
journal_.debug <<
|
journal_.debug <<
|
||||||
"activated " << peer->getRemoteAddress() <<
|
"activated " << peer->getRemoteAddress() <<
|
||||||
" (" << peer->id() <<
|
" (" << peer->id() <<
|
||||||
":" << RipplePublicKey(peer->getNodePublic()) << ")";
|
":" << peer->getNodePublic().toPublicKey() << ")";
|
||||||
|
|
||||||
// We just accepted this peer so we have non-zero active peers
|
// We just accepted this peer so we have non-zero active peers
|
||||||
assert(size() != 0);
|
assert(size() != 0);
|
||||||
|
|||||||
@@ -800,7 +800,7 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMHello> const& m)
|
|||||||
hello_ = *m;
|
hello_ = *m;
|
||||||
|
|
||||||
auto const result = overlay_.peerFinder().activate (slot_,
|
auto const result = overlay_.peerFinder().activate (slot_,
|
||||||
RipplePublicKey (publicKey_), cluster);
|
publicKey_.toPublicKey(), cluster);
|
||||||
|
|
||||||
if (result == PeerFinder::Result::success)
|
if (result == PeerFinder::Result::success)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <ripple/peerfinder/Manager.h>
|
#include <ripple/peerfinder/Manager.h>
|
||||||
#include <ripple/peerfinder/Slot.h>
|
#include <ripple/peerfinder/Slot.h>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
namespace PeerFinder {
|
namespace PeerFinder {
|
||||||
|
|||||||
@@ -22,13 +22,13 @@
|
|||||||
|
|
||||||
#include <ripple/crypto/Base58Data.h>
|
#include <ripple/crypto/Base58Data.h>
|
||||||
#include <ripple/crypto/ECDSACanonical.h>
|
#include <ripple/crypto/ECDSACanonical.h>
|
||||||
|
#include <ripple/types/RipplePublicKey.h>
|
||||||
#include <ripple/types/UInt160.h>
|
#include <ripple/types/UInt160.h>
|
||||||
#include <ripple/protocol/UintTypes.h>
|
#include <ripple/protocol/UintTypes.h>
|
||||||
#include <ripple/types/RippleAccountID.h>
|
#include <ripple/types/RippleAccountID.h>
|
||||||
#include <ripple/types/RippleAccountPrivateKey.h>
|
#include <ripple/types/RippleAccountPrivateKey.h>
|
||||||
#include <ripple/types/RippleAccountPublicKey.h>
|
#include <ripple/types/RippleAccountPublicKey.h>
|
||||||
#include <ripple/types/RipplePrivateKey.h>
|
#include <ripple/types/RipplePrivateKey.h>
|
||||||
#include <ripple/types/RipplePublicKey.h>
|
|
||||||
#include <ripple/types/RipplePublicKeyHash.h>
|
#include <ripple/types/RipplePublicKeyHash.h>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
@@ -69,6 +69,12 @@ public:
|
|||||||
|
|
||||||
static void clearCache ();
|
static void clearCache ();
|
||||||
|
|
||||||
|
/** Returns the public key.
|
||||||
|
Precondition: version == VER_NODE_PUBLIC
|
||||||
|
*/
|
||||||
|
RipplePublicKey
|
||||||
|
toPublicKey() const;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Node Public - Also used for Validators
|
// Node Public - Also used for Validators
|
||||||
//
|
//
|
||||||
@@ -115,15 +121,6 @@ public:
|
|||||||
Base58::Alphabet const& alphabet = Base58::getRippleAlphabet());
|
Base58::Alphabet const& alphabet = Base58::getRippleAlphabet());
|
||||||
void setAccountID (Account const& hash160In);
|
void setAccountID (Account const& hash160In);
|
||||||
|
|
||||||
#if 0
|
|
||||||
static RippleAddress createAccountID (std::string const& strAccountID)
|
|
||||||
{
|
|
||||||
RippleAddress na;
|
|
||||||
na.setAccountID (strAccountID);
|
|
||||||
return na;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static RippleAddress createAccountID (Account const& uiAccountID);
|
static RippleAddress createAccountID (Account const& uiAccountID);
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -287,17 +284,6 @@ operator>=(RippleAddress const& lhs, RippleAddress const& rhs)
|
|||||||
return !(lhs < rhs);
|
return !(lhs < rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** RipplePublicKey */
|
|
||||||
template <>
|
|
||||||
struct RipplePublicKeyTraits::assign <RippleAddress>
|
|
||||||
{
|
|
||||||
void operator() (value_type& value, RippleAddress const& v) const
|
|
||||||
{
|
|
||||||
Blob const& b (v.getNodePublic ());
|
|
||||||
construct (&b.front(), &b.back()+1, value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** RipplePublicKeyHash */
|
/** RipplePublicKeyHash */
|
||||||
template <>
|
template <>
|
||||||
struct RipplePublicKeyHashTraits::assign <RippleAddress>
|
struct RipplePublicKeyHashTraits::assign <RippleAddress>
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
#include <ripple/crypto/RFC1751.h>
|
#include <ripple/crypto/RFC1751.h>
|
||||||
#include <ripple/protocol/RippleAddress.h>
|
#include <ripple/protocol/RippleAddress.h>
|
||||||
#include <ripple/protocol/Serializer.h>
|
#include <ripple/protocol/Serializer.h>
|
||||||
|
#include <ripple/types/RipplePublicKey.h>
|
||||||
#include <beast/unit_test/suite.h>
|
#include <beast/unit_test/suite.h>
|
||||||
#include <openssl/ripemd.h>
|
#include <openssl/ripemd.h>
|
||||||
#include <openssl/bn.h>
|
#include <openssl/bn.h>
|
||||||
@@ -137,6 +138,13 @@ RippleAddress RippleAddress::createNodePublic (std::string const& strPublic)
|
|||||||
return naNew;
|
return naNew;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RipplePublicKey
|
||||||
|
RippleAddress::toPublicKey() const
|
||||||
|
{
|
||||||
|
assert (nVersion == VER_NODE_PUBLIC);
|
||||||
|
return RipplePublicKey (vchData.begin(), vchData.end());
|
||||||
|
}
|
||||||
|
|
||||||
NodeID RippleAddress::getNodeID () const
|
NodeID RippleAddress::getNodeID () const
|
||||||
{
|
{
|
||||||
switch (nVersion)
|
switch (nVersion)
|
||||||
@@ -969,7 +977,7 @@ public:
|
|||||||
expect (deprecatedPublicKey.humanNodePublic () ==
|
expect (deprecatedPublicKey.humanNodePublic () ==
|
||||||
"n94a1u4jAz288pZLtw6yFWVbi89YamiC6JBXPVUj5zmExe5fTVg9",
|
"n94a1u4jAz288pZLtw6yFWVbi89YamiC6JBXPVUj5zmExe5fTVg9",
|
||||||
deprecatedPublicKey.humanNodePublic ());
|
deprecatedPublicKey.humanNodePublic ());
|
||||||
RipplePublicKey publicKey (deprecatedPublicKey);
|
RipplePublicKey publicKey = deprecatedPublicKey.toPublicKey();
|
||||||
expect (publicKey.to_string() == deprecatedPublicKey.humanNodePublic(),
|
expect (publicKey.to_string() == deprecatedPublicKey.humanNodePublic(),
|
||||||
publicKey.to_string());
|
publicKey.to_string());
|
||||||
|
|
||||||
|
|||||||
@@ -20,26 +20,80 @@
|
|||||||
#ifndef RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
|
#ifndef RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
|
||||||
#define RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
|
#define RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
|
||||||
|
|
||||||
#include <ripple/types/CryptoIdentifier.h>
|
#include <ripple/types/Base58.h>
|
||||||
#include <ripple/types/IdentifierType.h>
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iterator>
|
||||||
|
#include <ostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <beast/cxx14/type_traits.h> // <type_traits>
|
||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
|
|
||||||
class RipplePublicKeyTraits
|
// Simplified public key that avoids the complexities of RippleAddress
|
||||||
: public CryptoIdentifier <33, 28, true>
|
class RipplePublicKey
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
std::array<std::uint8_t, 33> data_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename Other>
|
/** Construct from a range of unsigned char. */
|
||||||
struct assign
|
template <class FwdIt, class = std::enable_if_t<std::is_same<unsigned char,
|
||||||
|
typename std::iterator_traits<FwdIt>::value_type>::value>>
|
||||||
|
RipplePublicKey (FwdIt first, FwdIt last);
|
||||||
|
|
||||||
|
template <class = void>
|
||||||
|
std::string
|
||||||
|
to_string() const;
|
||||||
|
|
||||||
|
friend
|
||||||
|
bool
|
||||||
|
operator< (RipplePublicKey const& lhs, RipplePublicKey const& rhs)
|
||||||
{
|
{
|
||||||
void operator() (value_type& value, Other const& other)
|
return lhs.data_ < rhs.data_;
|
||||||
{
|
}
|
||||||
value = other;
|
|
||||||
}
|
friend
|
||||||
};
|
bool
|
||||||
|
operator== (RipplePublicKey const& lhs, RipplePublicKey const& rhs)
|
||||||
|
{
|
||||||
|
return lhs.data_ == rhs.data_;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef IdentifierType <RipplePublicKeyTraits> RipplePublicKey;
|
template <class FwdIt, class>
|
||||||
|
RipplePublicKey::RipplePublicKey (FwdIt first, FwdIt last)
|
||||||
|
{
|
||||||
|
assert(std::distance(first, last) == data_.size());
|
||||||
|
// VFALCO TODO Use 4-arg copy from C++14
|
||||||
|
std::copy (first, last, data_.begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class>
|
||||||
|
std::string
|
||||||
|
RipplePublicKey::to_string() const
|
||||||
|
{
|
||||||
|
// The expanded form of the key is:
|
||||||
|
// <type> <key> <checksum>
|
||||||
|
std::array <std::uint8_t, 1 + 33 + 4> e;
|
||||||
|
e[0] = 28; // node public key type
|
||||||
|
std::copy (data_.begin(), data_.end(), e.begin() + 1);
|
||||||
|
Base58::fourbyte_hash256 (&*(e.begin() + 34), e.data(), 34);
|
||||||
|
// Convert key + checksum to little endian with an extra pad byte
|
||||||
|
std::array <std::uint8_t, 4 + 33 + 1 + 1> le;
|
||||||
|
std::reverse_copy (e.begin(), e.end(), le.begin());
|
||||||
|
le.back() = 0; // make BIGNUM positive
|
||||||
|
return Base58::raw_encode (le.data(),
|
||||||
|
le.data() + le.size(), Base58::getRippleAlphabet(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline
|
||||||
|
std::ostream&
|
||||||
|
operator<< (std::ostream& os, RipplePublicKey const& k)
|
||||||
|
{
|
||||||
|
return os << k.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user