bitcoin-cli validateaddress return value #23572

issue jsarenik opened this issue on November 22, 2021
  1. jsarenik commented at 11:50 AM on November 22, 2021: none

    <!-- Describe the issue -->

    $ bitcoin-cli validateaddress invalid
    {
      "isvalid": false,
      "error": "Invalid address format"
    }
    $ echo $?
    0
    

    Expected behavior

    $ bitcoin-cli validateaddress invalid
    {
      "isvalid": false,
      "error": "Invalid address format"
    }
    $ echo $?
    1
    

    <!--- What behavior did you expect? -->

    I expect the process return value to be greater than zero and this would help a lot in scripting.

    To reproduce

    Just run the commands listed above. 100% reproducible on current master 19815b096.

  2. jsarenik added the label Bug on Nov 22, 2021
  3. MarcoFalke commented at 12:35 PM on November 22, 2021: member

    Duplicate of #18476?

  4. jsarenik commented at 9:31 PM on November 24, 2021: none

    #18476 seems similar, but this one differs in its goals. Current output for validateaddress is correct and there is no need to change it, just the returned value when the process ends would simplify the scripts which now has to process the output and decide success/failure status of the bitcoin-cli command in another step (while some other bitcoin-cli commands properly return what they should, see below).

    Successful exit status example (return = 0)

    $ bitcoin-cli getblockhash 1
    00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048
    $ echo $?
    0
    

    Error exit status example (return > 0)

    $ bitcoin-cli getblockhash 800000
    error code: -8
    error message:
    Block height out of range
    $ echo $?
    8
    
  5. w0xlt commented at 7:46 AM on November 25, 2021: contributor

    The getblockhash returns an exit status other than 0 because a block height out of range throws an exception. The bitcoin-cli client detects this exception and returns the error code. But validateaddress simply returns a JSON with the error message. It doesn't throw an exception.

    The reason is that out of range block height is not expected in normal flow. However, if you validate an address, it can be invalid, so the server simply returns that information.

    But if there is an understanding that validateaddress should throw an exception, I can open a PR to change this behavior.

  6. jsarenik commented at 8:40 AM on November 29, 2021: none

    @w0xlt Thank you for explanation. I am actually OK with it as it is. Closing the issue which was just a high-level UNIX idea from my scripting point of view.

    Example work-around:

    #!/bin/sh
    
    bitcoin-cli validateaddress $address | grep -q Invalid && {
      echo this is done when address is invalid
    }
    

    See where it is used in practice: https://github.com/jsarenik/bitcoin-faucet-shell/blob/13ac1dedc08747f79edd5f07d5037171394544b7/cgi-bin/GET.sh#L15

  7. jsarenik closed this on Nov 29, 2021

  8. DrahtBot locked this on Nov 29, 2022
Labels

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-20 21:14 UTC

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