Skip to content

Commit 8da64ca

Browse files
committed
Merge pull request #4014
4e9667b Improve and expand base58 comments (rxl)
2 parents 913e90d + 4e9667b commit 8da64ca

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/base58.h

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@
2727
#include <boost/variant/apply_visitor.hpp>
2828
#include <boost/variant/static_visitor.hpp>
2929

30+
/* All alphanumeric characters except for "0", "I", "O", and "l" */
3031
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
3132

32-
// Encode a byte sequence as a base58-encoded string
33+
/**
34+
* Encode a byte sequence as a base58-encoded string
35+
*/
3336
inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
3437
{
3538
CAutoBN_CTX pctx;
3639
CBigNum bn58 = 58;
3740
CBigNum bn0 = 0;
3841

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 */
4144
std::vector<unsigned char> vchTmp(pend-pbegin+1, 0);
4245
reverse_copy(pbegin, pend, vchTmp.begin());
4346

@@ -47,8 +50,8 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char
4750

4851
// Convert bignum to std::string
4952
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
5255
str.reserve((pend - pbegin) * 138 / 100 + 1);
5356
CBigNum dv;
5457
CBigNum rem;
@@ -70,14 +73,18 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char
7073
return str;
7174
}
7275

73-
// Encode a byte vector as a base58-encoded string
76+
/**
77+
* Encode a byte vector as a base58-encoded string
78+
*/
7479
inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
7580
{
7681
return EncodeBase58(&vch[0], &vch[0] + vch.size());
7782
}
7883

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+
*/
8188
inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
8289
{
8390
CAutoBN_CTX pctx;
@@ -109,7 +116,7 @@ inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
109116
// Get bignum as little endian data
110117
std::vector<unsigned char> vchTmp = bn.getvch();
111118

112-
// Trim off sign byte if present
119+
// Trim off the sign byte if present
113120
if (vchTmp.size() >= 2 && vchTmp.end()[-1] == 0 && vchTmp.end()[-2] >= 0x80)
114121
vchTmp.erase(vchTmp.end()-1);
115122

@@ -124,17 +131,18 @@ inline bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet)
124131
return true;
125132
}
126133

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+
*/
129138
inline bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
130139
{
131140
return DecodeBase58(str.c_str(), vchRet);
132141
}
133142

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+
*/
138146
inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
139147
{
140148
// add 4-byte hash check to the end
@@ -144,8 +152,10 @@ inline std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
144152
return EncodeBase58(vch);
145153
}
146154

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+
*/
149159
inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
150160
{
151161
if (!DecodeBase58(psz, vchRet))
@@ -155,6 +165,7 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
155165
vchRet.clear();
156166
return false;
157167
}
168+
// re-calculate the checksum, insure it matches the included 4-byte checksum
158169
uint256 hash = Hash(vchRet.begin(), vchRet.end()-4);
159170
if (memcmp(&hash, &vchRet.end()[-4], 4) != 0)
160171
{
@@ -165,18 +176,18 @@ inline bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRe
165176
return true;
166177
}
167178

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+
*/
170183
inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
171184
{
172185
return DecodeBase58Check(str.c_str(), vchRet);
173186
}
174187

175-
176-
177-
178-
179-
/** Base class for all base58-encoded data */
188+
/**
189+
* Base class for all base58-encoded data
190+
*/
180191
class CBase58Data
181192
{
182193
protected:
@@ -347,7 +358,9 @@ bool inline CBitcoinAddressVisitor::operator()(const CKeyID &id) const {
347358
bool inline CBitcoinAddressVisitor::operator()(const CScriptID &id) const { return addr->Set(id); }
348359
bool inline CBitcoinAddressVisitor::operator()(const CNoDestination &id) const { return false; }
349360

350-
/** A base58-encoded secret key */
361+
/**
362+
* A base58-encoded secret key
363+
*/
351364
class CBitcoinSecret : public CBase58Data
352365
{
353366
public:
@@ -393,7 +406,6 @@ class CBitcoinSecret : public CBase58Data
393406
}
394407
};
395408

396-
397409
template<typename K, int Size, CChainParams::Base58Type Type> class CBitcoinExtKeyBase : public CBase58Data
398410
{
399411
public:

0 commit comments

Comments
 (0)