27
27
#include < boost/variant/apply_visitor.hpp>
28
28
#include < boost/variant/static_visitor.hpp>
29
29
30
+ /* All alphanumeric characters except for "0", "I", "O", and "l" */
30
31
static const char * pszBase58 = " 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ;
31
32
32
- // Encode a byte sequence as a base58-encoded string
33
+ /* *
34
+ * Encode a byte sequence as a base58-encoded string
35
+ */
33
36
inline std::string EncodeBase58 (const unsigned char * pbegin, const unsigned char * pend)
34
37
{
35
38
CAutoBN_CTX pctx;
36
39
CBigNum bn58 = 58 ;
37
40
CBigNum bn0 = 0 ;
38
41
39
- // Convert big endian data to little endian
40
- // Extra zero at the end make sure bignum will interpret as a positive number
42
+ // Convert big endian data to little endian - the extra zero at the end will
43
+ // ensure bignum interprets it as a positive number */
41
44
std::vector<unsigned char > vchTmp (pend-pbegin+1 , 0 );
42
45
reverse_copy (pbegin, pend, vchTmp.begin ());
43
46
@@ -47,8 +50,8 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char
47
50
48
51
// Convert bignum to std::string
49
52
std::string str;
50
- // Expected size increase from base58 conversion is approximately 137%
51
- // use 138% to be safe
53
+ // The expected size increase from base58 conversion is approximately 137%,
54
+ // but use 138% to be safe
52
55
str.reserve ((pend - pbegin) * 138 / 100 + 1 );
53
56
CBigNum dv;
54
57
CBigNum rem;
@@ -70,14 +73,18 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char
70
73
return str;
71
74
}
72
75
73
- // Encode a byte vector as a base58-encoded string
76
+ /* *
77
+ * Encode a byte vector as a base58-encoded string
78
+ */
74
79
inline std::string EncodeBase58 (const std::vector<unsigned char >& vch)
75
80
{
76
81
return EncodeBase58 (&vch[0 ], &vch[0 ] + vch.size ());
77
82
}
78
83
79
- // Decode a base58-encoded string psz into byte vector vchRet
80
- // returns true if decoding is successful
84
+ /* *
85
+ * Decode a base58-encoded string (psz) into a byte vector (vchRet)
86
+ * return true if decoding is successful
87
+ */
81
88
inline bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vchRet)
82
89
{
83
90
CAutoBN_CTX pctx;
@@ -109,7 +116,7 @@ inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
109
116
// Get bignum as little endian data
110
117
std::vector<unsigned char > vchTmp = bn.getvch ();
111
118
112
- // Trim off sign byte if present
119
+ // Trim off the sign byte if present
113
120
if (vchTmp.size () >= 2 && vchTmp.end ()[-1 ] == 0 && vchTmp.end ()[-2 ] >= 0x80 )
114
121
vchTmp.erase (vchTmp.end ()-1 );
115
122
@@ -124,17 +131,18 @@ inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
124
131
return true ;
125
132
}
126
133
127
- // Decode a base58-encoded string str into byte vector vchRet
128
- // returns true if decoding is successful
134
+ /* *
135
+ * Decode a base58-encoded string (str) into a byte vector (vchRet)
136
+ * return true if decoding is successful
137
+ */
129
138
inline bool DecodeBase58 (const std::string& str, std::vector<unsigned char >& vchRet)
130
139
{
131
140
return DecodeBase58 (str.c_str (), vchRet);
132
141
}
133
142
134
-
135
-
136
-
137
- // Encode a byte vector to a base58-encoded string, including checksum
143
+ /* *
144
+ * Encode a byte vector into a base58-encoded string, including checksum
145
+ */
138
146
inline std::string EncodeBase58Check (const std::vector<unsigned char >& vchIn)
139
147
{
140
148
// add 4-byte hash check to the end
@@ -144,8 +152,10 @@ inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
144
152
return EncodeBase58 (vch);
145
153
}
146
154
147
- // Decode a base58-encoded string psz that includes a checksum, into byte vector vchRet
148
- // returns true if decoding is successful
155
+ /* *
156
+ * Decode a base58-encoded string (psz) that includes a checksum into a byte
157
+ * vector (vchRet), return true if decoding is successful
158
+ */
149
159
inline bool DecodeBase58Check (const char * psz, std::vector<unsigned char >& vchRet)
150
160
{
151
161
if (!DecodeBase58 (psz, vchRet))
@@ -155,6 +165,7 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
155
165
vchRet.clear ();
156
166
return false ;
157
167
}
168
+ // re-calculate the checksum, insure it matches the included 4-byte checksum
158
169
uint256 hash = Hash (vchRet.begin (), vchRet.end ()-4 );
159
170
if (memcmp (&hash, &vchRet.end ()[-4 ], 4 ) != 0 )
160
171
{
@@ -165,18 +176,18 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
165
176
return true ;
166
177
}
167
178
168
- // Decode a base58-encoded string str that includes a checksum, into byte vector vchRet
169
- // returns true if decoding is successful
179
+ /* *
180
+ * Decode a base58-encoded string (str) that includes a checksum into a byte
181
+ * vector (vchRet), return true if decoding is successful
182
+ */
170
183
inline bool DecodeBase58Check (const std::string& str, std::vector<unsigned char >& vchRet)
171
184
{
172
185
return DecodeBase58Check (str.c_str (), vchRet);
173
186
}
174
187
175
-
176
-
177
-
178
-
179
- /* * Base class for all base58-encoded data */
188
+ /* *
189
+ * Base class for all base58-encoded data
190
+ */
180
191
class CBase58Data
181
192
{
182
193
protected:
@@ -347,7 +358,9 @@ bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const {
347
358
bool inline CBitcoinAddressVisitor::operator ()(const CScriptID &id) const { return addr->Set (id); }
348
359
bool inline CBitcoinAddressVisitor::operator ()(const CNoDestination &id) const { return false ; }
349
360
350
- /* * A base58-encoded secret key */
361
+ /* *
362
+ * A base58-encoded secret key
363
+ */
351
364
class CBitcoinSecret : public CBase58Data
352
365
{
353
366
public:
@@ -393,7 +406,6 @@ class CBitcoinSecret : public CBase58Data
393
406
}
394
407
};
395
408
396
-
397
409
template <typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
398
410
{
399
411
public:
0 commit comments