Skip to content

feat: implement xpub network consistency check#848

Merged
apoelstra merged 1 commit into
rust-bitcoin:masterfrom
nseguias:feat/xpub-network-consistency
Aug 1, 2025
Merged

feat: implement xpub network consistency check#848
apoelstra merged 1 commit into
rust-bitcoin:masterfrom
nseguias:feat/xpub-network-consistency

Conversation

@nseguias

Copy link
Copy Markdown
Contributor

Description

This PR addresses issue #844 by implementing a network consistency check for extended public keys (xpubs) in descriptors. The change introduces an XKeyNetwork enum and a xkey_network() method on Descriptor<DescriptorPublicKey> and Descriptor<DefiniteDescriptorKey> to:

  • Detect the presence of xpubs.
  • Identify if they belong to a single network (e.g., mainnet or testnet).
  • Flag cases where xpubs have mixed networks, which could lead to unsafe usage (e.g., sending mainnet funds to testnet addresses).

The implementation iterates over keys to determine consistency, returning XKeyNetwork::NoXKeys, XKeyNetwork::Mixed, or XKeyNetwork::Single(Network).

Changes

  • Added XKeyNetwork enum to represent network states.
  • Implemented xkey_network() method.

Testing

  • Added unit tests to verify behavior with single-network, mixed-network, and no-xpub descriptors.
  • Tested against existing descriptor examples to ensure compatibility.

Notes

  • This is a safety feature and does not enforce network consistency at parse time by default.

Related Issues

@apoelstra

Copy link
Copy Markdown
Member

Nice! In general we prefer the detailed messages to be on both the PR description and the commit message. For a single-commit PR it's fine. (And you could've reduced the detail, but everything here is somewhat informative even if it's wordy and has a bunch of unnecessary headings.)

@apoelstra apoelstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 497e994; successfully ran local tests

@nseguias

nseguias commented Aug 1, 2025

Copy link
Copy Markdown
Contributor Author

Noted! Thanks for reviewing & testing

@apoelstra apoelstra merged commit a1408ef into rust-bitcoin:master Aug 1, 2025
31 checks passed
heap-coder added a commit to heap-coder/rust-miniscript that referenced this pull request Sep 27, 2025
…consistency check

497e9943d766eaeae70f57e27514c8310d03775c feat: implement xpub network consistency check (nahem)

Pull request description:

  ## Description
  This PR addresses issue #844 by implementing a network consistency check for extended public keys (xpubs) in descriptors. The change introduces an `XKeyNetwork` enum and a `xkey_network()` method on `Descriptor<DescriptorPublicKey>` and `Descriptor<DefiniteDescriptorKey>` to:
  - Detect the presence of xpubs.
  - Identify if they belong to a single network (e.g., mainnet or testnet).
  - Flag cases where xpubs have mixed networks, which could lead to unsafe usage (e.g., sending mainnet funds to testnet addresses).
  
  The implementation iterates over keys to determine consistency, returning `XKeyNetwork::NoXKeys`, `XKeyNetwork::Mixed`, or `XKeyNetwork::Single(Network)`.
  
  ## Changes
  - Added `XKeyNetwork` enum to represent network states.
  - Implemented `xkey_network()` method.
  
  ## Testing
  - Added unit tests to verify behavior with single-network, mixed-network, and no-xpub descriptors.
  - Tested against existing descriptor examples to ensure compatibility.
  
  ## Notes
  - This is a safety feature and does not enforce network consistency at parse time by default.
  
  ## Related Issues
  - Closes #844


ACKs for top commit:
  apoelstra:
    ACK 497e9943d766eaeae70f57e27514c8310d03775c; successfully ran local tests


Tree-SHA512: 5e4aa1ecf63201b45e4f8003427cfb7c178074416dcbe79d9cc39fca5e327a48c74f10d1daaad1ad8fbc99a09e7e76f30867a29bb111a0ced0db907ba73f69c6
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.

Network checking on DescriptorPublicKey and DefiniteDescriptorKey

2 participants