Add more info about prefix in error message for invalid address #21755

pull ghost wants to merge 1 commits into bitcoin:master from changing 1 files +12 −2
  1. ghost commented at 3:22 PM on April 22, 2021: none

    Add info about valid prefixes from https://en.bitcoin.it/wiki/List_of_address_prefixes

    Mentioned about correct prefix for Base58 encoded address and Bech32 address for mainnet and testnet. Regtest and Signet can be ignored because the error message will become too long and both networks are mostly used by devs who are either aware of prefixes or understand errors or know the right place to ask questions if any doubt.

    We have so many prefixes and networks that ideally such error should just have one link which user can open and check all the information about correct prefixes. Although this can also work for time being.

    Fixes https://github.com/bitcoin/bitcoin/issues/21741

  2. in src/key_io.cpp:103 in 4229992061 outdated
     100 |      if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) {
     101 |          // Bech32 decoding
     102 |          error_str = "";
     103 |          if (dec.hrp != params.Bech32HRP()) {
     104 | -            error_str = "Invalid prefix for Bech32 address";
     105 | +            error_str = "Invalid prefix for Bech32 address. Valid Bech32 address starts with `bc1` (mainnet) and `tb1` (testnet)";
    


    maflcko commented at 3:34 PM on April 22, 2021:

    Why not simply print dec.hrp != params.Bech32HRP()?


    unknown commented at 4:21 PM on April 22, 2021:

    You mean use something like this?

    error_str = "Invalid prefix for Bech32 address. Details:" + dec.hrp + "!=" + params.Bech32HRP();
    

    Which will give this error if wrong prefix for a bech32 address is used:

    image

    Looks more confusing for some users.


    unknown commented at 4:26 PM on April 22, 2021:

    Maybe this works better:

    error_str = "Invalid prefix `" + dec.hrp + "` for Bech32 address. Suggested prefix: `"  + params.Bech32HRP() + "`";
    

    unknown commented at 4:35 PM on April 22, 2021:

    image


    meshcollider commented at 4:38 AM on April 27, 2021:

    'Suggested prefix' implies you (the user) can just change it, which obviously isn't the case. I think the error message does need to emphasise the fact you might be using an address from the wrong network.


    unknown commented at 8:44 PM on April 28, 2021:

    I wanted to mention "Wrong network" in this error but I am not sure if wrong network is the only reason why a prefix can be wrong and condition in if statement at L102 becomes true.


    meshcollider commented at 3:02 AM on May 6, 2021:

    @prayank23 if the decoding succeeded but the HRP is wrong then I think "Wrong network" is a valid assumption. It would either be that or the input was another type of data entirely, using the same checksum, which is much more unlikely and can be safely ignored.

  3. DrahtBot added the label Utils/log/libs on Apr 22, 2021
  4. DrahtBot commented at 9:09 PM on April 22, 2021: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  5. Add more info in error message
    + Mention prefix and network
    af9979837d
  6. ghost commented at 12:30 PM on May 12, 2021: none

    @meshcollider @MarcoFalke Let me know if this code and error message looks better

    Code: L102-L113 in https://github.com/bitcoin/bitcoin/commit/af9979837d1a9d4844d2701f4eeb7aed353d8af9

    Error:

    prayank@ubuntu:~$ bitcoin-cli getaddressinfo bc1qec3jrdfay25ny537hwfr3c7ta2zaxzhftpqz6z
    error code: -5
    error message:
    Invalid prefix 'bc' for Bech32 address on testnet
    
  7. luke-jr commented at 12:21 AM on June 11, 2021: member

    I think it's better without these details. At the very least, normal users shouldn't need to see "mainnet".

  8. brunoerg commented at 2:54 AM on June 13, 2021: contributor

    Concept ACK

    Invalid prefix 'bc' for Bech32 address on testnet seems good for me. I like the idea of mentioning the network (on testnet, for example), it clarifies the relation between the prefixes and the networks.

  9. tylerchambers commented at 5:42 PM on June 13, 2021: contributor

    LGTM. Provides more context as to what went wrong vs the previous message which was somewhat unclear.

  10. unknown closed this on Jul 12, 2021

  11. Rspigler commented at 9:28 PM on July 12, 2021: contributor

    Please mark up for grabs

  12. meshcollider added the label Up for grabs on Jan 19, 2022
  13. maflcko removed the label Up for grabs on Mar 13, 2023
  14. bitcoin locked this on Nov 11, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me