bitcoin-util: Add netmagic command #35610

pull ekzyis wants to merge 1 commits into bitcoin:master from ekzyis:bitcoin-util-netmagic changing 9 files +75 −0
  1. ekzyis commented at 9:20 PM on June 26, 2026: contributor

    This adds a netmagic command to bitcoin-util. It will return the network magic bytes of the selected chain:

    $ bitcoin-util netmagic
    f9beb4d9
    
    $ bitcoin-util -regtest netmagic
    fabfb5da
    
    $ bitcoin-util -testnet4 netmagic
    1c163f28
    
    $ bitcoin-util -signet netmagic
    0a03cf40
    
    # default challenge
    $ bitcoin-util -signet -signetchallenge=512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae netmagic
    0a03cf40
    
    $ bitcoin-util -signet -signetchallenge=51 netmagic
    54d26fbd
    

    This will be particularly useful for #34566 to determine the datadir path of a custom signet, before starting bitcoind, since #34566 will add the network magic as a suffix. This was mentioned in #34566 (comment). It uses the same code from init.cpp to print the signet derived magic:

    https://github.com/bitcoin/bitcoin/blob/ea9afb61a1c52979d0e64e4dae9b3a611f7b9a5e/src/init.cpp#L967-L969

    Since it does not depend on #34566, and the changes are quite simple, I created a separate PR for this for easier review and discussion.

    I have tested this by invoking the command with the options above.

  2. DrahtBot commented at 9:21 PM on June 26, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35610.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK stickies-v, sedited
    Stale ACK pablomartin4btc, willcl-ark, pinheadmz

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. sedited commented at 9:24 PM on June 26, 2026: contributor

    Concept ACK

  4. pinheadmz commented at 9:34 PM on June 26, 2026: member

    Concept ACK

  5. in src/bitcoin-util.cpp:156 in 7806c62429
     150 | @@ -150,6 +151,17 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
     151 |      return EXIT_SUCCESS;
     152 |  }
     153 |  
     154 | +static int NetMagic(const std::vector<std::string>& args, std::string& strPrint)
     155 | +{
     156 | +    if (args.size() != 0) {
    


    pablomartin4btc commented at 9:54 PM on June 26, 2026:
        if (!args.empty()) {
    

    ekzyis commented at 10:30 PM on June 26, 2026:

    done in eed9f0edde0

  6. pablomartin4btc commented at 9:54 PM on June 26, 2026: member

    ACK 7806c624293f12a85838b1163c140ae680fa9d1e

    <details> <summary>Perhaps you can add some tests in <code>test/functional/data/util/bitcoin-util-test.json</code> (read from <code>test/functional/tool_utils.py</code>)...</summary>

    e.g.

      { "exec": "./bitcoin-util",   
        "args": ["netmagic"],
        "output_cmp": "netmagic-mainnet.hex",
        "description": "netmagic returns mainnet magic bytes"
      },
      { "exec": "./bitcoin-util",
        "args": ["-regtest", "netmagic"],
        "output_cmp": "netmagic-regtest.hex",
        "description": "netmagic returns regtest magic bytes"
      },
      { "exec": "./bitcoin-util",
        "args": ["netmagic", "extra_arg"],
        "return_code": 1,
        "error_txt": "netmagic does not take arguments",
        "description": "netmagic rejects extra arguments"
      }
    

    The small .hex files should be in test/functional/data/util/ as well.

    </details>

  7. DrahtBot requested review from sedited on Jun 26, 2026
  8. DrahtBot requested review from pinheadmz on Jun 26, 2026
  9. ekzyis force-pushed on Jun 26, 2026
  10. ekzyis force-pushed on Jun 26, 2026
  11. DrahtBot added the label CI failed on Jun 26, 2026
  12. ekzyis commented at 10:32 PM on June 26, 2026: contributor

    Perhaps you can add some tests

    Ohh, right, thanks! Added tests in eed9f0edde0

  13. DrahtBot removed the label CI failed on Jun 26, 2026
  14. pablomartin4btc commented at 9:38 PM on June 27, 2026: member

    ACK eed9f0edde0a106057db0d790dc75e0f19a99b4d

  15. willcl-ark approved
  16. willcl-ark commented at 12:36 PM on June 29, 2026: member

    ACK eed9f0edde0a106057db0d790dc75e0f19a99b4d

    seems useful and simple enough to add.

  17. in test/functional/data/util/bitcoin-util-test.json:36 in eed9f0edde outdated
      28 | @@ -29,6 +29,55 @@
      29 |      "error_txt": "Could not decode block header",
      30 |      "description": ""
      31 |    },
      32 | +  {
      33 | +    "exec": "./bitcoin-util",
      34 | +    "args": ["netmagic"],
      35 | +    "return_code": 0,
      36 | +    "output_cmp": "netmagic-mainnet.hex",
    


    pinheadmz commented at 7:04 PM on June 29, 2026:

    Any idea why we save all the expected test vectors in separate files instead of hard-coding them here?


    ekzyis commented at 10:19 PM on June 29, 2026:

    My guess would be that it's easier to compare binary data when it's written to a file instead of embedding it in JSON

  18. pinheadmz approved
  19. pinheadmz commented at 7:11 PM on June 29, 2026: member

    ACK eed9f0edde0a106057db0d790dc75e0f19a99b4d

    Very simple utility command that will be super helpful for #34566

    Built on macos/arm64 and tested it out with a few custom signets.

    I think there could be some followups to this util as well. For example, the help response includes the option -signetseednode which isn't used and might be confusing. I also wonder if the util could work like bitcoin-cli and read bitcoin.conf for configuration setting like signet=1 and signetchallenge

    <details><summary>Show Signature</summary>

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA256
    
    ACK eed9f0edde0a106057db0d790dc75e0f19a99b4d
    -----BEGIN PGP SIGNATURE-----
    
    iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmpCwewbFIAAAAAABAAO
    bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick630UP/jnRpr2ppKgVQjArulzJ
    ZQ7xtuzhp5yC/2Lc0j/wB0Mkc6JMtGqjSreC3x4VcjE+CRTnHn+tZhQHyYttMGrv
    0A4nvMnkOMuQjZ2sB4KKUYEU8sz7JVHKYEtYLmMl6+lNMhjQx6eV5WkJJGwKgm7f
    THdWCvvNmCNBPDftZAIZDdIKjNCRF3IJjsESGMfMCdhUzlkG/6o5TzlC3hna0VFu
    VkS+UkR0Wwgjti2N6mBzagRj1MxFBOx9czUCf+3QBkZoX95sPyzJTujqAsIl1G4z
    kW0FxYscG9D4icUUQRK/AvZk/TMTtn2QESHZoxZMOfNy1VvKxinVgo2z+ydWgWwG
    05XLo/se8mi24rU0BcNn0xGc83DM2kuhp3ViNHdIlQxV35M2C0gX5+zIXfnCP4To
    umZuZySTvJHUQsaH/VSwd/sajZSo9I4QpWwlntjji29+1yc+KsdEbJK4cA/vsjyB
    zrKtsocUimWjO9eHUznJX4yFjYcVHTCQUKaZX5btScnx54XMWsOjcEggXPCthDvh
    rFitLwwDapFayF9q9Ny9h4k6iCr04E708T62zNvs3iIb/wNi3Jx4zRc0VNb4MJYD
    XrQrUv9q53jCcQAQ2Olo26VyftlqEAQtdxy7IEUJ1rY/R2z8jDvvEw4jrkNUY56J
    vMiIfObno8yux7XiCjSWOM27
    =LAnr
    -----END PGP SIGNATURE-----
    

    pinheadmz's public key is on openpgp.org

    </details>

  20. sedited commented at 8:49 PM on June 29, 2026: contributor

    Can you add a release note?

  21. DrahtBot requested review from sedited on Jun 29, 2026
  22. bitcoin-util: Add netmagic command a318f43254
  23. ekzyis force-pushed on Jun 29, 2026
  24. ekzyis commented at 10:35 PM on June 29, 2026: contributor

    a318f43254 added release note + rebased on master (3c76bd4356)

    The release note isn't very detailed, but I think the command doesn't need much explanation.

  25. stickies-v approved
  26. stickies-v commented at 10:46 AM on June 30, 2026: contributor

    ACK a318f43254c36532a1991ec708af5af7a9fb7c69

    Composable small utility that gets the job done with minimal overhead. Nice.

  27. DrahtBot requested review from pablomartin4btc on Jun 30, 2026
  28. DrahtBot requested review from willcl-ark on Jun 30, 2026
  29. DrahtBot requested review from pinheadmz on Jun 30, 2026
  30. sedited approved
  31. sedited commented at 10:58 AM on June 30, 2026: contributor

    ACK a318f43254c36532a1991ec708af5af7a9fb7c69

  32. sedited merged this on Jun 30, 2026
  33. sedited closed this on Jun 30, 2026

  34. ekzyis deleted the branch on Jun 30, 2026

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-07-04 04:50 UTC

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