Add CryptoIdentifierType and RippleCryptoIdentifier Traits

This commit is contained in:
Vinnie Falco
2013-09-23 10:07:09 -07:00
parent 1dd50f4422
commit 321898f71e
28 changed files with 984 additions and 164 deletions

View File

@@ -130,6 +130,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\types\impl\RippleCryptoIdentifierTests.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\types\impl\strHex.cpp"> <ClCompile Include="..\..\src\ripple\types\impl\strHex.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -1608,8 +1614,17 @@
<ClInclude Include="..\..\src\ripple\types\api\base_uint.h" /> <ClInclude Include="..\..\src\ripple\types\api\base_uint.h" />
<ClInclude Include="..\..\src\ripple\types\api\Blob.h" /> <ClInclude Include="..\..\src\ripple\types\api\Blob.h" />
<ClInclude Include="..\..\src\ripple\types\api\ByteOrder.h" /> <ClInclude Include="..\..\src\ripple\types\api\ByteOrder.h" />
<ClInclude Include="..\..\src\ripple\types\api\CryptoIdentifierStorage.h" />
<ClInclude Include="..\..\src\ripple\types\api\CryptoIdentifierType.h" />
<ClInclude Include="..\..\src\ripple\types\api\HashMaps.h" /> <ClInclude Include="..\..\src\ripple\types\api\HashMaps.h" />
<ClInclude Include="..\..\src\ripple\types\api\RandomNumbers.h" /> <ClInclude Include="..\..\src\ripple\types\api\RandomNumbers.h" />
<ClInclude Include="..\..\src\ripple\types\api\RippleAccountID.h" />
<ClInclude Include="..\..\src\ripple\types\api\RippleAccountPrivateKey.h" />
<ClInclude Include="..\..\src\ripple\types\api\RippleAccountPublicKey.h" />
<ClInclude Include="..\..\src\ripple\types\api\RippleCryptoIdentifier.h" />
<ClInclude Include="..\..\src\ripple\types\api\RipplePrivateKey.h" />
<ClInclude Include="..\..\src\ripple\types\api\RipplePublicKey.h" />
<ClInclude Include="..\..\src\ripple\types\api\RipplePublicKeyHash.h" />
<ClInclude Include="..\..\src\ripple\types\api\strHex.h" /> <ClInclude Include="..\..\src\ripple\types\api\strHex.h" />
<ClInclude Include="..\..\src\ripple\types\api\UInt128.h" /> <ClInclude Include="..\..\src\ripple\types\api\UInt128.h" />
<ClInclude Include="..\..\src\ripple\types\api\UInt160.h" /> <ClInclude Include="..\..\src\ripple\types\api\UInt160.h" />
@@ -1734,8 +1749,6 @@
<ClInclude Include="..\..\src\ripple_basics\ripple_basics.h" /> <ClInclude Include="..\..\src\ripple_basics\ripple_basics.h" />
<ClInclude Include="..\..\src\ripple_basics\system\BoostIncludes.h" /> <ClInclude Include="..\..\src\ripple_basics\system\BoostIncludes.h" />
<ClInclude Include="..\..\src\ripple_basics\types\BasicTypes.h" /> <ClInclude Include="..\..\src\ripple_basics\types\BasicTypes.h" />
<ClInclude Include="..\..\src\ripple_basics\types\RipplePublicKey.h" />
<ClInclude Include="..\..\src\ripple_basics\types\RipplePublicKeyHash.h" />
<ClInclude Include="..\..\src\ripple_basics\utility\CountedObject.h" /> <ClInclude Include="..\..\src\ripple_basics\utility\CountedObject.h" />
<ClInclude Include="..\..\src\ripple_basics\utility\IniFile.h" /> <ClInclude Include="..\..\src\ripple_basics\utility\IniFile.h" />
<ClInclude Include="..\..\src\ripple_basics\utility\PlatformMacros.h" /> <ClInclude Include="..\..\src\ripple_basics\utility\PlatformMacros.h" />

View File

@@ -1056,6 +1056,9 @@
<ClCompile Include="..\..\src\ripple_app\peers\PackedMessage.cpp"> <ClCompile Include="..\..\src\ripple_app\peers\PackedMessage.cpp">
<Filter>[2] Old Ripple\ripple_app\peers</Filter> <Filter>[2] Old Ripple\ripple_app\peers</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\types\impl\RippleCryptoIdentifierTests.cpp">
<Filter>[1] Ripple\types\impl</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\ripple_basics\containers\KeyCache.h"> <ClInclude Include="..\..\src\ripple_basics\containers\KeyCache.h">
@@ -2022,12 +2025,6 @@
<ClInclude Include="..\..\src\ripple\rpc\impl\ManagerImpl.h"> <ClInclude Include="..\..\src\ripple\rpc\impl\ManagerImpl.h">
<Filter>[1] Ripple\rpc\impl</Filter> <Filter>[1] Ripple\rpc\impl</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple_basics\types\RipplePublicKey.h">
<Filter>[2] Old Ripple\ripple_basics\types</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple_basics\types\RipplePublicKeyHash.h">
<Filter>[2] Old Ripple\ripple_basics\types</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\ripple_types.h"> <ClInclude Include="..\..\src\ripple\types\ripple_types.h">
<Filter>[1] Ripple\types</Filter> <Filter>[1] Ripple\types</Filter>
</ClInclude> </ClInclude>
@@ -2082,6 +2079,33 @@
<ClInclude Include="..\..\src\ripple_app\peers\PackedMessage.h"> <ClInclude Include="..\..\src\ripple_app\peers\PackedMessage.h">
<Filter>[2] Old Ripple\ripple_app\peers</Filter> <Filter>[2] Old Ripple\ripple_app\peers</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RipplePublicKey.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RipplePublicKeyHash.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\CryptoIdentifierType.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RippleCryptoIdentifier.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\CryptoIdentifierStorage.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RippleAccountID.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RipplePrivateKey.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RippleAccountPrivateKey.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\types\api\RippleAccountPublicKey.h">
<Filter>[1] Ripple\types\api</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto"> <CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto">

View File

@@ -25,6 +25,7 @@
#include "../beast/modules/beast_db/beast_db.cpp" #include "../beast/modules/beast_db/beast_db.cpp"
#include "../beast/modules/beast_sqdb/beast_sqdb.cpp" #include "../beast/modules/beast_sqdb/beast_sqdb.cpp"
#include "../beast/beast/crypto/Crypto.cpp"
#include "../beast/beast/http/HTTP.cpp" #include "../beast/beast/http/HTTP.cpp"
#include "../beast/beast/net/Net.cpp" #include "../beast/beast/net/Net.cpp"
#include "../beast/beast/strings/Strings.cpp" #include "../beast/beast/strings/Strings.cpp"

View File

@@ -36,6 +36,7 @@ public:
CBigNum (uint64 n); CBigNum (uint64 n);
explicit CBigNum (uint256 n); explicit CBigNum (uint256 n);
explicit CBigNum (Blob const& vch); explicit CBigNum (Blob const& vch);
CBigNum (unsigned char const* begin, unsigned char const* end);
~CBigNum (); ~CBigNum ();
void setuint (unsigned int n); void setuint (unsigned int n);
@@ -46,6 +47,7 @@ public:
void setuint64 (uint64 n); void setuint64 (uint64 n);
void setuint256 (uint256 const& n); void setuint256 (uint256 const& n);
uint256 getuint256 (); uint256 getuint256 ();
void setvch (unsigned char const* begin, unsigned char const* end);
void setvch (Blob const& vch); void setvch (Blob const& vch);
Blob getvch () const; Blob getvch () const;
CBigNum& SetCompact (unsigned int nCompact); CBigNum& SetCompact (unsigned int nCompact);

View File

@@ -102,7 +102,13 @@ CBigNum::CBigNum (uint256 n)
CBigNum::CBigNum (Blob const& vch) CBigNum::CBigNum (Blob const& vch)
{ {
BN_init (this); BN_init (this);
setvch (vch); setvch (&vch.front(), &vch.back()+1);
}
CBigNum::CBigNum (unsigned char const* begin, unsigned char const* end)
{
BN_init (this);
setvch (begin, end);
} }
void CBigNum::setuint (unsigned int n) void CBigNum::setuint (unsigned int n)
@@ -224,10 +230,11 @@ uint256 CBigNum::getuint256 ()
return ret; return ret;
} }
void CBigNum::setvch (Blob const& vch) void CBigNum::setvch (unsigned char const* begin, unsigned char const* end)
{ {
Blob vch2 (vch.size () + 4); std::size_t const size (std::distance (begin, end));
unsigned int nSize = vch.size (); Blob vch2 (size + 4);
unsigned int nSize (size);
// BIGNUM's byte stream format expects 4 bytes of // BIGNUM's byte stream format expects 4 bytes of
// big endian size data info at the front // big endian size data info at the front
vch2[0] = (nSize >> 24) & 0xff; vch2[0] = (nSize >> 24) & 0xff;
@@ -235,10 +242,15 @@ void CBigNum::setvch (Blob const& vch)
vch2[2] = (nSize >> 8) & 0xff; vch2[2] = (nSize >> 8) & 0xff;
vch2[3] = (nSize >> 0) & 0xff; vch2[3] = (nSize >> 0) & 0xff;
// swap data to big endian // swap data to big endian
std::reverse_copy (vch.begin (), vch.end (), vch2.begin () + 4); std::reverse_copy (begin, end, vch2.begin() + 4);
BN_mpi2bn (&vch2[0], vch2.size (), this); BN_mpi2bn (&vch2[0], vch2.size (), this);
} }
void CBigNum::setvch (Blob const& vch)
{
setvch (&vch.front(), &vch.back()+1);
}
Blob CBigNum::getvch () const Blob CBigNum::getvch () const
{ {
unsigned int nSize = BN_bn2mpi (this, NULL); unsigned int nSize = BN_bn2mpi (this, NULL);

View File

@@ -19,6 +19,8 @@
#ifndef RIPPLE_TYPES_BASE58_H #ifndef RIPPLE_TYPES_BASE58_H
#define RIPPLE_TYPES_BASE58_H #define RIPPLE_TYPES_BASE58_H
#include <iterator>
#include "Blob.h" #include "Blob.h"
namespace ripple { namespace ripple {
@@ -27,23 +29,73 @@ namespace ripple {
class Base58 class Base58
{ {
public: public:
// VFALCO TODO clean up this poor API
static char const* getCurrentAlphabet ();
static void setCurrentAlphabet (char const* alphabet);
static char const* getBitcoinAlphabet (); static char const* getBitcoinAlphabet ();
static char const* getRippleAlphabet (); static char const* getRippleAlphabet ();
static char const* getTestnetAlphabet (); static char const* getTestnetAlphabet ();
static std::string encode ( static std::string raw_encode (
const unsigned char* pbegin, unsigned char const* begin, unsigned char const* end,
const unsigned char* pend, char const* alphabet, bool withCheck);
char const* alphabet,
bool withCheck);
static std::string encode (const unsigned char* pbegin, const unsigned char* pend); static void fourbyte_hash256 (void* out, void const* in, std::size_t bytes);
static std::string encode (Blob const& vch);
static std::string encodeWithCheck (Blob const& vchIn); template <class InputIt>
static std::string encode (InputIt first, InputIt last, char const* alphabet, bool withCheck)
{
typedef typename std::iterator_traits<InputIt>::value_type value_type;
std::vector <typename mpl::RemoveConst <value_type>::type> v;
std::size_t const size (std::distance (first, last));
if (withCheck)
{
v.reserve (size + 1 + 4);
v.insert (v.begin(), first, last);
unsigned char hash [4];
fourbyte_hash256 (hash, &v.front(), v.size());
v.resize (0);
// Place the hash reversed in the front
std::copy (std::reverse_iterator <unsigned char const*> (hash+4),
std::reverse_iterator <unsigned char const*> (hash),
std::back_inserter (v));
}
else
{
v.reserve (size + 1);
}
// Append input little endian
std::copy (std::reverse_iterator <InputIt> (last),
std::reverse_iterator <InputIt> (first),
std::back_inserter (v));
// Pad zero to make the BIGNUM positive
v.push_back (0);
return raw_encode (&v.front(), &v.back()+1, alphabet, withCheck);
}
// VFALCO NOTE Avoid this interface which uses globals, explicitly
// pass the alphabet in the call to encode!
//
static char const* getCurrentAlphabet ();
static void setCurrentAlphabet (char const* alphabet);
template <class Container>
static std::string encode (Container const& container)
{
return encode (container.container.begin(), container.end(),
getCurrentAlphabet(), false);
}
template <class Container>
static std::string encodeWithCheck (Container const& container)
{
return encode (&container.front(), &container.back()+1,
getCurrentAlphabet(), true);
}
static std::string encode (const unsigned char* pbegin, const unsigned char* pend)
{
return encode (pbegin, pend, getCurrentAlphabet(), false);
}
//--------------------------------------------------------------------------
static bool decode (const char* psz, Blob& vchRet, const char* pAlphabet = getCurrentAlphabet ()); static bool decode (const char* psz, Blob& vchRet, const char* pAlphabet = getCurrentAlphabet ());
static bool decode (const std::string& str, Blob& vchRet); static bool decode (const std::string& str, Blob& vchRet);

View File

@@ -0,0 +1,171 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_CRYPTOIDENTIFIERSTORAGE_H_INCLUDED
#define RIPPLE_TYPES_CRYPTOIDENTIFIERSTORAGE_H_INCLUDED
#include "beast/beast/FixedArray.h"
namespace ripple {
/** A padded FixedArray used with CryptoIdentifierType traits. */
template <std::size_t PreSize, std::size_t Size, std::size_t PostSize>
class CryptoIdentifierStorage
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef uint8 value_type;
typedef value_type* iterator;
typedef value_type const* const_iterator;
typedef value_type& reference;
typedef value_type const& const_reference;
static size_type const pre_size = PreSize;
static size_type const size = Size;
static size_type const post_size = PostSize;
static size_type const storage_size = pre_size + size + post_size;
typedef FixedArray <
uint8, storage_size> storage_type;
/** Value hashing function.
The seed prevents crafted inputs from causing degenarate parent containers.
*/
class hasher
{
public:
explicit hasher (HashValue seedToUse = Random::getSystemRandom ().nextInt ())
: m_seed (seedToUse)
{
}
std::size_t operator() (CryptoIdentifierStorage const& storage) const
{
std::size_t hash;
Murmur::Hash (storage.cbegin (), storage.size, m_seed, &hash);
return hash;
}
private:
std::size_t m_seed;
};
/** Container equality testing function. */
class equal
{
public:
bool operator() (CryptoIdentifierStorage const& lhs,
CryptoIdentifierStorage const& rhs) const
{
return lhs == rhs;
}
};
// iterator access
iterator begin() { return &m_storage[pre_size]; }
const_iterator begin() const { return &m_storage[pre_size]; }
const_iterator cbegin() const { return &m_storage[pre_size]; }
iterator end() { return &m_storage[storage_size-post_size]; }
const_iterator end() const { return &m_storage[storage_size-post_size]; }
const_iterator cend() const { return &m_storage[storage_size-post_size]; }
typedef std::reverse_iterator <iterator> reverse_iterator;
typedef std::reverse_iterator <const_iterator> const_reverse_iterator;
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
reference operator[](size_type i)
{
bassert (i < size);
return m_storage[pre_size+i];
}
const_reference operator[](size_type i) const
{
bassert (i < size);
return m_storage[pre_size+i];
}
reference front() { return m_storage[pre_size]; }
reference back() { return m_storage[storage_size-post_size-1]; }
const_reference front () const { return m_storage[pre_size]; }
const_reference back() const { return m_storage[storage_size-post_size-1]; }
value_type const* data() const { return &m_storage[pre_size]; }
value_type* data() { return &m_storage[pre_size]; }
value_type* c_array() { return &m_storage[pre_size]; }
void assign (value_type value) { fill (value); }
void fill (value_type value) { std::fill_n (begin(), size, value); }
void clear () { fill (value_type ()); }
// Access storage
storage_type& storage() { return m_storage; }
storage_type const& storage() const { return m_storage; }
void rangecheck (size_type i)
{
if (i >= size)
throw std::out_of_range ("CryptoIdentifierStorage<>: index out of range");
}
private:
storage_type m_storage;
};
//------------------------------------------------------------------------------
template <std::size_t PrePadSize, std::size_t Size, std::size_t PostPadSize>
bool operator== (CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& lhs,
CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& rhs)
{
return std::equal (lhs.begin(), lhs.end(), rhs.begin());
}
template <std::size_t PrePadSize, std::size_t Size, std::size_t PostPadSize>
bool operator!= (CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& lhs,
CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& rhs)
{
return !(lhs==rhs);
}
template <std::size_t PrePadSize, std::size_t Size, std::size_t PostPadSize>
bool operator< (CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& lhs,
CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& rhs)
{
return std::lexicographical_compare (lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <std::size_t PrePadSize, std::size_t Size, std::size_t PostPadSize>
bool operator> (CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& lhs,
CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& rhs)
{
return rhs<lhs;
}
template <std::size_t PrePadSize, std::size_t Size, std::size_t PostPadSize>
bool operator<= (CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& lhs,
CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& rhs)
{
return !(rhs<lhs);
}
template <std::size_t PrePadSize, std::size_t Size, std::size_t PostPadSize>
bool operator>= (CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& lhs,
CryptoIdentifierStorage <PrePadSize, Size, PostPadSize> const& rhs)
{
return !(lhs<rhs);
}
}
#endif

View File

@@ -0,0 +1,215 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_CRYPTOIDENTIFIERTYPE_H_INCLUDED
#define RIPPLE_TYPES_CRYPTOIDENTIFIERTYPE_H_INCLUDED
#include <functional>
#include <iterator>
#include <ios>
#include <sstream>
#include <string>
namespace ripple {
//------------------------------------------------------------------------------
/** Template for generalizing the cryptographic primitives used. */
template <class Traits>
class CryptoIdentifierType : public Traits::base
{
public:
static std::size_t const size = Traits::size;
typedef typename Traits::value_type value_type;
typedef typename value_type::const_iterator const_iterator;
typedef typename value_type::const_reverse_iterator const_reverse_iterator;
/** Wraps Traits::hasher. */
class hasher
{
public:
hasher()
{ }
template <typename Arg>
hasher (Arg arg) : m_hasher (arg)
{ }
std::size_t operator() (CryptoIdentifierType const& id) const
{ return m_hasher(id.value()); }
private:
typename Traits::hasher m_hasher;
};
/** Wraps Traits::equal. */
class equal
{
public:
equal()
{ }
template <typename Arg>
equal (Arg arg) : m_equal (arg)
{ }
bool operator() (CryptoIdentifierType const& lhs,
CryptoIdentifierType const& rhs) const
{ return m_equal (lhs.value(), rhs.value()); }
private:
typename Traits::equal m_equal;
};
/** Create an uninitialized value. */
CryptoIdentifierType ()
{
}
/** Create a copy from another value . */
CryptoIdentifierType (value_type const& value)
: m_value (value)
{
}
/** Create a copy of the value from range of bytes. */
CryptoIdentifierType (uint8 const* begin, uint8 const* end)
{
Traits::construct (begin, end, m_value);
}
/** Conversion construction from any specialized type. */
template <typename Other>
explicit CryptoIdentifierType (Other const& other)
{
this->operator= (other);
}
/** Assign a copy from another value. */
CryptoIdentifierType& operator= (value_type const& value)
{
m_value = value;
return *this;
}
/** Copy conversion from any specialized type. */
template <typename Other>
CryptoIdentifierType& operator= (Other const& other)
{
typename Traits::template assign <Other> () (
m_value, other);
return *this;
}
/** Access the value. */
value_type const& value() const
{
return m_value;
}
/** Iterator access. */
/** @{ */
const_iterator begin() const { return value().begin(); }
const_iterator end() const { return value().end(); }
const_iterator cbegin() const { return value().cbegin(); }
const_iterator cend() const { return value().cend(); }
const_reverse_iterator rbegin() const { return value().rbegin(); }
const_reverse_iterator rend() const { return value().rend(); }
const_reverse_iterator crbegin() const { return value().crbegin(); }
const_reverse_iterator crend() const { return value().crend(); }
/** @} */
/** Conversion to std::string. */
std::string to_string() const
{
return Traits::to_string (m_value);
}
private:
value_type m_value;
};
//------------------------------------------------------------------------------
template <class Traits>
std::ostream& operator<< (std::ostream& os,
CryptoIdentifierType <Traits> const& id)
{
os << id.to_string();
return os;
}
template <class Traits>
std::istream& operator>> (std::istream& is,
CryptoIdentifierType <Traits> const& id)
{
return is;
}
//------------------------------------------------------------------------------
}
namespace std {
/** Specialization for hash. */
template <class Traits>
struct hash <ripple::CryptoIdentifierType <Traits> >
{
public:
typedef ripple::CryptoIdentifierType <Traits> argument_type;
typedef std::size_t result_type;
hash ()
{
static typename argument_type::hasher s_hash;
m_hash = s_hash;
}
template <typename Arg>
explicit hash (Arg arg)
: m_hash (arg)
{
}
result_type operator() (argument_type const& key) const
{
return m_hash (key);
}
private:
typename argument_type::hasher m_hash;
};
//------------------------------------------------------------------------------
/** Specialization for equal_to. */
template <class Traits>
struct equal_to <ripple::CryptoIdentifierType <Traits> >
{
public:
typedef bool result_type;
typedef ripple::CryptoIdentifierType <Traits> argument_type;
typedef argument_type first_argument_type;
typedef argument_type second_argument_type;
equal_to ()
{
}
template <typename Arg>
explicit equal_to (Arg arg)
: m_equal (arg)
{
}
result_type operator() (argument_type const& lhs,
argument_type const& rhs) const
{
return m_equal (lhs, rhs);
}
private:
typename argument_type::equal m_equal;
};
}
#endif

View File

@@ -0,0 +1,49 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_RIPPLEACCOUNTID_H_INCLUDED
#define RIPPLE_TYPES_RIPPLEACCOUNTID_H_INCLUDED
#include "RippleCryptoIdentifier.h"
namespace ripple {
class RippleAccountIDTraits
: public RippleCryptoIdentifier <20, 0, true>
{
public:
template <typename Other>
struct assign
{
void operator() (value_type& value, Other const& other)
{
value = other;
}
};
/** Convert to std::string.
*/
// VFALCO TODO Cache results in an associative map, to replicated
// the optimization performed in RippledAddress.cp
//
static std::string to_string (value_type const& value)
{
value_type::storage_type const& storage (value.storage());
// We will convert to little endian with an extra pad byte
FixedArray <uint8, value_type::storage_size + 1> le;
std::reverse_copy (storage.begin(), storage.end(), le.begin());
// Set pad byte zero to make BIGNUM always positive
le.back() = 0;
return Base58::raw_encode (le.begin(), le.end(),
Base58::getRippleAlphabet(), checked);
}
};
typedef CryptoIdentifierType <RippleAccountIDTraits> RippleAccountID;
}
#endif

View File

@@ -0,0 +1,32 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_RIPPLEACCOUNTPRIVATEKEY_H_INCLUDED
#define RIPPLE_TYPES_RIPPLEACCOUNTPRIVATEKEY_H_INCLUDED
#include "RippleCryptoIdentifier.h"
namespace ripple {
class RippleAccountPrivateKeyTraits
: public RippleCryptoIdentifier <32, 34, true>
{
public:
template <typename Other>
struct assign
{
void operator() (value_type& value, Other const& other)
{
value = other;
}
};
};
typedef CryptoIdentifierType <RippleAccountPrivateKeyTraits> RippleAccountPrivateKey;
}
#endif

View File

@@ -0,0 +1,32 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_RIPPLEACCOUNTPUBLICKEY_H_INCLUDED
#define RIPPLE_TYPES_RIPPLEACCOUNTPUBLICKEY_H_INCLUDED
#include "RippleCryptoIdentifier.h"
namespace ripple {
class RippleAccountPublicKeyTraits
: public RippleCryptoIdentifier <33, 35, true>
{
public:
template <typename Other>
struct assign
{
void operator() (value_type& value, Other const& other)
{
value = other;
}
};
};
typedef CryptoIdentifierType <RippleAccountPublicKeyTraits> RippleAccountPublicKey;
}
#endif

View File

@@ -0,0 +1,106 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_RIPPLECRYPTOIDENTIFIER_H_INCLUDED
#define RIPPLE_TYPES_RIPPLECRYPTOIDENTIFIER_H_INCLUDED
#include "beast/beast/FixedArray.h"
#include "beast/beast/intrusive/IntrusiveArray.h"
#include "beast/beast/crypto/Sha256.h"
#include "Base58.h"
#include "CryptoIdentifierStorage.h"
namespace ripple {
/** Shared CryptoIdentifierType traits for Ripple crypto identifiers.
@tparam Size The number of bytes in the identifier, exclusive of version,
checksum, or padding.
@tparam Token A byte prepended to the binary data that distinguishes
the type of identifier.
@tparam Checked A `bool` indicating whether or not the string
representation includes an appended a four byte checksum on
the data including the Token.
*/
template <std::size_t Size, uint8 Token, bool Checked>
class RippleCryptoIdentifier
{
public:
typedef std::size_t size_type;
// 1 token byte,
static std::size_t const pre_size = 1;
static size_type const size = Size;
// 4 checksum bytes (optional)
static std::size_t const post_size = (Checked ? 4 : 0);
static uint8 const token = Token;
static bool const checked = Checked;
// This is what the wrapper creates, it includes the padding.
typedef CryptoIdentifierStorage <
pre_size, size, post_size> value_type;
typedef typename value_type::hasher hasher;
typedef typename value_type::equal equal;
/** Initialize from an input sequence. */
static void construct (
uint8 const* begin, uint8 const* end,
value_type& value)
{
value.storage()[0] = Token;
bassert (std::distance (begin, end) == size);
std::copy (begin, end, value.begin());
if (Checked)
{
Sha256::digest_type digest;
Sha256::hash (Sha256::hash (value.storage().cbegin(),
value.storage().cend() - post_size), digest);
// We use the first 4 bytes as a checksum
std::copy (digest.begin(), digest.begin() + 4,
value.end());
}
}
/** Base class for CryptoIdentifierType. */
class base
{
public:
template <class UnsignedIntegralType>
static value_type createFromInteger (UnsignedIntegralType i)
{
static_bassert (size >= sizeof (UnsignedIntegralType));
FixedArray <uint8, size> data;
data.fill (0);
i = toNetworkByteOrder <UnsignedIntegralType> (i);
std::memcpy (data.end () - sizeof (i), &i, std::min (size, sizeof (i)));
value_type value;
construct (data.begin(), data.end(), value);
return value;
}
};
/** Convert to std::string. */
static std::string to_string (value_type const& value)
{
typename value_type::storage_type const& storage (value.storage());
// We will convert to little endian with an extra pad byte
FixedArray <uint8, value_type::storage_size + 1> le;
std::reverse_copy (storage.begin(), storage.end(), le.begin());
// Set pad byte zero to make BIGNUM always positive
le.back() = 0;
return Base58::raw_encode (le.begin(), le.end(),
Base58::getRippleAlphabet(), Checked);
}
};
}
#endif

View File

@@ -0,0 +1,32 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_RIPPLEPRIVATEKEY_H_INCLUDED
#define RIPPLE_TYPES_RIPPLEPRIVATEKEY_H_INCLUDED
#include "RippleCryptoIdentifier.h"
namespace ripple {
class RipplePrivateKeyTraits
: public RippleCryptoIdentifier <32, 32, true>
{
public:
template <typename Other>
struct assign
{
void operator() (value_type& value, Other const& other)
{
value = other;
}
};
};
typedef CryptoIdentifierType <RipplePrivateKeyTraits> RipplePrivateKey;
}
#endif

View File

@@ -0,0 +1,32 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
#define RIPPLE_TYPES_RIPPLEPUBLICKEY_H_INCLUDED
#include "RippleCryptoIdentifier.h"
namespace ripple {
class RipplePublicKeyTraits
: public RippleCryptoIdentifier <33, 28, true>
{
public:
template <typename Other>
struct assign
{
void operator() (value_type& value, Other const& other)
{
value = other;
}
};
};
typedef CryptoIdentifierType <RipplePublicKeyTraits> RipplePublicKey;
}
#endif

View File

@@ -4,10 +4,15 @@
*/ */
//============================================================================== //==============================================================================
#ifndef RIPPLE_BASICS_RIPPLEPUBLICKEYHASH_H_INCLUDED #ifndef RIPPLE_TYPES_RIPPLEPUBLICKEYHASH_H_INCLUDED
#define RIPPLE_BASICS_RIPPLEPUBLICKEYHASH_H_INCLUDED #define RIPPLE_TYPES_RIPPLEPUBLICKEYHASH_H_INCLUDED
namespace ripple {
/** A container holding the hash of a public key in binary format. */ /** A container holding the hash of a public key in binary format. */
typedef UnsignedInteger <20> RipplePublicKeyHash; typedef UnsignedInteger <20> RipplePublicKeyHash;
}
#endif #endif

View File

@@ -13,14 +13,12 @@ namespace ripple {
char const* Base58::s_currentAlphabet = Base58::getRippleAlphabet (); char const* Base58::s_currentAlphabet = Base58::getRippleAlphabet ();
char const* Base58::getCurrentAlphabet () void Base58::fourbyte_hash256 (void* out, void const* in, std::size_t bytes)
{ {
return s_currentAlphabet; unsigned char const* const p (
} static_cast <unsigned char const*>(in));
uint256 hash (SHA256Hash (p, p + bytes));
void Base58::setCurrentAlphabet (char const* alphabet) memcpy (out, hash.begin(), 4);
{
s_currentAlphabet = alphabet;
} }
char const* Base58::getBitcoinAlphabet () char const* Base58::getBitcoinAlphabet ()
@@ -38,27 +36,23 @@ char const* Base58::getTestnetAlphabet ()
return "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz"; return "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz";
} }
std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pend) std::string Base58::raw_encode (
unsigned char const* begin, unsigned char const* end,
char const* alphabet, bool withCheck)
{ {
char const* alphabet = getCurrentAlphabet ();
CAutoBN_CTX pctx; CAutoBN_CTX pctx;
CBigNum bn58 = 58; CBigNum bn58 = 58;
CBigNum bn0 = 0; CBigNum bn0 = 0;
// Convert big endian data to little endian
// Extra zero at the end make sure bignum will interpret as a positive number
Blob vchTmp (pend - pbegin + 1, 0);
std::reverse_copy (pbegin, pend, vchTmp.begin ());
// Convert little endian data to bignum // Convert little endian data to bignum
CBigNum bn (vchTmp); CBigNum bn (begin, end);
std::size_t const size (std::distance (begin, end));
// Convert bignum to std::string // Convert bignum to std::string
std::string str; std::string str;
// Expected size increase from base58 conversion is approximately 137% // Expected size increase from base58 conversion is approximately 137%
// use 138% to be safe // use 138% to be safe
str.reserve ((pend - pbegin) * 138 / 100 + 1); str.reserve (size * 138 / 100 + 1);
CBigNum dv; CBigNum dv;
CBigNum rem; CBigNum rem;
@@ -71,9 +65,8 @@ std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pe
unsigned int c = rem.getuint (); unsigned int c = rem.getuint ();
str += alphabet [c]; str += alphabet [c];
} }
// Leading zeroes encoded as base58 zeros for (const unsigned char* p = end-2; p >= begin && *p == 0; p--)
for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
str += alphabet [0]; str += alphabet [0];
// Convert little endian std::string to big endian // Convert little endian std::string to big endian
@@ -81,20 +74,18 @@ std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pe
return str; return str;
} }
std::string Base58::encode (Blob const& vch) char const* Base58::getCurrentAlphabet ()
{ {
return encode (&vch[0], &vch[0] + vch.size ()); return s_currentAlphabet;
} }
std::string Base58::encodeWithCheck (Blob const& vchIn) void Base58::setCurrentAlphabet (char const* alphabet)
{ {
// add 4-byte hash check to the end s_currentAlphabet = alphabet;
Blob vch (vchIn);
uint256 hash = SHA256Hash (vch.begin (), vch.end ());
vch.insert (vch.end (), (unsigned char*)&hash, (unsigned char*)&hash + 4);
return encode (vch);
} }
//------------------------------------------------------------------------------
bool Base58::decode (const char* psz, Blob& vchRet, const char* pAlpha) bool Base58::decode (const char* psz, Blob& vchRet, const char* pAlpha)
{ {
assert (pAlpha != 0); assert (pAlpha != 0);

View File

@@ -0,0 +1,11 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
namespace ripple {
// Relocated to RippleAddress.cpp
}

View File

@@ -24,3 +24,4 @@
#include "impl/UInt128.cpp" #include "impl/UInt128.cpp"
#include "impl/UInt160.cpp" #include "impl/UInt160.cpp"
#include "impl/UInt256.cpp" #include "impl/UInt256.cpp"
#include "impl/RippleCryptoIdentifierTests.cpp"

View File

@@ -8,6 +8,7 @@
#define RIPPLE_TYPES_H_INCLUDED #define RIPPLE_TYPES_H_INCLUDED
#include "beast/modules/beast_core/beast_core.h" #include "beast/modules/beast_core/beast_core.h"
#include "beast/modules/beast_crypto/beast_crypto.h" // for UnsignedInteger, Remove ASAP!
#include "beast/modules/beast_core/system/BeforeBoost.h" #include "beast/modules/beast_core/system/BeforeBoost.h"
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
@@ -36,5 +37,15 @@ using namespace beast;
# include "api/UInt256.h" # include "api/UInt256.h"
# include "api/RandomNumbers.h" # include "api/RandomNumbers.h"
#include "api/HashMaps.h" #include "api/HashMaps.h"
#include "api/CryptoIdentifierType.h"
# include "api/CryptoIdentifierStorage.h"
#include "api/RippleAccountID.h"
#include "api/RippleAccountPublicKey.h"
#include "api/RippleAccountPrivateKey.h"
#include "api/RipplePublicKey.h"
#include "api/RipplePrivateKey.h"
#include "api/RipplePublicKeyHash.h"
#endif #endif

View File

@@ -22,7 +22,7 @@ public:
} }
}; };
typedef boost::unordered_map <PublicKey, Info, PublicKey::HashFunction> MapType; typedef boost::unordered_map <PublicKey, Info, PublicKey::hasher> MapType;
ChosenList (std::size_t expectedSize = 0) ChosenList (std::size_t expectedSize = 0)
{ {

View File

@@ -46,7 +46,7 @@ public:
}; };
typedef boost::unordered_map < typedef boost::unordered_map <
PublicKey, ValidatorInfo, PublicKey::HashFunction> MapType; PublicKey, ValidatorInfo, PublicKey::hasher> MapType;
struct State struct State
{ {
@@ -318,12 +318,10 @@ public:
iter != list->map().end(); ++iter) iter != list->map().end(); ++iter)
{ {
Json::Value entry (Json::objectValue); Json::Value entry (Json::objectValue);
/*
ChosenList::MapType::key_type const& key (iter->first); ChosenList::MapType::key_type const& key (iter->first);
ChosenList::MapType::mapped_type const& value (iter->second); entry ["key"] = key.to_string();
entry ["key"] = key; //ChosenList::MapType::mapped_type const& value (iter->second);
entry ["value"] = value; //entry ["value"] = value.to_string();
*/
entries.append (entry); entries.append (entry);
} }
} }

View File

@@ -84,7 +84,7 @@ bool Utilities::parseInfoLine (
else if (deprecatedPublicKey.setNodePublic (encodedKey)) else if (deprecatedPublicKey.setNodePublic (encodedKey))
{ {
// We got a public key. // We got a public key.
RipplePublicKey publicKey (deprecatedPublicKey.toRipplePublicKey ()); RipplePublicKey publicKey (deprecatedPublicKey);
success = true; success = true;
} }
else else
@@ -238,15 +238,15 @@ Time Utilities::stringToTime (String s)
std::string Utilities::publicKeyToString (PublicKey const& publicKey) std::string Utilities::publicKeyToString (PublicKey const& publicKey)
{ {
std::string s (PublicKey::sizeInBytes, ' '); std::string s (PublicKey::size, ' ');
std::copy (publicKey.cbegin(), publicKey.cend(), s.begin()); std::copy (publicKey.cbegin(), publicKey.cend(), s.begin());
return s; return s;
} }
PublicKey Utilities::stringToPublicKey (std::string const& s) PublicKey Utilities::stringToPublicKey (std::string const& s)
{ {
bassert (s.size() == PublicKey::sizeInBytes); bassert (s.size() == PublicKey::size);
return PublicKey (&s.front()); return PublicKey ();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@@ -8,7 +8,6 @@
#define RIPPLE_VALIDATORS_H_INCLUDED #define RIPPLE_VALIDATORS_H_INCLUDED
#include "beast/modules/beast_core/beast_core.h" #include "beast/modules/beast_core/beast_core.h"
#include "../ripple_basics/ripple_basics.h" // for RipplePublicKey, remove asap
#include "beast/beast/http/URL.h" #include "beast/beast/http/URL.h"

View File

@@ -22,7 +22,7 @@
#include <boost/array.hpp> #include <boost/array.hpp>
#include <boost/asio/read_until.hpp> #include <boost/asio/read_until.hpp>
#include <boost/asio/streambuf.hpp> #include <boost/asio/streambuf.hpp>
#include <boost/iostreams/concepts.hpp> //#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/stream.hpp> #include <boost/iostreams/stream.hpp>
#include <boost/mem_fn.hpp> #include <boost/mem_fn.hpp>
#include <boost/pointer_cast.hpp> #include <boost/pointer_cast.hpp>

View File

@@ -49,8 +49,6 @@ namespace ripple
using namespace beast; using namespace beast;
#include "types/BasicTypes.h" #include "types/BasicTypes.h"
#include "types/RipplePublicKey.h"
#include "types/RipplePublicKeyHash.h"
# include "log/LogSeverity.h" # include "log/LogSeverity.h"
# include "log/LogFile.h" # include "log/LogFile.h"

View File

@@ -1,80 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_BASICS_RIPPLEPUBLICKEY_H_INCLUDED
#define RIPPLE_BASICS_RIPPLEPUBLICKEY_H_INCLUDED
/** A container used to hold a public key in binary format. */
typedef UnsignedInteger <33> RipplePublicKey;
#if 0
class RipplePublicKey
{
private:
typedef UnsignedInteger <33> integer_type;
public:
enum
{
size = integer_type::sizeInBytes
};
typedef integer_type::value_type value_type;
typedef integer_type::iterator iterator;
typedef integer_type::const_iterator const_iterator;
class HashFunction
{
public:
HashFunction (HashValue seedToUse = Random::getSystemRandom().nextInt())
: m_hash (seedToUse)
{
}
HashValue operator() (RipplePublicKey const& value) const
{
return m_hash (value);
}
private:
integer_type::HashFunction m_hash;
};
iterator begin()
{
return m_value.begin();
}
iterator end()
{
return m_value.end();
}
const_iterator begin() const
{
return m_value.begin();
}
const_iterator end() const
{
return m_value.end();
}
const_iterator cbegin() const
{
return m_value.cbegin();
}
const_iterator cend() const
{
return m_value.cend();
}
private:
integer_type m_value;
};
#endif
#endif

View File

@@ -12,15 +12,6 @@ RippleAddress::RippleAddress ()
nVersion = VER_NONE; nVersion = VER_NONE;
} }
RipplePublicKey RippleAddress::toRipplePublicKey () const
{
Blob const& b (getNodePublic ());
check_precondition (b.size () == RipplePublicKey::sizeInBytes);
return RipplePublicKey (&b [0]);
}
void RippleAddress::clear () void RippleAddress::clear ()
{ {
nVersion = VER_NONE; nVersion = VER_NONE;
@@ -318,6 +309,10 @@ std::string RippleAddress::humanAccountID () const
if (it != rncMap.end ()) if (it != rncMap.end ())
return it->second; return it->second;
// VFALCO NOTE Why do we throw everything out? We could keep two maps
// here, switch back and forth keep one of them full and clear the
// other on a swap - but always check both maps for cache hits.
//
if (rncMap.size () > 10000) if (rncMap.size () > 10000)
rncMap.clear (); rncMap.clear ();
@@ -953,3 +948,74 @@ public:
}; };
static RippleAddressTests rippleAddressTests; static RippleAddressTests rippleAddressTests;
//------------------------------------------------------------------------------
class RippleCryptoIdentifierTests : public UnitTest
{
public:
void runTest ()
{
beginTestCase ("Seed");
RippleAddress seed;
expect (seed.setSeedGeneric ("masterpassphrase"));
expect (seed.humanSeed () == "snoPBrXtMeMyMHUVTgbuqAfg1SUTb", seed.humanSeed ());
beginTestCase ("RipplePublicKey");
RippleAddress deprecatedPublicKey (RippleAddress::createNodePublic (seed));
expect (deprecatedPublicKey.humanNodePublic () ==
"n94a1u4jAz288pZLtw6yFWVbi89YamiC6JBXPVUj5zmExe5fTVg9",
deprecatedPublicKey.humanNodePublic ());
RipplePublicKey publicKey (deprecatedPublicKey);
expect (publicKey.to_string() == deprecatedPublicKey.humanNodePublic(),
publicKey.to_string());
beginTestCase ("RipplePrivateKey");
RippleAddress deprecatedPrivateKey (RippleAddress::createNodePrivate (seed));
expect (deprecatedPrivateKey.humanNodePrivate () ==
"pnen77YEeUd4fFKG7iycBWcwKpTaeFRkW2WFostaATy1DSupwXe",
deprecatedPrivateKey.humanNodePrivate ());
RipplePrivateKey privateKey (deprecatedPrivateKey);
expect (privateKey.to_string() == deprecatedPrivateKey.humanNodePrivate(),
privateKey.to_string());
beginTestCase ("Generator");
RippleAddress generator (RippleAddress::createGeneratorPublic (seed));
expect (generator.humanGenerator () ==
"fhuJKrhSDzV2SkjLn9qbwm5AaRmrxDPfFsHDCP6yfDZWcxDFz4mt",
generator.humanGenerator ());
beginTestCase ("RippleAccountID");
RippleAddress deprecatedAccountPublicKey (
RippleAddress::createAccountPublic (generator, 0));
expect (deprecatedAccountPublicKey.humanAccountID () ==
"rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
deprecatedAccountPublicKey.humanAccountID ());
RippleAccountID accountID (deprecatedAccountPublicKey);
expect (accountID.to_string() ==
deprecatedAccountPublicKey.humanAccountID(),
accountID.to_string());
beginTestCase ("RippleAccountPublicKey");
expect (deprecatedAccountPublicKey.humanAccountPublic () ==
"aBQG8RQAzjs1eTKFEAQXr2gS4utcDiEC9wmi7pfUPTi27VCahwgw",
deprecatedAccountPublicKey.humanAccountPublic ());
beginTestCase ("RippleAccountPrivateKey");
RippleAddress deprecatedAccountPrivateKey (
RippleAddress::createAccountPrivate (generator, seed, 0));
expect (deprecatedAccountPrivateKey.humanAccountPrivate () ==
"p9JfM6HHi64m6mvB6v5k7G2b1cXzGmYiCNJf6GHPKvFTWdeRVjh",
deprecatedAccountPrivateKey.humanAccountPrivate ());
RippleAccountPrivateKey accountPrivateKey (deprecatedAccountPrivateKey);
expect (accountPrivateKey.to_string() ==
deprecatedAccountPrivateKey.humanAccountPrivate(),
privateKey.to_string());
}
RippleCryptoIdentifierTests () : UnitTest ("RippleCryptoIdentifier", "ripple")
{
}
};
static RippleCryptoIdentifierTests rippleCryptoIdentifierTests;

View File

@@ -31,11 +31,6 @@ private:
public: public:
RippleAddress (); RippleAddress ();
// Convert to a binary public key. If the underlying data
// is not appropriate, this will cause a fatal error.
//
RipplePublicKey toRipplePublicKey () const;
// For public and private key, checks if they are legal. // For public and private key, checks if they are legal.
bool isValid () const bool isValid () const
{ {
@@ -209,4 +204,56 @@ public:
static RippleAddress createSeedGeneric (const std::string& strText); static RippleAddress createSeedGeneric (const std::string& strText);
}; };
//------------------------------------------------------------------------------
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);
}
};
template <>
struct RipplePrivateKeyTraits::assign <RippleAddress>
{
void operator() (value_type& value, RippleAddress const& v) const
{
uint256 const ui (v.getNodePrivate ());
construct (ui.begin(), ui.end(), value);
}
};
template <>
struct RippleAccountIDTraits::assign <RippleAddress>
{
void operator() (value_type& value, RippleAddress const& v) const
{
uint160 const ui (v.getAccountID ());
construct (ui.begin(), ui.end(), value);
}
};
template <>
struct RippleAccountPublicKeyTraits::assign <RippleAddress>
{
void operator() (value_type& value, RippleAddress const& v) const
{
Blob const& b (v.getAccountPublic ());
construct (&b.front(), &b.back()+1, value);
}
};
template <>
struct RippleAccountPrivateKeyTraits::assign <RippleAddress>
{
void operator() (value_type& value, RippleAddress const& v) const
{
uint256 const ui (v.getAccountPrivate ());
construct (ui.begin(), ui.end(), value);
}
};
#endif #endif