15 #ifndef BITCOIN_BASE58_H
16 #define BITCOIN_BASE58_H
26 static const char* pszBase58 =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
29 inline std::string
EncodeBase58(
const unsigned char* pbegin,
const unsigned char* pend)
37 std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
38 reverse_copy(pbegin, pend, vchTmp.begin());
48 str.reserve((pend - pbegin) * 138 / 100 + 1);
53 if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
61 for (
const unsigned char* p = pbegin; p < pend && *p == 0; p++)
65 reverse(str.begin(), str.end());
70 inline std::string
EncodeBase58(
const std::vector<unsigned char>& vch)
77 inline bool DecodeBase58(
const char* psz, std::vector<unsigned char>& vchRet)
88 for (
const char* p = psz; *p; p++)
90 const char* p1 = strchr(pszBase58, *p);
100 if (!BN_mul(&bn, &bn, &bn58, pctx))
106 std::vector<unsigned char> vchTmp = bn.
getvch();
109 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
110 vchTmp.erase(vchTmp.end()-1);
113 int nLeadingZeros = 0;
114 for (
const char* p = psz; *p == pszBase58[0]; p++)
116 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
119 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
125 inline bool DecodeBase58(
const std::string& str, std::vector<unsigned char>& vchRet)
137 std::vector<unsigned char> vch(vchIn);
139 vch.insert(vch.end(), (
unsigned char*)&hash, (
unsigned char*)&hash + 4);
149 if (vchRet.size() < 4)
155 if (memcmp(&hash, &vchRet.
end()[-4], 4) != 0)
160 vchRet.resize(vchRet.size()-4);
183 typedef std::vector<unsigned char, zero_after_free_allocator<unsigned char> >
vector_uchar;
192 void SetData(
int nVersionIn,
const void* pdata,
size_t nSize)
194 nVersion = nVersionIn;
195 vchData.resize(nSize);
196 if (!vchData.empty())
197 memcpy(&vchData[0], pdata, nSize);
200 void SetData(
int nVersionIn,
const unsigned char *pbegin,
const unsigned char *pend)
202 SetData(nVersionIn, (
void*)pbegin, pend - pbegin);
208 std::vector<unsigned char> vchTemp;
216 nVersion = vchTemp[0];
217 vchData.resize(vchTemp.size() - 1);
218 if (!vchData.empty())
219 memcpy(&vchData[0], &vchTemp[1], vchData.size());
220 OPENSSL_cleanse(&vchTemp[0], vchData.size());
231 std::vector<unsigned char> vch(1, nVersion);
232 vch.insert(vch.end(), vchData.begin(), vchData.end());
238 if (nVersion < b58.
nVersion)
return -1;
239 if (nVersion > b58.
nVersion)
return 1;
240 if (vchData < b58.
vchData)
return -1;
241 if (vchData > b58.
vchData)
return 1;
298 unsigned int nExpectedSize = 20;
299 bool fExpectTestNet =
false;
304 fExpectTestNet =
false;
308 fExpectTestNet =
false;
313 fExpectTestNet =
true;
317 fExpectTestNet =
true;
376 default:
return false;
388 default:
return false;
424 bool fExpectTestNet =
false;
431 fExpectTestNet =
true;
460 #endif // BITCOIN_BASE58_H
void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
void SetKey(const CKey &vchSecret)
const unsigned char * begin() const
CBitcoinAddress(const CTxDestination &dest)
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vchRet)
CBitcoinAddressVisitor(CBitcoinAddress *addrIn)
unsigned long getulong() const
RAII encapsulated BN_CTX (OpenSSL bignum context)
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
void setulong(unsigned long n)
CTxDestination Get() const
bool GetKeyID(CKeyID &keyID) const
bool SetString(const char *pszSecret)
std::vector< unsigned char, zero_after_free_allocator< unsigned char > > vector_uchar
Base class for all base58-encoded data.
bool IsCompressed() const
bool Set(const CKeyID &id)
bool operator()(const CKeyID &id) const
A base58-encoded secret key.
bool SetString(const std::string &strSecret)
bool operator==(const CBase58Data &b58) const
C++ wrapper for BIGNUM (OpenSSL bignum)
std::vector< unsigned char > getvch() const
std::string ToString() const
uint256 Hash(const T1 pbegin, const T1 pend)
Errors thrown by the bignum class.
bool operator>(const CBase58Data &b58) const
void Set(const T pbegin, const T pend, bool fCompressedIn)
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
bool SetString(const std::string &str)
CBitcoinSecret(const CKey &vchSecret)
void setvch(const std::vector< unsigned char > &vch)
A reference to a CKey: the Hash160 of its serialized public key.
bool Set(const CTxDestination &dest)
bool SetString(const char *psz)
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
A reference to a CScript: the Hash160 of its serialization (see script.h)
bool operator<(const CBase58Data &b58) const
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
An encapsulated private key.
bool Set(const CScriptID &id)
CBitcoinAddress(const char *pszAddress)
void SetData(int nVersionIn, const void *pdata, size_t nSize)
unsigned int size() const
CBitcoinAddress(const std::string &strAddress)
bool operator<=(const CBase58Data &b58) const
int CompareTo(const CBase58Data &b58) const
bool operator>=(const CBase58Data &b58) const