use crate::util::address::AddressType;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum Error {
#[error("Invalid magic network byte")]
InvalidMagicByte,
}
pub enum NetworkByte {
Mainnet(u8, u8),
NotMainnet(u8),
}
impl NetworkByte {
pub fn as_vec(self) -> Vec<u8> {
match self {
NetworkByte::Mainnet(x, y) => vec![x, y],
NetworkByte::NotMainnet(x) => vec![x],
}
}
pub fn number_of_bytes(network: Network) -> usize {
match network {
Network::Mainnet => 2,
Network::Stagenet | Network::Testnet => 1,
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum Network {
Mainnet,
Stagenet,
Testnet,
}
impl Network {
pub fn as_u8(self, addr_type: &AddressType) -> NetworkByte {
use AddressType::*;
use Network::*;
match self {
Mainnet => match addr_type {
Standard => NetworkByte::Mainnet(178, 32),
Integrated(_) => NetworkByte::Mainnet(154, 53),
SubAddress => NetworkByte::Mainnet(176, 95),
},
Testnet => match addr_type {
Standard => NetworkByte::NotMainnet(53),
Integrated(_) => NetworkByte::NotMainnet(54),
SubAddress => NetworkByte::NotMainnet(63),
},
Stagenet => match addr_type {
Standard => NetworkByte::NotMainnet(24),
Integrated(_) => NetworkByte::NotMainnet(25),
SubAddress => NetworkByte::NotMainnet(36),
},
}
}
pub fn from_u8(byte: u8) -> Result<Network, Error> {
use Network::*;
match byte {
178 | 154 | 176 => Ok(Mainnet),
53 | 54 | 63 => Ok(Testnet),
24 | 25 | 36 => Ok(Stagenet),
_ => Err(Error::InvalidMagicByte),
}
}
}
impl Default for Network {
fn default() -> Network {
Network::Mainnet
}
}