Skip to content

Commit bb4d3e9

Browse files
committed
Address review comments for Bech32 error validation
1 parent 95d19f8 commit bb4d3e9

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/bech32.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ void push_range(int from, int to, std::vector<int>& vec)
391391
}
392392
}
393393

394-
/** Return index of first invalid character in a Bech32 string. */
394+
/** Return indices of invalid characters in a Bech32 string. */
395395
bool CheckCharacters(const std::string& str, std::vector<int>& errors) {
396396
bool lower = false, upper = false;
397397
for (size_t i = 0; i < str.size(); ++i) {

src/key_io.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,20 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
102102
return ScriptHash(hash);
103103
}
104104

105-
if (!std::equal(script_prefix.begin(), script_prefix.end(), data.begin()) &&
106-
!std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
107-
error_str = "Invalid prefix for Base58-encoded address";
108-
} else {
105+
// If the prefix of data matches either the script or pubkey prefix, the length must have been wrong
106+
if ((data.size() >= script_prefix.size() &&
107+
std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) ||
108+
(data.size() >= pubkey_prefix.size() &&
109+
std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) {
109110
error_str = "Invalid length for Base58 address";
111+
} else {
112+
error_str = "Invalid prefix for Base58-encoded address";
110113
}
111114
return CNoDestination();
112115
} else if (!is_bech32) {
113116
// Try Base58 decoding without the checksum, using a much larger max length
114117
if (!DecodeBase58(str, data, 100)) {
115-
error_str = "Invalid HRP or Base58 character in address";
118+
error_str = "Not a valid Bech32 or Base58 encoding";
116119
} else {
117120
error_str = "Invalid checksum or length of Base58 address";
118121
}

test/functional/rpc_invalid_address_message.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def check_invalid(self, addr, error_str, error_locations=None):
6161
def test_validateaddress(self):
6262
# Invalid Bech32
6363
self.check_invalid(BECH32_INVALID_SIZE, 'Invalid Bech32 address data size')
64-
self.check_invalid(BECH32_INVALID_PREFIX, 'Invalid HRP or Base58 character in address')
64+
self.check_invalid(BECH32_INVALID_PREFIX, 'Not a valid Bech32 or Base58 encoding')
6565
self.check_invalid(BECH32_INVALID_BECH32, 'Version 1+ witness address must use Bech32m checksum')
6666
self.check_invalid(BECH32_INVALID_BECH32M, 'Version 0 witness address must use Bech32 checksum')
6767
self.check_invalid(BECH32_INVALID_VERSION, 'Invalid Bech32 address witness version')
@@ -89,19 +89,19 @@ def test_validateaddress(self):
8989
self.check_valid(BASE58_VALID)
9090

9191
# Invalid address format
92-
self.check_invalid(INVALID_ADDRESS, 'Invalid HRP or Base58 character in address')
93-
self.check_invalid(INVALID_ADDRESS_2, 'Invalid HRP or Base58 character in address')
92+
self.check_invalid(INVALID_ADDRESS, 'Not a valid Bech32 or Base58 encoding')
93+
self.check_invalid(INVALID_ADDRESS_2, 'Not a valid Bech32 or Base58 encoding')
9494

9595
def test_getaddressinfo(self):
9696
node = self.nodes[0]
9797

9898
assert_raises_rpc_error(-5, "Invalid Bech32 address data size", node.getaddressinfo, BECH32_INVALID_SIZE)
9999

100-
assert_raises_rpc_error(-5, "Invalid HRP or Base58 character in address", node.getaddressinfo, BECH32_INVALID_PREFIX)
100+
assert_raises_rpc_error(-5, "Not a valid Bech32 or Base58 encoding", node.getaddressinfo, BECH32_INVALID_PREFIX)
101101

102102
assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", node.getaddressinfo, BASE58_INVALID_PREFIX)
103103

104-
assert_raises_rpc_error(-5, "Invalid HRP or Base58 character in address", node.getaddressinfo, INVALID_ADDRESS)
104+
assert_raises_rpc_error(-5, "Not a valid Bech32 or Base58 encoding", node.getaddressinfo, INVALID_ADDRESS)
105105

106106
def run_test(self):
107107
self.test_validateaddress()

0 commit comments

Comments
 (0)