Skip to content

Commit

Permalink
refactor base58_address.[cpp/h] into base58[.cpp/h]
Browse files Browse the repository at this point in the history
Signed-off-by: russeree <git@qrsnap.io>
  • Loading branch information
russeree committed Oct 1, 2023
1 parent 0000000 commit fffffff
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 75 deletions.
2 changes: 0 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ BITCOIN_CORE_H = \
undo.h \
util/any.h \
util/asmap.h \
util/base58_address.h \
util/batchpriority.h \
util/bip32.h \
util/bitdeque.h \
Expand Down Expand Up @@ -729,7 +728,6 @@ libbitcoin_util_a_SOURCES = \
support/cleanse.cpp \
sync.cpp \
util/asmap.cpp \
util/base58_address.cpp \
util/batchpriority.cpp \
util/bip32.cpp \
util/bytevectorhash.cpp \
Expand Down
40 changes: 38 additions & 2 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#include <util/strencodings.h>
#include <util/string.h>

#include <algorithm>
#include <assert.h>
#include <string.h>

#include <limits>

/** All alphanumeric characters except for "0", "I", "O", and "l" */
const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const int8_t mapBase58[256] = {
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static const int8_t mapBase58[256] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
Expand Down Expand Up @@ -165,3 +166,38 @@ bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRe
}
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
}

std::vector<char> Base58PrefixesFromVersionByte(size_t length, unsigned char version_byte)
{
std::vector<char> base58_prefix_char_range;

if (length) {
static const unsigned char MIN_PAYLOAD_FILL = 0x00;
static const unsigned char MAX_PAYLOAD_FILL = 0xFF;
static const char ENCODED_LEADING_ZEROES = '1';

const std::vector<unsigned char> payload_range = { MIN_PAYLOAD_FILL, MAX_PAYLOAD_FILL };
std::vector<unsigned char> base58_prefix_min_max;

for (const auto& payload : payload_range) {
std::vector<unsigned char> range_test_bound(std::max(1, (int)length), payload);
range_test_bound.at(0) = version_byte;
base58_prefix_min_max.emplace_back(EncodeBase58(range_test_bound).at(0));
}

auto IsBase58Char = [&](char c) { return std::string_view(pszBase58).find_first_of(c) != std::string::npos; };

if (base58_prefix_min_max.front() == ENCODED_LEADING_ZEROES) {
base58_prefix_char_range.emplace_back(base58_prefix_min_max.front());
} else if(IsBase58Char(base58_prefix_min_max.front()) && IsBase58Char(base58_prefix_min_max.back())) {
for (int i = 1; pszBase58[i] != '\0'; i++) {
base58_prefix_char_range.emplace_back(pszBase58[i]);
}
auto start_position = std::find(base58_prefix_char_range.begin(), base58_prefix_char_range.end(), base58_prefix_min_max.front());
std::rotate(base58_prefix_char_range.begin(), start_position, base58_prefix_char_range.end());
base58_prefix_char_range.erase(++std::find(base58_prefix_char_range.begin(), base58_prefix_char_range.end(), base58_prefix_min_max.back()), base58_prefix_char_range.end());
}
}

return base58_prefix_char_range;
};
15 changes: 12 additions & 3 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include <string>
#include <vector>

extern const char* pszBase58;
extern const int8_t mapBase58[256];

/**
* Encode a byte span as a base58-encoded string
*/
Expand All @@ -44,4 +41,16 @@ std::string EncodeBase58Check(Span<const unsigned char> input);
*/
[[nodiscard]] bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len);

/** Derives the first Base58 character prefixes for a given version byte and a length
*
* @param[in] length length of pre-encoded base58 data
* @param[in] version_byte The address version byte
* @return The possible range of base58 prefixes (eg. ['m','n'])
* @code
* std::vector result = Base58PrefixesFromVersionByte(31,0x05);
* // result will be ['3']
* @endcode
*/
std::vector<char> Base58PrefixesFromVersionByte(size_t length, unsigned char version_byte);

#endif // BITCOIN_BASE58_H
1 change: 0 additions & 1 deletion src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <script/interpreter.h>
#include <script/solver.h>
#include <tinyformat.h>
#include <util/base58_address.h>
#include <util/strencodings.h>

#include <algorithm>
Expand Down
1 change: 0 additions & 1 deletion src/test/base58_prefix_decoder_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <base58.h>
#include <util/base58_address.h>
#include <boost/test/unit_test.hpp>

#include <string>
Expand Down
43 changes: 0 additions & 43 deletions src/util/base58_address.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/util/base58_address.h

This file was deleted.

0 comments on commit fffffff

Please sign in to comment.