use core::fmt;
use core::str::FromStr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Network {
Mainnet,
Testnet,
Regtest,
}
impl Network {
pub const fn is_mainnet(self) -> bool {
matches!(self, Self::Mainnet)
}
pub const fn same_prefixes(self, other: Self) -> bool {
self.is_mainnet() == other.is_mainnet()
}
pub const fn p2pkh_prefix(self) -> [u8; 2] {
match self {
Self::Mainnet => [0x1c, 0xb8],
Self::Testnet | Self::Regtest => [0x1d, 0x25],
}
}
pub const fn p2sh_prefix(self) -> [u8; 2] {
match self {
Self::Mainnet => [0x1c, 0xbd],
Self::Testnet | Self::Regtest => [0x1c, 0xba],
}
}
pub const fn as_str(self) -> &'static str {
match self {
Self::Mainnet => "mainnet",
Self::Testnet => "testnet",
Self::Regtest => "regtest",
}
}
}
impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Network {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mainnet" => Ok(Self::Mainnet),
"testnet" => Ok(Self::Testnet),
"regtest" => Ok(Self::Regtest),
_ => Err(()),
}
}
}