Skip to content

Add testnet4 network support (Bitcoin Core 28.0+)#109

Closed
al-munazzim wants to merge 1 commit into
diybitcoinhardware:masterfrom
al-munazzim:testnet4-support
Closed

Add testnet4 network support (Bitcoin Core 28.0+)#109
al-munazzim wants to merge 1 commit into
diybitcoinhardware:masterfrom
al-munazzim:testnet4-support

Conversation

@al-munazzim
Copy link
Copy Markdown

Summary

Bitcoin Core 28.0+ introduces testnet4 with chain name testnet4 (returned by getblockchaininfo). This PR adds proper support for testnet4.

Changes

1. Add testnet4 to networks.py

Added testnet4 network configuration with the same address parameters as testnet3:

  • bech32 prefix: tb
  • BIP32 prefixes: tpub/tprv
  • Same WIF, P2PKH, P2SH prefixes as testnet

2. Fix liquid/networks.py get_network fallback behavior

Previously, get_network('testnet4') would silently fall back to elementsregtest (bech32 prefix ert), causing applications to generate Liquid addresses instead of testnet addresses.

Now the function checks Bitcoin networks before falling back:

  1. Check liquid-specific networks (liquidv1, elementsregtest, liquidtestnet)
  2. Check Bitcoin networks (main, test, regtest, signet, testnet4)
  3. Only fall back to elementsregtest for truly unknown networks

3. Add comprehensive tests

New test_networks.py with 8 tests covering:

  • testnet4 existence and parameters
  • Comparison with testnet3 parameters
  • get_network() behavior for Bitcoin networks
  • get_network() behavior for liquid networks
  • Fallback behavior for unknown networks

Testing

cd /tmp/embit && PYTHONPATH=src python3 -m unittest tests.tests.test_networks -v

All 8 new tests pass. Existing liquid tests also pass.

Fixes #108

- Add testnet4 to NETWORKS dict with same parameters as testnet3
  (tb1 addresses, tpub/tprv, same WIF prefix)
- Fix liquid.networks.get_network() to check Bitcoin networks before
  falling back to elementsregtest
- Add comprehensive tests for testnet4 and get_network() behavior

Fixes diybitcoinhardware#108
@al-munazzim
Copy link
Copy Markdown
Author

Related: This PR addresses the root cause of the issue being worked around in cryptoadvance/specter-desktop#2537.

specter-desktop currently has to manually map testnet4test before calling embit's get_network() because embit doesn't recognize testnet4. Once this PR is merged and released, that workaround in specter-desktop can be removed.

@k9ert
Copy link
Copy Markdown
Contributor

k9ert commented Feb 7, 2026

Some feedback copied from internal chat:
• In Liquid there is no testnet4
• Fallback to Bitcoin networks makes no sense for Liquid
• The reference project isn’t Bitcoin Core but Elements

Please take this into account and review the PR with that in mind.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class support for Bitcoin Core 28.0+ testnet4 chain naming and ensures Liquid’s get_network() resolves testnet4 (and other Bitcoin networks) to Bitcoin network parameters rather than falling back to Liquid regtest defaults.

Changes:

  • Added a testnet4 entry to embit.networks.NETWORKS (matching existing testnet parameters, with its own display name).
  • Adjusted embit.liquid.networks.get_network() lookup logic/documentation around Bitcoin-network fallback.
  • Added a new test module validating testnet4 parameters and liquid.networks.get_network() behavior, including fallback behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/tests/test_networks.py Adds unit tests for testnet4 and Liquid/Bitcoin network resolution behavior.
src/embit/networks.py Introduces the testnet4 network configuration (Bitcoin Core 28.0+ chain name).
src/embit/liquid/networks.py Updates get_network() resolution order to check Bitcoin networks before falling back.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +6 to +8
class NetworksTest(TestCase):
"""Tests for network configuration."""

Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new tests will be collected by pytest, but they won't run in the MicroPython test flow (cd tests && micropython ./run_tests.py) because tests/tests/__init__.py explicitly imports the test modules to execute and currently doesn't import test_networks. If MicroPython support is in scope for this change, add from .test_networks import * (or equivalent) to tests/tests/__init__.py so this coverage runs there too.

Copilot uses AI. Check for mistakes.
Comment on lines 14 to +20
if name in NETWORKS:
return NETWORKS[name]
else:
return NETWORKS["elementsregtest"]
# Check if it's a known Bitcoin network (added after NETWORKS is defined)
# This handles testnet4 and other Bitcoin networks correctly
if name in networks.NETWORKS:
return networks.NETWORKS[name]
# Only fall back to elementsregtest for unknown networks
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this module, NETWORKS is later populated with all Bitcoin networks via NETWORKS.update(networks.NETWORKS) (see bottom of file). Because of that, the if name in networks.NETWORKS: branch here is effectively dead code: any key in networks.NETWORKS will already be in NETWORKS, so the earlier if name in NETWORKS: would have returned. Consider either removing the redundant second lookup (and updating the docstring/comments), or removing the NETWORKS.update(...) merge and keeping explicit two-stage lookup to match the described behavior.

Copilot uses AI. Check for mistakes.
@al-munazzim
Copy link
Copy Markdown
Author

Closing to recreate against develop branch. Will also fix: testnet4 doesn't exist in Liquid - moving that logic to Bitcoin networks only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_network() doesn't recognize testnet4 chain (Bitcoin Core 28.0+)

3 participants