From ffffffffff604dfaec28cbf2fce0b6d0aab1c573 Mon Sep 17 00:00:00 2001 From: russeree Date: Sat, 23 Sep 2023 19:36:21 -0700 Subject: [PATCH] refactor base58_address.[cpp/h] into base58[.cpp/h] Signed-off-by: russeree --- src/Makefile.am | 2 -- src/base58.cpp | 40 ++++++++++++++++++++-- src/base58.h | 15 +++++++-- src/key_io.cpp | 1 - src/test/base58_prefix_decoder_tests.cpp | 1 - src/util/base58_address.cpp | 43 ------------------------ src/util/base58_address.h | 23 ------------- 7 files changed, 50 insertions(+), 75 deletions(-) delete mode 100644 src/util/base58_address.cpp delete mode 100644 src/util/base58_address.h diff --git a/src/Makefile.am b/src/Makefile.am index 614c67a0def557..e542a067a476c7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ @@ -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 \ diff --git a/src/base58.cpp b/src/base58.cpp index 4efa72c441415e..0b64e55f31c38a 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -9,14 +9,15 @@ #include #include +#include #include #include #include /** 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, @@ -165,3 +166,38 @@ bool DecodeBase58Check(const std::string& str, std::vector& vchRe } return DecodeBase58Check(str.c_str(), vchRet, max_ret); } + +std::vector Base58PrefixesFromVersionByte(size_t length, unsigned char version_byte) +{ + std::vector 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 payload_range = { MIN_PAYLOAD_FILL, MAX_PAYLOAD_FILL }; + std::vector base58_prefix_min_max; + + for (const auto& payload : payload_range) { + std::vector 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; +}; diff --git a/src/base58.h b/src/base58.h index e178c108ea995d..e2ab2d8adb9693 100644 --- a/src/base58.h +++ b/src/base58.h @@ -19,9 +19,6 @@ #include #include -extern const char* pszBase58; -extern const int8_t mapBase58[256]; - /** * Encode a byte span as a base58-encoded string */ @@ -44,4 +41,16 @@ std::string EncodeBase58Check(Span input); */ [[nodiscard]] bool DecodeBase58Check(const std::string& str, std::vector& 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 Base58PrefixesFromVersionByte(size_t length, unsigned char version_byte); + #endif // BITCOIN_BASE58_H diff --git a/src/key_io.cpp b/src/key_io.cpp index 35e4d56bed892b..3f4eb999b232e9 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -9,7 +9,6 @@ #include