Skip to main content

neco_secp/
error.rs

1use core::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum SecpError {
5    InvalidSecretKey,
6    InvalidPublicKey,
7    InvalidSignature,
8    ExhaustedAttempts,
9    InvalidHex(&'static str),
10    InvalidEvent(&'static str),
11    InvalidNip19(&'static str),
12    InvalidNip04(&'static str),
13    InvalidNip44(&'static str),
14    Json(String),
15}
16
17impl fmt::Display for SecpError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Self::InvalidSecretKey => f.write_str("invalid secret key"),
21            Self::InvalidPublicKey => f.write_str("invalid public key"),
22            Self::InvalidSignature => f.write_str("invalid signature"),
23            Self::ExhaustedAttempts => f.write_str("exhausted attempts"),
24            Self::InvalidHex(message) => f.write_str(message),
25            Self::InvalidEvent(message) => f.write_str(message),
26            Self::InvalidNip19(message) => f.write_str(message),
27            Self::InvalidNip04(message) => f.write_str(message),
28            Self::InvalidNip44(message) => f.write_str(message),
29            Self::Json(error) => write!(f, "json error: {error}"),
30        }
31    }
32}
33
34impl std::error::Error for SecpError {}
35
36#[cfg(feature = "nostr")]
37impl From<neco_json::ParseError> for SecpError {
38    fn from(value: neco_json::ParseError) -> Self {
39        Self::Json(value.to_string())
40    }
41}
42
43#[cfg(feature = "nostr")]
44impl From<neco_json::EncodeError> for SecpError {
45    fn from(value: neco_json::EncodeError) -> Self {
46        Self::Json(value.to_string())
47    }
48}