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";
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 inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
67 return EncodeBase58(&vch[0], &vch[0] + vch.size());
70 inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
80 // Convert big endian string to bignum
81 for (const char* p = psz; *p; p++)
83 const char* p1 = strchr(pszBase58, *p);
92 bnChar.setulong(p1 - pszBase58);
93 if (!BN_mul(&bn, &bn, &bn58, pctx))
94 throw bignum_error("DecodeBase58 : BN_mul failed");
98 // Get bignum as little endian data
99 std::vector<unsigned char> vchTmp = bn.getvch();
101 // Trim off sign byte if present
102 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
103 vchTmp.erase(vchTmp.end()-1);
105 // Restore leading zeros
106 int nLeadingZeros = 0;
107 for (const char* p = psz; *p == pszBase58[0]; p++)
109 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
111 // Convert little endian data to big endian
112 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
116 inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
118 return DecodeBase58(str.c_str(), vchRet);
125 inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
127 // add 4-byte hash check to the end
128 std::vector<unsigned char> vch(vchIn);
129 uint256 hash = Hash(vch.begin(), vch.end());
130 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
131 return EncodeBase58(vch);
134 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
136 if (!DecodeBase58(psz, vchRet))
138 if (vchRet.size() < 4)
143 uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
144 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
149 vchRet.resize(vchRet.size()-4);
153 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
155 return DecodeBase58Check(str.c_str(), vchRet);
166 unsigned char nVersion;
167 std::vector<unsigned char> vchData;
177 if (!vchData.empty())
178 memset(&vchData[0], 0, vchData.size());
181 void SetData(int nVersionIn, const void* pdata, size_t nSize)
183 nVersion = nVersionIn;
184 vchData.resize(nSize);
185 if (!vchData.empty())
186 memcpy(&vchData[0], pdata, nSize);
189 void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
191 SetData(nVersionIn, (void*)pbegin, pend - pbegin);
195 bool SetString(const char* psz)
197 std::vector<unsigned char> vchTemp;
198 DecodeBase58Check(psz, vchTemp);
205 nVersion = vchTemp[0];
206 vchData.resize(vchTemp.size() - 1);
207 if (!vchData.empty())
208 memcpy(&vchData[0], &vchTemp[1], vchData.size());
209 memset(&vchTemp[0], 0, vchTemp.size());
213 bool SetString(const std::string& str)
215 return SetString(str.c_str());
218 std::string ToString() const
220 std::vector<unsigned char> vch(1, nVersion);
221 vch.insert(vch.end(), vchData.begin(), vchData.end());
222 return EncodeBase58Check(vch);
225 int CompareTo(const CBase58Data& b58) const
227 if (nVersion < b58.nVersion) return -1;
228 if (nVersion > b58.nVersion) return 1;
229 if (vchData < b58.vchData) return -1;
230 if (vchData > b58.vchData) return 1;
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; }
238 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
242 class CBitcoinAddress : public CBase58Data
245 bool SetHash160(const uint160& hash160)
247 SetData(fTestNet ? 111 : 0, &hash160, 20);
251 bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
253 return SetHash160(Hash160(vchPubKey));
258 int nExpectedSize = 20;
259 bool fExpectTestNet = false;
266 fExpectTestNet = true;
272 return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
279 CBitcoinAddress(uint160 hash160In)
281 SetHash160(hash160In);
284 CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
286 SetPubKey(vchPubKey);
289 CBitcoinAddress(const std::string& strAddress)
291 SetString(strAddress);
294 CBitcoinAddress(const char* pszAddress)
296 SetString(pszAddress);
299 uint160 GetHash160() const
301 assert(vchData.size() == 20);
303 memcpy(&hash160, &vchData[0], 20);