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
25 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
27 // Encode a byte sequence as a base58-encoded string
28 inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
34 // Convert big endian data to little endian
35 // Extra zero at the end make sure bignum will interpret as a positive number
36 std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
37 reverse_copy(pbegin, pend, vchTmp.begin());
39 // Convert little endian data to bignum
43 // Convert bignum to std::string
45 // Expected size increase from base58 conversion is approximately 137%
46 // use 138% to be safe
47 str.reserve((pend - pbegin) * 138 / 100 + 1);
52 if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
53 throw bignum_error("EncodeBase58 : BN_div failed");
55 unsigned int c = rem.getulong();
59 // Leading zeroes encoded as base58 zeros
60 for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
63 // Convert little endian std::string to big endian
64 reverse(str.begin(), str.end());
68 // Encode a byte vector as a base58-encoded string
69 inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
71 return EncodeBase58(&vch[0], &vch[0] + vch.size());
74 // Decode a base58-encoded string psz into byte vector vchRet
75 // returns true if decoding is succesful
76 inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
86 // Convert big endian string to bignum
87 for (const char* p = psz; *p; p++)
89 const char* p1 = strchr(pszBase58, *p);
98 bnChar.setulong(p1 - pszBase58);
99 if (!BN_mul(&bn, &bn, &bn58, pctx))
100 throw bignum_error("DecodeBase58 : BN_mul failed");
104 // Get bignum as little endian data
105 std::vector<unsigned char> vchTmp = bn.getvch();
107 // Trim off sign byte if present
108 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
109 vchTmp.erase(vchTmp.end()-1);
111 // Restore leading zeros
112 int nLeadingZeros = 0;
113 for (const char* p = psz; *p == pszBase58[0]; p++)
115 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
117 // Convert little endian data to big endian
118 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
122 // Decode a base58-encoded string str into byte vector vchRet
123 // returns true if decoding is succesful
124 inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
126 return DecodeBase58(str.c_str(), vchRet);
132 // Encode a byte vector to a base58-encoded string, including checksum
133 inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
135 // add 4-byte hash check to the end
136 std::vector<unsigned char> vch(vchIn);
137 uint256 hash = Hash(vch.begin(), vch.end());
138 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
139 return EncodeBase58(vch);
142 // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
143 // returns true if decoding is succesful
144 inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
146 if (!DecodeBase58(psz, vchRet))
148 if (vchRet.size() < 4)
153 uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
154 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
159 vchRet.resize(vchRet.size()-4);
163 // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
164 // returns true if decoding is succesful
165 inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
167 return DecodeBase58Check(str.c_str(), vchRet);
174 // Base class for all base58-encoded data
179 unsigned char nVersion;
181 // the actually encoded data
182 std::vector<unsigned char> vchData;
192 // zero the memory, as it may contain sensitive data
193 if (!vchData.empty())
194 memset(&vchData[0], 0, vchData.size());
197 void SetData(int nVersionIn, const void* pdata, size_t nSize)
199 nVersion = nVersionIn;
200 vchData.resize(nSize);
201 if (!vchData.empty())
202 memcpy(&vchData[0], pdata, nSize);
205 void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
207 SetData(nVersionIn, (void*)pbegin, pend - pbegin);
211 bool SetString(const char* psz)
213 std::vector<unsigned char> vchTemp;
214 DecodeBase58Check(psz, vchTemp);
221 nVersion = vchTemp[0];
222 vchData.resize(vchTemp.size() - 1);
223 if (!vchData.empty())
224 memcpy(&vchData[0], &vchTemp[1], vchData.size());
225 memset(&vchTemp[0], 0, vchTemp.size());
229 bool SetString(const std::string& str)
231 return SetString(str.c_str());
234 std::string ToString() const
236 std::vector<unsigned char> vch(1, nVersion);
237 vch.insert(vch.end(), vchData.begin(), vchData.end());
238 return EncodeBase58Check(vch);
241 int CompareTo(const CBase58Data& b58) const
243 if (nVersion < b58.nVersion) return -1;
244 if (nVersion > b58.nVersion) return 1;
245 if (vchData < b58.vchData) return -1;
246 if (vchData > b58.vchData) return 1;
250 bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
251 bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
252 bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
253 bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
254 bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
257 // base58-encoded bitcoin addresses
258 // Public-key-hash-addresses have version 0 (or 192 testnet)
259 // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key
260 // Script-hash-addresses (OP_EVAL) have version 5 (or 196 testnet)
261 // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script
262 class CBitcoinAddress : public CBase58Data
269 PUBKEY_ADDRESS_TEST = 192,
270 PUBKEY_ADDRESS_TEST_LEGACY = 111, // Deprecated: old testnet address
271 SCRIPT_ADDRESS_TEST = 196,
274 bool SetHash160(const uint160& hash160)
276 SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &hash160, 20);
280 void SetPubKey(const std::vector<unsigned char>& vchPubKey)
282 SetHash160(Hash160(vchPubKey));
285 bool SetScriptHash160(const uint160& hash160)
287 SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &hash160, 20);
293 int nExpectedSize = 20;
294 bool fExpectTestNet = false;
298 nExpectedSize = 20; // Hash of public key
299 fExpectTestNet = false;
302 nExpectedSize = 20; // OP_EVAL, hash of CScript
303 fExpectTestNet = false;
306 case PUBKEY_ADDRESS_TEST_LEGACY:
307 case PUBKEY_ADDRESS_TEST:
309 fExpectTestNet = true;
311 case SCRIPT_ADDRESS_TEST:
313 fExpectTestNet = true;
319 return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
321 bool IsScript() const
326 return nVersion == SCRIPT_ADDRESS_TEST;
327 return nVersion == SCRIPT_ADDRESS;
334 CBitcoinAddress(uint160 hash160In)
336 SetHash160(hash160In);
339 CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
341 SetPubKey(vchPubKey);
344 CBitcoinAddress(const std::string& strAddress)
346 SetString(strAddress);
349 CBitcoinAddress(const char* pszAddress)
351 SetString(pszAddress);
354 uint160 GetHash160() const
356 assert(vchData.size() == 20);
358 memcpy(&hash160, &vchData[0], 20);
363 class CBitcoinSecret : public CBase58Data
366 void SetSecret(const CSecret& vchSecret)
368 SetData(fTestNet ? 239 : 128, &vchSecret[0], vchSecret.size());
374 vchSecret.resize(vchData.size());
375 memcpy(&vchSecret[0], &vchData[0], vchData.size());
381 int nExpectedSize = 32;
382 bool fExpectTestNet = false;
389 fExpectTestNet = true;
395 return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
398 CBitcoinSecret(const CSecret& vchSecret)
400 SetSecret(vchSecret);