1 // Copyright (c) 2014 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
15 #include <boost/variant/apply_visitor.hpp>
16 #include <boost/variant/static_visitor.hpp>
18 /* All alphanumeric characters except for "0", "I", "O", and "l" */
19 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
21 bool DecodeBase58(const char *psz, std::vector<unsigned char>& vch) {
22 // Skip leading spaces.
23 while (*psz && isspace(*psz))
25 // Skip and count leading '1's.
31 // Allocate enough space in big-endian base256 representation.
32 std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.
33 // Process the characters.
34 while (*psz && !isspace(*psz)) {
35 // Decode base58 character
36 const char *ch = strchr(pszBase58, *psz);
39 // Apply "b256 = b256 * 58 + ch".
40 int carry = ch - pszBase58;
41 for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {
49 // Skip trailing spaces.
54 // Skip leading zeroes in b256.
55 std::vector<unsigned char>::iterator it = b256.begin();
56 while (it != b256.end() && *it == 0)
58 // Copy result into output vector.
59 vch.reserve(zeroes + (b256.end() - it));
60 vch.assign(zeroes, 0x00);
61 while (it != b256.end())
62 vch.push_back(*(it++));
66 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) {
67 // Skip & count leading zeroes.
69 while (pbegin != pend && *pbegin == 0) {
73 // Allocate enough space in big-endian base58 representation.
74 std::vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up.
76 while (pbegin != pend) {
78 // Apply "b58 = b58 * 256 + ch".
79 for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) {
87 // Skip leading zeroes in base58 result.
88 std::vector<unsigned char>::iterator it = b58.begin();
89 while (it != b58.end() && *it == 0)
91 // Translate the result into a string.
93 str.reserve(zeroes + (b58.end() - it));
94 str.assign(zeroes, '1');
95 while (it != b58.end())
96 str += pszBase58[*(it++)];
100 std::string EncodeBase58(const std::vector<unsigned char>& vch) {
101 return EncodeBase58(&vch[0], &vch[0] + vch.size());
104 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet) {
105 return DecodeBase58(str.c_str(), vchRet);
108 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn) {
109 // add 4-byte hash check to the end
110 std::vector<unsigned char> vch(vchIn);
111 uint256 hash = Hash(vch.begin(), vch.end());
112 vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
113 return EncodeBase58(vch);
116 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet) {
117 if (!DecodeBase58(psz, vchRet) ||
123 // re-calculate the checksum, insure it matches the included 4-byte checksum
124 uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
125 if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
130 vchRet.resize(vchRet.size()-4);
134 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet) {
135 return DecodeBase58Check(str.c_str(), vchRet);
138 CBase58Data::CBase58Data() {
143 void CBase58Data::SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize) {
144 vchVersion = vchVersionIn;
145 vchData.resize(nSize);
146 if (!vchData.empty())
147 memcpy(&vchData[0], pdata, nSize);
150 void CBase58Data::SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend) {
151 SetData(vchVersionIn, (void*)pbegin, pend - pbegin);
154 bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes) {
155 std::vector<unsigned char> vchTemp;
156 bool rc58 = DecodeBase58Check(psz, vchTemp);
157 if ((!rc58) || (vchTemp.size() < nVersionBytes)) {
162 vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
163 vchData.resize(vchTemp.size() - nVersionBytes);
164 if (!vchData.empty())
165 memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size());
166 OPENSSL_cleanse(&vchTemp[0], vchData.size());
170 bool CBase58Data::SetString(const std::string& str) {
171 return SetString(str.c_str());
174 std::string CBase58Data::ToString() const {
175 std::vector<unsigned char> vch = vchVersion;
176 vch.insert(vch.end(), vchData.begin(), vchData.end());
177 return EncodeBase58Check(vch);
180 int CBase58Data::CompareTo(const CBase58Data& b58) const {
181 if (vchVersion < b58.vchVersion) return -1;
182 if (vchVersion > b58.vchVersion) return 1;
183 if (vchData < b58.vchData) return -1;
184 if (vchData > b58.vchData) return 1;
190 class CBitcoinAddressVisitor : public boost::static_visitor<bool> {
192 CBitcoinAddress *addr;
194 CBitcoinAddressVisitor(CBitcoinAddress *addrIn) : addr(addrIn) { }
196 bool operator()(const CKeyID &id) const { return addr->Set(id); }
197 bool operator()(const CScriptID &id) const { return addr->Set(id); }
198 bool operator()(const CNoDestination &no) const { return false; }
203 bool CBitcoinAddress::Set(const CKeyID &id) {
204 SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
208 bool CBitcoinAddress::Set(const CScriptID &id) {
209 SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20);
213 bool CBitcoinAddress::Set(const CTxDestination &dest) {
214 return boost::apply_visitor(CBitcoinAddressVisitor(this), dest);
217 bool CBitcoinAddress::IsValid() const {
218 bool fCorrectSize = vchData.size() == 20;
219 bool fKnownVersion = vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS) ||
220 vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
221 return fCorrectSize && fKnownVersion;
224 CTxDestination CBitcoinAddress::Get() const {
226 return CNoDestination();
228 memcpy(&id, &vchData[0], 20);
229 if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
231 else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
232 return CScriptID(id);
234 return CNoDestination();
237 bool CBitcoinAddress::GetKeyID(CKeyID &keyID) const {
238 if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
241 memcpy(&id, &vchData[0], 20);
246 bool CBitcoinAddress::IsScript() const {
247 return IsValid() && vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
250 void CBitcoinSecret::SetKey(const CKey& vchSecret) {
251 assert(vchSecret.IsValid());
252 SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), vchSecret.size());
253 if (vchSecret.IsCompressed())
254 vchData.push_back(1);
257 CKey CBitcoinSecret::GetKey() {
259 ret.Set(&vchData[0], &vchData[32], vchData.size() > 32 && vchData[32] == 1);
263 bool CBitcoinSecret::IsValid() const {
264 bool fExpectedFormat = vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1);
265 bool fCorrectVersion = vchVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
266 return fExpectedFormat && fCorrectVersion;
269 bool CBitcoinSecret::SetString(const char* pszSecret) {
270 return CBase58Data::SetString(pszSecret) && IsValid();
273 bool CBitcoinSecret::SetString(const std::string& strSecret) {
274 return SetString(strSecret.c_str());