1 // Copyright (c) 2009 Satoshi Nakamoto
\r
2 // Distributed under the MIT/X11 software license, see the accompanying
\r
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
\r
7 // Why base-58 instead of standard base-64 encoding?
\r
8 // - Don't want 0OIl characters that look the same in some fonts and
\r
9 // could be used to create visually identical looking account numbers.
\r
10 // - A string with non-alphanumeric characters is not as easily accepted as an account number.
\r
11 // - E-mail usually won't line-break if there's no punctuation to break at.
\r
12 // - Doubleclicking selects the whole number as one word if it's all alphanumeric.
\r
16 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
\r
19 inline string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
\r
25 // Convert big endian data to little endian
\r
26 // Extra zero at the end make sure bignum will interpret as a positive number
\r
27 vector<unsigned char> vchTmp(pend-pbegin+1, 0);
\r
28 reverse_copy(pbegin, pend, vchTmp.begin());
\r
30 // Convert little endian data to bignum
\r
34 // Convert bignum to string
\r
36 str.reserve((pend - pbegin) * 138 / 100 + 1);
\r
41 if (!BN_div(&dv, &rem, &bn, &bn58, pctx))
\r
42 throw bignum_error("EncodeBase58 : BN_div failed");
\r
44 unsigned int c = rem.getulong();
\r
45 str += pszBase58[c];
\r
48 // Leading zeroes encoded as base58 zeros
\r
49 for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
\r
50 str += pszBase58[0];
\r
52 // Convert little endian string to big endian
\r
53 reverse(str.begin(), str.end());
\r
57 inline string EncodeBase58(const vector<unsigned char>& vch)
\r
59 return EncodeBase58(&vch[0], &vch[0] + vch.size());
\r
62 inline bool DecodeBase58(const char* psz, vector<unsigned char>& vchRet)
\r
69 while (isspace(*psz))
\r
72 // Convert big endian string to bignum
\r
73 for (const char* p = psz; *p; p++)
\r
75 const char* p1 = strchr(pszBase58, *p);
\r
84 bnChar.setulong(p1 - pszBase58);
\r
85 if (!BN_mul(&bn, &bn, &bn58, pctx))
\r
86 throw bignum_error("DecodeBase58 : BN_mul failed");
\r
90 // Get bignum as little endian data
\r
91 vector<unsigned char> vchTmp = bn.getvch();
\r
93 // Trim off sign byte if present
\r
94 if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
\r
95 vchTmp.erase(vchTmp.end()-1);
\r
97 // Restore leading zeros
\r
98 int nLeadingZeros = 0;
\r
99 for (const char* p = psz; *p == pszBase58[0]; p++)
\r
101 vchRet.assign(nLeadingZeros + vchTmp.size(), 0);
\r
103 // Convert little endian data to big endian
\r
104 reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size());
\r
108 inline bool DecodeBase58(const string& str, vector<unsigned char>& vchRet)
\r
110 return DecodeBase58(str.c_str(), vchRet);
\r
117 inline string EncodeBase58Check(const vector<unsigned char>& vchIn)
\r
119 // add 4-byte hash check to the end
\r
120 vector<unsigned char> vch(vchIn);
\r
121 uint256 hash = Hash(vch.begin(), vch.end());
\r
122 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
\r
123 return EncodeBase58(vch);
\r
126 inline bool DecodeBase58Check(const char* psz, vector<unsigned char>& vchRet)
\r
128 if (!DecodeBase58(psz, vchRet))
\r
130 if (vchRet.size() < 4)
\r
135 uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
\r
136 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
\r
141 vchRet.resize(vchRet.size()-4);
\r
145 inline bool DecodeBase58Check(const string& str, vector<unsigned char>& vchRet)
\r
147 return DecodeBase58Check(str.c_str(), vchRet);
\r
155 static const unsigned char ADDRESSVERSION = 0;
\r
157 inline string Hash160ToAddress(uint160 hash160)
\r
159 // add 1-byte version number to the front
\r
160 vector<unsigned char> vch(1, ADDRESSVERSION);
\r
161 vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160));
\r
162 return EncodeBase58Check(vch);
\r
165 inline bool AddressToHash160(const char* psz, uint160& hash160Ret)
\r
167 vector<unsigned char> vch;
\r
168 if (!DecodeBase58Check(psz, vch))
\r
172 unsigned char nVersion = vch[0];
\r
173 if (vch.size() != sizeof(hash160Ret) + 1)
\r
175 memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret));
\r
176 return (nVersion <= ADDRESSVERSION);
\r
179 inline bool AddressToHash160(const string& str, uint160& hash160Ret)
\r
181 return AddressToHash160(str.c_str(), hash160Ret);
\r
184 inline bool IsValidBitcoinAddress(const char* psz)
\r
187 return AddressToHash160(psz, hash160);
\r
190 inline bool IsValidBitcoinAddress(const string& str)
\r
192 return IsValidBitcoinAddress(str.c_str());
\r
198 inline string PubKeyToAddress(const vector<unsigned char>& vchPubKey)
\r
200 return Hash160ToAddress(Hash160(vchPubKey));
\r