Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2020-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test error messages for 'getaddressinfo' and 'validateaddress' RPC commands."""7from test_framework.test_framework import BitcoinTestFramework9from test_framework.util import (10assert_equal,11assert_raises_rpc_error,12)14BECH32_VALID = 'bcrt1qtmp74ayg7p24uslctssvjm06q5phz4yrxucgnv'15BECH32_VALID_UNKNOWN_WITNESS = 'bcrt1p424qxxyd0r'16BECH32_VALID_CAPITALS = 'BCRT1QPLMTZKC2XHARPPZDLNPAQL78RSHJ68U33RAH7R'17BECH32_VALID_MULTISIG = 'bcrt1qdg3myrgvzw7ml9q0ejxhlkyxm7vl9r56yzkfgvzclrf4hkpx9yfqhpsuks'19BECH32_INVALID_BECH32 = 'bcrt1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqdmchcc'20BECH32_INVALID_BECH32M = 'bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7k35mrzd'21BECH32_INVALID_VERSION = 'bcrt130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqynjegk'22BECH32_INVALID_SIZE = 'bcrt1s0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav25430mtr'23BECH32_INVALID_V0_SIZE = 'bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kqqq5k3my'24BECH32_INVALID_PREFIX = 'bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx'25BECH32_TOO_LONG = 'bcrt1q049edschfnwystcqnsvyfpj23mpsg3jcedq9xv049edschfnwystcqnsvyfpj23mpsg3jcedq9xv049edschfnwystcqnsvyfpj23m'26BECH32_ONE_ERROR = 'bcrt1q049edschfnwystcqnsvyfpj23mpsg3jcedq9xv'27BECH32_ONE_ERROR_CAPITALS = 'BCRT1QPLMTZKC2XHARPPZDLNPAQL78RSHJ68U32RAH7R'28BECH32_TWO_ERRORS = 'bcrt1qax9suht3qv95sw33xavx8crpxduefdrsvgsklu' # should be bcrt1qax9suht3qv95sw33wavx8crpxduefdrsvgsklx29BECH32_NO_SEPARATOR = 'bcrtq049ldschfnwystcqnsvyfpj23mpsg3jcedq9xv'30BECH32_INVALID_CHAR = 'bcrt1q04oldschfnwystcqnsvyfpj23mpsg3jcedq9xv'31BECH32_MULTISIG_TWO_ERRORS = 'bcrt1qdg3myrgvzw7ml8q0ejxhlkyxn7vl9r56yzkfgvzclrf4hkpx9yfqhpsuks'32BECH32_WRONG_VERSION = 'bcrt1ptmp74ayg7p24uslctssvjm06q5phz4yrxucgnv'34BASE58_VALID = 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn'35BASE58_INVALID_PREFIX = '17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem'36BASE58_INVALID_CHECKSUM = 'mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJJfn'37BASE58_INVALID_LENGTH = '2VKf7XKMrp4bVNVmuRbyCewkP8FhGLP2E54LHDPakr9Sq5mtU2'39INVALID_ADDRESS = 'asfah14i8fajz0123f'40INVALID_ADDRESS_2 = '1q049ldschfnwystcqnsvyfpj23mpsg3jcedq9xv'42class InvalidAddressErrorMessageTest(BitcoinTestFramework):43def set_test_params(self):44self.setup_clean_chain = True45self.num_nodes = 146self.uses_wallet = None48def check_valid(self, addr):49info = self.nodes[0].validateaddress(addr)50assert info['isvalid']51assert 'error' not in info52assert 'error_locations' not in info54def check_invalid(self, addr, error_str, error_locations=None):55res = self.nodes[0].validateaddress(addr)56assert not res['isvalid']57assert_equal(res['error'], error_str)58if error_locations:59assert_equal(res['error_locations'], error_locations)60else:61assert_equal(res['error_locations'], [])63def test_validateaddress(self):64# Invalid Bech3265self.check_invalid(BECH32_INVALID_SIZE, "Invalid Bech32 address program size (41 bytes)")66self.check_invalid(BECH32_INVALID_PREFIX, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.')67self.check_invalid(BECH32_INVALID_BECH32, 'Version 1+ witness address must use Bech32m checksum')68self.check_invalid(BECH32_INVALID_BECH32M, 'Version 0 witness address must use Bech32 checksum')69self.check_invalid(BECH32_INVALID_VERSION, 'Invalid Bech32 address witness version')70self.check_invalid(BECH32_INVALID_V0_SIZE, "Invalid Bech32 v0 address program size (21 bytes), per BIP141")71self.check_invalid(BECH32_TOO_LONG, 'Bech32 string too long', [90])72self.check_invalid(BECH32_ONE_ERROR, 'Invalid Bech32 checksum', [9])73self.check_invalid(BECH32_TWO_ERRORS, 'Invalid Bech32 checksum', [22, 43])74self.check_invalid(BECH32_ONE_ERROR_CAPITALS, 'Invalid Bech32 checksum', [38])75self.check_invalid(BECH32_NO_SEPARATOR, 'Missing separator')76self.check_invalid(BECH32_INVALID_CHAR, 'Invalid Base 32 character', [8])77self.check_invalid(BECH32_MULTISIG_TWO_ERRORS, 'Invalid Bech32 checksum', [19, 30])78self.check_invalid(BECH32_WRONG_VERSION, 'Invalid Bech32 checksum', [5])80# Valid Bech3281self.check_valid(BECH32_VALID)82self.check_valid(BECH32_VALID_UNKNOWN_WITNESS)83self.check_valid(BECH32_VALID_CAPITALS)84self.check_valid(BECH32_VALID_MULTISIG)86# Invalid Base5887self.check_invalid(BASE58_INVALID_PREFIX, 'Invalid or unsupported Base58-encoded address.')88self.check_invalid(BASE58_INVALID_CHECKSUM, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)')89self.check_invalid(BASE58_INVALID_LENGTH, 'Invalid checksum or length of Base58 address (P2PKH or P2SH)')91# Valid Base5892self.check_valid(BASE58_VALID)94# Invalid address format95self.check_invalid(INVALID_ADDRESS, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.')96self.check_invalid(INVALID_ADDRESS_2, 'Invalid or unsupported Segwit (Bech32) or Base58 encoding.')98node = self.nodes[0]101if not self.options.usecli:102# Missing arg returns the help text103assert_raises_rpc_error(-1, "Return information about the given bitcoin address.", node.validateaddress)104# Explicit None is not allowed for required parameters105assert_raises_rpc_error(-3, "JSON value of type null is not of expected type string", node.validateaddress, None)107def test_getaddressinfo(self):108node = self.nodes[0]110assert_raises_rpc_error(-5, "Invalid Bech32 address program size (41 bytes)", node.getaddressinfo, BECH32_INVALID_SIZE)111assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, BECH32_INVALID_PREFIX)112assert_raises_rpc_error(-5, "Invalid or unsupported Base58-encoded address.", node.getaddressinfo, BASE58_INVALID_PREFIX)113assert_raises_rpc_error(-5, "Invalid or unsupported Segwit (Bech32) or Base58 encoding.", node.getaddressinfo, INVALID_ADDRESS)114assert "isscript" not in node.getaddressinfo(BECH32_VALID_UNKNOWN_WITNESS)116def run_test(self):117self.test_validateaddress()119if self.is_wallet_compiled():120self.init_wallet(node=0)121self.test_getaddressinfo()124if __name__ == '__main__':125InvalidAddressErrorMessageTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.