mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Add CryptoIdentifierType and RippleCryptoIdentifier Traits
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include "../beast/modules/beast_db/beast_db.cpp"
|
||||
#include "../beast/modules/beast_sqdb/beast_sqdb.cpp"
|
||||
|
||||
#include "../beast/beast/crypto/Crypto.cpp"
|
||||
#include "../beast/beast/http/HTTP.cpp"
|
||||
#include "../beast/beast/net/Net.cpp"
|
||||
#include "../beast/beast/strings/Strings.cpp"
|
||||
|
||||
@@ -36,6 +36,7 @@ public:
|
||||
CBigNum (uint64 n);
|
||||
explicit CBigNum (uint256 n);
|
||||
explicit CBigNum (Blob const& vch);
|
||||
CBigNum (unsigned char const* begin, unsigned char const* end);
|
||||
~CBigNum ();
|
||||
|
||||
void setuint (unsigned int n);
|
||||
@@ -46,6 +47,7 @@ public:
|
||||
void setuint64 (uint64 n);
|
||||
void setuint256 (uint256 const& n);
|
||||
uint256 getuint256 ();
|
||||
void setvch (unsigned char const* begin, unsigned char const* end);
|
||||
void setvch (Blob const& vch);
|
||||
Blob getvch () const;
|
||||
CBigNum& SetCompact (unsigned int nCompact);
|
||||
|
||||
@@ -102,7 +102,13 @@ CBigNum::CBigNum (uint256 n)
|
||||
CBigNum::CBigNum (Blob const& vch)
|
||||
{
|
||||
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)
|
||||
@@ -224,10 +230,11 @@ uint256 CBigNum::getuint256 ()
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CBigNum::setvch (Blob const& vch)
|
||||
void CBigNum::setvch (unsigned char const* begin, unsigned char const* end)
|
||||
{
|
||||
Blob vch2 (vch.size () + 4);
|
||||
unsigned int nSize = vch.size ();
|
||||
std::size_t const size (std::distance (begin, end));
|
||||
Blob vch2 (size + 4);
|
||||
unsigned int nSize (size);
|
||||
// BIGNUM's byte stream format expects 4 bytes of
|
||||
// big endian size data info at the front
|
||||
vch2[0] = (nSize >> 24) & 0xff;
|
||||
@@ -235,10 +242,15 @@ void CBigNum::setvch (Blob const& vch)
|
||||
vch2[2] = (nSize >> 8) & 0xff;
|
||||
vch2[3] = (nSize >> 0) & 0xff;
|
||||
// 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);
|
||||
}
|
||||
|
||||
void CBigNum::setvch (Blob const& vch)
|
||||
{
|
||||
setvch (&vch.front(), &vch.back()+1);
|
||||
}
|
||||
|
||||
Blob CBigNum::getvch () const
|
||||
{
|
||||
unsigned int nSize = BN_bn2mpi (this, NULL);
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
#ifndef RIPPLE_TYPES_BASE58_H
|
||||
#define RIPPLE_TYPES_BASE58_H
|
||||
|
||||
#include <iterator>
|
||||
|
||||
#include "Blob.h"
|
||||
|
||||
namespace ripple {
|
||||
@@ -27,23 +29,73 @@ namespace ripple {
|
||||
class Base58
|
||||
{
|
||||
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* getRippleAlphabet ();
|
||||
static char const* getTestnetAlphabet ();
|
||||
|
||||
static std::string encode (
|
||||
const unsigned char* pbegin,
|
||||
const unsigned char* pend,
|
||||
char const* alphabet,
|
||||
bool withCheck);
|
||||
static std::string raw_encode (
|
||||
unsigned char const* begin, unsigned char const* end,
|
||||
char const* alphabet, bool withCheck);
|
||||
|
||||
static std::string encode (const unsigned char* pbegin, const unsigned char* pend);
|
||||
static std::string encode (Blob const& vch);
|
||||
static std::string encodeWithCheck (Blob const& vchIn);
|
||||
static void fourbyte_hash256 (void* out, void const* in, std::size_t bytes);
|
||||
|
||||
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 std::string& str, Blob& vchRet);
|
||||
|
||||
171
src/ripple/types/api/CryptoIdentifierStorage.h
Normal file
171
src/ripple/types/api/CryptoIdentifierStorage.h
Normal 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
|
||||
215
src/ripple/types/api/CryptoIdentifierType.h
Normal file
215
src/ripple/types/api/CryptoIdentifierType.h
Normal 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
|
||||
49
src/ripple/types/api/RippleAccountID.h
Normal file
49
src/ripple/types/api/RippleAccountID.h
Normal 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
|
||||
32
src/ripple/types/api/RippleAccountPrivateKey.h
Normal file
32
src/ripple/types/api/RippleAccountPrivateKey.h
Normal 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
|
||||
32
src/ripple/types/api/RippleAccountPublicKey.h
Normal file
32
src/ripple/types/api/RippleAccountPublicKey.h
Normal 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
|
||||
106
src/ripple/types/api/RippleCryptoIdentifier.h
Normal file
106
src/ripple/types/api/RippleCryptoIdentifier.h
Normal 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
|
||||
32
src/ripple/types/api/RipplePrivateKey.h
Normal file
32
src/ripple/types/api/RipplePrivateKey.h
Normal 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
|
||||
32
src/ripple/types/api/RipplePublicKey.h
Normal file
32
src/ripple/types/api/RipplePublicKey.h
Normal 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
|
||||
18
src/ripple/types/api/RipplePublicKeyHash.h
Normal file
18
src/ripple/types/api/RipplePublicKeyHash.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_TYPES_RIPPLEPUBLICKEYHASH_H_INCLUDED
|
||||
#define RIPPLE_TYPES_RIPPLEPUBLICKEYHASH_H_INCLUDED
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** A container holding the hash of a public key in binary format. */
|
||||
typedef UnsignedInteger <20> RipplePublicKeyHash;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,14 +13,12 @@ namespace ripple {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void Base58::setCurrentAlphabet (char const* alphabet)
|
||||
{
|
||||
s_currentAlphabet = alphabet;
|
||||
unsigned char const* const p (
|
||||
static_cast <unsigned char const*>(in));
|
||||
uint256 hash (SHA256Hash (p, p + bytes));
|
||||
memcpy (out, hash.begin(), 4);
|
||||
}
|
||||
|
||||
char const* Base58::getBitcoinAlphabet ()
|
||||
@@ -38,27 +36,23 @@ char const* Base58::getTestnetAlphabet ()
|
||||
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;
|
||||
CBigNum bn58 = 58;
|
||||
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
|
||||
CBigNum bn (vchTmp);
|
||||
CBigNum bn (begin, end);
|
||||
std::size_t const size (std::distance (begin, end));
|
||||
|
||||
// Convert bignum to std::string
|
||||
std::string str;
|
||||
// Expected size increase from base58 conversion is approximately 137%
|
||||
// use 138% to be safe
|
||||
str.reserve ((pend - pbegin) * 138 / 100 + 1);
|
||||
str.reserve (size * 138 / 100 + 1);
|
||||
CBigNum dv;
|
||||
CBigNum rem;
|
||||
|
||||
@@ -71,9 +65,8 @@ std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pe
|
||||
unsigned int c = rem.getuint ();
|
||||
str += alphabet [c];
|
||||
}
|
||||
|
||||
// Leading zeroes encoded as base58 zeros
|
||||
for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
|
||||
|
||||
for (const unsigned char* p = end-2; p >= begin && *p == 0; p--)
|
||||
str += alphabet [0];
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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
|
||||
Blob vch (vchIn);
|
||||
uint256 hash = SHA256Hash (vch.begin (), vch.end ());
|
||||
vch.insert (vch.end (), (unsigned char*)&hash, (unsigned char*)&hash + 4);
|
||||
return encode (vch);
|
||||
s_currentAlphabet = alphabet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
bool Base58::decode (const char* psz, Blob& vchRet, const char* pAlpha)
|
||||
{
|
||||
assert (pAlpha != 0);
|
||||
|
||||
11
src/ripple/types/impl/RippleCryptoIdentifierTests.cpp
Normal file
11
src/ripple/types/impl/RippleCryptoIdentifierTests.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
namespace ripple {
|
||||
|
||||
// Relocated to RippleAddress.cpp
|
||||
|
||||
}
|
||||
@@ -24,3 +24,4 @@
|
||||
#include "impl/UInt128.cpp"
|
||||
#include "impl/UInt160.cpp"
|
||||
#include "impl/UInt256.cpp"
|
||||
#include "impl/RippleCryptoIdentifierTests.cpp"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#define RIPPLE_TYPES_H_INCLUDED
|
||||
|
||||
#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 <boost/functional/hash.hpp>
|
||||
@@ -36,5 +37,15 @@ using namespace beast;
|
||||
# include "api/UInt256.h"
|
||||
# include "api/RandomNumbers.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
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
};
|
||||
|
||||
typedef boost::unordered_map <
|
||||
PublicKey, ValidatorInfo, PublicKey::HashFunction> MapType;
|
||||
PublicKey, ValidatorInfo, PublicKey::hasher> MapType;
|
||||
|
||||
struct State
|
||||
{
|
||||
@@ -318,12 +318,10 @@ public:
|
||||
iter != list->map().end(); ++iter)
|
||||
{
|
||||
Json::Value entry (Json::objectValue);
|
||||
/*
|
||||
ChosenList::MapType::key_type const& key (iter->first);
|
||||
ChosenList::MapType::mapped_type const& value (iter->second);
|
||||
entry ["key"] = key;
|
||||
entry ["value"] = value;
|
||||
*/
|
||||
entry ["key"] = key.to_string();
|
||||
//ChosenList::MapType::mapped_type const& value (iter->second);
|
||||
//entry ["value"] = value.to_string();
|
||||
entries.append (entry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ bool Utilities::parseInfoLine (
|
||||
else if (deprecatedPublicKey.setNodePublic (encodedKey))
|
||||
{
|
||||
// We got a public key.
|
||||
RipplePublicKey publicKey (deprecatedPublicKey.toRipplePublicKey ());
|
||||
RipplePublicKey publicKey (deprecatedPublicKey);
|
||||
success = true;
|
||||
}
|
||||
else
|
||||
@@ -238,15 +238,15 @@ Time Utilities::stringToTime (String s)
|
||||
|
||||
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());
|
||||
return s;
|
||||
}
|
||||
|
||||
PublicKey Utilities::stringToPublicKey (std::string const& s)
|
||||
{
|
||||
bassert (s.size() == PublicKey::sizeInBytes);
|
||||
return PublicKey (&s.front());
|
||||
bassert (s.size() == PublicKey::size);
|
||||
return PublicKey ();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#define RIPPLE_VALIDATORS_H_INCLUDED
|
||||
|
||||
#include "beast/modules/beast_core/beast_core.h"
|
||||
#include "../ripple_basics/ripple_basics.h" // for RipplePublicKey, remove asap
|
||||
|
||||
#include "beast/beast/http/URL.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user