1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin Developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
8 // Why base-58 instead of standard base-64 encoding?
9 // - Don't want 0OIl characters that look the same in some fonts and
10 // could be used to create visually identical looking account numbers.
11 // - A string with non-alphanumeric characters is not as easily accepted as an account number.
12 // - E-mail usually won't line-break if there's no punctuation to break at.
13 // - Doubleclicking selects the whole number as one word if it's all alphanumeric.
15 #ifndef BITCOIN_BASE58_H
16 #define BITCOIN_BASE58_H
22 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
24 // Encode a byte sequence as a base58-encoded string
25 inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
31 // Convert big endian data to little endian
32 // Extra zero at the end make sure bignum will interpret as a positive number
33 std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
34 reverse_copy(pbegin, pend, vchTmp.begin());
36 // Convert little endian data to bignum
40 // Convert bignum to std::string
42 // Expected size increase from base58 conversion is approximately 137%
43 // use 138% to be safe
44 str.reserve((pend - pbegin) * 138 / 100 + 1);
49 if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
50 throw bignum_error("EncodeBase58 : BN_div failed");
52 unsigned int c = rem.getulong();
56 // Leading zeroes encoded as base58 zeros
57 for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
60 // Convert little endian std::string to big endian
61 reverse(str.begin(), str.end());
65 // Encode a byte vector as a base58-encoded string
66 inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
68 return EncodeBase58(&vch[0], &vch[0] + vch.size());
71 // Decode a base58-encoded string psz into byte vector vchRet
72 // returns true if decoding is succesful
73 inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
83 // Convert big endian string to bignum
84 for (const char* p = psz; *p; p++)
86 const char* p1 = strchr(pszBase58, *p);
95 bnChar.setulong(p1 - pszBase58);
96 if (!BN_mul(&bn, &bn, &bn58, pctx))
97 throw bignum_error("DecodeBase58 : BN_mul failed");
101 // Get bignum as little endian data
102 std::vector<unsigned char> vchTmp = bn.getvch();
104 // Trim off sign byte if present
105 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
106 vchTmp.erase(vchTmp.end()-1);
108 // Restore leading zeros
109 int nLeadingZeros = 0;
110 for (const char* p = psz; *p == pszBase58[0]; p++)
112 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
114 // Convert little endian data to big endian
115 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
119 // Decode a base58-encoded string str into byte vector vchRet
120 // returns true if decoding is succesful
121 inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
123 return DecodeBase58(str.c_str(), vchRet);
129 // Encode a byte vector to a base58-encoded string, including checksum
130 inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
132 // add 4-byte hash check to the end
133 std::vector<unsigned char> vch(vchIn);
134 uint256 hash = Hash(vch.begin(), vch.end());
135 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
136 return EncodeBase58(vch);
139 // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
140 // returns true if decoding is succesful
141 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
143 if (!DecodeBase58(psz, vchRet))
145 if (vchRet.size() < 4)
150 uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
151 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
156 vchRet.resize(vchRet.size()-4);
160 // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
161 // returns true if decoding is succesful
162 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
164 return DecodeBase58Check(str.c_str(), vchRet);
171 // Base class for all base58-encoded data
176 unsigned char nVersion;
178 // the actually encoded data
179 std::vector<unsigned char> vchData;
189 // zero the memory, as it may contain sensitive data
190 if (!vchData.empty())
191 memset(&vchData[0], 0, vchData.size());
194 void SetData(int nVersionIn, const void* pdata, size_t nSize)
196 nVersion = nVersionIn;
197 vchData.resize(nSize);
198 if (!vchData.empty())
199 memcpy(&vchData[0], pdata, nSize);
202 void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
204 SetData(nVersionIn, (void*)pbegin, pend - pbegin);
208 bool SetString(const char* psz)
210 std::vector<unsigned char> vchTemp;
211 DecodeBase58Check(psz, vchTemp);
218 nVersion = vchTemp[0];
219 vchData.resize(vchTemp.size() - 1);
220 if (!vchData.empty())
221 memcpy(&vchData[0], &vchTemp[1], vchData.size());
222 memset(&vchTemp[0], 0, vchTemp.size());
226 bool SetString(const std::string& str)
228 return SetString(str.c_str());
231 std::string ToString() const
233 std::vector<unsigned char> vch(1, nVersion);
234 vch.insert(vch.end(), vchData.begin(), vchData.end());
235 return EncodeBase58Check(vch);
238 int CompareTo(const CBase58Data& b58) const
240 if (nVersion < b58.nVersion) return -1;
241 if (nVersion > b58.nVersion) return 1;
242 if (vchData < b58.vchData) return -1;
243 if (vchData > b58.vchData) return 1;
247 bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
248 bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
249 bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
250 bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
251 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
254 // base58-encoded bitcoin addresses
255 // Addresses have version 0 or 111 (testnet)
256 // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key
257 class CBitcoinAddress : public CBase58Data
260 bool SetHash160(const uint160& hash160)
262 SetData(fTestNet ? 111 : 0, &hash160, 20);
266 bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
268 return SetHash160(Hash160(vchPubKey));
273 unsigned int nExpectedSize = 20;
274 bool fExpectTestNet = false;
281 fExpectTestNet = true;
287 return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
294 CBitcoinAddress(uint160 hash160In)
296 SetHash160(hash160In);
299 CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
301 SetPubKey(vchPubKey);
304 CBitcoinAddress(const std::string& strAddress)
306 SetString(strAddress);
309 CBitcoinAddress(const char* pszAddress)
311 SetString(pszAddress);
314 uint160 GetHash160() const
316 assert(vchData.size() == 20);
318 memcpy(&hash160, &vchData[0], 20);