Skip to main content

mini_sign/
errors.rs

1use scrypt::errors::InvalidOutputLen;
2
3/// Error kind for minisign-rs
4#[derive(Debug, Clone)]
5pub enum ErrorKind {
6    Io,
7    Kdf,
8    PrehashedMismatch,
9    PublicKey,
10    SecretKey,
11    SignatureError,
12}
13impl std::fmt::Display for ErrorKind {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            ErrorKind::Io => write!(f, "io error"),
17            ErrorKind::Kdf => write!(f, "kdf error"),
18            ErrorKind::PrehashedMismatch => write!(f, "prehashed mismatch"),
19            ErrorKind::PublicKey => write!(f, "public key error"),
20            ErrorKind::SecretKey => write!(f, "secret key error"),
21            ErrorKind::SignatureError => write!(f, "signature error"),
22        }
23    }
24}
25/// Error type for minisign-rs
26///
27/// This type is used for all errors in minisign-rs
28#[derive(Debug)]
29pub struct SError {
30    kind: ErrorKind,
31    error: Box<dyn std::error::Error + Send + Sync>,
32}
33impl std::error::Error for SError {}
34impl std::fmt::Display for SError {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(f, "kind:{} error:{}", self.kind, self.error)
37    }
38}
39impl SError {
40    pub(crate) fn new<E>(kind: ErrorKind, error: E) -> Self
41    where
42        E: Into<Box<dyn std::error::Error + Send + Sync>>,
43    {
44        Self {
45            kind,
46            error: error.into(),
47        }
48    }
49}
50// impl From<rand_core::Error> for SError {
51//     fn from(error: rand_core::Error) -> Self {
52//         Self {
53//             kind: ErrorKind::Kdf,
54//             error: Box::new(error),
55//         }
56//     }
57//
58impl From<getrandom::Error> for SError {
59    fn from(error: getrandom::Error) -> Self {
60        Self {
61            kind: ErrorKind::Io,
62            error: Box::new(error),
63        }
64    }
65}
66impl From<std::io::Error> for SError {
67    fn from(error: std::io::Error) -> Self {
68        Self {
69            kind: ErrorKind::Io,
70            error: Box::new(error),
71        }
72    }
73}
74impl From<ed25519_dalek::SignatureError> for SError {
75    fn from(error: ed25519_dalek::SignatureError) -> Self {
76        Self {
77            kind: ErrorKind::SignatureError,
78            error: Box::new(error),
79        }
80    }
81}
82impl From<scrypt::errors::InvalidParams> for SError {
83    fn from(err: scrypt::errors::InvalidParams) -> SError {
84        SError::new(ErrorKind::Kdf, err.to_string())
85    }
86}
87impl From<scrypt::errors::InvalidOutputLen> for SError {
88    fn from(err: InvalidOutputLen) -> SError {
89        SError::new(ErrorKind::Kdf, err.to_string())
90    }
91}
92pub type Result<T> = std::result::Result<T, SError>;