use crate::util::address::AddressType;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum Error {
#[error("Invalid magic network byte")]
InvalidMagicByte,
}
#[derive(Default, Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum Network {
#[default]
Mainnet,
Stagenet,
Testnet,
}
impl Network {
pub fn as_u8(self, addr_type: &AddressType) -> u8 {
use AddressType::*;
use Network::*;
match self {
Mainnet => match addr_type {
Standard => 18,
Integrated(_) => 19,
SubAddress => 42,
},
Testnet => match addr_type {
Standard => 53,
Integrated(_) => 54,
SubAddress => 63,
},
Stagenet => match addr_type {
Standard => 24,
Integrated(_) => 25,
SubAddress => 36,
},
}
}
pub fn from_u8(byte: u8) -> Result<Network, Error> {
use Network::*;
match byte {
18 | 19 | 42 => Ok(Mainnet),
53 | 54 | 63 => Ok(Testnet),
24 | 25 | 36 => Ok(Stagenet),
_ => Err(Error::InvalidMagicByte),
}
}
}