Split Network functionality into extension trait
What changed, and why it matters
This commit is a routine internal code reorganization. It moves some methods of the Network type into a new 'extension trait' called NetworkExt so that the underlying Network type can be shared with a separate crate. There is no security bug being fixed here.
No security action required. Reviewers may want to verify downstream code imports NetworkExt where needed, since the trait is no longer implicitly in scope via Network.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors bitcoin::Network by extracting chain_hash, from_chain_hash, and params into a sealed NetworkExt trait implemented for Network. It also adds a private as_display_str helper. This is an API-structural change to allow reuse of Network via the bitcoin-network-kind crate; no vulnerability is patched and no unsafe code is introduced.
Changed components
bitcoin/src/network/mod.rsbitcoin/src/network/params.rsInspect captured patch +34 / −15
diff --git a/bitcoin/src/network/mod.rs b/bitcoin/src/network/mod.rs
index 3de1b280..53dbcfe3 100644
--- a/bitcoin/src/network/mod.rs
+++ b/bitcoin/src/network/mod.rs
@@ -162,18 +162,35 @@ impl Network {
Ok(network)
}
+ /// Returns a string representation of the `Network` enum variant.
+ /// This is useful for displaying the network type as a string.
+ const fn as_display_str(&self) -> &'static str {
+ match self {
+ Self::Bitcoin => "bitcoin",
+ Self::Testnet(TestnetVersion::V3) => "testnet",
+ Self::Testnet(TestnetVersion::V4) => "testnet4",
+ Self::Signet => "signet",
+ Self::Regtest => "regtest",
+ }
+ }
+}
+
+/// Extension functionality for the [`Network`] type.
+// `define_extension_trait` chokes on the rustdoc example code.
+pub trait NetworkExt: sealed::Sealed {
/// Return the network's chain hash (genesis block hash).
///
/// # Examples
///
/// ```rust
/// use bitcoin::Network;
+ /// use bitcoin::network::NetworkExt as _;
/// use bitcoin::constants::ChainHash;
///
/// let network = Network::Bitcoin;
/// assert_eq!(network.chain_hash(), ChainHash::BITCOIN);
/// ```
- pub fn chain_hash(self) -> ChainHash { ChainHash::using_genesis_block_const(self) }
+ fn chain_hash(self) -> ChainHash;
/// Constructs a new `Network` from the chain hash (genesis block hash).
///
@@ -181,16 +198,25 @@ impl Network {
///
/// ```rust
/// use bitcoin::Network;
+ /// use bitcoin::network::NetworkExt as _;
/// use bitcoin::constants::ChainHash;
///
/// assert_eq!(Ok(Network::Bitcoin), Network::try_from(ChainHash::BITCOIN));
/// ```
- pub fn from_chain_hash(chain_hash: ChainHash) -> Option<Self> {
+ fn from_chain_hash(chain_hash: ChainHash) -> Option<Self>;
+
+ /// Returns the associated network parameters.
+ fn params(self) -> &'static Params;
+}
+impl NetworkExt for Network {
+ fn chain_hash(self) -> ChainHash { ChainHash::using_genesis_block_const(self) }
+
+ fn from_chain_hash(chain_hash: ChainHash) -> Option<Self> {
Self::try_from(chain_hash).ok()
}
/// Returns the associated network parameters.
- pub const fn params(self) -> &'static Params {
+ fn params(self) -> &'static Params {
match self {
Self::Bitcoin => &Params::BITCOIN,
Self::Testnet(TestnetVersion::V3) => &Params::TESTNET3,
@@ -199,18 +225,11 @@ impl Network {
Self::Regtest => &Params::REGTEST,
}
}
+}
- /// Returns a string representation of the `Network` enum variant.
- /// This is useful for displaying the network type as a string.
- const fn as_display_str(self) -> &'static str {
- match self {
- Self::Bitcoin => "bitcoin",
- Self::Testnet(TestnetVersion::V3) => "testnet",
- Self::Testnet(TestnetVersion::V4) => "testnet4",
- Self::Signet => "signet",
- Self::Regtest => "regtest",
- }
- }
+mod sealed {
+ pub trait Sealed: Sized {}
+ impl Sealed for super::Network {}
}
#[cfg(feature = "serde")]
diff --git a/bitcoin/src/network/params.rs b/bitcoin/src/network/params.rs
index 7fd79a00..32eca90f 100644
--- a/bitcoin/src/network/params.rs
+++ b/bitcoin/src/network/params.rs
@@ -60,7 +60,7 @@
//! # }
//! ```
-use super::{Network, TestnetVersion};
+use super::{Network, TestnetVersion, NetworkExt as _};
#[cfg(doc)]
use crate::pow::CompactTarget;
use crate::pow::Target;
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.