Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub enum Error {
InvalidAddress(String),
InvalidSharedSecret(String),
InvalidVin(String),
InvalidNetwork(String),
Secp256k1Error(secp256k1::Error),
OutOfRangeError(secp256k1::scalar::OutOfRangeError),
IOError(std::io::Error),
Expand All @@ -20,6 +21,7 @@ impl fmt::Display for Error {
Error::InvalidAddress(msg) => write!(f, "{}", msg),
Error::InvalidSharedSecret(msg) => write!(f, "{}", msg),
Error::InvalidVin(msg) => write!(f, "{}", msg),
Error::InvalidNetwork(msg) => write!(f, "Invalid network: {}", msg),
Error::Secp256k1Error(e) => e.fmt(f),
Error::OutOfRangeError(e) => e.fmt(f),
Error::IOError(e) => e.fmt(f),
Expand Down
24 changes: 24 additions & 0 deletions src/utils/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ pub enum Network {
Regtest,
}

impl From<Network> for &str {
fn from(value: Network) -> Self {
match value {
Network::Mainnet => "bitcoin", // we use the same string as rust-bitcoin for compatibility
Network::Regtest => "regtest",
Network::Testnet => "testnet",
}
}
}

impl TryFrom<&str> for Network {
type Error = crate::Error;

fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
let res = match value {
"bitcoin" | "main" => Self::Mainnet, // We also take the core style argument
"regtest" => Self::Regtest,
"testnet" | "signet" | "test" => Self::Testnet, // core arg
_ => return Err(Error::InvalidNetwork(value.to_string())),
};
Ok(res)
}
}

/// A silent payment address struct that can be used to deserialize a silent payment address string.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct SilentPaymentAddress {
Expand Down
Loading