From 6d5ef23e61d3a45443df5a71c059df1716a7fd4a Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 17 Nov 2023 13:21:06 +1100 Subject: [PATCH] Add NetworkKind Add a new type `NetworkKind` the describes the kind of network we are on, ether mainnet or one of the test nets (testnet, regtest, signet). Do not use the type yet. --- bitcoin/src/lib.rs | 2 +- bitcoin/src/network.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 53b94258..0ebbe5a2 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -129,7 +129,7 @@ pub use crate::{ crypto::key::{self, PrivateKey, PubkeyHash, PublicKey, CompressedPublicKey, WPubkeyHash, XOnlyPublicKey}, crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag}, merkle_tree::MerkleBlock, - network::Network, + network::{Network, NetworkKind}, pow::{CompactTarget, Target, Work}, psbt::Psbt, sighash::{EcdsaSighashType, TapSighashType}, diff --git a/bitcoin/src/network.rs b/bitcoin/src/network.rs index 32102715..9c76d70f 100644 --- a/bitcoin/src/network.rs +++ b/bitcoin/src/network.rs @@ -31,6 +31,33 @@ use crate::constants::ChainHash; use crate::p2p::Magic; use crate::prelude::{String, ToOwned}; +/// What kind of network we are on. +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum NetworkKind { + /// The Bitcoin mainnet network. + Main, + /// Some kind of testnet network. + Test, +} + +// We explicitly do not provide `is_testnet`, using `!network.is_mainnet()` is less +// ambiguous due to confusion caused by signet/testnet/regtest. +impl NetworkKind { + /// Returns true if this is real mainnet bitcoin. + pub fn is_mainnet(&self) -> bool { *self == NetworkKind::Main } +} + +impl From for NetworkKind { + fn from(n: Network) -> Self { + use Network::*; + + match n { + Bitcoin => NetworkKind::Main, + Testnet | Signet | Regtest => NetworkKind::Test, + } + } +} + /// The cryptocurrency network to act on. #[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]