diff --git a/SConstruct b/SConstruct index 2826933754..fe051edb35 100644 --- a/SConstruct +++ b/SConstruct @@ -123,7 +123,8 @@ RIPPLE_SRCS = [ 'src/cpp/leveldb_core.cpp', 'src/cpp/websocket_core.cpp', 'modules/ripple_basics/ripple_basics.cpp', - 'modules/ripple_client/ripple_client.cpp', + 'modules/ripple_client/ripple_client.cpp', + 'modules/ripple_data/ripple_data.cpp', 'modules/ripple_db/ripple_db.cpp', 'modules/ripple_json/ripple_json.cpp', 'modules/ripple_ledger/ripple_ledger.cpp', @@ -139,6 +140,7 @@ for dir in ['.', 'ripple', 'database', 'json', 'leveldb/db', 'leveldb/port', 'le for dir in [ 'ripple_basics', 'ripple_client', + 'ripple_data', 'ripple_db', 'ripple_json', 'ripple_ledger', diff --git a/modules/ripple_basics/ripple_basics.cpp b/modules/ripple_basics/ripple_basics.cpp index 01b56d0452..785ef01135 100644 --- a/modules/ripple_basics/ripple_basics.cpp +++ b/modules/ripple_basics/ripple_basics.cpp @@ -22,8 +22,6 @@ @ingroup ripple_basics */ -#include "ripple_basics.h" - #include #include @@ -38,8 +36,6 @@ #include #endif - - #include #include #include @@ -47,7 +43,7 @@ #include // VFALCO: TODO, Replace OpenSSL randomness with a dependency-free implementation -// Perhaps Schneier's Yarrow or a variant. Abstract the collection of +// Perhaps Schneier's Fortuna or a variant. Abstract the collection of // entropy and provide OS-specific implementation. We can re-use the // BearShare source code for this. // @@ -55,6 +51,11 @@ // #include // Because of ripple_RandomNumbers.cpp + +#include "ripple_basics.h" + + + // VFALCO: TODO, fix these warnings! #ifdef _MSC_VER //#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" diff --git a/modules/ripple_basics/ripple_basics.h b/modules/ripple_basics/ripple_basics.h index fde95024a2..5103a4d425 100644 --- a/modules/ripple_basics/ripple_basics.h +++ b/modules/ripple_basics/ripple_basics.h @@ -104,6 +104,8 @@ namespace boost { // VFALCO: TODO, remove this dependency!!! #include // for DiffieHellmanUtil +#include // For HashUtilities +#include // For HashUtilities #include "../ripple_json/ripple_json.h" @@ -127,5 +129,6 @@ namespace boost { #include "utility/ripple_UptimeTimer.h" #include "types/ripple_UInt256.h" +#include "utility/ripple_HashUtilities.h" // requires UInt256 #endif diff --git a/modules/ripple_basics/types/ripple_UInt256.h b/modules/ripple_basics/types/ripple_UInt256.h index 58752081b5..16c837cf50 100644 --- a/modules/ripple_basics/types/ripple_UInt256.h +++ b/modules/ripple_basics/types/ripple_UInt256.h @@ -505,7 +505,6 @@ inline bool operator!=(const uint256& a, const uint256& b) { return (base_ui inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; } inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; } inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; } -extern std::size_t hash_value(const uint256&); template inline std::ostream& operator<<(std::ostream& out, const base_uint& u) { @@ -731,8 +730,6 @@ inline const uint160 operator^(const uint160& a, const uint160& b) { return (b inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; } inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; } -extern std::size_t hash_value(const uint160&); - inline const std::string strHex(const uint160& ui) { return strHex(ui.begin(), ui.size()); diff --git a/src/cpp/ripple/BitcoinUtil.h b/modules/ripple_basics/utility/ripple_HashUtilities.h similarity index 86% rename from src/cpp/ripple/BitcoinUtil.h rename to modules/ripple_basics/utility/ripple_HashUtilities.h index c124db3e63..be6aa4823d 100644 --- a/src/cpp/ripple/BitcoinUtil.h +++ b/modules/ripple_basics/utility/ripple_HashUtilities.h @@ -1,24 +1,11 @@ -#ifndef __BITCOIN_UTIL__ -#define __BITCOIN_UTIL__ - -// TODO: these things should all go somewhere - -#include -#include -#include - -std::string strprintf(const char* format, ...); -std::string FormatFullVersion(); -void RandAddSeedPerfmon(); - -#define loop for (;;) -#define PAIR(t1, t2) pair - -#if !defined(WIN32) && !defined(WIN64) -#define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d) -#endif +#ifndef RIPPLE_HASHUTILITIES_H +#define RIPPLE_HASHUTILITIES_H +// VFALCO: NOTE, these came from BitcoinUtil.h +// VFALCO: TODO, Rewrite the callers so we don't need templates, +// then define these in a .cpp so they are no longer inline. +// template inline uint256 SHA256Hash(const T1 pbegin, const T1 pend) { diff --git a/modules/ripple_basics/utility/ripple_RandomNumbers.cpp b/modules/ripple_basics/utility/ripple_RandomNumbers.cpp index 54b46da4f5..9fc67373ea 100644 --- a/modules/ripple_basics/utility/ripple_RandomNumbers.cpp +++ b/modules/ripple_basics/utility/ripple_RandomNumbers.cpp @@ -106,3 +106,77 @@ bool AddSystemEntropy() } #endif + +//------------------------------------------------------------------------------ + +// +// "Never go to sea with two chronometers; take one or three." +// Our three time sources are: +// - System clock +// - Median of other nodes's clocks +// - The user (asking the user to fix the system clock if the first two disagree) +// + +void RandAddSeedPerfmon() +{ + // VFALCO: This is how we simulate local functions + struct + { + int64 operator() () const + { + return time (NULL); + } + } GetTime; + + struct + { + void operator() () + { + struct + { + // VFALCO: TODO, clean this up + int64 operator() () const + { + int64 nCounter = 0; +#if defined(WIN32) || defined(WIN64) + QueryPerformanceCounter((LARGE_INTEGER*)&nCounter); +#else + timeval t; + gettimeofday(&t, NULL); + nCounter = t.tv_sec * 1000000 + t.tv_usec; +#endif + return nCounter; + } + } GetPerformanceCounter; + + // Seed with CPU performance counter + int64 nCounter = GetPerformanceCounter(); + RAND_add(&nCounter, sizeof(nCounter), 1.5); + memset(&nCounter, 0, sizeof(nCounter)); + } + } RandAddSeed; + + RandAddSeed(); + + // This can take up to 2 seconds, so only do it every 10 minutes + static int64 nLastPerfmon; + if (GetTime () < nLastPerfmon + 10 * 60) + return; + nLastPerfmon = GetTime (); + +#ifdef WIN32 + // Don't need this on Linux, OpenSSL automatically uses /dev/urandom + // Seed with the entire set of perfmon data + unsigned char pdata[250000]; + memset(pdata, 0, sizeof(pdata)); + unsigned long nSize = sizeof(pdata); + long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); + RegCloseKey(HKEY_PERFORMANCE_DATA); + if (ret == ERROR_SUCCESS) + { + RAND_add(pdata, nSize, nSize/100.0); + memset(pdata, 0, nSize); + //printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize); + } +#endif +} diff --git a/modules/ripple_basics/utility/ripple_RandomNumbers.h b/modules/ripple_basics/utility/ripple_RandomNumbers.h index c4560215f2..a2b7594620 100644 --- a/modules/ripple_basics/utility/ripple_RandomNumbers.h +++ b/modules/ripple_basics/utility/ripple_RandomNumbers.h @@ -45,4 +45,7 @@ inline static void getRand (void *buf, int num) return getRand (reinterpret_cast(buf), num); } +// Lifted from BitcoinUtil.h +extern void RandAddSeedPerfmon(); + #endif diff --git a/modules/ripple_basics/utility/ripple_StringUtilities.cpp b/modules/ripple_basics/utility/ripple_StringUtilities.cpp index 13ab9c0748..79a0d031d5 100644 --- a/modules/ripple_basics/utility/ripple_StringUtilities.cpp +++ b/modules/ripple_basics/utility/ripple_StringUtilities.cpp @@ -1,4 +1,35 @@ +#if !defined(WIN32) && !defined(WIN64) +#define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d) +#endif + +std::string strprintf(const char* format, ...) +{ + char buffer[50000]; + char* p = buffer; + int limit = sizeof(buffer); + int ret; + for (;;) + { + va_list arg_ptr; + va_start(arg_ptr, format); + ret = _vsnprintf(p, limit, format, arg_ptr); + va_end(arg_ptr); + if (ret >= 0 && ret < limit) + break; + if (p != buffer) + delete[] p; + limit *= 2; + p = new char[limit]; + if (p == NULL) + throw std::bad_alloc(); + } + std::string str(p, p+ret); + if (p != buffer) + delete[] p; + return str; +} + char charHex(int iDigit) { return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit; diff --git a/modules/ripple_basics/utility/ripple_StringUtilities.h b/modules/ripple_basics/utility/ripple_StringUtilities.h index bc18d67318..05efbd97ff 100644 --- a/modules/ripple_basics/utility/ripple_StringUtilities.h +++ b/modules/ripple_basics/utility/ripple_StringUtilities.h @@ -58,6 +58,8 @@ //------------------------------------------------------------------------------ +extern std::string strprintf(const char* format, ...); + extern std::string urlEncode(const std::string& strSrc); template diff --git a/modules/ripple_client/ripple_client.cpp b/modules/ripple_client/ripple_client.cpp index 39b6bcd122..e31af32280 100644 --- a/modules/ripple_client/ripple_client.cpp +++ b/modules/ripple_client/ripple_client.cpp @@ -24,6 +24,8 @@ #include "ripple_client.h" +#include "../ripple_data/ripple_data.h" + // VFALCO: TODO, fix these warnings! #ifdef _MSC_VER //#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" diff --git a/modules/ripple_data/crypto/ripple_Base58.cpp b/modules/ripple_data/crypto/ripple_Base58.cpp new file mode 100644 index 0000000000..47361c74df --- /dev/null +++ b/modules/ripple_data/crypto/ripple_Base58.cpp @@ -0,0 +1,183 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin Developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +char const* Base58::s_currentAlphabet = Base58::getRippleAlphabet (); + +char const* Base58::getCurrentAlphabet () +{ + return s_currentAlphabet; +} + +void Base58::setCurrentAlphabet (char const* alphabet) +{ + s_currentAlphabet = alphabet; +} + +char const* Base58::getBitcoinAlphabet () +{ + return "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; +} + +char const* Base58::getRippleAlphabet () +{ + return "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"; +} + +char const* Base58::getTestnetAlphabet () +{ + return "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz"; +} + +std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pend) +{ + 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 + std::vector vchTmp(pend-pbegin+1, 0); + std::reverse_copy(pbegin, pend, vchTmp.begin()); + + // Convert little endian data to bignum + CBigNum bn(vchTmp); + + // 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); + CBigNum dv; + CBigNum rem; + while (bn > bn0) + { + if (!BN_div(&dv, &rem, &bn, &bn58, pctx)) + throw bignum_error("EncodeBase58 : BN_div failed"); + bn = dv; + 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++) + str += alphabet [0]; + + // Convert little endian std::string to big endian + reverse(str.begin(), str.end()); + return str; +} + +std::string Base58::encode (const std::vector& vch) +{ + return encode (&vch[0], &vch[0] + vch.size()); +} + +std::string Base58::encodeWithCheck (const std::vector& vchIn) +{ + // add 4-byte hash check to the end + std::vector 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, std::vector& vchRet, const char* pAlpha) +{ + assert (pAlpha != 0); + + CAutoBN_CTX pctx; + vchRet.clear(); + CBigNum bn58 = 58; + CBigNum bn = 0; + CBigNum bnChar; + while (isspace(*psz)) + psz++; + + // Convert big endian string to bignum + for (const char* p = psz; *p; p++) + { + const char* p1 = strchr(pAlpha, *p); + if (p1 == NULL) + { + while (isspace(*p)) + p++; + if (*p != '\0') + return false; + break; + } + bnChar.setuint(p1 - pAlpha); + if (!BN_mul(&bn, &bn, &bn58, pctx)) + throw bignum_error("DecodeBase58 : BN_mul failed"); + bn += bnChar; + } + + // Get bignum as little endian data + std::vector vchTmp = bn.getvch(); + + // Trim off sign byte if present + if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80) + vchTmp.erase(vchTmp.end()-1); + + // Restore leading zeros + int nLeadingZeros = 0; + for (const char* p = psz; *p == pAlpha[0]; p++) + nLeadingZeros++; + vchRet.assign(nLeadingZeros + vchTmp.size(), 0); + + // Convert little endian data to big endian + std::reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size()); + return true; +} + +bool Base58::decode (const std::string& str, std::vector& vchRet) +{ + return decode (str.c_str(), vchRet); +} + +bool Base58::decodeWithCheck (const char* psz, std::vector& vchRet, const char* pAlphabet) +{ + if (!decode (psz, vchRet, pAlphabet)) + return false; + if (vchRet.size() < 4) + { + vchRet.clear(); + return false; + } + uint256 hash = SHA256Hash(vchRet.begin(), vchRet.end()-4); + if (memcmp(&hash, &vchRet.end()[-4], 4) != 0) + { + vchRet.clear(); + return false; + } + vchRet.resize(vchRet.size()-4); + return true; +} + +bool Base58::decodeWithCheck (const std::string& str, std::vector& vchRet, const char* pAlphabet) +{ + return decodeWithCheck (str.c_str(), vchRet, pAlphabet); +} + +// vim:ts=4 diff --git a/modules/ripple_data/crypto/ripple_Base58.h b/modules/ripple_data/crypto/ripple_Base58.h new file mode 100644 index 0000000000..c5f0b44102 --- /dev/null +++ b/modules/ripple_data/crypto/ripple_Base58.h @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin Developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// +// Why base-58 instead of standard base-64 encoding? +// - Don't want 0OIl characters that look the same in some fonts and +// could be used to create visually identical looking account numbers. +// - A string with non-alphanumeric characters is not as easily accepted as an account number. +// - E-mail usually won't line-break if there's no punctuation to break at. +// - Doubleclicking selects the whole number as one word if it's all alphanumeric. +// +#ifndef RIPPLE_BASE58_H +#define RIPPLE_BASE58_H + +/** Performs Base 58 encoding and decoding. +*/ +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); + static std::string encode (const std::vector& vch); + static std::string encodeWithCheck (const std::vector& vchIn); + + static bool decode (const char* psz, std::vector& vchRet, const char* pAlphabet=getCurrentAlphabet ()); + static bool decode (const std::string& str, std::vector& vchRet); + static bool decodeWithCheck (const char* psz, std::vector& vchRet, const char* pAlphabet=getCurrentAlphabet ()); + static bool decodeWithCheck (const std::string& str, std::vector& vchRet, const char* pAlphabet); + +private: + static char const* s_currentAlphabet; +}; + +#endif +// vim:ts=4 diff --git a/modules/ripple_data/crypto/ripple_Base58Data.cpp b/modules/ripple_data/crypto/ripple_Base58Data.cpp new file mode 100644 index 0000000000..35ac093cf9 --- /dev/null +++ b/modules/ripple_data/crypto/ripple_Base58Data.cpp @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin Developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// +// Why base-58 instead of standard base-64 encoding? +// - Don't want 0OIl characters that look the same in some fonts and +// could be used to create visually identical looking account numbers. +// - A string with non-alphanumeric characters is not as easily accepted as an account number. +// - E-mail usually won't line-break if there's no punctuation to break at. +// - Doubleclicking selects the whole number as one word if it's all alphanumeric. +// + +CBase58Data::CBase58Data() + : nVersion(1) +{ +} + +CBase58Data::~CBase58Data() +{ + if (!vchData.empty()) + memset(&vchData[0], 0, vchData.size()); +} + +void CBase58Data::SetData(int nVersionIn, const std::vector& vchDataIn) +{ + nVersion = nVersionIn; + vchData = vchDataIn; +} + +void CBase58Data::SetData(int nVersionIn, const void* pdata, size_t nSize) +{ + nVersion = nVersionIn; + vchData.resize(nSize); + if (nSize) + memcpy(&vchData[0], pdata, nSize); +} + +void CBase58Data::SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) +{ + SetData(nVersionIn, (void*)pbegin, pend - pbegin); +} + +bool CBase58Data::SetString(const char* psz, unsigned char version, const char* pAlphabet) +{ + std::vector vchTemp; + Base58::decodeWithCheck (psz, vchTemp, pAlphabet); + if (vchTemp.empty() || vchTemp[0] != version) + { + vchData.clear(); + nVersion = 1; + return false; + } + nVersion = vchTemp[0]; + vchData.resize(vchTemp.size() - 1); + if (!vchData.empty()) + memcpy(&vchData[0], &vchTemp[1], vchData.size()); + memset(&vchTemp[0], 0, vchTemp.size()); + return true; +} + +bool CBase58Data::SetString(const std::string& str, unsigned char version) +{ + return SetString(str.c_str(), version); +} + +std::string CBase58Data::ToString() const +{ + std::vector vch(1, nVersion); + + vch.insert(vch.end(), vchData.begin(), vchData.end()); + + return Base58::encodeWithCheck (vch); +} + +int CBase58Data::CompareTo(const CBase58Data& b58) const +{ + if (nVersion < b58.nVersion) return -1; + if (nVersion > b58.nVersion) return 1; + if (vchData < b58.vchData) return -1; + if (vchData > b58.vchData) return 1; + return 0; +} + +bool CBase58Data::operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; } +bool CBase58Data::operator!=(const CBase58Data& b58) const { return CompareTo(b58) != 0; } +bool CBase58Data::operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; } +bool CBase58Data::operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; } +bool CBase58Data::operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; } +bool CBase58Data::operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; } + +// vim:ts=4 diff --git a/modules/ripple_data/crypto/ripple_Base58Data.h b/modules/ripple_data/crypto/ripple_Base58Data.h new file mode 100644 index 0000000000..7f9892a6ea --- /dev/null +++ b/modules/ripple_data/crypto/ripple_Base58Data.h @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin Developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// +// Why base-58 instead of standard base-64 encoding? +// - Don't want 0OIl characters that look the same in some fonts and +// could be used to create visually identical looking account numbers. +// - A string with non-alphanumeric characters is not as easily accepted as an account number. +// - E-mail usually won't line-break if there's no punctuation to break at. +// - Doubleclicking selects the whole number as one word if it's all alphanumeric. +// +#ifndef RIPPLE_BASE58DATA_H +#define RIPPLE_BASE58DATA_H + +class CBase58Data +{ +protected: + unsigned char nVersion; + std::vector vchData; + + CBase58Data(); + ~CBase58Data(); + + void SetData(int nVersionIn, const std::vector& vchDataIn); + void SetData(int nVersionIn, const void* pdata, size_t nSize); + void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend); + +public: + bool SetString(const char* psz, unsigned char version, const char* pAlphabet = 0); + bool SetString(const std::string& str, unsigned char version); + + std::string ToString() const; + int CompareTo(const CBase58Data& b58) const; + + bool operator==(const CBase58Data& b58) const; + bool operator!=(const CBase58Data& b58) const; + bool operator<=(const CBase58Data& b58) const; + bool operator>=(const CBase58Data& b58) const; + bool operator< (const CBase58Data& b58) const; + bool operator> (const CBase58Data& b58) const; + + friend std::size_t hash_value(const CBase58Data& b58); +}; + +#endif +// vim:ts=4 diff --git a/modules/ripple_data/crypto/ripple_CBigNum.cpp b/modules/ripple_data/crypto/ripple_CBigNum.cpp new file mode 100644 index 0000000000..f577b6c6d0 --- /dev/null +++ b/modules/ripple_data/crypto/ripple_CBigNum.cpp @@ -0,0 +1,530 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +CBigNum::CBigNum() +{ + BN_init(this); +} + +CBigNum::CBigNum(const CBigNum& b) +{ + BN_init(this); + if (!BN_copy(this, &b)) + { + BN_clear_free(this); + throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); + } +} + +CBigNum& CBigNum::operator=(const CBigNum& b) +{ + if (!BN_copy(this, &b)) + throw bignum_error("CBigNum::operator= : BN_copy failed"); + return (*this); +} + +CBigNum::~CBigNum() +{ + BN_clear_free(this); +} + +CBigNum::CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } +CBigNum::CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } +CBigNum::CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } +CBigNum::CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } +CBigNum::CBigNum(int64 n) { BN_init(this); setint64(n); } +CBigNum::CBigNum(unsigned char n) { BN_init(this); setulong(n); } +CBigNum::CBigNum(unsigned short n) { BN_init(this); setulong(n); } +CBigNum::CBigNum(unsigned int n) { BN_init(this); setulong(n); } +CBigNum::CBigNum(uint64 n) { BN_init(this); setuint64(n); } +CBigNum::CBigNum(uint256 n) { BN_init(this); setuint256(n); } + +CBigNum::CBigNum(const std::vector& vch) +{ + BN_init(this); + setvch(vch); +} + +void CBigNum::setuint(unsigned int n) +{ + setulong(static_cast(n)); +} + +unsigned int CBigNum::getuint() const +{ + return BN_get_word(this); +} + +int CBigNum::getint() const +{ + unsigned long n = BN_get_word(this); + if (!BN_is_negative(this)) + return (n > INT_MAX ? INT_MAX : n); + else + return (n > INT_MAX ? INT_MIN : -(int)n); +} + +void CBigNum::setint64(int64 n) +{ + unsigned char pch[sizeof(n) + 6]; + unsigned char* p = pch + 4; + bool fNegative = false; + if (n < (int64)0) + { + n = -n; + fNegative = true; + } + bool fLeadingZeroes = true; + for (int i = 0; i < 8; i++) + { + unsigned char c = (n >> 56) & 0xff; + n <<= 8; + if (fLeadingZeroes) + { + if (c == 0) + continue; + if (c & 0x80) + *p++ = (fNegative ? 0x80 : 0); + else if (fNegative) + c |= 0x80; + fLeadingZeroes = false; + } + *p++ = c; + } + unsigned int nSize = p - (pch + 4); + pch[0] = (nSize >> 24) & 0xff; + pch[1] = (nSize >> 16) & 0xff; + pch[2] = (nSize >> 8) & 0xff; + pch[3] = (nSize) & 0xff; + BN_mpi2bn(pch, p - pch, this); +} + +uint64 CBigNum::getuint64() const +{ +#if (ULONG_MAX > UINT_MAX) + return static_cast(getulong()); +#else + int len = BN_num_bytes(this); + if (len > 8) + throw std::runtime_error("BN getuint64 overflow"); + + unsigned char buf[8]; + memset(buf, 0, sizeof(buf)); + BN_bn2bin(this, buf + 8 - len); + return + static_cast(buf[0]) << 56 | static_cast(buf[1]) << 48 | + static_cast(buf[2]) << 40 | static_cast(buf[3]) << 32 | + static_cast(buf[4]) << 24 | static_cast(buf[5]) << 16 | + static_cast(buf[6]) << 8 | static_cast(buf[7]); +#endif +} + +void CBigNum::setuint64(uint64 n) +{ +#if (ULONG_MAX > UINT_MAX) + setulong(static_cast(n)); +#else + unsigned char buf[8]; + buf[0] = static_cast((n >> 56) & 0xff); + buf[1] = static_cast((n >> 48) & 0xff); + buf[2] = static_cast((n >> 40) & 0xff); + buf[3] = static_cast((n >> 32) & 0xff); + buf[4] = static_cast((n >> 24) & 0xff); + buf[5] = static_cast((n >> 16) & 0xff); + buf[6] = static_cast((n >> 8) & 0xff); + buf[7] = static_cast((n) & 0xff); + BN_bin2bn(buf, 8, this); +#endif +} + +void CBigNum::setuint256(const uint256& n) +{ + BN_bin2bn(n.begin(), n.size(), NULL); +} + +uint256 CBigNum::getuint256() +{ + uint256 ret; + unsigned int size = BN_num_bytes(this); + if (size > ret.size()) + return ret; + BN_bn2bin(this, ret.begin() + (ret.size() - BN_num_bytes(this))); + return ret; +} + +void CBigNum::setvch(const std::vector& vch) +{ + std::vector vch2(vch.size() + 4); + unsigned int nSize = vch.size(); + // BIGNUM's byte stream format expects 4 bytes of + // big endian size data info at the front + vch2[0] = (nSize >> 24) & 0xff; + vch2[1] = (nSize >> 16) & 0xff; + 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); + BN_mpi2bn(&vch2[0], vch2.size(), this); +} + +std::vector CBigNum::getvch() const +{ + unsigned int nSize = BN_bn2mpi(this, NULL); + if (nSize < 4) + return std::vector(); + std::vector vch(nSize); + BN_bn2mpi(this, &vch[0]); + vch.erase(vch.begin(), vch.begin() + 4); + reverse(vch.begin(), vch.end()); + return vch; +} + +CBigNum& CBigNum::SetCompact(unsigned int nCompact) +{ + unsigned int nSize = nCompact >> 24; + std::vector vch(4 + nSize); + vch[3] = nSize; + if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff; + if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff; + if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff; + BN_mpi2bn(&vch[0], vch.size(), this); + return *this; +} + +unsigned int CBigNum::GetCompact() const +{ + unsigned int nSize = BN_bn2mpi(this, NULL); + std::vector vch(nSize); + nSize -= 4; + BN_bn2mpi(this, &vch[0]); + unsigned int nCompact = nSize << 24; + if (nSize >= 1) nCompact |= (vch[4] << 16); + if (nSize >= 2) nCompact |= (vch[5] << 8); + if (nSize >= 3) nCompact |= (vch[6] << 0); + return nCompact; +} + +void CBigNum::SetHex(const std::string& str) +{ + // skip 0x + const char* psz = str.c_str(); + while (isspace(*psz)) + psz++; + bool fNegative = false; + if (*psz == '-') + { + fNegative = true; + psz++; + } + if (psz[0] == '0' && tolower(psz[1]) == 'x') + psz += 2; + while (isspace(*psz)) + psz++; + + // hex string to bignum + static char phexdigit[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, + 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; + *this = 0; + while (isxdigit(*psz)) + { + *this <<= 4; + int n = phexdigit[(int) *psz++]; + *this += n; + } + if (fNegative) + *this = 0 - *this; +} + +std::string CBigNum::ToString(int nBase) const +{ + CAutoBN_CTX pctx; + CBigNum bnBase = nBase; + CBigNum bn0 = 0; + std::string str; + CBigNum bn = *this; + BN_set_negative(&bn, false); + CBigNum dv; + CBigNum rem; + if (BN_cmp(&bn, &bn0) == 0) + return "0"; + while (BN_cmp(&bn, &bn0) > 0) + { + if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) + throw bignum_error("CBigNum::ToString() : BN_div failed"); + bn = dv; + unsigned int c = rem.getuint(); + str += "0123456789abcdef"[c]; + } + if (BN_is_negative(this)) + str += "-"; + reverse(str.begin(), str.end()); + return str; +} + +std::string CBigNum::GetHex() const +{ + return ToString(16); +} + +bool CBigNum::operator!() const +{ + return BN_is_zero(this); +} + +CBigNum& CBigNum::operator+=(const CBigNum& b) +{ + if (!BN_add(this, this, &b)) + throw bignum_error("CBigNum::operator+= : BN_add failed"); + return *this; +} + +CBigNum& CBigNum::operator-=(const CBigNum& b) +{ + *this = *this - b; + return *this; +} + +CBigNum& CBigNum::operator*=(const CBigNum& b) +{ + CAutoBN_CTX pctx; + if (!BN_mul(this, this, &b, pctx)) + throw bignum_error("CBigNum::operator*= : BN_mul failed"); + return *this; +} + +CBigNum& CBigNum::operator/=(const CBigNum& b) +{ + *this = *this / b; + return *this; +} + +CBigNum& CBigNum::operator%=(const CBigNum& b) +{ + *this = *this % b; + return *this; +} + +CBigNum& CBigNum::operator<<=(unsigned int shift) +{ + if (!BN_lshift(this, this, shift)) + throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); + return *this; +} + +CBigNum& CBigNum::operator>>=(unsigned int shift) +{ + // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number + // if built on ubuntu 9.04 or 9.10, probably depends on version of openssl + CBigNum a = 1; + a <<= shift; + if (BN_cmp(&a, this) > 0) + { + *this = 0; + return *this; + } + + if (!BN_rshift(this, this, shift)) + throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); + return *this; +} + + +CBigNum& CBigNum::operator++() +{ + // prefix operator + if (!BN_add(this, this, BN_value_one())) + throw bignum_error("CBigNum::operator++ : BN_add failed"); + return *this; +} + +const CBigNum CBigNum::operator++(int) +{ + // postfix operator + const CBigNum ret = *this; + ++(*this); + return ret; +} + +CBigNum& CBigNum::operator--() +{ + // prefix operator + CBigNum r; + if (!BN_sub(&r, this, BN_value_one())) + throw bignum_error("CBigNum::operator-- : BN_sub failed"); + *this = r; + return *this; +} + +const CBigNum CBigNum::operator--(int) +{ + // postfix operator + const CBigNum ret = *this; + --(*this); + return ret; +} + +void CBigNum::setulong(unsigned long n) +{ + if (!BN_set_word(this, n)) + throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); +} + +unsigned long CBigNum::getulong() const +{ + return BN_get_word(this); +} + +const CBigNum operator+(const CBigNum& a, const CBigNum& b) +{ + CBigNum r; + if (!BN_add(&r, &a, &b)) + throw bignum_error("CBigNum::operator+ : BN_add failed"); + return r; +} + +const CBigNum operator-(const CBigNum& a, const CBigNum& b) +{ + CBigNum r; + if (!BN_sub(&r, &a, &b)) + throw bignum_error("CBigNum::operator- : BN_sub failed"); + return r; +} + +const CBigNum operator-(const CBigNum& a) +{ + CBigNum r(a); + BN_set_negative(&r, !BN_is_negative(&r)); + return r; +} + +const CBigNum operator*(const CBigNum& a, const CBigNum& b) +{ + CAutoBN_CTX pctx; + CBigNum r; + if (!BN_mul(&r, &a, &b, pctx)) + throw bignum_error("CBigNum::operator* : BN_mul failed"); + return r; +} + +const CBigNum operator/(const CBigNum& a, const CBigNum& b) +{ + CAutoBN_CTX pctx; + CBigNum r; + if (!BN_div(&r, NULL, &a, &b, pctx)) + throw bignum_error("CBigNum::operator/ : BN_div failed"); + return r; +} + +const CBigNum operator%(const CBigNum& a, const CBigNum& b) +{ + CAutoBN_CTX pctx; + CBigNum r; + if (!BN_mod(&r, &a, &b, pctx)) + throw bignum_error("CBigNum::operator% : BN_div failed"); + return r; +} + +const CBigNum operator<<(const CBigNum& a, unsigned int shift) +{ + CBigNum r; + if (!BN_lshift(&r, &a, shift)) + throw bignum_error("CBigNum:operator<< : BN_lshift failed"); + return r; +} + +const CBigNum operator>>(const CBigNum& a, unsigned int shift) +{ + CBigNum r = a; + r >>= shift; + return r; +} + +bool operator== (const CBigNum& a, const CBigNum& b) +{ + return (BN_cmp(&a, &b) == 0); +} + +bool operator!= (const CBigNum& a, const CBigNum& b) +{ + return (BN_cmp(&a, &b) != 0); +} + +bool operator<= (const CBigNum& a, const CBigNum& b) +{ + return (BN_cmp(&a, &b) <= 0); +} + +bool operator>= (const CBigNum& a, const CBigNum& b) +{ + return (BN_cmp(&a, &b) >= 0); +} + +bool operator< (const CBigNum& a, const CBigNum& b) +{ + return (BN_cmp(&a, &b) < 0); +} + +bool operator> (const CBigNum& a, const CBigNum& b) +{ + return (BN_cmp(&a, &b) > 0); +} + +#if (ULONG_MAX > UINT_MAX) + +int BN_add_word64 (BIGNUM* bn, uint64 word) +{ + return BN_add_word(bn, word); +} + +int BN_sub_word64 (BIGNUM* bn, uint64 word) +{ + return BN_sub_word(bn, word); +} + +int BN_mul_word64 (BIGNUM* bn, uint64 word) +{ + return BN_mul_word(bn, word); +} + +uint64 BN_div_word64 (BIGNUM* bn, uint64 word) +{ + return BN_div_word(bn, word); +} + +#else + +int BN_add_word64 (BIGNUM *a, uint64 w) +{ + CBigNum bn(w); + return BN_add(a, &bn, a); +} + +int BN_sub_word64 (BIGNUM *a, uint64 w) +{ + CBigNum bn(w); + return BN_sub(a, &bn, a); +} + +int BN_mul_word64 (BIGNUM *a, uint64 w) +{ + CBigNum bn(w); + CAutoBN_CTX ctx; + return BN_mul(a, &bn, a, ctx); +} + +uint64 BN_div_word64 (BIGNUM *a, uint64 w) +{ + CBigNum bn(w); + CAutoBN_CTX ctx; + return (BN_div(a, NULL, a, &bn, ctx) == 1) ? 0 : ((uint64)-1); +} + +#endif diff --git a/modules/ripple_data/crypto/ripple_CBigNum.h b/modules/ripple_data/crypto/ripple_CBigNum.h new file mode 100644 index 0000000000..c7c46b7bc9 --- /dev/null +++ b/modules/ripple_data/crypto/ripple_CBigNum.h @@ -0,0 +1,151 @@ +//------------------------------------------------------------------------------ +//============================================================================== + +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +#ifndef RIPPLE_CBIGNUM_H +#define RIPPLE_CBIGNUM_H + +//------------------------------------------------------------------------------ + +class bignum_error : public std::runtime_error +{ +public: + explicit bignum_error(const std::string& str) : std::runtime_error(str) {} +}; + +//------------------------------------------------------------------------------ + +class CAutoBN_CTX +{ +private: + CAutoBN_CTX(const CAutoBN_CTX&); // no implementation + CAutoBN_CTX& operator=(const CAutoBN_CTX&); // no implementation + +protected: + BN_CTX* pctx; + CAutoBN_CTX& operator=(BN_CTX* pnew) { pctx = pnew; return *this; } + +public: + CAutoBN_CTX() + { + pctx = BN_CTX_new(); + if (pctx == NULL) + throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL"); + } + + ~CAutoBN_CTX() + { + if (pctx != NULL) + BN_CTX_free(pctx); + } + + operator BN_CTX*() { return pctx; } + BN_CTX& operator*() { return *pctx; } + BN_CTX** operator&() { return &pctx; } + bool operator!() { return (pctx == NULL); } +}; + +//------------------------------------------------------------------------------ + +// VFALCO: TODO figure out a way to remove the dependency on openssl in the +// header. Maybe rewrite this to use cryptopp. + +class CBigNum : public BIGNUM +{ +public: + CBigNum(); + CBigNum(const CBigNum& b); + CBigNum& operator=(const CBigNum& b); + CBigNum(char n); + CBigNum(short n); + CBigNum(int n); + CBigNum(long n); + CBigNum(int64 n); + CBigNum(unsigned char n); + CBigNum(unsigned short n); + CBigNum(unsigned int n); + CBigNum(uint64 n); + explicit CBigNum(uint256 n); + explicit CBigNum(const std::vector& vch); + ~CBigNum(); + + void setuint(unsigned int n); + unsigned int getuint() const; + int getint() const; + void setint64(int64 n); + uint64 getuint64() const; + void setuint64(uint64 n); + void setuint256(const uint256& n); + uint256 getuint256(); + void setvch(const std::vector& vch); + std::vector getvch() const; + CBigNum& SetCompact(unsigned int nCompact); + unsigned int GetCompact() const; + void SetHex(const std::string& str); + std::string ToString(int nBase=10) const; + std::string GetHex() const; + bool operator!() const; + CBigNum& operator+=(const CBigNum& b); + CBigNum& operator-=(const CBigNum& b); + CBigNum& operator*=(const CBigNum& b); + CBigNum& operator/=(const CBigNum& b); + CBigNum& operator%=(const CBigNum& b); + CBigNum& operator<<=(unsigned int shift); + CBigNum& operator>>=(unsigned int shift); + CBigNum& operator++(); + CBigNum& operator--(); + const CBigNum operator++(int); + const CBigNum operator--(int); + + friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b); + friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b); + friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b); + +private: + // private because the size of an unsigned long varies by platform + + void setulong(unsigned long n); + unsigned long getulong() const; +}; + +const CBigNum operator+(const CBigNum& a, const CBigNum& b); +const CBigNum operator-(const CBigNum& a, const CBigNum& b); +const CBigNum operator-(const CBigNum& a); +const CBigNum operator*(const CBigNum& a, const CBigNum& b); +const CBigNum operator/(const CBigNum& a, const CBigNum& b); +const CBigNum operator%(const CBigNum& a, const CBigNum& b); +const CBigNum operator<<(const CBigNum& a, unsigned int shift); +const CBigNum operator>>(const CBigNum& a, unsigned int shift); + +bool operator==(const CBigNum& a, const CBigNum& b); +bool operator!=(const CBigNum& a, const CBigNum& b); +bool operator<=(const CBigNum& a, const CBigNum& b); +bool operator>=(const CBigNum& a, const CBigNum& b); +bool operator<(const CBigNum& a, const CBigNum& b); +bool operator>(const CBigNum& a, const CBigNum& b); + +//------------------------------------------------------------------------------ + +// VFALCO: NOTE, this seems as good a place as any for this. + +// Here's the old implementation using macros, in case something broke +//#if (ULONG_MAX > UINT_MAX) +//#define BN_add_word64(bn, word) BN_add_word(bn, word) +//#define BN_sub_word64(bn, word) BN_sub_word(bn, word) +//#define BN_mul_word64(bn, word) BN_mul_word(bn, word) +//#define BN_div_word64(bn, word) BN_div_word(bn, word) +//#endif + +// VFALCO: I believe only STAmount uses these +extern int BN_add_word64 (BIGNUM *a, uint64 w); +extern int BN_sub_word64 (BIGNUM *a, uint64 w); +extern int BN_mul_word64 (BIGNUM *a, uint64 w); +extern uint64 BN_div_word64 (BIGNUM *a, uint64 w); + +#endif + +// vim:ts=4 diff --git a/modules/ripple_data/crypto/ripple_CKey.cpp b/modules/ripple_data/crypto/ripple_CKey.cpp new file mode 100644 index 0000000000..d4deeb6c23 --- /dev/null +++ b/modules/ripple_data/crypto/ripple_CKey.cpp @@ -0,0 +1,7 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + + +// VFALCO: TODO, move inlined stuff from CKey into here \ No newline at end of file diff --git a/src/cpp/ripple/key.h b/modules/ripple_data/crypto/ripple_CKey.h similarity index 87% rename from src/cpp/ripple/key.h rename to modules/ripple_data/crypto/ripple_CKey.h index 12913255ea..161e8251e8 100644 --- a/src/cpp/ripple/key.h +++ b/modules/ripple_data/crypto/ripple_CKey.h @@ -1,21 +1,27 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2011 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_KEY_H -#define BITCOIN_KEY_H - -#include -#include -#include - -#include -#include -#include - -#include - -#include "RippleAddress.h" +#ifndef RIPPLE_CKEY_H +#define RIPPLE_CKEY_H // secp256k1: // const unsigned int PRIVATE_KEY_SIZE = 279; @@ -26,6 +32,8 @@ // see www.keylength.com // script supports up to 75 for single byte push +// VFALCO: NOTE, this is unused +/* int static inline EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key) { int okay = 0; @@ -62,7 +70,7 @@ err: return (okay); } - +*/ class key_error : public std::runtime_error { @@ -70,11 +78,6 @@ public: explicit key_error(const std::string& str) : std::runtime_error(str) {} }; -//JED: typedef std::vector > CPrivKey; -//typedef std::vector > CSecret; - -typedef std::vector CPrivKey; -typedef std::vector CSecret; class CKey { protected: diff --git a/src/cpp/ripple/ECIES.cpp b/modules/ripple_data/crypto/ripple_CKeyECIES.cpp similarity index 97% rename from src/cpp/ripple/ECIES.cpp rename to modules/ripple_data/crypto/ripple_CKeyECIES.cpp index ac9c371550..43a46c2ea5 100644 --- a/src/cpp/ripple/ECIES.cpp +++ b/modules/ripple_data/crypto/ripple_CKeyECIES.cpp @@ -1,14 +1,3 @@ -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "key.h" // ECIES uses elliptic curve keys to send an encrypted message. diff --git a/src/cpp/ripple/rfc1751.cpp b/modules/ripple_data/crypto/ripple_RFC1751.cpp similarity index 94% rename from src/cpp/ripple/rfc1751.cpp rename to modules/ripple_data/crypto/ripple_RFC1751.cpp index edfb6d7d5e..bcfcc5471a 100644 --- a/src/cpp/ripple/rfc1751.cpp +++ b/modules/ripple_data/crypto/ripple_RFC1751.cpp @@ -2,6 +2,7 @@ // RFC 1751 code converted to C++/Boost. // +// VFALCO: TODO, move these #include #include #include @@ -11,7 +12,7 @@ #include #include -static const char* pcDict[2048] = { +char const* RFC1751::s_dictionary [2048] = { "A", "ABE", "ACE", "ACT", "AD", "ADA", "ADD", "AGO", "AID", "AIM", "AIR", "ALL", "ALP", "AM", "AMY", "AN", "ANA", "AND", "ANN", "ANT", "ANY", "APE", "APS", "APT", "ARC", "ARE", "ARK", @@ -238,7 +239,7 @@ static const char* pcDict[2048] = { /* Extract 'length' bits from the char array 's' starting with bit 'start' */ -static unsigned long extract(char *s, int start, int length) +unsigned long RFC1751::extract(char *s, int start, int length) { unsigned char cl; unsigned char cc; @@ -263,7 +264,7 @@ static unsigned long extract(char *s, int start, int length) // Encode 8 bytes in 'c' as a string of English words. // Returns a pointer to a static buffer -static void btoe(std::string& strHuman, const std::string& strData) +void RFC1751::btoe(std::string& strHuman, const std::string& strData) { char caBuffer[9]; /* add in room for the parity 2 bits*/ int p, i; @@ -277,15 +278,15 @@ static void btoe(std::string& strHuman, const std::string& strData) caBuffer[8] = char(p) << 6; strHuman = std::string() - + pcDict[extract(caBuffer, 0, 11)] + " " - + pcDict[extract(caBuffer, 11, 11)] + " " - + pcDict[extract(caBuffer, 22, 11)] + " " - + pcDict[extract(caBuffer, 33, 11)] + " " - + pcDict[extract(caBuffer, 44, 11)] + " " - + pcDict[extract(caBuffer, 55, 11)]; + + s_dictionary[extract(caBuffer, 0, 11)] + " " + + s_dictionary[extract(caBuffer, 11, 11)] + " " + + s_dictionary[extract(caBuffer, 22, 11)] + " " + + s_dictionary[extract(caBuffer, 33, 11)] + " " + + s_dictionary[extract(caBuffer, 44, 11)] + " " + + s_dictionary[extract(caBuffer, 55, 11)]; } -static void insert(char *s, int x, int start, int length) +void RFC1751::insert(char *s, int x, int start, int length) { unsigned char cl; unsigned char cc; @@ -315,7 +316,7 @@ static void insert(char *s, int x, int start, int length) } } -static void standard(std::string& strWord) +void RFC1751::standard(std::string& strWord) { BOOST_FOREACH(char cLetter, strWord) { @@ -343,7 +344,7 @@ static void standard(std::string& strWord) } // Binary search of dictionary. -static int wsrch(const std::string& strWord, int iMin, int iMax) +int RFC1751::wsrch (const std::string& strWord, int iMin, int iMax) { int iResult = -1; @@ -351,7 +352,7 @@ static int wsrch(const std::string& strWord, int iMin, int iMax) { // Have a range to search. int iMid = iMin+(iMax-iMin)/2; - int iDir = strWord.compare(pcDict[iMid]); + int iDir = strWord.compare(s_dictionary[iMid]); if (!iDir) { @@ -376,7 +377,7 @@ static int wsrch(const std::string& strWord, int iMin, int iMax) // 0 word not in data base // -1 badly formed in put ie > 4 char word // -2 words OK but parity is wrong -static int etob(std::string& strData, std::vector vsHuman) +int RFC1751::etob (std::string& strData, std::vector vsHuman) { int i, p, v,l; char b[9]; @@ -393,7 +394,7 @@ static int etob(std::string& strData, std::vector vsHuman) if(l > 4 || l < 1) return -1; - standard(strWord); + standard (strWord); v = wsrch(strWord, l < 4 ? 0 : 571, @@ -418,13 +419,15 @@ static int etob(std::string& strData, std::vector vsHuman) return 1; } -// eng2key() convert words seperated by spaces into a 128 bit key in big-endian format. -// eng2key() returns -// 1 if succeeded -// 0 if word not in dictionary -// -1 if badly formed string -// -2 if words are okay but parity is wrong. -int eng2key(std::string& strKey, const std::string& strHuman) +/** Convert words seperated by spaces into a 128 bit key in big-endian format. + + @return + 1 if succeeded + 0 if word not in dictionary + -1 if badly formed string + -2 if words are okay but parity is wrong. +*/ +int RFC1751::getKeyFromEnglish (std::string& strKey, const std::string& strHuman) { std::vector vWords; std::string strFirst, strSecond; @@ -451,8 +454,9 @@ int eng2key(std::string& strKey, const std::string& strHuman) return rc; } -// key2eng() given a 128 bit key in big-endian format, convert to human. -void key2eng(std::string& strHuman, const std::string& strKey) +/** Convert to human from a 128 bit key in big-endian format +*/ +void RFC1751::getEnglishFromKey (std::string& strHuman, const std::string& strKey) { std::string strFirst, strSecond; diff --git a/modules/ripple_data/crypto/ripple_RFC1751.h b/modules/ripple_data/crypto/ripple_RFC1751.h new file mode 100644 index 0000000000..78be4a314c --- /dev/null +++ b/modules/ripple_data/crypto/ripple_RFC1751.h @@ -0,0 +1,22 @@ +#ifndef RIPPLE_RFC1751_H +#define RIPPLE_RFC1751_H + +class RFC1751 +{ +public: + static int getKeyFromEnglish (std::string& strKey, const std::string& strHuman); + + static void getEnglishFromKey (std::string& strHuman, const std::string& strKey); + +private: + static unsigned long extract (char *s, int start, int length); + static void btoe (std::string& strHuman, const std::string& strData); + static void insert (char *s, int x, int start, int length); + static void standard (std::string& strWord); + static int wsrch (const std::string& strWord, int iMin, int iMax); + static int etob (std::string& strData, std::vector vsHuman); + + static char const* s_dictionary []; +}; + +#endif diff --git a/modules/ripple_data/ripple_data.cpp b/modules/ripple_data/ripple_data.cpp new file mode 100644 index 0000000000..31b444d310 --- /dev/null +++ b/modules/ripple_data/ripple_data.cpp @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +/** Add this to get the @ref ripple_data module. + + @file ripple_data.cpp + @ingroup ripple_data +*/ + +// RippleAddress +#include +#include +#include +#include +#include +#include +#include +#include + +// FieldNames +#include +#include +#include +#include + +// CKeyECIES +#include +#include +#include +#include +#include +#include +#include +#include + + + +#include "ripple_data.h" + + + +#include "crypto/ripple_Base58.h" // for RippleAddress +#include "crypto/ripple_RFC1751.h" + +// VFALCO: TODO, fix these warnings! +#ifdef _MSC_VER +//#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" +#pragma warning (disable: 4018) // signed/unsigned mismatch +//#pragma warning (disable: 4244) // conversion, possible loss of data +#endif + +#include "crypto/ripple_CBigNum.cpp" +#include "crypto/ripple_CKey.cpp" +#include "crypto/ripple_CKeyECIES.cpp" +#include "crypto/ripple_Base58.cpp" +#include "crypto/ripple_Base58Data.cpp" +#include "crypto/ripple_RFC1751.cpp" + +#include "types/ripple_FieldNames.cpp" +#include "types/ripple_RippleAddress.cpp" +#include "types/ripple_Serializer.cpp" + +#ifdef _MSC_VER +//#pragma warning (pop) +#endif diff --git a/modules/ripple_data/ripple_data.h b/modules/ripple_data/ripple_data.h new file mode 100644 index 0000000000..1ae674e594 --- /dev/null +++ b/modules/ripple_data/ripple_data.h @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +/** Include this to get the @ref ripple_data module. + + @file ripple_data.h + @ingroup ripple_data +*/ + +/** Ripple specific data representation and manipulation. + + These form the building blocks of Ripple data. + + @defgroup ripple_data +*/ + +#ifndef RIPPLE_DATA_H +#define RIPPLE_DATA_H + +// Base58Data +#include +#include +#include + +// CBigNum +#include +#include +#include + +// CKey +#include +#include +#include +#include +#include +#include +#include + +// Serializer +#include +#include +#include +#include + +// VFALCO: TODO, try to reduce these dependencies +#include "../ripple_basics/ripple_basics.h" + +#include "crypto/ripple_CBigNum.h" +//#include "crypto/ripple_CKey.h" +#include "crypto/ripple_Base58.h" // VFALCO: TODO, Can be moved to .cpp if we clean up setAlphabet stuff +#include "crypto/ripple_Base58Data.h" + +#include "types/ripple_FieldNames.h" +#include "types/ripple_RippleAddress.h" +#include "crypto/ripple_CKey.h" // needs RippleAddress VFALCO: TODO, remove this dependency cycle +#include "types/ripple_Serializer.h" // needs CKey + +#endif diff --git a/src/cpp/ripple/FieldNames.cpp b/modules/ripple_data/types/ripple_FieldNames.cpp similarity index 94% rename from src/cpp/ripple/FieldNames.cpp rename to modules/ripple_data/types/ripple_FieldNames.cpp index d73a802700..cd8af10d4a 100644 --- a/src/cpp/ripple/FieldNames.cpp +++ b/modules/ripple_data/types/ripple_FieldNames.cpp @@ -1,12 +1,4 @@ -#include "FieldNames.h" - -#include - -#include -#include -#include - // These must stay at the top of this file std::map SField::codeToField; @@ -22,7 +14,7 @@ SField sfIndex(STI_HASH256, 258, "index"); #define FIELD(name, type, index) SField sf##name(FIELD_CODE(STI_##type, index), STI_##type, index, #name); #define TYPE(name, type, index) -#include "SerializeProto.h" +#include "modules/ripple_data/types/ripple_SerializeDeclarations.h" #undef FIELD #undef TYPE @@ -73,7 +65,7 @@ SField::ref SField::getField(int code) #define FIELD(name, type, index) #define TYPE(name, type, index) case STI_##type: -#include "SerializeProto.h" +#include "modules/ripple_data/types/ripple_SerializeDeclarations.h" #undef FIELD #undef TYPE diff --git a/src/cpp/ripple/FieldNames.h b/modules/ripple_data/types/ripple_FieldNames.h similarity index 95% rename from src/cpp/ripple/FieldNames.h rename to modules/ripple_data/types/ripple_FieldNames.h index ffb481f00e..c4525e4574 100644 --- a/src/cpp/ripple/FieldNames.h +++ b/modules/ripple_data/types/ripple_FieldNames.h @@ -1,5 +1,5 @@ -#ifndef __FIELDNAMES__ -#define __FIELDNAMES__ +#ifndef RIPPLE_FIELDNAMES_H +#define RIPPLE_FIELDNAMES_H #include @@ -16,7 +16,7 @@ enum SerializedTypeID #define TYPE(name, field, value) STI_##field = value, #define FIELD(name, field, value) -#include "SerializeProto.h" +#include "modules/ripple_data/types/ripple_SerializeDeclarations.h" #undef TYPE #undef FIELD @@ -126,7 +126,7 @@ extern SField sfInvalid, sfGeneric, sfLedgerEntry, sfTransaction, sfValidation; #define FIELD(name, type, index) extern SField sf##name; #define TYPE(name, type, index) -#include "SerializeProto.h" +#include "modules/ripple_data/types/ripple_SerializeDeclarations.h" #undef FIELD #undef TYPE diff --git a/src/cpp/ripple/RippleAddress.cpp b/modules/ripple_data/types/ripple_RippleAddress.cpp similarity index 96% rename from src/cpp/ripple/RippleAddress.cpp rename to modules/ripple_data/types/ripple_RippleAddress.cpp index 0eff98a8c7..5cd4b4f2a4 100644 --- a/src/cpp/ripple/RippleAddress.cpp +++ b/modules/ripple_data/types/ripple_RippleAddress.cpp @@ -1,33 +1,12 @@ -#include -#include "RippleAddress.h" -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "key.h" -#include "BitcoinUtil.h" -#include "rfc1751.h" -#include "Serializer.h" -#include "Application.h" +// VFALCO: TODO, remove this when it's safe to do so. +#ifdef __APPLICATION__ +#error Including Application.h is disallowed! +#endif SETUP_LOG (RippleAddress) -std::size_t hash_value(const CBase58Data& b58) -{ - std::size_t seed = theApp->getNonceST() + (b58.nVersion * 0x9e3779b9); - boost::hash_combine(seed, b58.vchData); - return seed; -} - RippleAddress::RippleAddress() : mIsValid(false) { nVersion = VER_NONE; @@ -728,7 +707,7 @@ std::string RippleAddress::humanSeed1751() const strBig.assign(strLittle.rbegin(), strLittle.rend()); - key2eng(strHuman, strBig); + RFC1751::getEnglishFromKey (strHuman, strBig); return strHuman; } @@ -755,7 +734,7 @@ std::string RippleAddress::humanSeed() const int RippleAddress::setSeed1751(const std::string& strHuman1751) { std::string strKey; - int iResult = eng2key(strKey, strHuman1751); + int iResult = RFC1751::getKeyFromEnglish (strKey, strHuman1751); if (1 == iResult) { @@ -775,8 +754,6 @@ bool RippleAddress::setSeed(const std::string& strSeed) return mIsValid; } -extern const char *ALPHABET; - bool RippleAddress::setSeedGeneric(const std::string& strText) { RippleAddress naTemp; diff --git a/src/cpp/ripple/RippleAddress.h b/modules/ripple_data/types/ripple_RippleAddress.h similarity index 98% rename from src/cpp/ripple/RippleAddress.h rename to modules/ripple_data/types/ripple_RippleAddress.h index c86bfddd9c..dbbcb4935d 100644 --- a/src/cpp/ripple/RippleAddress.h +++ b/modules/ripple_data/types/ripple_RippleAddress.h @@ -1,7 +1,5 @@ -#ifndef __RIPPLE_ADDRESS__ -#define __RIPPLE_ADDRESS__ - -#include "base58.h" +#ifndef RIPPLE_RIPPLEADDRESS_H +#define RIPPLE_RIPPLEADDRESS_H // // Used to hold addresses and parse and produce human formats. diff --git a/src/cpp/ripple/SerializeProto.h b/modules/ripple_data/types/ripple_SerializeDeclarations.h similarity index 100% rename from src/cpp/ripple/SerializeProto.h rename to modules/ripple_data/types/ripple_SerializeDeclarations.h diff --git a/src/cpp/ripple/Serializer.cpp b/modules/ripple_data/types/ripple_Serializer.cpp similarity index 100% rename from src/cpp/ripple/Serializer.cpp rename to modules/ripple_data/types/ripple_Serializer.cpp diff --git a/src/cpp/ripple/Serializer.h b/modules/ripple_data/types/ripple_Serializer.h similarity index 97% rename from src/cpp/ripple/Serializer.h rename to modules/ripple_data/types/ripple_Serializer.h index eb1eeebaae..af9f750562 100644 --- a/src/cpp/ripple/Serializer.h +++ b/modules/ripple_data/types/ripple_Serializer.h @@ -1,14 +1,5 @@ -#ifndef __SERIALIZER__ -#define __SERIALIZER__ - -#include -#include -#include - -#include - -#include "key.h" -#include "FieldNames.h" +#ifndef RIPPLE_SERIALIZER_H +#define RIPPLE_SERIALIZER_H class Serializer { diff --git a/modules/ripple_ledger/ripple_ledger.cpp b/modules/ripple_ledger/ripple_ledger.cpp index ac0aba833c..f7faaa00c0 100644 --- a/modules/ripple_ledger/ripple_ledger.cpp +++ b/modules/ripple_ledger/ripple_ledger.cpp @@ -33,6 +33,8 @@ #include "ripple_ledger.h" +#include "../ripple_data/ripple_data.h" + //#define WIN32_LEAN_AND_MEAN #include @@ -72,14 +74,11 @@ #include "src/cpp/ripple/AccountSetTransactor.h" #include "src/cpp/ripple/AccountState.h" #include "src/cpp/ripple/Application.h" -#include "src/cpp/ripple/BitcoinUtil.h" #include "src/cpp/ripple/CanonicalTXSet.h" #include "src/cpp/ripple/ChangeTransactor.h" #include "src/cpp/ripple/Config.h" #include "src/cpp/ripple/FeatureTable.h" -#include "src/cpp/ripple/FieldNames.h" #include "src/cpp/ripple/HashPrefixes.h" -#include "src/cpp/ripple/key.h" #include "src/cpp/ripple/Ledger.h" #include "src/cpp/ripple/LedgerAcquire.h" #include "src/cpp/ripple/LedgerConsensus.h" @@ -100,7 +99,6 @@ #include "src/cpp/ripple/PaymentTransactor.h" #include "src/cpp/ripple/PFRequest.h" #include "src/cpp/ripple/RegularKeySetTransactor.h" -#include "src/cpp/ripple/RippleAddress.h" #include "src/cpp/ripple/RippleCalc.h" #include "src/cpp/ripple/RippleState.h" #include "src/cpp/ripple/SerializedLedger.h" @@ -108,7 +106,6 @@ #include "src/cpp/ripple/SerializedTransaction.h" #include "src/cpp/ripple/SerializedTypes.h" #include "src/cpp/ripple/SerializedValidation.h" -#include "src/cpp/ripple/Serializer.h" #include "src/cpp/ripple/SHAMapSync.h" #include "src/cpp/ripple/Transaction.h" #include "src/cpp/ripple/TransactionEngine.h" @@ -166,7 +163,6 @@ #include "src/cpp/ripple/SerializedTransaction.cpp" #include "src/cpp/ripple/SerializedTypes.cpp" #include "src/cpp/ripple/SerializedValidation.cpp" -#include "src/cpp/ripple/Serializer.cpp" // transactions #include "src/cpp/ripple/AccountSetTransactor.cpp" diff --git a/modules/ripple_main/misc/ripple_HashValue.cpp b/modules/ripple_main/misc/ripple_HashValue.cpp new file mode 100644 index 0000000000..4f0f1d05a8 --- /dev/null +++ b/modules/ripple_main/misc/ripple_HashValue.cpp @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +std::size_t hash_value(const uint256& u) +{ + std::size_t seed = theApp->getNonceST(); + + return u.hash_combine(seed); +} + +std::size_t hash_value(const uint160& u) +{ + std::size_t seed = theApp->getNonceST(); + + return u.hash_combine(seed); +} + +std::size_t hash_value(const CBase58Data& b58) +{ + std::size_t seed = theApp->getNonceST() + (b58.nVersion * 0x9e3779b9); + boost::hash_combine(seed, b58.vchData); + return seed; +} diff --git a/modules/ripple_main/misc/ripple_HashValue.h b/modules/ripple_main/misc/ripple_HashValue.h new file mode 100644 index 0000000000..54b51a48ea --- /dev/null +++ b/modules/ripple_main/misc/ripple_HashValue.h @@ -0,0 +1,34 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef RIPPLE_HASH_VALUE_H +#define RIPPLE_HASH_VALUE_H + +// VFALCO: TODO, clean this up +// +// These are needed for boost::hash stuff. The implemnetations access +// the Application object for the nonce, introducing a nasty dependency +// so I have split them away from the relevant classes and put them here. + +extern std::size_t hash_value(const uint160&); + +extern std::size_t hash_value(const uint256&); + +extern std::size_t hash_value(const CBase58Data& b58); + +#endif diff --git a/modules/ripple_main/ripple_main.cpp b/modules/ripple_main/ripple_main.cpp index bcf2e2b726..a2c3f176a0 100644 --- a/modules/ripple_main/ripple_main.cpp +++ b/modules/ripple_main/ripple_main.cpp @@ -24,6 +24,8 @@ #include "ripple_main.h" +#include "../ripple_data/ripple_data.h" + // VFALCO: TODO, fix these warnings! #ifdef _MSC_VER //#pragma warning (push) // Causes spurious C4503 "decorated name exceeds maximum length" @@ -42,6 +44,8 @@ #include "src/cpp/ripple/main.cpp" #include "src/cpp/ripple/ValidationCollection.cpp" +#include "misc/ripple_HashValue.cpp" + #ifdef _MSC_VER //#pragma warning (pop) #endif diff --git a/modules/ripple_mess/ripple_mess.cpp b/modules/ripple_mess/ripple_mess.cpp index fbe63aff25..6e34862369 100644 --- a/modules/ripple_mess/ripple_mess.cpp +++ b/modules/ripple_mess/ripple_mess.cpp @@ -24,17 +24,7 @@ #include "ripple_mess.h" -// VFALCO: TODO, clean this up -// This is here for Amount*.cpp -#include "src/cpp/ripple/bignum.h" -#if (ULONG_MAX > UINT_MAX) -#define BN_add_word64(bn, word) BN_add_word(bn, word) -#define BN_sub_word64(bn, word) BN_sub_word(bn, word) -#define BN_mul_word64(bn, word) BN_mul_word(bn, word) -#define BN_div_word64(bn, word) BN_div_word(bn, word) -#else -#include "src/cpp/ripple/BigNum64.h" -#endif +#include "../ripple_data/ripple_data.h" static const uint64 tenTo14 = 100000000000000ull; static const uint64 tenTo14m1 = tenTo14 - 1; @@ -50,17 +40,12 @@ static const uint64 tenTo17m1 = tenTo17 - 1; #include "src/cpp/ripple/Amount.cpp" #include "src/cpp/ripple/AmountRound.cpp" -#include "src/cpp/ripple/BitcoinUtil.cpp" // no log #include "src/cpp/ripple/DeterministicKeys.cpp" -#include "src/cpp/ripple/ECIES.cpp" // no log -#include "src/cpp/ripple/FieldNames.cpp" // no log #include "src/cpp/ripple/HashedObject.cpp" #include "src/cpp/ripple/PackedMessage.cpp" // no log #include "src/cpp/ripple/ParameterTable.cpp" // no log #include "src/cpp/ripple/ParseSection.cpp" #include "src/cpp/ripple/ProofOfWork.cpp" -#include "src/cpp/ripple/RippleAddress.cpp" -#include "src/cpp/ripple/rfc1751.cpp" // no log #include "src/cpp/ripple/SHAMap.cpp" #include "src/cpp/ripple/SHAMapDiff.cpp" // no log #include "src/cpp/ripple/SHAMapNodes.cpp" // no log diff --git a/modules/ripple_net/ripple_net.cpp b/modules/ripple_net/ripple_net.cpp index 1f6e542ad1..942ad54c06 100644 --- a/modules/ripple_net/ripple_net.cpp +++ b/modules/ripple_net/ripple_net.cpp @@ -24,6 +24,8 @@ #include "ripple_net.h" +#include "../ripple_data/ripple_data.h" + #include "../websocketpp/src/logger/logger.hpp" // for ripple_LogWebSockets.cpp // VFALCO: TODO, fix these warnings! diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 02efb6610c..afc68d059d 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -235,6 +235,57 @@ true + + true + true + true + true + + + true + true + + + true + true + true + true + + + true + true + true + true + + + true + true + + + true + true + true + true + + + + true + true + true + true + + + true + true + true + true + + + true + true + true + true + true @@ -256,6 +307,12 @@ + + true + true + true + true + @@ -639,12 +696,6 @@ true true - - true - true - true - true - true true @@ -693,24 +744,12 @@ true true - - true - true - true - true - true true true true - - true - true - true - true - true true @@ -923,18 +962,6 @@ true true - - true - true - true - true - - - true - true - true - true - true true @@ -1019,12 +1046,6 @@ true true - - true - true - true - true - true true @@ -1213,6 +1234,7 @@ + @@ -1223,6 +1245,16 @@ + + + + + + + + + + @@ -1235,6 +1267,7 @@ + @@ -1554,10 +1587,6 @@ - - - - @@ -1565,7 +1594,6 @@ - @@ -1573,7 +1601,6 @@ - @@ -1604,8 +1631,6 @@ - - @@ -1621,8 +1646,6 @@ - - diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index dff1e49226..187e811e0b 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -82,9 +82,6 @@ {6097a179-ddad-4c69-9a34-2e3fc2c9fa1d} - - {ed7b05c1-13c1-417c-a0c7-fb93a8edc5c9} - {29cd2103-d553-4d82-9e6a-224e3b1cb667} @@ -124,6 +121,21 @@ {b78453cf-8932-43ad-9312-09e5a8ca3375} + + {8d0241d2-071c-4d6c-a15e-980cc51b26ce} + + + {095d33d8-dbf7-44d0-a675-6722b02bb3be} + + + {ed7b05c1-13c1-417c-a0c7-fb93a8edc5c9} + + + {a624c4e9-2c7b-4b98-9dff-f57d876999f0} + + + {2f3572a9-2882-4656-ab93-82b7761c9e3d} + @@ -468,27 +480,15 @@ 1. Modules\ripple_ledger\serialization - - 1. Modules\ripple_ledger\serialization - 1. Modules\ripple_mess\types 1. Modules\ripple_mess\types - - 1. Modules\ripple_mess\types - 1. Modules\ripple_mess\types - - 1. Modules\ripple_mess\types - - - 1. Modules\ripple_mess\types - 1. Modules\ripple_mess\types @@ -504,9 +504,6 @@ 1. Modules\ripple_mess\types - - 1. Modules\ripple_mess\types - 1. Modules\ripple_basics @@ -610,28 +607,28 @@ 1. Modules\ripple_db\storage - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old 1. Modules\ripple_main @@ -651,9 +648,6 @@ 1. Modules\ripple_ledger\contracts - - 1. Modules\ripple_mess\types - 1. Modules\ripple_ledger\transactions @@ -676,7 +670,7 @@ 1. Modules\ripple_mess\containers - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old 1. Modules\ripple_ledger\processing @@ -759,6 +753,39 @@ 1. Modules\ripple_net\_unsorted + + 1. Modules\ripple_data + + + 1. Modules\ripple_main\misc + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\types + + + 1. Modules\ripple_data\types + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\types + + + 1. Modules\ripple_data\crypto + @@ -1094,33 +1121,12 @@ 1. Modules\ripple_ledger\serialization - - 1. Modules\ripple_ledger\serialization - - - 1. Modules\ripple_mess\types - - - 1. Modules\ripple_mess\types - - - 1. Modules\ripple_mess\types - - - 1. Modules\ripple_mess\types - - - 1. Modules\ripple_mess\types - 1. Modules\ripple_mess\types 1. Modules\ripple_mess\types - - 1. Modules\ripple_mess\types - 1. Modules\ripple_mess\types @@ -1133,9 +1139,6 @@ 1. Modules\ripple_mess\types - - 1. Modules\ripple_mess\types - 1. Modules\ripple_basics @@ -1239,25 +1242,25 @@ 1. Modules\ripple_db\storage - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old 1. Modules\ripple_main @@ -1277,9 +1280,6 @@ 1. Modules\ripple_ledger\contracts - - 1. Modules\ripple_mess\types - 1. Modules\ripple_ledger\transactions @@ -1299,7 +1299,7 @@ 1. Modules\ripple_mess\types - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old 1. Modules\ripple_ledger\processing @@ -1308,14 +1308,11 @@ 1. Modules\ripple_ledger\processing - 1. Modules\ripple_main\core + 1. Modules\ripple_main\_old 1. Modules\ripple_mess\protobuf - - 1. Modules\ripple_mess\types - 1. Modules\ripple_mess @@ -1427,6 +1424,42 @@ 1. Modules\ripple_net\_unsorted + + 1. Modules\ripple_basics\utility + + + 1. Modules\ripple_data + + + 1. Modules\ripple_main\misc + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\types + + + 1. Modules\ripple_data\types + + + 1. Modules\ripple_data\types + + + 1. Modules\ripple_data\crypto + + + 1. Modules\ripple_data\types + diff --git a/src/cpp/ripple/AccountState.h b/src/cpp/ripple/AccountState.h index eb853e8410..47e52a14eb 100644 --- a/src/cpp/ripple/AccountState.h +++ b/src/cpp/ripple/AccountState.h @@ -9,7 +9,6 @@ #include -#include "RippleAddress.h" #include "SerializedLedger.h" class AccountState diff --git a/src/cpp/ripple/Amount.cpp b/src/cpp/ripple/Amount.cpp index 8072f1e488..27029aedcb 100644 --- a/src/cpp/ripple/Amount.cpp +++ b/src/cpp/ripple/Amount.cpp @@ -16,17 +16,6 @@ SETUP_LOG (STAmount) uint64 STAmount::uRateOne = STAmount::getRate(STAmount(1), STAmount(1)); - - -#if (ULONG_MAX > UINT_MAX) -#define BN_add_word64(bn, word) BN_add_word(bn, word) -#define BN_sub_word64(bn, word) BN_sub_word(bn, word) -#define BN_mul_word64(bn, word) BN_mul_word(bn, word) -#define BN_div_word64(bn, word) BN_div_word(bn, word) -#else -#include "BigNum64.h" -#endif - bool STAmount::issuerFromString(uint160& uDstIssuer, const std::string& sIssuer) { bool bSuccess = true; diff --git a/src/cpp/ripple/AmountRound.cpp b/src/cpp/ripple/AmountRound.cpp index b2c21fe28e..4bd97062c5 100644 --- a/src/cpp/ripple/AmountRound.cpp +++ b/src/cpp/ripple/AmountRound.cpp @@ -3,15 +3,6 @@ #include "SerializedTypes.h" -#if (ULONG_MAX > UINT_MAX) -#define BN_add_word64(bn, word) BN_add_word(bn, word) -#define BN_sub_word64(bn, word) BN_sub_word(bn, word) -#define BN_mul_word64(bn, word) BN_mul_word(bn, word) -#define BN_div_word64(bn, word) BN_div_word(bn, word) -#else -#include "BigNum64.h" -#endif - // CAUTION: This is early code and is *NOT* ready for real use yet. static void canonicalizeRound(bool isNative, uint64& value, int& offset, bool roundUp) diff --git a/src/cpp/ripple/Application.cpp b/src/cpp/ripple/Application.cpp index 1da62bd3d9..a2dcdf4697 100644 --- a/src/cpp/ripple/Application.cpp +++ b/src/cpp/ripple/Application.cpp @@ -10,8 +10,6 @@ #include "Config.h" #include "PeerDoor.h" #include "RPCDoor.h" -#include "BitcoinUtil.h" -#include "key.h" #include "../database/SqliteDatabase.h" diff --git a/src/cpp/ripple/BigNum64.h b/src/cpp/ripple/BigNum64.h deleted file mode 100644 index 9aac72625d..0000000000 --- a/src/cpp/ripple/BigNum64.h +++ /dev/null @@ -1,33 +0,0 @@ - -#ifndef BIGNUM64_H -#define BIGNUM64_H - -// Support 64-bit word operations on 32-bit platforms - -static int BN_add_word64(BIGNUM *a, uint64 w) -{ - CBigNum bn(w); - return BN_add(a, &bn, a); -} - -static int BN_sub_word64(BIGNUM *a, uint64 w) -{ - CBigNum bn(w); - return BN_sub(a, &bn, a); -} - -static int BN_mul_word64(BIGNUM *a, uint64 w) -{ - CBigNum bn(w); - CAutoBN_CTX ctx; - return BN_mul(a, &bn, a, ctx); -} - -static uint64 BN_div_word64(BIGNUM *a, uint64 w) -{ - CBigNum bn(w); - CAutoBN_CTX ctx; - return (BN_div(a, NULL, a, &bn, ctx) == 1) ? 0 : ((uint64)-1); -} - -#endif diff --git a/src/cpp/ripple/BitcoinUtil.cpp b/src/cpp/ripple/BitcoinUtil.cpp deleted file mode 100644 index 44beac43bd..0000000000 --- a/src/cpp/ripple/BitcoinUtil.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include "BitcoinUtil.h" -#include -#include -#include - -#if defined(WIN32) || defined(WIN64) -#include -#else -#include -#endif - -using namespace std; - -std::string gFormatStr("v1"); - -std::string FormatFullVersion() -{ - return(gFormatStr); -} - - -string strprintf(const char* format, ...) -{ - char buffer[50000]; - char* p = buffer; - int limit = sizeof(buffer); - int ret; - loop - { - va_list arg_ptr; - va_start(arg_ptr, format); - ret = _vsnprintf(p, limit, format, arg_ptr); - va_end(arg_ptr); - if (ret >= 0 && ret < limit) - break; - if (p != buffer) - delete[] p; - limit *= 2; - p = new char[limit]; - if (p == NULL) - throw std::bad_alloc(); - } - string str(p, p+ret); - if (p != buffer) - delete[] p; - return str; -} - - -inline int64 GetPerformanceCounter() -{ - int64 nCounter = 0; -#if defined(WIN32) || defined(WIN64) - QueryPerformanceCounter((LARGE_INTEGER*)&nCounter); -#else - timeval t; - gettimeofday(&t, NULL); - nCounter = t.tv_sec * 1000000 + t.tv_usec; -#endif - return nCounter; -} - -void RandAddSeed() -{ - // Seed with CPU performance counter - int64 nCounter = GetPerformanceCounter(); - RAND_add(&nCounter, sizeof(nCounter), 1.5); - memset(&nCounter, 0, sizeof(nCounter)); -} -// -// "Never go to sea with two chronometers; take one or three." -// Our three time sources are: -// - System clock -// - Median of other nodes's clocks -// - The user (asking the user to fix the system clock if the first two disagree) -// -int64 GetTime() -{ - return time(NULL); -} - -void RandAddSeedPerfmon() -{ - RandAddSeed(); - - // This can take up to 2 seconds, so only do it every 10 minutes - static int64 nLastPerfmon; - if (GetTime() < nLastPerfmon + 10 * 60) - return; - nLastPerfmon = GetTime(); - -#ifdef WIN32 - // Don't need this on Linux, OpenSSL automatically uses /dev/urandom - // Seed with the entire set of perfmon data - unsigned char pdata[250000]; - memset(pdata, 0, sizeof(pdata)); - unsigned long nSize = sizeof(pdata); - long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); - RegCloseKey(HKEY_PERFORMANCE_DATA); - if (ret == ERROR_SUCCESS) - { - RAND_add(pdata, nSize, nSize/100.0); - memset(pdata, 0, nSize); - //printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize); - } -#endif -} diff --git a/src/cpp/ripple/CallRPC.cpp b/src/cpp/ripple/CallRPC.cpp index 3193e36832..89f2ed4e20 100644 --- a/src/cpp/ripple/CallRPC.cpp +++ b/src/cpp/ripple/CallRPC.cpp @@ -24,7 +24,6 @@ #include "RPC.h" #include "RPCErr.h" #include "Config.h" -#include "BitcoinUtil.h" #include "CallRPC.h" diff --git a/src/cpp/ripple/Config.cpp b/src/cpp/ripple/Config.cpp index a4a0b1a920..6a0d3a4880 100644 --- a/src/cpp/ripple/Config.cpp +++ b/src/cpp/ripple/Config.cpp @@ -80,8 +80,6 @@ #define DEFAULT_FEE_OPERATION 1 Config theConfig; -const char* ALPHABET = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"; -const char* ALPHABET_BITCOIN = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; void Config::setup(const std::string& strConf, bool bTestNet, bool bQuiet) { @@ -112,7 +110,7 @@ void Config::setup(const std::string& strConf, bool bTestNet, bool bQuiet) SIGN_PROPOSAL = TESTNET ? sHP_TestNetProposal : sHP_Proposal; if (TESTNET) - ALPHABET = "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz"; + Base58::setCurrentAlphabet (Base58::getTestnetAlphabet ()); if (!strConf.empty()) { diff --git a/src/cpp/ripple/Config.h b/src/cpp/ripple/Config.h index 422e9ebb41..501d7aa529 100644 --- a/src/cpp/ripple/Config.h +++ b/src/cpp/ripple/Config.h @@ -6,7 +6,6 @@ #include #include -#include "RippleAddress.h" #include "ParseSection.h" #include "SerializedTypes.h" @@ -200,8 +199,6 @@ public: extern Config theConfig; -extern const char* ALPHABET; -extern const char* ALPHABET_BITCOIN; #endif // vim:ts=4 diff --git a/src/cpp/ripple/DeterministicKeys.cpp b/src/cpp/ripple/DeterministicKeys.cpp index 7d28b101ed..2adfa17c01 100644 --- a/src/cpp/ripple/DeterministicKeys.cpp +++ b/src/cpp/ripple/DeterministicKeys.cpp @@ -11,8 +11,6 @@ #include -#include "Serializer.h" - // <-- seed uint128 CKey::PassPhraseToKey(const std::string& passPhrase) { diff --git a/src/cpp/ripple/HashedObject.cpp b/src/cpp/ripple/HashedObject.cpp index 7bfc5a9926..9366be5d99 100644 --- a/src/cpp/ripple/HashedObject.cpp +++ b/src/cpp/ripple/HashedObject.cpp @@ -10,7 +10,6 @@ #include "../database/SqliteDatabase.h" -#include "Serializer.h" #include "Application.h" SETUP_LOG (HashedObject) diff --git a/src/cpp/ripple/HashedObject.h b/src/cpp/ripple/HashedObject.h index 8ddfa6bfe9..f85a66ca6a 100644 --- a/src/cpp/ripple/HashedObject.h +++ b/src/cpp/ripple/HashedObject.h @@ -6,6 +6,8 @@ #include #include +#include "modules/ripple_main/misc/ripple_HashValue.h" + #include "ScopedLock.h" #include "InstanceCounter.h" diff --git a/src/cpp/ripple/Ledger.h b/src/cpp/ripple/Ledger.h index 5d8c9bfcdd..1226aad7ac 100644 --- a/src/cpp/ripple/Ledger.h +++ b/src/cpp/ripple/Ledger.h @@ -12,7 +12,6 @@ #include "TransactionMeta.h" #include "AccountState.h" #include "NicknameState.h" -#include "BitcoinUtil.h" #include "SHAMap.h" #include "InstanceCounter.h" #include "LoadMonitor.h" diff --git a/src/cpp/ripple/LedgerConsensus.h b/src/cpp/ripple/LedgerConsensus.h index d4864eafe5..5f8e769ce9 100644 --- a/src/cpp/ripple/LedgerConsensus.h +++ b/src/cpp/ripple/LedgerConsensus.h @@ -8,7 +8,6 @@ #include #include -#include "key.h" #include "Transaction.h" #include "LedgerAcquire.h" #include "LedgerProposal.h" diff --git a/src/cpp/ripple/LedgerProposal.h b/src/cpp/ripple/LedgerProposal.h index 3c7b417fb0..2cae5111b1 100644 --- a/src/cpp/ripple/LedgerProposal.h +++ b/src/cpp/ripple/LedgerProposal.h @@ -6,8 +6,6 @@ #include -#include "RippleAddress.h" -#include "Serializer.h" #include "InstanceCounter.h" DEFINE_INSTANCE(LedgerProposal); diff --git a/src/cpp/ripple/NetworkOPs.cpp b/src/cpp/ripple/NetworkOPs.cpp index 29845310cd..5eac4efe35 100644 --- a/src/cpp/ripple/NetworkOPs.cpp +++ b/src/cpp/ripple/NetworkOPs.cpp @@ -8,7 +8,6 @@ #include "HashPrefixes.h" #include "LedgerConsensus.h" #include "LedgerTiming.h" -#include "RippleAddress.h" SETUP_LOG (NetworkOPs) diff --git a/src/cpp/ripple/PFRequest.h b/src/cpp/ripple/PFRequest.h index a933ee6dff..4308f5b2e4 100644 --- a/src/cpp/ripple/PFRequest.h +++ b/src/cpp/ripple/PFRequest.h @@ -9,7 +9,6 @@ #include #include -#include "RippleAddress.h" #include "SerializedTypes.h" #include "Pathfinder.h" diff --git a/src/cpp/ripple/Pathfinder.h b/src/cpp/ripple/Pathfinder.h index 3ddbb486b1..23946b4516 100644 --- a/src/cpp/ripple/Pathfinder.h +++ b/src/cpp/ripple/Pathfinder.h @@ -4,7 +4,6 @@ #include #include "SerializedTypes.h" -#include "RippleAddress.h" #include "RippleCalc.h" #include "OrderBookDB.h" #include "AccountItems.h" diff --git a/src/cpp/ripple/ProofOfWork.cpp b/src/cpp/ripple/ProofOfWork.cpp index 280e0a41d1..f55bab7f7b 100644 --- a/src/cpp/ripple/ProofOfWork.cpp +++ b/src/cpp/ripple/ProofOfWork.cpp @@ -9,8 +9,6 @@ #include -#include "Serializer.h" - SETUP_LOG (ProofOfWork) bool powResultInfo(POWResult powCode, std::string& strToken, std::string& strHuman) diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index 49755949e8..de3461a881 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -14,7 +14,6 @@ #include "Application.h" #include "AccountItems.h" #include "Wallet.h" -#include "RippleAddress.h" #include "RippleCalc.h" #include "RPCErr.h" #include "AccountState.h" @@ -514,7 +513,7 @@ Json::Value RPCHandler::accountFromString(Ledger::ref lrLedger, RippleAddress& n } else if (bStrict) { - return naAccount.setAccountID(strIdent, ALPHABET_BITCOIN) + return naAccount.setAccountID(strIdent, Base58::getBitcoinAlphabet ()) ? rpcError(rpcACT_BITCOIN) : rpcError(rpcACT_MALFORMED); } diff --git a/src/cpp/ripple/RPCHandler.h b/src/cpp/ripple/RPCHandler.h index ed3fd12a21..ef92b71d15 100644 --- a/src/cpp/ripple/RPCHandler.h +++ b/src/cpp/ripple/RPCHandler.h @@ -3,7 +3,6 @@ #include -#include "RippleAddress.h" #include "SerializedTypes.h" #include "Ledger.h" #include "NetworkOPs.h" diff --git a/src/cpp/ripple/RPCServer.h b/src/cpp/ripple/RPCServer.h index 2be4c0d956..66b24c5ccc 100644 --- a/src/cpp/ripple/RPCServer.h +++ b/src/cpp/ripple/RPCServer.h @@ -7,7 +7,6 @@ #include #include "HTTPRequest.h" -#include "RippleAddress.h" #include "NetworkOPs.h" #include "SerializedLedger.h" #include "RPCHandler.h" diff --git a/src/cpp/ripple/SHAMap.cpp b/src/cpp/ripple/SHAMap.cpp index 20223a6573..bd30d1980d 100644 --- a/src/cpp/ripple/SHAMap.cpp +++ b/src/cpp/ripple/SHAMap.cpp @@ -9,8 +9,6 @@ #include #include -#include "Serializer.h" -#include "BitcoinUtil.h" #include "SHAMap.h" #include "Application.h" @@ -38,20 +36,6 @@ std::size_t hash_value(const SHAMapNode& mn) return mn.getMHash(); } -std::size_t hash_value(const uint256& u) -{ - std::size_t seed = theApp->getNonceST(); - - return u.hash_combine(seed); -} - -std::size_t hash_value(const uint160& u) -{ - std::size_t seed = theApp->getNonceST(); - - return u.hash_combine(seed); -} - SHAMap::SHAMap(SHAMapType t, uint32 seq) : mSeq(seq), mLedgerSeq(0), mState(smsModifying), mType(t) { diff --git a/src/cpp/ripple/SHAMap.h b/src/cpp/ripple/SHAMap.h index 9c4553b61b..76d8f418a6 100644 --- a/src/cpp/ripple/SHAMap.h +++ b/src/cpp/ripple/SHAMap.h @@ -10,7 +10,6 @@ #include #include "ScopedLock.h" -#include "Serializer.h" #include "HashedObject.h" #include "InstanceCounter.h" diff --git a/src/cpp/ripple/SHAMapNodes.cpp b/src/cpp/ripple/SHAMapNodes.cpp index 87189a6515..2efd7ea3cf 100644 --- a/src/cpp/ripple/SHAMapNodes.cpp +++ b/src/cpp/ripple/SHAMapNodes.cpp @@ -11,8 +11,6 @@ #include -#include "Serializer.h" -#include "BitcoinUtil.h" #include "HashPrefixes.h" SETUP_LOG (SHAMapNode) diff --git a/src/cpp/ripple/SerializedLedger.h b/src/cpp/ripple/SerializedLedger.h index 827cbb196b..e4f43a44d4 100644 --- a/src/cpp/ripple/SerializedLedger.h +++ b/src/cpp/ripple/SerializedLedger.h @@ -3,7 +3,6 @@ #include "SerializedObject.h" #include "LedgerFormats.h" -#include "RippleAddress.h" #include "InstanceCounter.h" DEFINE_INSTANCE(SerializedLedgerEntry); diff --git a/src/cpp/ripple/SerializedTransaction.h b/src/cpp/ripple/SerializedTransaction.h index 7691d8416b..018863397b 100644 --- a/src/cpp/ripple/SerializedTransaction.h +++ b/src/cpp/ripple/SerializedTransaction.h @@ -7,7 +7,6 @@ #include "SerializedObject.h" #include "TransactionFormats.h" -#include "RippleAddress.h" #include "InstanceCounter.h" #define TXN_SQL_NEW 'N' diff --git a/src/cpp/ripple/SerializedTypes.h b/src/cpp/ripple/SerializedTypes.h index e170d40b9d..0cec69816c 100644 --- a/src/cpp/ripple/SerializedTypes.h +++ b/src/cpp/ripple/SerializedTypes.h @@ -4,8 +4,6 @@ #include #include -#include "Serializer.h" -#include "FieldNames.h" #include "InstanceCounter.h" // CAUTION: Do not create a vector (or similar container) of any object derived from diff --git a/src/cpp/ripple/SerializedValidation.h b/src/cpp/ripple/SerializedValidation.h index 72e1fc50ec..3d69591cdf 100644 --- a/src/cpp/ripple/SerializedValidation.h +++ b/src/cpp/ripple/SerializedValidation.h @@ -2,7 +2,6 @@ #define __VALIDATION__ #include "SerializedObject.h" -#include "RippleAddress.h" #include "InstanceCounter.h" DEFINE_INSTANCE(SerializedValidation); diff --git a/src/cpp/ripple/Transaction.h b/src/cpp/ripple/Transaction.h index 75d9005785..6de1f59b71 100644 --- a/src/cpp/ripple/Transaction.h +++ b/src/cpp/ripple/Transaction.h @@ -11,9 +11,7 @@ #include #include -#include "key.h" #include "ripple.pb.h" -#include "Serializer.h" #include "SHAMap.h" #include "SerializedTransaction.h" #include "TransactionErr.h" diff --git a/src/cpp/ripple/TransactionMeta.h b/src/cpp/ripple/TransactionMeta.h index eae8ade4df..cf734e6f29 100644 --- a/src/cpp/ripple/TransactionMeta.h +++ b/src/cpp/ripple/TransactionMeta.h @@ -6,7 +6,6 @@ #include #include -#include "Serializer.h" #include "SerializedTypes.h" #include "SerializedObject.h" #include "SerializedLedger.h" diff --git a/src/cpp/ripple/UniqueNodeList.cpp b/src/cpp/ripple/UniqueNodeList.cpp index 62fa68d5d2..59f1f4c771 100644 --- a/src/cpp/ripple/UniqueNodeList.cpp +++ b/src/cpp/ripple/UniqueNodeList.cpp @@ -18,7 +18,6 @@ #include "Application.h" #include "HttpsClient.h" #include "ParseSection.h" -#include "Serializer.h" #define VALIDATORS_FETCH_SECONDS 30 #define VALIDATORS_FILE_BYTES_MAX (50 << 10) diff --git a/src/cpp/ripple/UniqueNodeList.h b/src/cpp/ripple/UniqueNodeList.h index 0604d315b0..6cfb45bd44 100644 --- a/src/cpp/ripple/UniqueNodeList.h +++ b/src/cpp/ripple/UniqueNodeList.h @@ -8,7 +8,6 @@ #include #include -#include "RippleAddress.h" #include "Config.h" #include "HttpsClient.h" #include "ParseSection.h" diff --git a/src/cpp/ripple/Wallet.h b/src/cpp/ripple/Wallet.h index 4b2f1864c7..c270cab4d3 100644 --- a/src/cpp/ripple/Wallet.h +++ b/src/cpp/ripple/Wallet.h @@ -12,8 +12,6 @@ #include "openssl/ec.h" #include "openssl/dh.h" -#include "Serializer.h" - class Ledger; class Wallet diff --git a/src/cpp/ripple/base58.h b/src/cpp/ripple/base58.h deleted file mode 100644 index b56f662f07..0000000000 --- a/src/cpp/ripple/base58.h +++ /dev/null @@ -1,252 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2011 The Bitcoin Developers -// Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. - - -// -// Why base-58 instead of standard base-64 encoding? -// - Don't want 0OIl characters that look the same in some fonts and -// could be used to create visually identical looking account numbers. -// - A string with non-alphanumeric characters is not as easily accepted as an account number. -// - E-mail usually won't line-break if there's no punctuation to break at. -// - Doubleclicking selects the whole number as one word if it's all alphanumeric. -// -#ifndef BITCOIN_BASE58_H -#define BITCOIN_BASE58_H - -#include -#include -#include -#include - -#include "bignum.h" -#include "BitcoinUtil.h" - -extern const char* ALPHABET; - -inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) -{ - 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 - std::vector vchTmp(pend-pbegin+1, 0); - std::reverse_copy(pbegin, pend, vchTmp.begin()); - - // Convert little endian data to bignum - CBigNum bn(vchTmp); - - // 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); - CBigNum dv; - CBigNum rem; - while (bn > bn0) - { - if (!BN_div(&dv, &rem, &bn, &bn58, pctx)) - throw bignum_error("EncodeBase58 : BN_div failed"); - bn = dv; - 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++) - str += ALPHABET[0]; - - // Convert little endian std::string to big endian - reverse(str.begin(), str.end()); - return str; -} - -inline std::string EncodeBase58(const std::vector& vch) -{ - return EncodeBase58(&vch[0], &vch[0] + vch.size()); -} - -inline bool DecodeBase58(const char* psz, std::vector& vchRet, const char* pAlphabet=0) -{ - const char* pAlpha = pAlphabet ? pAlphabet : ALPHABET; - - CAutoBN_CTX pctx; - vchRet.clear(); - CBigNum bn58 = 58; - CBigNum bn = 0; - CBigNum bnChar; - while (isspace(*psz)) - psz++; - - // Convert big endian string to bignum - for (const char* p = psz; *p; p++) - { - const char* p1 = strchr(pAlpha, *p); - if (p1 == NULL) - { - while (isspace(*p)) - p++; - if (*p != '\0') - return false; - break; - } - bnChar.setuint(p1 - pAlpha); - if (!BN_mul(&bn, &bn, &bn58, pctx)) - throw bignum_error("DecodeBase58 : BN_mul failed"); - bn += bnChar; - } - - // Get bignum as little endian data - std::vector vchTmp = bn.getvch(); - - // Trim off sign byte if present - if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80) - vchTmp.erase(vchTmp.end()-1); - - // Restore leading zeros - int nLeadingZeros = 0; - for (const char* p = psz; *p == pAlpha[0]; p++) - nLeadingZeros++; - vchRet.assign(nLeadingZeros + vchTmp.size(), 0); - - // Convert little endian data to big endian - std::reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size()); - return true; -} - -inline bool DecodeBase58(const std::string& str, std::vector& vchRet) -{ - return DecodeBase58(str.c_str(), vchRet); -} - - - - - -inline std::string EncodeBase58Check(const std::vector& vchIn) -{ - // add 4-byte hash check to the end - std::vector vch(vchIn); - uint256 hash = SHA256Hash(vch.begin(), vch.end()); - vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4); - return EncodeBase58(vch); -} - -inline bool DecodeBase58Check(const char* psz, std::vector& vchRet, const char* pAlphabet=0) -{ - if (!DecodeBase58(psz, vchRet, pAlphabet)) - return false; - if (vchRet.size() < 4) - { - vchRet.clear(); - return false; - } - uint256 hash = SHA256Hash(vchRet.begin(), vchRet.end()-4); - if (memcmp(&hash, &vchRet.end()[-4], 4) != 0) - { - vchRet.clear(); - return false; - } - vchRet.resize(vchRet.size()-4); - return true; -} - -inline bool DecodeBase58Check(const std::string& str, std::vector& vchRet, const char* pAlphabet) -{ - return DecodeBase58Check(str.c_str(), vchRet, pAlphabet); -} - - -class CBase58Data -{ -protected: - unsigned char nVersion; - std::vector vchData; - - CBase58Data() : nVersion(1) - { ; } - - ~CBase58Data() - { - if (!vchData.empty()) - memset(&vchData[0], 0, vchData.size()); - } - - void SetData(int nVersionIn, const std::vector& vchDataIn) - { - nVersion = nVersionIn; - vchData = vchDataIn; - } - - void SetData(int nVersionIn, const void* pdata, size_t nSize) - { - nVersion = nVersionIn; - vchData.resize(nSize); - if (nSize) - memcpy(&vchData[0], pdata, nSize); - } - - void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) - { - SetData(nVersionIn, (void*)pbegin, pend - pbegin); - } - -public: - bool SetString(const char* psz, unsigned char version, const char* pAlphabet = 0) - { - std::vector vchTemp; - DecodeBase58Check(psz, vchTemp, pAlphabet); - if (vchTemp.empty() || vchTemp[0] != version) - { - vchData.clear(); - nVersion = 1; - return false; - } - nVersion = vchTemp[0]; - vchData.resize(vchTemp.size() - 1); - if (!vchData.empty()) - memcpy(&vchData[0], &vchTemp[1], vchData.size()); - memset(&vchTemp[0], 0, vchTemp.size()); - return true; - } - - bool SetString(const std::string& str, unsigned char version) - { - return SetString(str.c_str(), version); - } - - std::string ToString() const - { - std::vector vch(1, nVersion); - - vch.insert(vch.end(), vchData.begin(), vchData.end()); - - return EncodeBase58Check(vch); - } - - int CompareTo(const CBase58Data& b58) const - { - if (nVersion < b58.nVersion) return -1; - if (nVersion > b58.nVersion) return 1; - if (vchData < b58.vchData) return -1; - if (vchData > b58.vchData) return 1; - return 0; - } - - bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; } - bool operator!=(const CBase58Data& b58) const { return CompareTo(b58) != 0; } - bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; } - bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; } - bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; } - bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; } - - friend std::size_t hash_value(const CBase58Data& b58); -}; - -extern std::size_t hash_value(const CBase58Data& b58); - -#endif -// vim:ts=4 diff --git a/src/cpp/ripple/bignum.h b/src/cpp/ripple/bignum.h deleted file mode 100644 index d2e4e55490..0000000000 --- a/src/cpp/ripple/bignum.h +++ /dev/null @@ -1,536 +0,0 @@ -// Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2011 The Bitcoin developers -// Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_BIGNUM_H -#define BITCOIN_BIGNUM_H - -#include -#include -#include - -#include "BitcoinUtil.h" - -class bignum_error : public std::runtime_error -{ -public: - explicit bignum_error(const std::string& str) : std::runtime_error(str) {} -}; - - - -class CAutoBN_CTX -{ -private: - CAutoBN_CTX(const CAutoBN_CTX&); // no implementation - CAutoBN_CTX& operator=(const CAutoBN_CTX&); // no implementation - -protected: - BN_CTX* pctx; - CAutoBN_CTX& operator=(BN_CTX* pnew) { pctx = pnew; return *this; } - -public: - CAutoBN_CTX() - { - pctx = BN_CTX_new(); - if (pctx == NULL) - throw bignum_error("CAutoBN_CTX : BN_CTX_new() returned NULL"); - } - - ~CAutoBN_CTX() - { - if (pctx != NULL) - BN_CTX_free(pctx); - } - - operator BN_CTX*() { return pctx; } - BN_CTX& operator*() { return *pctx; } - BN_CTX** operator&() { return &pctx; } - bool operator!() { return (pctx == NULL); } -}; - - - -class CBigNum : public BIGNUM -{ -public: - CBigNum() - { - BN_init(this); - } - - CBigNum(const CBigNum& b) - { - BN_init(this); - if (!BN_copy(this, &b)) - { - BN_clear_free(this); - throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); - } - } - - CBigNum& operator=(const CBigNum& b) - { - if (!BN_copy(this, &b)) - throw bignum_error("CBigNum::operator= : BN_copy failed"); - return (*this); - } - - ~CBigNum() - { - BN_clear_free(this); - } - - CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(int64 n) { BN_init(this); setint64(n); } - CBigNum(unsigned char n) { BN_init(this); setulong(n); } - CBigNum(unsigned short n) { BN_init(this); setulong(n); } - CBigNum(unsigned int n) { BN_init(this); setulong(n); } - CBigNum(uint64 n) { BN_init(this); setuint64(n); } - explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } - - explicit CBigNum(const std::vector& vch) - { - BN_init(this); - setvch(vch); - } - - void setuint(unsigned int n) - { - setulong(static_cast(n)); - } - - unsigned int getuint() const - { - return BN_get_word(this); - } - - int getint() const - { - unsigned long n = BN_get_word(this); - if (!BN_is_negative(this)) - return (n > INT_MAX ? INT_MAX : n); - else - return (n > INT_MAX ? INT_MIN : -(int)n); - } - - void setint64(int64 n) - { - unsigned char pch[sizeof(n) + 6]; - unsigned char* p = pch + 4; - bool fNegative = false; - if (n < (int64)0) - { - n = -n; - fNegative = true; - } - bool fLeadingZeroes = true; - for (int i = 0; i < 8; i++) - { - unsigned char c = (n >> 56) & 0xff; - n <<= 8; - if (fLeadingZeroes) - { - if (c == 0) - continue; - if (c & 0x80) - *p++ = (fNegative ? 0x80 : 0); - else if (fNegative) - c |= 0x80; - fLeadingZeroes = false; - } - *p++ = c; - } - unsigned int nSize = p - (pch + 4); - pch[0] = (nSize >> 24) & 0xff; - pch[1] = (nSize >> 16) & 0xff; - pch[2] = (nSize >> 8) & 0xff; - pch[3] = (nSize) & 0xff; - BN_mpi2bn(pch, p - pch, this); - } - - uint64 getuint64() const - { -#if (ULONG_MAX > UINT_MAX) - return static_cast(getulong()); -#else - int len = BN_num_bytes(this); - if (len > 8) - throw std::runtime_error("BN getuint64 overflow"); - - unsigned char buf[8]; - memset(buf, 0, sizeof(buf)); - BN_bn2bin(this, buf + 8 - len); - return - static_cast(buf[0]) << 56 | static_cast(buf[1]) << 48 | - static_cast(buf[2]) << 40 | static_cast(buf[3]) << 32 | - static_cast(buf[4]) << 24 | static_cast(buf[5]) << 16 | - static_cast(buf[6]) << 8 | static_cast(buf[7]); -#endif - } - - void setuint64(uint64 n) - { -#if (ULONG_MAX > UINT_MAX) - setulong(static_cast(n)); -#else - unsigned char buf[8]; - buf[0] = static_cast((n >> 56) & 0xff); - buf[1] = static_cast((n >> 48) & 0xff); - buf[2] = static_cast((n >> 40) & 0xff); - buf[3] = static_cast((n >> 32) & 0xff); - buf[4] = static_cast((n >> 24) & 0xff); - buf[5] = static_cast((n >> 16) & 0xff); - buf[6] = static_cast((n >> 8) & 0xff); - buf[7] = static_cast((n) & 0xff); - BN_bin2bn(buf, 8, this); -#endif - } - - void setuint256(const uint256& n) - { - BN_bin2bn(n.begin(), n.size(), NULL); - } - - uint256 getuint256() - { - uint256 ret; - unsigned int size = BN_num_bytes(this); - if (size > ret.size()) - return ret; - BN_bn2bin(this, ret.begin() + (ret.size() - BN_num_bytes(this))); - return ret; - } - - void setvch(const std::vector& vch) - { - std::vector vch2(vch.size() + 4); - unsigned int nSize = vch.size(); - // BIGNUM's byte stream format expects 4 bytes of - // big endian size data info at the front - vch2[0] = (nSize >> 24) & 0xff; - vch2[1] = (nSize >> 16) & 0xff; - 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); - BN_mpi2bn(&vch2[0], vch2.size(), this); - } - - std::vector getvch() const - { - unsigned int nSize = BN_bn2mpi(this, NULL); - if (nSize < 4) - return std::vector(); - std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); - vch.erase(vch.begin(), vch.begin() + 4); - reverse(vch.begin(), vch.end()); - return vch; - } - - CBigNum& SetCompact(unsigned int nCompact) - { - unsigned int nSize = nCompact >> 24; - std::vector vch(4 + nSize); - vch[3] = nSize; - if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff; - if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff; - if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff; - BN_mpi2bn(&vch[0], vch.size(), this); - return *this; - } - - unsigned int GetCompact() const - { - unsigned int nSize = BN_bn2mpi(this, NULL); - std::vector vch(nSize); - nSize -= 4; - BN_bn2mpi(this, &vch[0]); - unsigned int nCompact = nSize << 24; - if (nSize >= 1) nCompact |= (vch[4] << 16); - if (nSize >= 2) nCompact |= (vch[5] << 8); - if (nSize >= 3) nCompact |= (vch[6] << 0); - return nCompact; - } - - void SetHex(const std::string& str) - { - // skip 0x - const char* psz = str.c_str(); - while (isspace(*psz)) - psz++; - bool fNegative = false; - if (*psz == '-') - { - fNegative = true; - psz++; - } - if (psz[0] == '0' && tolower(psz[1]) == 'x') - psz += 2; - while (isspace(*psz)) - psz++; - - // hex string to bignum - static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; - *this = 0; - while (isxdigit(*psz)) - { - *this <<= 4; - int n = phexdigit[(int) *psz++]; - *this += n; - } - if (fNegative) - *this = 0 - *this; - } - - std::string ToString(int nBase=10) const - { - CAutoBN_CTX pctx; - CBigNum bnBase = nBase; - CBigNum bn0 = 0; - std::string str; - CBigNum bn = *this; - BN_set_negative(&bn, false); - CBigNum dv; - CBigNum rem; - if (BN_cmp(&bn, &bn0) == 0) - return "0"; - while (BN_cmp(&bn, &bn0) > 0) - { - if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) - throw bignum_error("CBigNum::ToString() : BN_div failed"); - bn = dv; - unsigned int c = rem.getuint(); - str += "0123456789abcdef"[c]; - } - if (BN_is_negative(this)) - str += "-"; - reverse(str.begin(), str.end()); - return str; - } - - std::string GetHex() const - { - return ToString(16); - } - /* JED - unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const - { - return ::GetSerializeSize(getvch(), nType, nVersion); - } - - template - void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const - { - ::Serialize(s, getvch(), nType, nVersion); - } - - template - void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) - { - std::vector vch; - ::Unserialize(s, vch, nType, nVersion); - setvch(vch); - }*/ - - - bool operator!() const - { - return BN_is_zero(this); - } - - CBigNum& operator+=(const CBigNum& b) - { - if (!BN_add(this, this, &b)) - throw bignum_error("CBigNum::operator+= : BN_add failed"); - return *this; - } - - CBigNum& operator-=(const CBigNum& b) - { - *this = *this - b; - return *this; - } - - CBigNum& operator*=(const CBigNum& b) - { - CAutoBN_CTX pctx; - if (!BN_mul(this, this, &b, pctx)) - throw bignum_error("CBigNum::operator*= : BN_mul failed"); - return *this; - } - - CBigNum& operator/=(const CBigNum& b) - { - *this = *this / b; - return *this; - } - - CBigNum& operator%=(const CBigNum& b) - { - *this = *this % b; - return *this; - } - - CBigNum& operator<<=(unsigned int shift) - { - if (!BN_lshift(this, this, shift)) - throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); - return *this; - } - - CBigNum& operator>>=(unsigned int shift) - { - // Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number - // if built on ubuntu 9.04 or 9.10, probably depends on version of openssl - CBigNum a = 1; - a <<= shift; - if (BN_cmp(&a, this) > 0) - { - *this = 0; - return *this; - } - - if (!BN_rshift(this, this, shift)) - throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); - return *this; - } - - - CBigNum& operator++() - { - // prefix operator - if (!BN_add(this, this, BN_value_one())) - throw bignum_error("CBigNum::operator++ : BN_add failed"); - return *this; - } - - const CBigNum operator++(int) - { - // postfix operator - const CBigNum ret = *this; - ++(*this); - return ret; - } - - CBigNum& operator--() - { - // prefix operator - CBigNum r; - if (!BN_sub(&r, this, BN_value_one())) - throw bignum_error("CBigNum::operator-- : BN_sub failed"); - *this = r; - return *this; - } - - const CBigNum operator--(int) - { - // postfix operator - const CBigNum ret = *this; - --(*this); - return ret; - } - - - friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b); - friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b); - friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b); - - private: - - // private because the size of an unsigned long varies by platform - - void setulong(unsigned long n) - { - if (!BN_set_word(this, n)) - throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); - } - - unsigned long getulong() const - { - return BN_get_word(this); - } - -}; - - - -inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) -{ - CBigNum r; - if (!BN_add(&r, &a, &b)) - throw bignum_error("CBigNum::operator+ : BN_add failed"); - return r; -} - -inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) -{ - CBigNum r; - if (!BN_sub(&r, &a, &b)) - throw bignum_error("CBigNum::operator- : BN_sub failed"); - return r; -} - -inline const CBigNum operator-(const CBigNum& a) -{ - CBigNum r(a); - BN_set_negative(&r, !BN_is_negative(&r)); - return r; -} - -inline const CBigNum operator*(const CBigNum& a, const CBigNum& b) -{ - CAutoBN_CTX pctx; - CBigNum r; - if (!BN_mul(&r, &a, &b, pctx)) - throw bignum_error("CBigNum::operator* : BN_mul failed"); - return r; -} - -inline const CBigNum operator/(const CBigNum& a, const CBigNum& b) -{ - CAutoBN_CTX pctx; - CBigNum r; - if (!BN_div(&r, NULL, &a, &b, pctx)) - throw bignum_error("CBigNum::operator/ : BN_div failed"); - return r; -} - -inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) -{ - CAutoBN_CTX pctx; - CBigNum r; - if (!BN_mod(&r, &a, &b, pctx)) - throw bignum_error("CBigNum::operator% : BN_div failed"); - return r; -} - -inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) -{ - CBigNum r; - if (!BN_lshift(&r, &a, shift)) - throw bignum_error("CBigNum:operator<< : BN_lshift failed"); - return r; -} - -inline const CBigNum operator>>(const CBigNum& a, unsigned int shift) -{ - CBigNum r = a; - r >>= shift; - return r; -} - -inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); } -inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); } -inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); } -inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); } -inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); } -inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); } - -#endif - -// vim:ts=4 diff --git a/src/cpp/ripple/rfc1751.h b/src/cpp/ripple/rfc1751.h deleted file mode 100644 index 1d27f5e8c6..0000000000 --- a/src/cpp/ripple/rfc1751.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef _RFC_1751_ -#define _RFC_1751_ - -int eng2key(std::string& strKey, const std::string& strHuman); -void key2eng(std::string& strHuman, const std::string& strKey); - -#endif diff --git a/src/cpp/ripple/rpc.cpp b/src/cpp/ripple/rpc.cpp index 594bd34462..1f003441ee 100644 --- a/src/cpp/ripple/rpc.cpp +++ b/src/cpp/ripple/rpc.cpp @@ -9,7 +9,6 @@ #include #include "RPC.h" -#include "BitcoinUtil.h" #include "Config.h" #include "Version.h" @@ -25,6 +24,15 @@ using namespace boost::asio; unsigned int const gMaxHTTPHeaderSize = 0x02000000; +std::string gFormatStr("v1"); + +// VFALCO: TODO, clean up this nonsense +std::string FormatFullVersion() +{ + return(gFormatStr); +} + + Json::Value JSONRPCError(int code, const std::string& message) { Json::Value error(Json::objectValue); @@ -142,7 +150,7 @@ int ReadHTTPStatus(std::basic_istream& stream) int ReadHTTPHeader(std::basic_istream& stream, std::map& mapHeadersRet) { int nLen = 0; - loop + for (;;) { std::string str; std::getline(stream, str);