rust_bls_bn254/
errors.rs

1use std::{error::Error, fmt, io};
2
3use hex::FromHexError;
4use serde_json::Error as SerdeError;
5use thiserror::Error as ErrorThis;
6
7#[derive(Debug)]
8pub enum BLSError {
9    SignatureNotInSubgroup,
10    SignatureListEmpty,
11    PublicKeyNotInSubgroup,
12    PublicKeyListEmpty,
13}
14
15impl Error for BLSError {}
16
17impl fmt::Display for BLSError {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match *self {
20            BLSError::SignatureNotInSubgroup => write!(f, "Signature not in subgroup"),
21            BLSError::PublicKeyNotInSubgroup => write!(f, "Public key not in subgroup"),
22            BLSError::SignatureListEmpty => write!(f, "Signature array is empty"),
23            BLSError::PublicKeyListEmpty => write!(f, "The public key list is empty"),
24        }
25    }
26}
27
28#[derive(ErrorThis, Debug)]
29pub enum KeystoreError {
30    #[error("Invalid KDF function provided.")]
31    WrongKdfFunction,
32
33    #[error("Serde serialization/deserialization error: {0}")]
34    SerdeError(#[from] SerdeError),
35
36    #[error("Hex decode error: {0}")]
37    FromHexError(#[from] FromHexError),
38
39    #[error("IO Error error: {0}")]
40    IOError(#[from] io::Error),
41
42    #[error("Invalid KDF parameters.")]
43    WrongKDFParameters,
44
45    #[error("Derive Child Sk Error error: {0}")]
46    DeriveChildSkError(String),
47
48    #[error("Derive Master Sk Error error: {0}")]
49    DeriveMasterSkError(String),
50
51    #[error("Generic error: {0}")]
52    GenericError(String),
53
54    #[error("Scrypt Error: {0}")]
55    ScryptError(String),
56
57    #[error("PBKDF2 Error: {0}")]
58    PBKDF2Error(String),
59
60    #[error("Encryption Error: {0}")]
61    EncryptionError(String),
62
63    #[error("Decryption Error: {0}")]
64    DecryptionError(String),
65
66    #[error("Path to nodes Error: {0}")]
67    PathToNodes(String),
68
69    #[error("Reconstruct mnemonic Error: {0}")]
70    ReconstructMnemonicError(String),
71
72    #[error("Mnemonic Error: {0}")]
73    MnemonicError(String),
74}
75
76impl KeystoreError {
77    // Function to map other errors to GenericError
78    pub fn from<E: fmt::Display>(error: E) -> KeystoreError {
79        KeystoreError::GenericError(error.to_string())
80    }
81}
82
83impl From<&str> for KeystoreError {
84    fn from(err: &str) -> KeystoreError {
85        KeystoreError::GenericError(err.to_string())
86    }
87}