aries_bbssignatures/
errors.rs

1use thiserror::Error;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::pok_sig::PoKOfSignatureProofStatus;
6
7/// The kinds of errors that can be generated
8#[derive(Debug, Error, Clone)]
9pub enum BBSErrorKind {
10    /// Error during key generation
11    #[error("Key generation error")]
12    KeyGenError,
13    /// When there are more messages than public key generators
14    #[error("Public key to message mismatch. Expected {0}, found {1}")]
15    PublicKeyGeneratorMessageCountMismatch(usize, usize),
16    /// When the signature is the incorrect size when calling from_bytes
17    #[error("Signature incorrect size. Expected 193, found {0}")]
18    SignatureIncorrectSize(usize),
19    /// When the signature bytes are not a valid curve point
20    #[error("Signature cannot be loaded due to a bad value")]
21    SignatureValueIncorrectSize,
22    /// When a signature contains a zero or a point at infinity
23    #[error("Malformed signature")]
24    MalformedSignature,
25    /// When a secret key is all zeros
26    #[error("Malformed secret key")]
27    MalformedSecretKey,
28    /// When the public key bytes are not valid curve points
29    #[error("Malformed public key")]
30    MalformedPublicKey,
31    /// Signature proof-of-knowledge error
32    #[error("Signature proof-of-knowledge error: {msg}")]
33    SignaturePoKError {
34        /// The error message
35        msg: String,
36    },
37    /// Incorrect number of bytes passed to from_bytes methods
38    #[error("Invalid number of bytes. Expected {0}, found {1}")]
39    InvalidNumberOfBytes(usize, usize),
40    /// Failed signature poof of knowledge
41    #[error("The proof failed due to {status}")]
42    InvalidProof {
43        /// The status of the invalid proof
44        status: PoKOfSignatureProofStatus,
45    },
46    /// A Generic error
47    #[error("{msg}")]
48    GeneralError {
49        /// The error message
50        msg: String,
51    },
52}
53
54/// Wrapper to hold the kind of error and a backtrace
55#[derive(Debug, Error, Clone)]
56#[error(transparent)]
57pub struct BBSError {
58    #[from]
59    inner: BBSErrorKind,
60}
61
62impl BBSError {
63    /// Get the inner error kind
64    pub fn from_kind(kind: BBSErrorKind) -> Self {
65        BBSError { inner: kind }
66    }
67
68    /// Get the inner error kind
69    pub fn kind(&self) -> &BBSErrorKind {
70        &self.inner
71    }
72}
73
74impl From<String> for BBSError {
75    fn from(msg: String) -> BBSError {
76        BBSError::from_kind(BBSErrorKind::GeneralError { msg })
77    }
78}
79
80impl From<std::io::Error> for BBSError {
81    fn from(err: std::io::Error) -> BBSError {
82        BBSError::from(format!("{:?}", err))
83    }
84}
85
86#[cfg(feature = "wasm")]
87impl From<BBSError> for JsValue {
88    fn from(error: BBSError) -> Self {
89        JsValue::from_str(&format!("{}", error))
90    }
91}
92
93#[cfg(feature = "wasm")]
94impl From<JsValue> for BBSError {
95    fn from(js: JsValue) -> Self {
96        if js.is_string() {
97            BBSError::from(BBSErrorKind::GeneralError {
98                msg: js.as_string().unwrap(),
99            })
100        } else {
101            BBSError::from(BBSErrorKind::GeneralError {
102                msg: "".to_string(),
103            })
104        }
105    }
106}
107
108#[cfg(feature = "wasm")]
109impl From<serde_wasm_bindgen::Error> for BBSError {
110    fn from(err: serde_wasm_bindgen::Error) -> Self {
111        BBSError::from(BBSErrorKind::GeneralError {
112            msg: format!("{:?}", err),
113        })
114    }
115}
116
117#[cfg(feature = "wasm")]
118impl From<BBSError> for serde_wasm_bindgen::Error {
119    fn from(err: BBSError) -> Self {
120        serde_wasm_bindgen::Error::new(err)
121    }
122}