diff options
author | Samuel Dobson <dobsonsa68@gmail.com> | 2021-11-23 11:13:24 +1300 |
---|---|---|
committer | Samuel Dobson <dobsonsa68@gmail.com> | 2021-11-23 15:48:59 +1300 |
commit | bb4d3e9b970be2a8de3e146623801fc8cbbeb0c7 (patch) | |
tree | a7ba4cd8be2afaeeff6abef8ef88f8537ca69020 /src | |
parent | 95d19f8c1a40a7531d2bb00febd245d127293a64 (diff) | |
download | bitcoin-bb4d3e9b970be2a8de3e146623801fc8cbbeb0c7.tar.xz |
Address review comments for Bech32 error validation
Diffstat (limited to 'src')
-rw-r--r-- | src/bech32.cpp | 2 | ||||
-rw-r--r-- | src/key_io.cpp | 13 |
2 files changed, 9 insertions, 6 deletions
diff --git a/src/bech32.cpp b/src/bech32.cpp index ea44480a6c..ec69965d07 100644 --- a/src/bech32.cpp +++ b/src/bech32.cpp @@ -391,7 +391,7 @@ void push_range(int from, int to, std::vector<int>& vec) } } -/** Return index of first invalid character in a Bech32 string. */ +/** Return indices of invalid characters in a Bech32 string. */ bool CheckCharacters(const std::string& str, std::vector<int>& errors) { bool lower = false, upper = false; for (size_t i = 0; i < str.size(); ++i) { diff --git a/src/key_io.cpp b/src/key_io.cpp index 6908c5ea52..c89493e29d 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -102,17 +102,20 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par return ScriptHash(hash); } - if (!std::equal(script_prefix.begin(), script_prefix.end(), data.begin()) && - !std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) { - error_str = "Invalid prefix for Base58-encoded address"; - } else { + // If the prefix of data matches either the script or pubkey prefix, the length must have been wrong + if ((data.size() >= script_prefix.size() && + std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) || + (data.size() >= pubkey_prefix.size() && + std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) { error_str = "Invalid length for Base58 address"; + } else { + error_str = "Invalid prefix for Base58-encoded address"; } return CNoDestination(); } else if (!is_bech32) { // Try Base58 decoding without the checksum, using a much larger max length if (!DecodeBase58(str, data, 100)) { - error_str = "Invalid HRP or Base58 character in address"; + error_str = "Not a valid Bech32 or Base58 encoding"; } else { error_str = "Invalid checksum or length of Base58 address"; } |