aboutsummaryrefslogtreecommitdiff
path: root/src/base58.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/base58.h')
-rw-r--r--src/base58.h144
1 files changed, 36 insertions, 108 deletions
diff --git a/src/base58.h b/src/base58.h
index ebe5376825..4fb436c5ed 100644
--- a/src/base58.h
+++ b/src/base58.h
@@ -14,7 +14,6 @@
#ifndef BITCOIN_BASE58_H
#define BITCOIN_BASE58_H
-#include "bignum.h"
#include "chainparams.h"
#include "hash.h"
#include "key.h"
@@ -27,114 +26,39 @@
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
-static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
-
-// Encode a byte sequence as a base58-encoded string
-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<unsigned char> vchTmp(pend-pbegin+1, 0);
- reverse_copy(pbegin, pend, vchTmp.begin());
-
- // Convert little endian data to bignum
- CBigNum bn;
- bn.setvch(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.getulong();
- str += pszBase58[c];
- }
-
- // Leading zeroes encoded as base58 zeros
- for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
- str += pszBase58[0];
-
- // Convert little endian std::string to big endian
- reverse(str.begin(), str.end());
- return str;
-}
+/**
+ * Encode a byte sequence as a base58-encoded string.
+ * pbegin and pend cannot be NULL, unless both are.
+ */
+std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend);
-// Encode a byte vector as a base58-encoded string
+/**
+ * Encode a byte vector as a base58-encoded string
+ */
inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
{
return EncodeBase58(&vch[0], &vch[0] + vch.size());
}
-// Decode a base58-encoded string psz into byte vector vchRet
-// returns true if decoding is successful
-inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
-{
- 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(pszBase58, *p);
- if (p1 == NULL)
- {
- while (isspace(*p))
- p++;
- if (*p != '\0')
- return false;
- break;
- }
- bnChar.setulong(p1 - pszBase58);
- if (!BN_mul(&bn, &bn, &bn58, pctx))
- throw bignum_error("DecodeBase58 : BN_mul failed");
- bn += bnChar;
- }
-
- // Get bignum as little endian data
- std::vector<unsigned char> 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 == pszBase58[0]; p++)
- nLeadingZeros++;
- vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
-
- // Convert little endian data to big endian
- reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
- return true;
-}
+/**
+ * Decode a base58-encoded string (psz) into a byte vector (vchRet).
+ * return true if decoding is successful.
+ * psz cannot be NULL.
+ */
+bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
-// Decode a base58-encoded string str into byte vector vchRet
-// returns true if decoding is successful
+/**
+ * Decode a base58-encoded string (str) into a byte vector (vchRet).
+ * return true if decoding is successful.
+ */
inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
{
return DecodeBase58(str.c_str(), vchRet);
}
-
-
-
-// Encode a byte vector to a base58-encoded string, including checksum
+/**
+ * Encode a byte vector into a base58-encoded string, including checksum
+ */
inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
{
// add 4-byte hash check to the end
@@ -144,8 +68,10 @@ inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
return EncodeBase58(vch);
}
-// Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
-// returns true if decoding is successful
+/**
+ * Decode a base58-encoded string (psz) that includes a checksum into a byte
+ * vector (vchRet), return true if decoding is successful
+ */
inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
{
if (!DecodeBase58(psz, vchRet))
@@ -155,6 +81,7 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
vchRet.clear();
return false;
}
+ // re-calculate the checksum, insure it matches the included 4-byte checksum
uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
{
@@ -165,18 +92,18 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
return true;
}
-// Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
-// returns true if decoding is successful
+/**
+ * Decode a base58-encoded string (str) that includes a checksum into a byte
+ * vector (vchRet), return true if decoding is successful
+ */
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
{
return DecodeBase58Check(str.c_str(), vchRet);
}
-
-
-
-
-/** Base class for all base58-encoded data */
+/**
+ * Base class for all base58-encoded data
+ */
class CBase58Data
{
protected:
@@ -347,7 +274,9 @@ bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const {
bool inline CBitcoinAddressVisitor::operator()(const CScriptID &id) const { return addr->Set(id); }
bool inline CBitcoinAddressVisitor::operator()(const CNoDestination &id) const { return false; }
-/** A base58-encoded secret key */
+/**
+ * A base58-encoded secret key
+ */
class CBitcoinSecret : public CBase58Data
{
public:
@@ -393,7 +322,6 @@ public:
}
};
-
template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
{
public: