Skip to main content

kobe_btc/
network.rs

1//! Bitcoin network types.
2
3use bitcoin::Network as BtcNetwork;
4use core::fmt;
5
6/// Supported Bitcoin networks.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum Network {
9    /// Bitcoin mainnet.
10    #[default]
11    Mainnet,
12    /// Bitcoin testnet.
13    Testnet,
14}
15
16impl Network {
17    /// Convert to bitcoin crate's Network type.
18    #[inline]
19    #[must_use]
20    pub const fn to_bitcoin_network(self) -> BtcNetwork {
21        match self {
22            Self::Mainnet => BtcNetwork::Bitcoin,
23            Self::Testnet => BtcNetwork::Testnet,
24        }
25    }
26
27    /// Get the BIP44 coin type for this network.
28    #[inline]
29    #[must_use]
30    pub const fn coin_type(self) -> u32 {
31        match self {
32            Self::Mainnet => 0,
33            Self::Testnet => 1,
34        }
35    }
36
37    /// Get network name as string.
38    #[inline]
39    #[must_use]
40    pub const fn name(self) -> &'static str {
41        match self {
42            Self::Mainnet => "mainnet",
43            Self::Testnet => "testnet",
44        }
45    }
46}
47
48impl fmt::Display for Network {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(f, "{}", self.name())
51    }
52}