aries_bbssignatures/
errors.rs1use thiserror::Error;
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::*;
4
5use crate::pok_sig::PoKOfSignatureProofStatus;
6
7#[derive(Debug, Error, Clone)]
9pub enum BBSErrorKind {
10 #[error("Key generation error")]
12 KeyGenError,
13 #[error("Public key to message mismatch. Expected {0}, found {1}")]
15 PublicKeyGeneratorMessageCountMismatch(usize, usize),
16 #[error("Signature incorrect size. Expected 193, found {0}")]
18 SignatureIncorrectSize(usize),
19 #[error("Signature cannot be loaded due to a bad value")]
21 SignatureValueIncorrectSize,
22 #[error("Malformed signature")]
24 MalformedSignature,
25 #[error("Malformed secret key")]
27 MalformedSecretKey,
28 #[error("Malformed public key")]
30 MalformedPublicKey,
31 #[error("Signature proof-of-knowledge error: {msg}")]
33 SignaturePoKError {
34 msg: String,
36 },
37 #[error("Invalid number of bytes. Expected {0}, found {1}")]
39 InvalidNumberOfBytes(usize, usize),
40 #[error("The proof failed due to {status}")]
42 InvalidProof {
43 status: PoKOfSignatureProofStatus,
45 },
46 #[error("{msg}")]
48 GeneralError {
49 msg: String,
51 },
52}
53
54#[derive(Debug, Error, Clone)]
56#[error(transparent)]
57pub struct BBSError {
58 #[from]
59 inner: BBSErrorKind,
60}
61
62impl BBSError {
63 pub fn from_kind(kind: BBSErrorKind) -> Self {
65 BBSError { inner: kind }
66 }
67
68 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}