]>
Commit | Line | Data |
---|---|---|
0a61b0df | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
88216419 | 2 | // Copyright (c) 2009-2012 The Bitcoin Developers |
0a61b0df | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
0a61b0df | 5 | |
6 | ||
7 | // | |
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. | |
14 | // | |
223b6f1b WL |
15 | #ifndef BITCOIN_BASE58_H |
16 | #define BITCOIN_BASE58_H | |
0a61b0df | 17 | |
223b6f1b WL |
18 | #include <string> |
19 | #include <vector> | |
20 | #include "bignum.h" | |
15a8590e | 21 | #include "key.h" |
10254401 | 22 | #include "script.h" |
0a61b0df | 23 | |
24 | static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | |
25 | ||
d825e6a3 | 26 | // Encode a byte sequence as a base58-encoded string |
223b6f1b | 27 | inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) |
0a61b0df | 28 | { |
29 | CAutoBN_CTX pctx; | |
30 | CBigNum bn58 = 58; | |
31 | CBigNum bn0 = 0; | |
32 | ||
33 | // Convert big endian data to little endian | |
34 | // Extra zero at the end make sure bignum will interpret as a positive number | |
223b6f1b | 35 | std::vector<unsigned char> vchTmp(pend-pbegin+1, 0); |
0a61b0df | 36 | reverse_copy(pbegin, pend, vchTmp.begin()); |
37 | ||
38 | // Convert little endian data to bignum | |
39 | CBigNum bn; | |
40 | bn.setvch(vchTmp); | |
41 | ||
223b6f1b WL |
42 | // Convert bignum to std::string |
43 | std::string str; | |
a9d3af88 DH |
44 | // Expected size increase from base58 conversion is approximately 137% |
45 | // use 138% to be safe | |
0a61b0df | 46 | str.reserve((pend - pbegin) * 138 / 100 + 1); |
47 | CBigNum dv; | |
48 | CBigNum rem; | |
49 | while (bn > bn0) | |
50 | { | |
51 | if (!BN_div(&dv, &rem, &bn, &bn58, pctx)) | |
52 | throw bignum_error("EncodeBase58 : BN_div failed"); | |
53 | bn = dv; | |
54 | unsigned int c = rem.getulong(); | |
55 | str += pszBase58[c]; | |
56 | } | |
57 | ||
58 | // Leading zeroes encoded as base58 zeros | |
59 | for (const unsigned char* p = pbegin; p < pend && *p == 0; p++) | |
60 | str += pszBase58[0]; | |
61 | ||
223b6f1b | 62 | // Convert little endian std::string to big endian |
0a61b0df | 63 | reverse(str.begin(), str.end()); |
64 | return str; | |
65 | } | |
66 | ||
d825e6a3 | 67 | // Encode a byte vector as a base58-encoded string |
223b6f1b | 68 | inline std::string EncodeBase58(const std::vector<unsigned char>& vch) |
0a61b0df | 69 | { |
70 | return EncodeBase58(&vch[0], &vch[0] + vch.size()); | |
71 | } | |
72 | ||
d825e6a3 PW |
73 | // Decode a base58-encoded string psz into byte vector vchRet |
74 | // returns true if decoding is succesful | |
223b6f1b | 75 | inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet) |
0a61b0df | 76 | { |
77 | CAutoBN_CTX pctx; | |
78 | vchRet.clear(); | |
79 | CBigNum bn58 = 58; | |
80 | CBigNum bn = 0; | |
81 | CBigNum bnChar; | |
82 | while (isspace(*psz)) | |
83 | psz++; | |
84 | ||
85 | // Convert big endian string to bignum | |
86 | for (const char* p = psz; *p; p++) | |
87 | { | |
88 | const char* p1 = strchr(pszBase58, *p); | |
89 | if (p1 == NULL) | |
90 | { | |
91 | while (isspace(*p)) | |
92 | p++; | |
93 | if (*p != '\0') | |
94 | return false; | |
95 | break; | |
96 | } | |
97 | bnChar.setulong(p1 - pszBase58); | |
98 | if (!BN_mul(&bn, &bn, &bn58, pctx)) | |
99 | throw bignum_error("DecodeBase58 : BN_mul failed"); | |
100 | bn += bnChar; | |
101 | } | |
102 | ||
103 | // Get bignum as little endian data | |
223b6f1b | 104 | std::vector<unsigned char> vchTmp = bn.getvch(); |
0a61b0df | 105 | |
106 | // Trim off sign byte if present | |
107 | if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80) | |
108 | vchTmp.erase(vchTmp.end()-1); | |
109 | ||
110 | // Restore leading zeros | |
111 | int nLeadingZeros = 0; | |
112 | for (const char* p = psz; *p == pszBase58[0]; p++) | |
113 | nLeadingZeros++; | |
114 | vchRet.assign(nLeadingZeros + vchTmp.size(), 0); | |
115 | ||
116 | // Convert little endian data to big endian | |
117 | reverse_copy(vchTmp.begin(), vchTmp.end(), vchRet.end() - vchTmp.size()); | |
118 | return true; | |
119 | } | |
120 | ||
d825e6a3 PW |
121 | // Decode a base58-encoded string str into byte vector vchRet |
122 | // returns true if decoding is succesful | |
223b6f1b | 123 | inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet) |
0a61b0df | 124 | { |
125 | return DecodeBase58(str.c_str(), vchRet); | |
126 | } | |
127 | ||
128 | ||
129 | ||
130 | ||
d825e6a3 | 131 | // Encode a byte vector to a base58-encoded string, including checksum |
223b6f1b | 132 | inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn) |
0a61b0df | 133 | { |
134 | // add 4-byte hash check to the end | |
223b6f1b | 135 | std::vector<unsigned char> vch(vchIn); |
0a61b0df | 136 | uint256 hash = Hash(vch.begin(), vch.end()); |
137 | vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4); | |
138 | return EncodeBase58(vch); | |
139 | } | |
140 | ||
d825e6a3 PW |
141 | // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet |
142 | // returns true if decoding is succesful | |
223b6f1b | 143 | inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet) |
0a61b0df | 144 | { |
145 | if (!DecodeBase58(psz, vchRet)) | |
146 | return false; | |
147 | if (vchRet.size() < 4) | |
148 | { | |
149 | vchRet.clear(); | |
150 | return false; | |
151 | } | |
152 | uint256 hash = Hash(vchRet.begin(), vchRet.end()-4); | |
153 | if (memcmp(&hash, &vchRet.end()[-4], 4) != 0) | |
154 | { | |
155 | vchRet.clear(); | |
156 | return false; | |
157 | } | |
158 | vchRet.resize(vchRet.size()-4); | |
159 | return true; | |
160 | } | |
161 | ||
d825e6a3 PW |
162 | // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet |
163 | // returns true if decoding is succesful | |
223b6f1b | 164 | inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet) |
0a61b0df | 165 | { |
166 | return DecodeBase58Check(str.c_str(), vchRet); | |
167 | } | |
168 | ||
169 | ||
170 | ||
171 | ||
172 | ||
6b8de05d | 173 | /** Base class for all base58-encoded data */ |
cb61b8dc | 174 | class CBase58Data |
0a61b0df | 175 | { |
2ffba736 | 176 | protected: |
d825e6a3 | 177 | // the version byte |
2ffba736 | 178 | unsigned char nVersion; |
d825e6a3 PW |
179 | |
180 | // the actually encoded data | |
2ffba736 | 181 | std::vector<unsigned char> vchData; |
0a61b0df | 182 | |
cb61b8dc | 183 | CBase58Data() |
2ffba736 | 184 | { |
cb61b8dc PW |
185 | nVersion = 0; |
186 | vchData.clear(); | |
187 | } | |
188 | ||
189 | ~CBase58Data() | |
190 | { | |
d825e6a3 | 191 | // zero the memory, as it may contain sensitive data |
03f8b545 AJ |
192 | if (!vchData.empty()) |
193 | memset(&vchData[0], 0, vchData.size()); | |
cb61b8dc PW |
194 | } |
195 | ||
196 | void SetData(int nVersionIn, const void* pdata, size_t nSize) | |
197 | { | |
198 | nVersion = nVersionIn; | |
199 | vchData.resize(nSize); | |
03f8b545 AJ |
200 | if (!vchData.empty()) |
201 | memcpy(&vchData[0], pdata, nSize); | |
cb61b8dc PW |
202 | } |
203 | ||
204 | void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend) | |
205 | { | |
206 | SetData(nVersionIn, (void*)pbegin, pend - pbegin); | |
2ffba736 | 207 | } |
0a61b0df | 208 | |
cb61b8dc PW |
209 | public: |
210 | bool SetString(const char* psz) | |
2ffba736 PW |
211 | { |
212 | std::vector<unsigned char> vchTemp; | |
cb61b8dc | 213 | DecodeBase58Check(psz, vchTemp); |
2ffba736 PW |
214 | if (vchTemp.empty()) |
215 | { | |
216 | vchData.clear(); | |
217 | nVersion = 0; | |
218 | return false; | |
219 | } | |
220 | nVersion = vchTemp[0]; | |
221 | vchData.resize(vchTemp.size() - 1); | |
03f8b545 AJ |
222 | if (!vchData.empty()) |
223 | memcpy(&vchData[0], &vchTemp[1], vchData.size()); | |
cb61b8dc | 224 | memset(&vchTemp[0], 0, vchTemp.size()); |
2ffba736 PW |
225 | return true; |
226 | } | |
0a61b0df | 227 | |
cb61b8dc PW |
228 | bool SetString(const std::string& str) |
229 | { | |
230 | return SetString(str.c_str()); | |
231 | } | |
232 | ||
233 | std::string ToString() const | |
2ffba736 | 234 | { |
cb61b8dc PW |
235 | std::vector<unsigned char> vch(1, nVersion); |
236 | vch.insert(vch.end(), vchData.begin(), vchData.end()); | |
237 | return EncodeBase58Check(vch); | |
2ffba736 | 238 | } |
0a61b0df | 239 | |
cb61b8dc | 240 | int CompareTo(const CBase58Data& b58) const |
2ffba736 | 241 | { |
cb61b8dc | 242 | if (nVersion < b58.nVersion) return -1; |
03f8b545 | 243 | if (nVersion > b58.nVersion) return 1; |
cb61b8dc PW |
244 | if (vchData < b58.vchData) return -1; |
245 | if (vchData > b58.vchData) return 1; | |
246 | return 0; | |
247 | } | |
248 | ||
249 | bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; } | |
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 | }; | |
255 | ||
ff0ee876 | 256 | /** base58-encoded Bitcoin addresses. |
6b8de05d PW |
257 | * Public-key-hash-addresses have version 0 (or 111 testnet). |
258 | * The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key. | |
259 | * Script-hash-addresses have version 5 (or 196 testnet). | |
260 | * The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script. | |
261 | */ | |
10254401 PW |
262 | class CBitcoinAddress; |
263 | class CBitcoinAddressVisitor : public boost::static_visitor<bool> | |
264 | { | |
265 | private: | |
266 | CBitcoinAddress *addr; | |
267 | public: | |
268 | CBitcoinAddressVisitor(CBitcoinAddress *addrIn) : addr(addrIn) { } | |
269 | bool operator()(const CKeyID &id) const; | |
270 | bool operator()(const CScriptID &id) const; | |
271 | bool operator()(const CNoDestination &no) const; | |
272 | }; | |
273 | ||
cb61b8dc PW |
274 | class CBitcoinAddress : public CBase58Data |
275 | { | |
276 | public: | |
9e470585 | 277 | enum |
cb61b8dc | 278 | { |
9e470585 GA |
279 | PUBKEY_ADDRESS = 0, |
280 | SCRIPT_ADDRESS = 5, | |
ce336fdc | 281 | PUBKEY_ADDRESS_TEST = 111, |
9e470585 GA |
282 | SCRIPT_ADDRESS_TEST = 196, |
283 | }; | |
284 | ||
10254401 PW |
285 | bool Set(const CKeyID &id) { |
286 | SetData(fTestNet ? PUBKEY_ADDRESS_TEST : PUBKEY_ADDRESS, &id, 20); | |
9e470585 | 287 | return true; |
cb61b8dc PW |
288 | } |
289 | ||
10254401 PW |
290 | bool Set(const CScriptID &id) { |
291 | SetData(fTestNet ? SCRIPT_ADDRESS_TEST : SCRIPT_ADDRESS, &id, 20); | |
292 | return true; | |
2ffba736 PW |
293 | } |
294 | ||
10254401 | 295 | bool Set(const CTxDestination &dest) |
e679ec96 | 296 | { |
10254401 | 297 | return boost::apply_visitor(CBitcoinAddressVisitor(this), dest); |
e679ec96 GA |
298 | } |
299 | ||
2ffba736 PW |
300 | bool IsValid() const |
301 | { | |
9fb89c26 | 302 | unsigned int nExpectedSize = 20; |
2ffba736 PW |
303 | bool fExpectTestNet = false; |
304 | switch(nVersion) | |
305 | { | |
9e470585 | 306 | case PUBKEY_ADDRESS: |
e679ec96 GA |
307 | nExpectedSize = 20; // Hash of public key |
308 | fExpectTestNet = false; | |
309 | break; | |
9e470585 | 310 | case SCRIPT_ADDRESS: |
922e8e29 | 311 | nExpectedSize = 20; // Hash of CScript |
e679ec96 | 312 | fExpectTestNet = false; |
2ffba736 PW |
313 | break; |
314 | ||
9e470585 | 315 | case PUBKEY_ADDRESS_TEST: |
e679ec96 GA |
316 | nExpectedSize = 20; |
317 | fExpectTestNet = true; | |
318 | break; | |
9e470585 | 319 | case SCRIPT_ADDRESS_TEST: |
e679ec96 | 320 | nExpectedSize = 20; |
2ffba736 PW |
321 | fExpectTestNet = true; |
322 | break; | |
0a61b0df | 323 | |
2ffba736 PW |
324 | default: |
325 | return false; | |
326 | } | |
327 | return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize; | |
328 | } | |
0a61b0df | 329 | |
2ffba736 PW |
330 | CBitcoinAddress() |
331 | { | |
2ffba736 PW |
332 | } |
333 | ||
10254401 | 334 | CBitcoinAddress(const CTxDestination &dest) |
2ffba736 | 335 | { |
10254401 | 336 | Set(dest); |
2ffba736 | 337 | } |
0a61b0df | 338 | |
2ffba736 PW |
339 | CBitcoinAddress(const std::string& strAddress) |
340 | { | |
cb61b8dc | 341 | SetString(strAddress); |
2ffba736 PW |
342 | } |
343 | ||
344 | CBitcoinAddress(const char* pszAddress) | |
345 | { | |
cb61b8dc | 346 | SetString(pszAddress); |
2ffba736 PW |
347 | } |
348 | ||
10254401 PW |
349 | CTxDestination Get() const { |
350 | if (!IsValid()) | |
351 | return CNoDestination(); | |
352 | switch (nVersion) { | |
353 | case PUBKEY_ADDRESS: | |
354 | case PUBKEY_ADDRESS_TEST: { | |
355 | uint160 id; | |
356 | memcpy(&id, &vchData[0], 20); | |
357 | return CKeyID(id); | |
358 | } | |
359 | case SCRIPT_ADDRESS: | |
360 | case SCRIPT_ADDRESS_TEST: { | |
361 | uint160 id; | |
362 | memcpy(&id, &vchData[0], 20); | |
363 | return CScriptID(id); | |
364 | } | |
365 | } | |
366 | return CNoDestination(); | |
367 | } | |
368 | ||
369 | bool GetKeyID(CKeyID &keyID) const { | |
370 | if (!IsValid()) | |
371 | return false; | |
372 | switch (nVersion) { | |
373 | case PUBKEY_ADDRESS: | |
374 | case PUBKEY_ADDRESS_TEST: { | |
375 | uint160 id; | |
376 | memcpy(&id, &vchData[0], 20); | |
377 | keyID = CKeyID(id); | |
378 | return true; | |
379 | } | |
380 | default: return false; | |
381 | } | |
382 | } | |
383 | ||
384 | bool IsScript() const { | |
385 | if (!IsValid()) | |
386 | return false; | |
387 | switch (nVersion) { | |
388 | case SCRIPT_ADDRESS: | |
389 | case SCRIPT_ADDRESS_TEST: { | |
390 | return true; | |
391 | } | |
392 | default: return false; | |
393 | } | |
2ffba736 | 394 | } |
2ffba736 | 395 | }; |
223b6f1b | 396 | |
10254401 PW |
397 | bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const { return addr->Set(id); } |
398 | bool inline CBitcoinAddressVisitor::operator()(const CScriptID &id) const { return addr->Set(id); } | |
399 | bool inline CBitcoinAddressVisitor::operator()(const CNoDestination &id) const { return false; } | |
400 | ||
6b8de05d | 401 | /** A base58-encoded secret key */ |
15a8590e PW |
402 | class CBitcoinSecret : public CBase58Data |
403 | { | |
404 | public: | |
11529c6e PW |
405 | void SetSecret(const CSecret& vchSecret, bool fCompressed) |
406 | { | |
407 | assert(vchSecret.size() == 32); | |
15a8590e | 408 | SetData(fTestNet ? 239 : 128, &vchSecret[0], vchSecret.size()); |
11529c6e PW |
409 | if (fCompressed) |
410 | vchData.push_back(1); | |
15a8590e PW |
411 | } |
412 | ||
11529c6e | 413 | CSecret GetSecret(bool &fCompressedOut) |
15a8590e PW |
414 | { |
415 | CSecret vchSecret; | |
11529c6e PW |
416 | vchSecret.resize(32); |
417 | memcpy(&vchSecret[0], &vchData[0], 32); | |
418 | fCompressedOut = vchData.size() == 33; | |
15a8590e PW |
419 | return vchSecret; |
420 | } | |
421 | ||
422 | bool IsValid() const | |
423 | { | |
15a8590e PW |
424 | bool fExpectTestNet = false; |
425 | switch(nVersion) | |
426 | { | |
427 | case 128: | |
428 | break; | |
429 | ||
430 | case 239: | |
431 | fExpectTestNet = true; | |
432 | break; | |
433 | ||
434 | default: | |
435 | return false; | |
436 | } | |
11529c6e | 437 | return fExpectTestNet == fTestNet && (vchData.size() == 32 || (vchData.size() == 33 && vchData[32] == 1)); |
15a8590e PW |
438 | } |
439 | ||
b3a6e613 CM |
440 | bool SetString(const char* pszSecret) |
441 | { | |
442 | return CBase58Data::SetString(pszSecret) && IsValid(); | |
443 | } | |
444 | ||
445 | bool SetString(const std::string& strSecret) | |
446 | { | |
447 | return SetString(strSecret.c_str()); | |
448 | } | |
449 | ||
11529c6e | 450 | CBitcoinSecret(const CSecret& vchSecret, bool fCompressed) |
15a8590e | 451 | { |
11529c6e | 452 | SetSecret(vchSecret, fCompressed); |
15a8590e PW |
453 | } |
454 | ||
455 | CBitcoinSecret() | |
456 | { | |
457 | } | |
458 | }; | |
459 | ||
223b6f1b | 460 | #endif |