rustywallet_silent/
network.rs

1//! Network types for Silent Payments.
2
3use crate::error::{Result, SilentPaymentError};
4
5/// Network for Silent Payments.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum Network {
8    /// Bitcoin mainnet
9    Mainnet,
10    /// Bitcoin testnet
11    Testnet,
12}
13
14impl Network {
15    /// Get the HRP (Human Readable Part) for bech32m encoding.
16    pub fn hrp(&self) -> &'static str {
17        match self {
18            Network::Mainnet => "sp",
19            Network::Testnet => "tsp",
20        }
21    }
22
23    /// Parse network from HRP.
24    pub fn from_hrp(hrp: &str) -> Result<Self> {
25        match hrp {
26            "sp" => Ok(Network::Mainnet),
27            "tsp" => Ok(Network::Testnet),
28            _ => Err(SilentPaymentError::InvalidNetwork(format!(
29                "Unknown HRP: {}",
30                hrp
31            ))),
32        }
33    }
34}
35
36impl std::fmt::Display for Network {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Network::Mainnet => write!(f, "mainnet"),
40            Network::Testnet => write!(f, "testnet"),
41        }
42    }
43}