From 962a0930e699b74b3c4d019427df6e2b3af5c831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A8le=20Oul=C3=A8s?= Date: Tue, 8 Nov 2022 16:24:03 +0100 Subject: [PATCH] Improve address decoding errors --- src/key_io.cpp | 10 +++++----- test/functional/rpc_invalid_address_message.py | 18 +++++++++--------- test/functional/wallet_basic.py | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/key_io.cpp b/src/key_io.cpp index 6dd6f82112c76..4659a595447e7 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -107,17 +107,17 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par 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"; + error_str = "Invalid length for Base58 address (P2PKH or P2SH)"; } else { - error_str = "Invalid prefix for Base58-encoded address"; + error_str = "Invalid or unsupported 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 = "Not a valid Bech32 or Base58 encoding"; + error_str = "Invalid or unsupported Segwit (Bech32) or Base58 encoding."; } else { - error_str = "Invalid checksum or length of Base58 address"; + error_str = "Invalid checksum or length of Base58 address (P2PKH or P2SH)"; } return CNoDestination(); } @@ -127,7 +127,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) { // Bech32 decoding if (dec.hrp != params.Bech32HRP()) { - error_str = "Invalid prefix for Bech32 address"; + error_str = strprintf("Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.Bech32HRP(), dec.hrp); return CNoDestination(); } int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16) diff --git a/test/functional/rpc_invalid_address_message.py b/test/functional/rpc_invalid_address_message.py index 0c29efb85a131..fd282a9bc15bb 100755 --- a/test/functional/rpc_invalid_address_message.py +++ b/test/functional/rpc_invalid_address_message.py @@ -64,7 +64,7 @@ def check_invalid(self, addr, error_str, error_locations=None): def test_validateaddress(self): # Invalid Bech32 self.check_invalid(BECH32_INVALID_SIZE, 'Invalid Bech32 address data size') - self.check_invalid(BECH32_INVALID_PREFIX, 'Not a valid Bech32 or Base58 encoding') + self.check_invalid(BECH32_INVALID_PREFIX, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.') 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_VERSION, 'Invalid Bech32 address witness version') @@ -84,16 +84,16 @@ def test_validateaddress(self): self.check_valid(BECH32_VALID_MULTISIG) # Invalid Base58 - self.check_invalid(BASE58_INVALID_PREFIX, 'Invalid prefix for Base58-encoded address') - self.check_invalid(BASE58_INVALID_CHECKSUM, 'Invalid checksum or length of Base58 address') - self.check_invalid(BASE58_INVALID_LENGTH, 'Invalid checksum or length of Base58 address') + self.check_invalid(BASE58_INVALID_PREFIX, 'Invalid or unsupported Base58-encoded address.') + self.check_invalid(BASE58_INVALID_CHECKSUM, '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)') # Valid Base58 self.check_valid(BASE58_VALID) # Invalid address format - self.check_invalid(INVALID_ADDRESS, 'Not a valid Bech32 or Base58 encoding') - self.check_invalid(INVALID_ADDRESS_2, 'Not a valid Bech32 or Base58 encoding') + self.check_invalid(INVALID_ADDRESS, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.') + self.check_invalid(INVALID_ADDRESS_2, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.') node = self.nodes[0] @@ -106,9 +106,9 @@ def test_getaddressinfo(self): node = self.nodes[0] assert_raises_rpc_error(-5, "Invalid Bech32 address data size", node.getaddressinfo, BECH32_INVALID_SIZE) - assert_raises_rpc_error(-5, "Not a valid Bech32 or Base58 encoding", node.getaddressinfo, BECH32_INVALID_PREFIX) - assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", node.getaddressinfo, BASE58_INVALID_PREFIX) - assert_raises_rpc_error(-5, "Not a valid Bech32 or Base58 encoding", node.getaddressinfo, INVALID_ADDRESS) + 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 Base58-encoded address.", node.getaddressinfo, BASE58_INVALID_PREFIX) + assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, INVALID_ADDRESS) def run_test(self): self.test_validateaddress() diff --git a/test/functional/wallet_basic.py b/test/functional/wallet_basic.py index 52022a2eee489..53ac01686a407 100755 --- a/test/functional/wallet_basic.py +++ b/test/functional/wallet_basic.py @@ -632,7 +632,7 @@ def run_test(self): assert_equal(total_txs, len(self.nodes[0].listtransactions("*", 99999))) # Test getaddressinfo on external address. Note that these addresses are taken from disablewallet.py - assert_raises_rpc_error(-5, "Invalid prefix for Base58-encoded address", self.nodes[0].getaddressinfo, "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy") + assert_raises_rpc_error(-5, "Invalid or unsupported Base58-encoded address.", self.nodes[0].getaddressinfo, "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy") address_info = self.nodes[0].getaddressinfo("mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ") assert_equal(address_info['address'], "mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ") assert_equal(address_info["scriptPubKey"], "76a9144e3854046c7bd1594ac904e4793b6a45b36dea0988ac")