Base58 decoding logic + bech32 decoding network awareness

This commit is contained in:
Reese Russell 2025-02-23 06:03:00 +00:00
parent 3fda88d78e
commit 6e7cf4c583
4 changed files with 73 additions and 60 deletions

View file

@ -83,14 +83,22 @@ public:
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str, std::vector<int>* error_locations) CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, std::string& error_str, std::vector<int>* error_locations)
{ {
std::vector<unsigned char> data;
uint160 hash;
error_str = ""; error_str = "";
// Note this will be false if it is a valid Bech32 address for a different network static const uint8_t MAX_BASE58_CHARS = 100;
bool is_bech32 = (ToLower(str.substr(0, params.Bech32HRP().size())) == params.Bech32HRP()); static const uint8_t MAX_BASE58_CHECK_CHARS = 21;
if (!is_bech32 && DecodeBase58Check(str, data, 21)) { std::vector<unsigned char> data, bech32_data;
auto [bech32_encoding, bech32_hrp, bech32_chars] = bech32::Decode(str);
auto [bech32_error, bech32_error_loc] = bech32::LocateErrors(str);
bool is_bech32 = bech32_encoding != bech32::Encoding::INVALID;
auto check_base58 = [&]() { return DecodeBase58Check(str, data, MAX_BASE58_CHECK_CHARS); };
if (!is_bech32 && check_base58()) {
uint160 hash;
// base58-encoded Bitcoin addresses. // base58-encoded Bitcoin addresses.
// Public-key-hash-addresses have version 0 (or 111 testnet). // Public-key-hash-addresses have version 0 (or 111 testnet).
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key. // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
@ -114,16 +122,41 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) { std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) {
error_str = "Invalid length for Base58 address (P2PKH or P2SH)"; error_str = "Invalid length for Base58 address (P2PKH or P2SH)";
} else { } else {
error_str = "Invalid or unsupported Base58-encoded address."; std::vector<std::string_view> encoded_prefixes;
const std::vector<std::string_view>& pubkey_prefixes = params.Base58EncodedPrefix(CChainParams::PUBKEY_ADDRESS);
const std::vector<std::string_view>& script_prefixes = params.Base58EncodedPrefix(CChainParams::SCRIPT_ADDRESS);
encoded_prefixes.insert(encoded_prefixes.end(), script_prefixes.begin(), script_prefixes.end());
encoded_prefixes.insert(encoded_prefixes.end(), pubkey_prefixes.begin(), pubkey_prefixes.end());
std::string base58_address_prefixes;
for (size_t i = 0; i < encoded_prefixes.size(); ++i) {
if (i > 0) {
base58_address_prefixes += (i == encoded_prefixes.size() - 1) ? ", or " : ", ";
}
base58_address_prefixes += std::string(encoded_prefixes[i]);
}
error_str = strprintf("Invalid Base58 %s address. Expected prefix %s", params.GetChainTypeDisplayString(), base58_address_prefixes);
} }
return CNoDestination(); return CNoDestination();
} else if (!is_bech32) { } else if (!is_bech32) {
bool is_base58 = DecodeBase58(str, data, MAX_BASE58_CHARS);
// Try Base58 decoding without the checksum, using a much larger max length // Try Base58 decoding without the checksum, using a much larger max length
if (!DecodeBase58(str, data, 100)) { if (!is_base58) {
error_str = "Invalid or unsupported Segwit (Bech32) or Base58 encoding."; // If bech32 decoding failed due to invalid base32 chars, address format is ambiguous; otherwise, report bech32 error
bool is_validBech32Chars = (bech32_error != "Invalid Base 32 character");
error_str = is_validBech32Chars ? "Bech32(m) address decoded with error: " + bech32_error : "Address is not valid Base58 or Bech32";
} else { } else {
if (bech32_error == "Invalid character or mixed case") {
error_str = "Invalid checksum or length of Base58 address (P2PKH or P2SH)";
}
// This covers the case where an address is encoded as valid base58 and invalid bech32(m) due to a non base32 error
error_str = "Invalid checksum or length of Base58 address (P2PKH or P2SH)"; error_str = "Invalid checksum or length of Base58 address (P2PKH or P2SH)";
} }
if (error_locations) {
*error_locations = std::move(bech32_error_loc);
}
return CNoDestination(); return CNoDestination();
} }
@ -136,7 +169,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
} }
// Bech32 decoding // Bech32 decoding
if (dec.hrp != params.Bech32HRP()) { if (dec.hrp != params.Bech32HRP()) {
error_str = strprintf("Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.Bech32HRP(), dec.hrp); error_str = strprintf("Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s)", params.Bech32HRP(), dec.hrp);
return CNoDestination(); return CNoDestination();
} }
int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16) int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)

View file

@ -65,19 +65,19 @@ class InvalidAddressErrorMessageTest(BitcoinTestFramework):
def test_validateaddress(self): def test_validateaddress(self):
# Invalid Bech32 # Invalid Bech32
self.check_invalid(BECH32_INVALID_SIZE, "Invalid Bech32 address program size (41 bytes)") self.check_invalid(BECH32_INVALID_SIZE, "Invalid Bech32 address program size (41 bytes)")
self.check_invalid(BECH32_INVALID_PREFIX, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.') self.check_invalid(BECH32_INVALID_PREFIX, 'Invalid or unsupported prefix for Segwit (Bech32) address (expected bcrt, got bc)')
self.check_invalid(BECH32_INVALID_BECH32, 'Version 1+ witness address must use Bech32m checksum') self.check_invalid(BECH32_INVALID_BECH32, 'Version 1+ witness address must use Bech32m checksum')
self.check_invalid(BECH32_INVALID_BECH32M, 'Version 0 witness address must use Bech32 checksum') self.check_invalid(BECH32_INVALID_BECH32M, 'Version 0 witness address must use Bech32 checksum')
self.check_invalid(BECH32_INVALID_VERSION, 'Invalid Bech32 address witness version') self.check_invalid(BECH32_INVALID_VERSION, 'Invalid Bech32 address witness version')
self.check_invalid(BECH32_INVALID_V0_SIZE, "Invalid Bech32 v0 address program size (21 bytes), per BIP141") self.check_invalid(BECH32_INVALID_V0_SIZE, "Invalid Bech32 v0 address program size (21 bytes), per BIP141")
self.check_invalid(BECH32_TOO_LONG, 'Bech32 string too long', list(range(90, 108))) self.check_invalid(BECH32_TOO_LONG, 'Bech32(m) address decoded with error: Bech32 string too long', list(range(90, 108)))
self.check_invalid(BECH32_ONE_ERROR, 'Invalid Bech32 checksum', [9]) self.check_invalid(BECH32_ONE_ERROR, 'Bech32(m) address decoded with error: Invalid Bech32 checksum', [9])
self.check_invalid(BECH32_TWO_ERRORS, 'Invalid Bech32 checksum', [22, 43]) self.check_invalid(BECH32_TWO_ERRORS, 'Bech32(m) address decoded with error: Invalid Bech32 checksum', [22, 43])
self.check_invalid(BECH32_ONE_ERROR_CAPITALS, 'Invalid Bech32 checksum', [38]) self.check_invalid(BECH32_ONE_ERROR_CAPITALS, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)', [38])
self.check_invalid(BECH32_NO_SEPARATOR, 'Missing separator') self.check_invalid(BECH32_NO_SEPARATOR, 'Bech32(m) address decoded with error: Missing separator')
self.check_invalid(BECH32_INVALID_CHAR, 'Invalid Base 32 character', [8]) self.check_invalid(BECH32_INVALID_CHAR, 'Address is not valid Base58 or Bech32', [8])
self.check_invalid(BECH32_MULTISIG_TWO_ERRORS, 'Invalid Bech32 checksum', [19, 30]) self.check_invalid(BECH32_MULTISIG_TWO_ERRORS, 'Bech32(m) address decoded with error: Invalid Bech32 checksum', [19, 30])
self.check_invalid(BECH32_WRONG_VERSION, 'Invalid Bech32 checksum', [5]) self.check_invalid(BECH32_WRONG_VERSION, 'Bech32(m) address decoded with error: Invalid Bech32 checksum', [5])
# Valid Bech32 # Valid Bech32
self.check_valid(BECH32_VALID) self.check_valid(BECH32_VALID)
@ -86,16 +86,16 @@ class InvalidAddressErrorMessageTest(BitcoinTestFramework):
self.check_valid(BECH32_VALID_MULTISIG) self.check_valid(BECH32_VALID_MULTISIG)
# Invalid Base58 # Invalid Base58
self.check_invalid(BASE58_INVALID_PREFIX, 'Invalid or unsupported Base58-encoded address.') self.check_invalid(BASE58_INVALID_PREFIX, 'Invalid Base58 regtest address. Expected prefix 2, m, or n')
self.check_invalid(BASE58_INVALID_CHECKSUM, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)') self.check_invalid(BASE58_INVALID_CHECKSUM, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)', [4, 6, 10, 12, 16, 25, 29, 30, 31])
self.check_invalid(BASE58_INVALID_LENGTH, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)') self.check_invalid(BASE58_INVALID_LENGTH, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)', [3, 8, 9, 11, 15, 16, 18, 19, 21, 22, 23, 27, 39, 40, 41, 44, 46, 47])
# Valid Base58 # Valid Base58
self.check_valid(BASE58_VALID) self.check_valid(BASE58_VALID)
# Invalid address format # Invalid address format
self.check_invalid(INVALID_ADDRESS, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.') self.check_invalid(INVALID_ADDRESS, 'Bech32(m) address decoded with error: Invalid separator position' , [14])
self.check_invalid(INVALID_ADDRESS_2, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.') self.check_invalid(INVALID_ADDRESS_2, 'Bech32(m) address decoded with error: Invalid separator position', [0])
node = self.nodes[0] node = self.nodes[0]
@ -108,9 +108,9 @@ class InvalidAddressErrorMessageTest(BitcoinTestFramework):
node = self.nodes[0] node = self.nodes[0]
assert_raises_rpc_error(-5, "Invalid Bech32 address program size (41 bytes)", node.getaddressinfo, BECH32_INVALID_SIZE) assert_raises_rpc_error(-5, "Invalid Bech32 address program size (41 bytes)", node.getaddressinfo, BECH32_INVALID_SIZE)
assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, BECH32_INVALID_PREFIX) assert_raises_rpc_error(-5, "Invalid or unsupported prefix for Segwit (Bech32) address (expected bcrt, got bc)", node.getaddressinfo, BECH32_INVALID_PREFIX)
assert_raises_rpc_error(-5, "Invalid or unsupported Base58-encoded address.", node.getaddressinfo, BASE58_INVALID_PREFIX) assert_raises_rpc_error(-5, "Invalid Base58 regtest address. Expected prefix 2, m, or n", node.getaddressinfo, BASE58_INVALID_PREFIX)
assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, INVALID_ADDRESS) assert_raises_rpc_error(-5, "Bech32(m) address decoded with error: Invalid separator position", node.getaddressinfo, INVALID_ADDRESS)
assert "isscript" not in node.getaddressinfo(BECH32_VALID_UNKNOWN_WITNESS) assert "isscript" not in node.getaddressinfo(BECH32_VALID_UNKNOWN_WITNESS)
def run_test(self): def run_test(self):

View file

@ -12,10 +12,10 @@ INVALID_DATA = [
# BIP 173 # BIP 173
( (
"tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty", "tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # Invalid hrp "Invalid or unsupported prefix for Segwit (Bech32) address (expected bc, got tc)", # Invalid hrp
[], [],
), ),
("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", "Invalid Bech32 checksum", [41]), ("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", "Bech32(m) address decoded with error: Invalid Bech32 checksum", [41]),
( (
"BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2", "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2",
"Version 1+ witness address must use Bech32m checksum", "Version 1+ witness address must use Bech32m checksum",
@ -38,12 +38,12 @@ INVALID_DATA = [
), ),
( (
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7", "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # tb1, Mixed case "Bech32(m) address decoded with error: Invalid character or mixed case", # tb1, Mixed case
[], [58],
), ),
( (
"BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3t4", "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3t4",
"Invalid character or mixed case", # bc1, Mixed case, not in BIP 173 test vectors "Bech32(m) address decoded with error: Invalid character or mixed case", # bc1, Mixed case, not in BIP 173 test vectors
[40], [40],
), ),
( (
@ -52,15 +52,15 @@ INVALID_DATA = [
[], [],
), ),
( (
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv", "bc1qfysxzmfq2phhyarvv9hxgtjgdajxcw3fpkpph893lw",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # tb1, Non-zero padding in 8-to-5 conversion "Invalid padding in Bech32 data section", # tb1, Non-zero padding in 8-to-5 conversion
[], [],
), ),
("bc1gmk9yu", "Empty Bech32 data section", []), ("bc1gmk9yu", "Empty Bech32 data section", []),
# BIP 350 # BIP 350
( (
"tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut", "tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # Invalid human-readable part "Invalid or unsupported prefix for Segwit (Bech32) address (expected bc, got tc)", # Invalid human-readable part
[], [],
), ),
( (
@ -69,29 +69,19 @@ INVALID_DATA = [
[], [],
), ),
( (
"tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf", "BC1PFYSXZMFQ2PHHYARVV9HXGTJGFAZYCWJYPQQQF9ZLQ6",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # tb1, Invalid checksum (Bech32 instead of Bech32m)
[],
),
(
"BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL",
"Version 1+ witness address must use Bech32m checksum", # Invalid checksum (Bech32 instead of Bech32m) "Version 1+ witness address must use Bech32m checksum", # Invalid checksum (Bech32 instead of Bech32m)
[], [],
), ),
( (
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh", "bc1qfysxzmfq2phhyarvv9hxgtjgfazycwjypqqqfm706a",
"Version 0 witness address must use Bech32 checksum", # Invalid checksum (Bech32m instead of Bech32) "Version 0 witness address must use Bech32 checksum", # Invalid checksum (Bech32m instead of Bech32)
[], [],
), ),
( (
"tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47", "Address is not valid Base58 or Bech32 ",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # tb1, Invalid checksum (Bech32m instead of Bech32) "Bech32(m) address decoded with error: Invalid character or mixed case", # Invalid character in checksum
[], [x for x in range(1, 21)] + [22, 23, 24] + [27, 28, 29, 30] + [32, 33, 34] + [37],
),
(
"bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4",
"Invalid Base 32 character", # Invalid character in checksum
[59],
), ),
( (
"BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R", "BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R",
@ -109,21 +99,11 @@ INVALID_DATA = [
"Invalid Bech32 v0 address program size (16 bytes), per BIP141", "Invalid Bech32 v0 address program size (16 bytes), per BIP141",
[], [],
), ),
(
"tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # tb1, Mixed case
[],
),
( (
"bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf", "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf",
"Invalid padding in Bech32 data section", # zero padding of more than 4 bits "Invalid padding in Bech32 data section", # zero padding of more than 4 bits
[], [],
), ),
(
"tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j",
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.", # tb1, Non-zero padding in 8-to-5 conversion
[],
),
("bc1gmk9yu", "Empty Bech32 data section", []), ("bc1gmk9yu", "Empty Bech32 data section", []),
] ]
VALID_DATA = [ VALID_DATA = [

View file

@ -650,7 +650,7 @@ class WalletTest(BitcoinTestFramework):
assert_equal(total_txs, len(self.nodes[0].listtransactions("*", 99999))) assert_equal(total_txs, len(self.nodes[0].listtransactions("*", 99999)))
# Test getaddressinfo on external address. Note that these addresses are taken from disablewallet.py # Test getaddressinfo on external address. Note that these addresses are taken from disablewallet.py
assert_raises_rpc_error(-5, "Invalid or unsupported Base58-encoded address.", self.nodes[0].getaddressinfo, "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy") assert_raises_rpc_error(-5, "Invalid Base58 regtest address", self.nodes[0].getaddressinfo, "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
address_info = self.nodes[0].getaddressinfo("mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ") address_info = self.nodes[0].getaddressinfo("mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ")
assert_equal(address_info['address'], "mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ") assert_equal(address_info['address'], "mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ")
assert_equal(address_info["scriptPubKey"], "76a9144e3854046c7bd1594ac904e4793b6a45b36dea0988ac") assert_equal(address_info["scriptPubKey"], "76a9144e3854046c7bd1594ac904e4793b6a45b36dea0988ac")