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:
Vinnie Falco
2014-12-12 14:20:28 -08:00
parent 2f6af906f4
commit 28b09bde4b
7 changed files with 89 additions and 40 deletions

View File

@@ -492,7 +492,7 @@ ConnectAttempt::onReadBody (error_code ec,
"Cluster name: " << name;
auto const result = overlay_.peerFinder().activate (
slot_, RipplePublicKey(publicKey), cluster);
slot_, publicKey.toPublicKey(), cluster);
if (result != PeerFinder::Result::success)
return fail("Outbound slots full");
@@ -605,7 +605,7 @@ ConnectAttempt::processResponse (beast::http::message const& m,
"Cluster name: " << name;
auto const result = overlay_.peerFinder().activate (slot_,
RipplePublicKey(publicKey), clusterNode);
publicKey.toPublicKey(), clusterNode);
if (result != PeerFinder::Result::success)
return fail("Outbound slots full");

View File

@@ -270,7 +270,7 @@ OverlayImpl::onHandoff (std::unique_ptr <beast::asio::ssl_bundle>&& ssl_bundle,
publicKey, name);
auto const result = m_peerFinder->activate (slot,
RipplePublicKey(publicKey), cluster);
publicKey.toPublicKey(), cluster);
if (result != PeerFinder::Result::success)
{
if (journal.trace) journal.trace <<
@@ -409,7 +409,7 @@ OverlayImpl::add_active (std::shared_ptr<PeerImp> const& peer)
journal_.debug <<
"activated " << peer->getRemoteAddress() <<
" (" << peer->id() <<
":" << RipplePublicKey(peer->getNodePublic()) << ")";
":" << peer->getNodePublic().toPublicKey() << ")";
// As we are not on the strand, run() must be called
// while holding the lock, otherwise new I/O can be
@@ -569,7 +569,7 @@ OverlayImpl::activate (std::shared_ptr<PeerImp> const& peer)
journal_.debug <<
"activated " << peer->getRemoteAddress() <<
" (" << peer->id() <<
":" << RipplePublicKey(peer->getNodePublic()) << ")";
":" << peer->getNodePublic().toPublicKey() << ")";
// We just accepted this peer so we have non-zero active peers
assert(size() != 0);

View File

@@ -800,7 +800,7 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMHello> const& m)
hello_ = *m;
auto const result = overlay_.peerFinder().activate (slot_,
RipplePublicKey (publicKey_), cluster);
publicKey_.toPublicKey(), cluster);
if (result == PeerFinder::Result::success)
{

View File

@@ -22,6 +22,7 @@
#include <ripple/peerfinder/Manager.h>
#include <ripple/peerfinder/Slot.h>
#include <random>
namespace ripple {
namespace PeerFinder {

View File

@@ -22,13 +22,13 @@
#include <ripple/crypto/Base58Data.h>
#include <ripple/crypto/ECDSACanonical.h>
#include <ripple/types/RipplePublicKey.h>
#include <ripple/types/UInt160.h>
#include <ripple/protocol/UintTypes.h>
#include <ripple/types/RippleAccountID.h>
#include <ripple/types/RippleAccountPrivateKey.h>
#include <ripple/types/RippleAccountPublicKey.h>
#include <ripple/types/RipplePrivateKey.h>
#include <ripple/types/RipplePublicKey.h>
#include <ripple/types/RipplePublicKeyHash.h>
namespace ripple {
@@ -69,6 +69,12 @@ public:
static void clearCache ();
/** Returns the public key.
Precondition: version == VER_NODE_PUBLIC
*/
RipplePublicKey
toPublicKey() const;
//
// Node Public - Also used for Validators
//
@@ -115,15 +121,6 @@ public:
Base58::Alphabet const& alphabet = Base58::getRippleAlphabet());
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);
//
@@ -287,17 +284,6 @@ operator>=(RippleAddress const& lhs, RippleAddress const& 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 */
template <>
struct RipplePublicKeyHashTraits::assign <RippleAddress>

View File

@@ -26,6 +26,7 @@
#include <ripple/crypto/RFC1751.h>
#include <ripple/protocol/RippleAddress.h>
#include <ripple/protocol/Serializer.h>
#include <ripple/types/RipplePublicKey.h>
#include <beast/unit_test/suite.h>
#include <openssl/ripemd.h>
#include <openssl/bn.h>
@@ -137,6 +138,13 @@ RippleAddress RippleAddress::createNodePublic (std::string const& strPublic)
return naNew;
}
RipplePublicKey
RippleAddress::toPublicKey() const
{
assert (nVersion == VER_NODE_PUBLIC);
return RipplePublicKey (vchData.begin(), vchData.end());
}
NodeID RippleAddress::getNodeID () const
{
switch (nVersion)
@@ -969,7 +977,7 @@ public:
expect (deprecatedPublicKey.humanNodePublic () ==
"n94a1u4jAz288pZLtw6yFWVbi89YamiC6JBXPVUj5zmExe5fTVg9",
deprecatedPublicKey.humanNodePublic ());
RipplePublicKey publicKey (deprecatedPublicKey);
RipplePublicKey publicKey = deprecatedPublicKey.toPublicKey();
expect (publicKey.to_string() == deprecatedPublicKey.humanNodePublic(),
publicKey.to_string());

View File

@@ -20,26 +20,80 @@
#ifndef RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
#define RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
#include <ripple/types/CryptoIdentifier.h>
#include <ripple/types/IdentifierType.h>
#include <ripple/types/Base58.h>
#include <algorithm>
#include <array>
#include <cstdint>
#include <iterator>
#include <ostream>
#include <stdexcept>
#include <beast/cxx14/type_traits.h> // <type_traits>
namespace ripple {
class RipplePublicKeyTraits
: public CryptoIdentifier <33, 28, true>
// Simplified public key that avoids the complexities of RippleAddress
class RipplePublicKey
{
private:
std::array<std::uint8_t, 33> data_;
public:
template <typename Other>
struct assign
/** Construct from a range of unsigned char. */
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)
{
value = other;
return lhs.data_ < rhs.data_;
}
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();
}
}