Anoncoin  0.9.4
P2P Digital Currency
base58.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "base58.h"
7 
8 #include "hash.h"
9 #include "uint256.h"
10 
11 #include <assert.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <vector>
15 #include <string>
16 #include <boost/variant/apply_visitor.hpp>
17 #include <boost/variant/static_visitor.hpp>
18 
19 /* All alphanumeric characters except for "0", "I", "O", and "l" */
20 static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
21 
22 bool DecodeBase58(const char *psz, std::vector<unsigned char>& vch) {
23  // Skip leading spaces.
24  while (*psz && isspace(*psz))
25  psz++;
26  // Skip and count leading '1's.
27  int zeroes = 0;
28  while (*psz == '1') {
29  zeroes++;
30  psz++;
31  }
32  // Allocate enough space in big-endian base256 representation.
33  std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.
34  // Process the characters.
35  while (*psz && !isspace(*psz)) {
36  // Decode base58 character
37  const char *ch = strchr(pszBase58, *psz);
38  if (ch == NULL)
39  return false;
40  // Apply "b256 = b256 * 58 + ch".
41  int carry = ch - pszBase58;
42  for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {
43  carry += 58 * (*it);
44  *it = carry % 256;
45  carry /= 256;
46  }
47  assert(carry == 0);
48  psz++;
49  }
50  // Skip trailing spaces.
51  while (isspace(*psz))
52  psz++;
53  if (*psz != 0)
54  return false;
55  // Skip leading zeroes in b256.
56  std::vector<unsigned char>::iterator it = b256.begin();
57  while (it != b256.end() && *it == 0)
58  it++;
59  // Copy result into output vector.
60  vch.reserve(zeroes + (b256.end() - it));
61  vch.assign(zeroes, 0x00);
62  while (it != b256.end())
63  vch.push_back(*(it++));
64  return true;
65 }
66 
67 std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) {
68  // Skip & count leading zeroes.
69  int zeroes = 0;
70  while (pbegin != pend && *pbegin == 0) {
71  pbegin++;
72  zeroes++;
73  }
74  // Allocate enough space in big-endian base58 representation.
75  std::vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up.
76  // Process the bytes.
77  while (pbegin != pend) {
78  int carry = *pbegin;
79  // Apply "b58 = b58 * 256 + ch".
80  for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) {
81  carry += 256 * (*it);
82  *it = carry % 58;
83  carry /= 58;
84  }
85  assert(carry == 0);
86  pbegin++;
87  }
88  // Skip leading zeroes in base58 result.
89  std::vector<unsigned char>::iterator it = b58.begin();
90  while (it != b58.end() && *it == 0)
91  it++;
92  // Translate the result into a string.
93  std::string str;
94  str.reserve(zeroes + (b58.end() - it));
95  str.assign(zeroes, '1');
96  while (it != b58.end())
97  str += pszBase58[*(it++)];
98  return str;
99 }
100 
101 std::string EncodeBase58(const std::vector<unsigned char>& vch) {
102  return EncodeBase58(&vch[0], &vch[0] + vch.size());
103 }
104 
105 bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet) {
106  return DecodeBase58(str.c_str(), vchRet);
107 }
108 
109 std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn) {
110  // add 4-byte hash check to the end
111  std::vector<unsigned char> vch(vchIn);
112  uint256 hash = Hash(vch.begin(), vch.end());
113  vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
114  return EncodeBase58(vch);
115 }
116 
117 bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet) {
118  if (!DecodeBase58(psz, vchRet) ||
119  (vchRet.size() < 4))
120  {
121  vchRet.clear();
122  return false;
123  }
124  // re-calculate the checksum, insure it matches the included 4-byte checksum
125  uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
126  if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
127  {
128  vchRet.clear();
129  return false;
130  }
131  vchRet.resize(vchRet.size()-4);
132  return true;
133 }
134 
135 bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet) {
136  return DecodeBase58Check(str.c_str(), vchRet);
137 }
138 
140  vchVersion.clear();
141  vchData.clear();
142 }
143 
144 void CBase58Data::SetData(const std::vector<unsigned char> &vchVersionIn, const void* pdata, size_t nSize) {
145  vchVersion = vchVersionIn;
146  vchData.resize(nSize);
147  if (!vchData.empty())
148  memcpy(&vchData[0], pdata, nSize);
149 }
150 
151 void CBase58Data::SetData(const std::vector<unsigned char> &vchVersionIn, const unsigned char *pbegin, const unsigned char *pend) {
152  SetData(vchVersionIn, (void*)pbegin, pend - pbegin);
153 }
154 
155 bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes) {
156  std::vector<unsigned char> vchTemp;
157  bool rc58 = DecodeBase58Check(psz, vchTemp);
158  if ((!rc58) || (vchTemp.size() < nVersionBytes)) {
159  vchData.clear();
160  vchVersion.clear();
161  return false;
162  }
163  vchVersion.assign(vchTemp.begin(), vchTemp.begin() + nVersionBytes);
164  vchData.resize(vchTemp.size() - nVersionBytes);
165  if (!vchData.empty())
166  memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size());
167  OPENSSL_cleanse(&vchTemp[0], vchData.size());
168  return true;
169 }
170 
171 bool CBase58Data::SetString(const std::string& str) {
172  return SetString(str.c_str());
173 }
174 
175 std::string CBase58Data::ToString() const {
176  std::vector<unsigned char> vch = vchVersion;
177  vch.insert(vch.end(), vchData.begin(), vchData.end());
178  return EncodeBase58Check(vch);
179 }
180 
181 int CBase58Data::CompareTo(const CBase58Data& b58) const {
182  if (vchVersion < b58.vchVersion) return -1;
183  if (vchVersion > b58.vchVersion) return 1;
184  if (vchData < b58.vchData) return -1;
185  if (vchData > b58.vchData) return 1;
186  return 0;
187 }
188 
189 namespace {
190  class CAnoncoinAddressVisitor : public boost::static_visitor<bool> {
191  private:
192  CAnoncoinAddress *addr;
193  public:
194  CAnoncoinAddressVisitor(CAnoncoinAddress *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 
202 bool CAnoncoinAddress::Set(const CKeyID &id) {
203  SetData(Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS), &id, 20);
204  return true;
205 }
206 
208  SetData(Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS), &id, 20);
209  return true;
210 }
211 
213  return boost::apply_visitor(CAnoncoinAddressVisitor(this), dest);
214 }
215 
217  bool fCorrectSize = vchData.size() == 20;
218  bool fKnownVersion = vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS) ||
220  return fCorrectSize && fKnownVersion;
221 }
222 
224  if (!IsValid())
225  return CNoDestination();
226  uint160 id;
227  memcpy(&id, &vchData[0], 20);
228  if (vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
229  return CKeyID(id);
230  else if (vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS))
231  return CScriptID(id);
232  else
233  return CNoDestination();
234 }
235 
236 bool CAnoncoinAddress::GetKeyID(CKeyID &keyID) const {
237  if (!IsValid() || vchVersion != Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS))
238  return false;
239  uint160 id;
240  memcpy(&id, &vchData[0], 20);
241  keyID = CKeyID(id);
242  return true;
243 }
244 
247 }
248 
249 void CAnoncoinSecret::SetKey(const CKey& vchSecret) {
250  assert(vchSecret.IsValid());
251  SetData(Params().Base58Prefix(CChainParams::SECRET_KEY), vchSecret.begin(), vchSecret.size());
252  if (vchSecret.IsCompressed())
253  vchData.push_back(1);
254 }
255 
257  CKey ret;
258  ret.Set(&vchData[0], &vchData[32], vchData.size() > 32 && vchData[32] == 1);
259  return ret;
260 }
261 
263  bool fExpectedFormat = vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1);
264  bool fCorrectVersion = vchVersion == Params().Base58Prefix(CChainParams::SECRET_KEY);
265  return fExpectedFormat && fCorrectVersion;
266 }
267 
268 bool CAnoncoinSecret::SetString(const char* pszSecret) {
269  return CBase58Data::SetString(pszSecret) && IsValid();
270 }
271 
272 bool CAnoncoinSecret::SetString(const std::string& strSecret) {
273  return SetString(strSecret.c_str());
274 }
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
Definition: base58.cpp:22
bool IsScript() const
Definition: base58.cpp:245
const unsigned char * begin() const
Definition: key.h:235
bool Set(const CKeyID &id)
Definition: base58.cpp:202
unsigned char * end()
Definition: uint256.h:351
bool IsValid() const
Definition: base58.cpp:262
CTxDestination Get() const
Definition: base58.cpp:223
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Encode a byte sequence as a base58-encoded string.
Definition: base58.cpp:67
bool GetKeyID(CKeyID &keyID) const
Definition: base58.cpp:236
vector_uchar vchData
Definition: base58.h:77
void SetData(const std::vector< unsigned char > &vchVersionIn, const void *pdata, size_t nSize)
Definition: base58.cpp:144
bool IsValid() const
Definition: base58.cpp:216
void SetKey(const CKey &vchSecret)
Definition: base58.cpp:249
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
Definition: chainparams.h:90
bool IsValid() const
Definition: key.h:239
Base class for all base58-encoded data.
Definition: base58.h:69
bool IsCompressed() const
Definition: key.h:242
base58-encoded Anoncoin addresses.
Definition: base58.h:102
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet), return true if decoding is successful.
Definition: base58.cpp:117
bool SetString(const char *pszSecret)
Definition: base58.cpp:268
std::string ToString() const
Definition: base58.cpp:175
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:20
void Set(const T pbegin, const T pend, bool fCompressedIn)
Definition: key.h:219
bool SetString(const char *psz, unsigned int nVersionBytes=1)
Definition: base58.cpp:155
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: keystore.h:17
256-bit unsigned integer
Definition: uint256.h:532
const CChainParams & Params()
Return the currently selected parameters.
void * memcpy(void *a, const void *b, size_t c)
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:27
160-bit unsigned integer
Definition: uint256.h:420
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:35
An encapsulated private key.
Definition: key.h:180
unsigned int size() const
Definition: key.h:234
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
Definition: base58.cpp:109
CKey GetKey()
Definition: base58.cpp:256
std::vector< unsigned char > vchVersion
Definition: base58.h:73
int CompareTo(const CBase58Data &b58) const
Definition: base58.cpp:181