use bitcoin::Network;
use bitcoin_hd::standards::DerivationBlockchain;
#[derive(
Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug, Display
)]
#[derive(StrictEncode, StrictDecode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
pub enum PublicNetwork {
#[display("mainnet")]
Mainnet,
#[default]
#[display("testnet")]
Testnet,
#[display("signet")]
Signet,
}
impl From<PublicNetwork> for Network {
fn from(network: PublicNetwork) -> Self { Network::from(&network) }
}
impl From<&PublicNetwork> for Network {
fn from(network: &PublicNetwork) -> Self {
match network {
PublicNetwork::Mainnet => Network::Bitcoin,
PublicNetwork::Testnet => Network::Testnet,
PublicNetwork::Signet => Network::Signet,
}
}
}
impl TryFrom<Network> for PublicNetwork {
type Error = ();
fn try_from(network: Network) -> Result<Self, Self::Error> {
Ok(match network {
Network::Bitcoin => PublicNetwork::Mainnet,
Network::Testnet => PublicNetwork::Testnet,
Network::Signet => PublicNetwork::Signet,
Network::Regtest => return Err(()),
})
}
}
impl From<PublicNetwork> for DerivationBlockchain {
fn from(network: PublicNetwork) -> Self { DerivationBlockchain::from(&network) }
}
impl From<&PublicNetwork> for DerivationBlockchain {
fn from(network: &PublicNetwork) -> Self {
match network {
PublicNetwork::Mainnet => DerivationBlockchain::Bitcoin,
PublicNetwork::Testnet => DerivationBlockchain::Testnet,
PublicNetwork::Signet => DerivationBlockchain::Testnet,
}
}
}
impl PublicNetwork {
pub fn is_testnet(self) -> bool {
matches!(self, PublicNetwork::Testnet | PublicNetwork::Signet)
}
pub fn electrum_port(self) -> u16 {
match self {
PublicNetwork::Mainnet => 50001,
PublicNetwork::Testnet => 60001,
PublicNetwork::Signet => 60601,
}
}
}