diff --git a/src/base58.h b/src/base58.h --- a/src/base58.h +++ b/src/base58.h @@ -17,20 +17,15 @@ #define BITCOIN_BASE58_H #include +#include #include #include /** - * Encode a byte sequence as a base58-encoded string. - * pbegin and pend cannot be nullptr, unless both are. + * Encode a byte span as a base58-encoded string */ -std::string EncodeBase58(const uint8_t *pbegin, const uint8_t *pend); - -/** - * Encode a byte vector as a base58-encoded string - */ -std::string EncodeBase58(const std::vector &vch); +std::string EncodeBase58(Span input); /** * Decode a base58-encoded string (psz) into a byte vector (vchRet). @@ -48,9 +43,9 @@ std::vector &vchRet, int max_ret_len); /** - * Encode a byte vector into a base58-encoded string, including checksum + * Encode a byte span into a base58-encoded string, including checksum */ -std::string EncodeBase58Check(const std::vector &vchIn); +std::string EncodeBase58Check(Span input); /** * Decode a base58-encoded string (psz) that includes a checksum into a byte diff --git a/src/base58.cpp b/src/base58.cpp --- a/src/base58.cpp +++ b/src/base58.cpp @@ -97,21 +97,21 @@ return true; } -std::string EncodeBase58(const uint8_t *pbegin, const uint8_t *pend) { +std::string EncodeBase58(Span input) { // Skip & count leading zeroes. int zeroes = 0; int length = 0; - while (pbegin != pend && *pbegin == 0) { - pbegin++; + while (input.size() > 0 && input[0] == 0) { + input = input.subspan(1); zeroes++; } // Allocate enough space in big-endian base58 representation. // log(256) / log(58), rounded up. - int size = (pend - pbegin) * 138 / 100 + 1; + int size = input.size() * 138 / 100 + 1; std::vector b58(size); // Process the bytes. - while (pbegin != pend) { - int carry = *pbegin; + while (input.size() > 0) { + int carry = input[0]; int i = 0; // Apply "b58 = b58 * 256 + ch". for (std::vector::reverse_iterator it = b58.rbegin(); @@ -123,7 +123,7 @@ assert(carry == 0); length = i; - pbegin++; + input = input.subspan(1); } // Skip leading zeroes in base58 result. std::vector::iterator it = b58.begin() + (size - length); @@ -140,10 +140,6 @@ return str; } -std::string EncodeBase58(const std::vector &vch) { - return EncodeBase58(vch.data(), vch.data() + vch.size()); -} - bool DecodeBase58(const std::string &str, std::vector &vchRet, int max_ret_len) { if (!ValidAsCString(str)) { @@ -152,9 +148,9 @@ return DecodeBase58(str.c_str(), vchRet, max_ret_len); } -std::string EncodeBase58Check(const std::vector &vchIn) { +std::string EncodeBase58Check(Span input) { // add 4-byte hash check to the end - std::vector vch(vchIn); + std::vector vch(input.begin(), input.end()); uint256 hash = Hash(vch); vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); return EncodeBase58(vch); diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -32,10 +32,7 @@ } std::vector sourcedata = ParseHex(test[0].get_str()); std::string base58string = test[1].get_str(); - BOOST_CHECK_MESSAGE( - EncodeBase58(sourcedata.data(), - sourcedata.data() + sourcedata.size()) == base58string, - strTest); + BOOST_CHECK_MESSAGE(EncodeBase58(sourcedata) == base58string, strTest); } }