forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin#20832: rpc: Better error messages for invalid addresses
8f0b64f Better error messages for invalid addresses (Bezdrighin) Pull request description: This PR addresses bitcoin#20809. We add more detailed error messages in case an invalid address is provided inside the 'validateaddress' and 'getaddressinfo' RPC calls. This also covers the case when a user provides an address from a wrong network. We also add a functional test to test the new error messages. ACKs for top commit: kristapsk: ACK 8f0b64f meshcollider: Code review ACK 8f0b64f Tree-SHA512: ca0f806ab573e96b79e98d9f8c810b81fa99c638d9b5e4d99dc18c8bd2568e6a802ec305fdfb2983574a97a19a46fd53b77645f8078fb77e9deb24ad2a22cf93
- Loading branch information
1 parent
7d634a1
commit 0a8df11
Showing
7 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2020 The Bitcoin Core developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
"""Test error messages for 'getaddressinfo' and 'validateaddress' RPC commands.""" | ||
|
||
from test_framework.test_framework import BitcoinTestFramework | ||
|
||
from test_framework.util import ( | ||
assert_equal, | ||
assert_raises_rpc_error, | ||
) | ||
|
||
BECH32_VALID = 'bcrt1qtmp74ayg7p24uslctssvjm06q5phz4yrxucgnv' | ||
BECH32_INVALID_SIZE = 'bcrt1sqqpl9r5c' | ||
BECH32_INVALID_PREFIX = 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4' | ||
|
||
BASE58_VALID = 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn' | ||
BASE58_INVALID_PREFIX = '17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem' | ||
|
||
INVALID_ADDRESS = 'asfah14i8fajz0123f' | ||
|
||
class InvalidAddressErrorMessageTest(BitcoinTestFramework): | ||
def set_test_params(self): | ||
self.setup_clean_chain = True | ||
self.num_nodes = 1 | ||
|
||
def skip_test_if_missing_module(self): | ||
self.skip_if_no_wallet() | ||
|
||
def test_validateaddress(self): | ||
node = self.nodes[0] | ||
|
||
# Bech32 | ||
info = node.validateaddress(BECH32_INVALID_SIZE) | ||
assert not info['isvalid'] | ||
assert_equal(info['error'], 'Invalid Bech32 address data size') | ||
|
||
info = node.validateaddress(BECH32_INVALID_PREFIX) | ||
assert not info['isvalid'] | ||
assert_equal(info['error'], 'Invalid prefix for Bech32 address') | ||
|
||
info = node.validateaddress(BECH32_VALID) | ||
assert info['isvalid'] | ||
assert 'error' not in info | ||
|
||
# Base58 | ||
info = node.validateaddress(BASE58_INVALID_PREFIX) | ||
assert not info['isvalid'] | ||
assert_equal(info['error'], 'Invalid prefix for Base58-encoded address') | ||
|
||
info = node.validateaddress(BASE58_VALID) | ||
assert info['isvalid'] | ||
assert 'error' not in info | ||
|
||
# Invalid address format | ||
info = node.validateaddress(INVALID_ADDRESS) | ||
assert not info['isvalid'] | ||
assert_equal(info['error'], 'Invalid address format') | ||
|
||
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, "Invalid prefix for Bech32 address", 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, "Invalid address format", node.getaddressinfo, INVALID_ADDRESS) | ||
|
||
def run_test(self): | ||
self.test_validateaddress() | ||
self.test_getaddressinfo() | ||
|
||
|
||
if __name__ == '__main__': | ||
InvalidAddressErrorMessageTest().main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters