]> Git Repo - VerusCoin.git/blob - src/base58.cpp
UniValue: compact (!pretty) output should not include extra whitespace
[VerusCoin.git] / src / base58.cpp
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.
4
5 #include "base58.h"
6
7 #include "hash.h"
8 #include "uint256.h"
9
10 #include <assert.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <vector>
14 #include <string>
15 #include <boost/variant/apply_visitor.hpp>
16 #include <boost/variant/static_visitor.hpp>
17
18 /* All alphanumeric characters except for "0", "I", "O", and "l" */
19 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
20
21 bool DecodeBase58(const char *psz, std::vector<unsigned char>& vch) {
22     // Skip leading spaces.
23     while (*psz && isspace(*psz))
24         psz++;
25     // Skip and count leading '1's.
26     int zeroes = 0;
27     while (*psz == '1') {
28         zeroes++;
29         psz++;
30     }
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);
37         if (ch == NULL)
38             return false;
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++) {
42             carry += 58 * (*it);
43             *it = carry % 256;
44             carry /= 256;
45         }
46         assert(carry == 0);
47         psz++;
48     }
49     // Skip trailing spaces.
50     while (isspace(*psz))
51         psz++;
52     if (*psz != 0)
53         return false;
54     // Skip leading zeroes in b256.
55     std::vector<unsigned char>::iterator it = b256.begin();
56     while (it != b256.end() && *it == 0)
57         it++;
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++));
63     return true;
64 }
65
66 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) {
67     // Skip & count leading zeroes.
68     int zeroes = 0;
69     while (pbegin != pend && *pbegin == 0) {
70         pbegin++;
71         zeroes++;
72     }
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.
75     // Process the bytes.
76     while (pbegin != pend) {
77         int carry = *pbegin;
78         // Apply "b58 = b58 * 256 + ch".
79         for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) {
80             carry += 256 * (*it);
81             *it = carry % 58;
82             carry /= 58;
83         }
84         assert(carry == 0);
85         pbegin++;
86     }
87     // Skip leading zeroes in base58 result.
88     std::vector<unsigned char>::iterator it = b58.begin();
89     while (it != b58.end() && *it == 0)
90         it++;
91     // Translate the result into a string.
92     std::string str;
93     str.reserve(zeroes + (b58.end() - it));
94     str.assign(zeroes, '1');
95     while (it != b58.end())
96         str += pszBase58[*(it++)];
97     return str;
98 }
99
100 std::string EncodeBase58(const std::vector<unsigned char>& vch) {
101     return EncodeBase58(&vch[0], &vch[0] + vch.size());
102 }
103
104 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet) {
105     return DecodeBase58(str.c_str(), vchRet);
106 }
107
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);
114 }
115
116 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet) {
117     if (!DecodeBase58(psz, vchRet) ||
118         (vchRet.size() < 4))
119     {
120         vchRet.clear();
121         return false;
122     }
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)
126     {
127         vchRet.clear();
128         return false;
129     }
130     vchRet.resize(vchRet.size()-4);
131     return true;
132 }
133
134 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet) {
135     return DecodeBase58Check(str.c_str(), vchRet);
136 }
137
138 CBase58Data::CBase58Data() {
139     vchVersion.clear();
140     vchData.clear();
141 }
142
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);
148 }
149
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);
152 }
153
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)) {
158         vchData.clear();
159         vchVersion.clear();
160         return false;
161     }
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());
167     return true;
168 }
169
170 bool CBase58Data::SetString(const std::string& str) {
171     return SetString(str.c_str());
172 }
173
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);
178 }
179
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;
185     return 0;
186 }
187
188 namespace {
189
190     class CBitcoinAddressVisitor : public boost::static_visitor<bool> {
191     private:
192         CBitcoinAddress *addr;
193     public:
194         CBitcoinAddressVisitor(CBitcoinAddress *addrIn) : addr(addrIn) { }
195
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; }
199     };
200
201 } // anon namespace
202
203 bool CBitcoinAddress::Set(const CKeyID &id) {
204     SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
205     return true;
206 }
207
208 bool CBitcoinAddress::Set(const CScriptID &id) {
209     SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20);
210     return true;
211 }
212
213 bool CBitcoinAddress::Set(const CTxDestination &dest) {
214     return boost::apply_visitor(CBitcoinAddressVisitor(this), dest);
215 }
216
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;
222 }
223
224 CTxDestination CBitcoinAddress::Get() const {
225     if (!IsValid())
226         return CNoDestination();
227     uint160 id;
228     memcpy(&id, &vchData[0], 20);
229     if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
230         return CKeyID(id);
231     else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
232         return CScriptID(id);
233     else
234         return CNoDestination();
235 }
236
237 bool CBitcoinAddress::GetKeyID(CKeyID &keyID) const {
238     if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
239         return false;
240     uint160 id;
241     memcpy(&id, &vchData[0], 20);
242     keyID = CKeyID(id);
243     return true;
244 }
245
246 bool CBitcoinAddress::IsScript() const {
247     return IsValid() && vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
248 }
249
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);
255 }
256
257 CKey CBitcoinSecret::GetKey() {
258     CKey ret;
259     ret.Set(&vchData[0], &vchData[32], vchData.size() > 32 && vchData[32] == 1);
260     return ret;
261 }
262
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;
267 }
268
269 bool CBitcoinSecret::SetString(const char* pszSecret) {
270     return CBase58Data::SetString(pszSecret) && IsValid();
271 }
272
273 bool CBitcoinSecret::SetString(const std::string& strSecret) {
274     return SetString(strSecret.c_str());
275 }
This page took 0.035097 seconds and 4 git commands to generate.