1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 // Why base-58 instead of standard base-64 encoding?
8 // - Don't want 0OIl characters that look the same in some fonts and
9 // could be used to create visually identical looking account numbers.
10 // - A string with non-alphanumeric characters is not as easily accepted as an account number.
11 // - E-mail usually won't line-break if there's no punctuation to break at.
12 // - Doubleclicking selects the whole number as one word if it's all alphanumeric.
14 #ifndef BITCOIN_BASE58_H
15 #define BITCOIN_BASE58_H
21 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
24 inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
30 // Convert big endian data to little endian
31 // Extra zero at the end make sure bignum will interpret as a positive number
32 std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
33 reverse_copy(pbegin, pend, vchTmp.begin());
35 // Convert little endian data to bignum
39 // Convert bignum to std::string
41 // Expected size increase from base58 conversion is approximately 137%
42 // use 138% to be safe
43 str.reserve((pend - pbegin) * 138 / 100 + 1);
48 if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
49 throw bignum_error("EncodeBase58 : BN_div failed");
51 unsigned int c = rem.getulong();
55 // Leading zeroes encoded as base58 zeros
56 for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
59 // Convert little endian std::string to big endian
60 reverse(str.begin(), str.end());
64 inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
66 return EncodeBase58(&vch[0], &vch[0] + vch.size());
69 inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
79 // Convert big endian string to bignum
80 for (const char* p = psz; *p; p++)
82 const char* p1 = strchr(pszBase58, *p);
91 bnChar.setulong(p1 - pszBase58);
92 if (!BN_mul(&bn, &bn, &bn58, pctx))
93 throw bignum_error("DecodeBase58 : BN_mul failed");
97 // Get bignum as little endian data
98 std::vector<unsigned char> vchTmp = bn.getvch();
100 // Trim off sign byte if present
101 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
102 vchTmp.erase(vchTmp.end()-1);
104 // Restore leading zeros
105 int nLeadingZeros = 0;
106 for (const char* p = psz; *p == pszBase58[0]; p++)
108 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
110 // Convert little endian data to big endian
111 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
115 inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
117 return DecodeBase58(str.c_str(), vchRet);
124 inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
126 // add 4-byte hash check to the end
127 std::vector<unsigned char> vch(vchIn);
128 uint256 hash = Hash(vch.begin(), vch.end());
129 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
130 return EncodeBase58(vch);
133 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
135 if (!DecodeBase58(psz, vchRet))
137 if (vchRet.size() < 4)
142 uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
143 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
148 vchRet.resize(vchRet.size()-4);
152 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
154 return DecodeBase58Check(str.c_str(), vchRet);
165 unsigned char nVersion;
166 std::vector<unsigned char> vchData;
176 if (!vchData.empty())
177 memset(&vchData[0], 0, vchData.size());
180 void SetData(int nVersionIn, const void* pdata, size_t nSize)
182 nVersion = nVersionIn;
183 vchData.resize(nSize);
184 if (!vchData.empty())
185 memcpy(&vchData[0], pdata, nSize);
188 void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
190 SetData(nVersionIn, (void*)pbegin, pend - pbegin);
194 bool SetString(const char* psz)
196 std::vector<unsigned char> vchTemp;
197 DecodeBase58Check(psz, vchTemp);
204 nVersion = vchTemp[0];
205 vchData.resize(vchTemp.size() - 1);
206 if (!vchData.empty())
207 memcpy(&vchData[0], &vchTemp[1], vchData.size());
208 memset(&vchTemp[0], 0, vchTemp.size());
212 bool SetString(const std::string& str)
214 return SetString(str.c_str());
217 std::string ToString() const
219 std::vector<unsigned char> vch(1, nVersion);
220 vch.insert(vch.end(), vchData.begin(), vchData.end());
221 return EncodeBase58Check(vch);
224 int CompareTo(const CBase58Data& b58) const
226 if (nVersion < b58.nVersion) return -1;
227 if (nVersion > b58.nVersion) return 1;
228 if (vchData < b58.vchData) return -1;
229 if (vchData > b58.vchData) return 1;
233 bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
234 bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
235 bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
236 bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
237 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
241 class CBitcoinAddress : public CBase58Data
244 bool SetHash160(const uint160& hash160)
246 SetData(fTestNet ? 111 : 0, &hash160, 20);
250 bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
252 return SetHash160(Hash160(vchPubKey));
257 int nExpectedSize = 20;
258 bool fExpectTestNet = false;
265 fExpectTestNet = true;
271 return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
278 CBitcoinAddress(uint160 hash160In)
280 SetHash160(hash160In);
283 CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
285 SetPubKey(vchPubKey);
288 CBitcoinAddress(const std::string& strAddress)
290 SetString(strAddress);
293 CBitcoinAddress(const char* pszAddress)
295 SetString(pszAddress);
298 uint160 GetHash160() const
300 assert(vchData.size() == 20);
302 memcpy(&hash160, &vchData[0], 20);