saa_common/
errors.rs

1#[cfg(any(feature = "std", not(feature = "substrate")))]
2use {thiserror::Error, saa_schema::wasm_serde};
3use {crate::String, std::string::FromUtf8Error};
4
5
6#[cfg(all(not(feature = "std"), feature = "substrate"))]
7#[derive(Debug, PartialEq, Eq, Clone, scale::Encode, scale::Decode)]
8pub enum AuthError {
9    NoCredentials,
10    InvalidLength(String),
11    RecoveryParam,
12    RecoveryMismatch,
13    Signature(String),
14    Recovery(String),
15    Generic(String),
16    Crypto(String),
17    SemVer(String),
18}
19
20
21
22#[cfg(any(feature = "std", not(feature = "substrate")))]
23#[wasm_serde]
24#[derive(Error)]
25pub enum AuthError {
26
27    #[error("No credentials provided or credentials are partially missing")]
28    NoCredentials,
29
30    #[error("{0}")]
31    MissingData(String),
32
33    #[error("Expected: {0};  Received: {1}")]
34    InvalidLength(u16, u16),
35
36    #[error("Values of v other than 27 and 28 not supported. Replay protection (EIP-155) cannot be used here.")]
37    RecoveryParam,
38    
39    #[error("Error recovering from the signature: Addresses do not match")]
40    RecoveryMismatch,
41
42    #[error("The provided credential was meant for a different chain")]
43    ChainIdMismatch,
44
45    #[error("The provided credential was meant for a different contract address")]
46    ContractMismatch,
47
48    #[error("The provided nonce has already been used")]
49    NonceUsed,
50
51    #[error("The given credential was not found on this account")]
52    NotFound, 
53
54    #[error("The given credential already exists on this account")]
55    AlreadyExists,
56
57    #[error("At least one of the credential must be usable for verifications")]
58    NoVerifying,
59
60    #[error("Wrong account number")]
61    DifferentNonce,
62
63    #[error("{0}")]
64    Signature(String),
65
66    #[error("{0}")]
67    Recovery(String),
68
69    #[error("{0}")]
70    Generic(String),
71
72    #[error("{0}")]
73    Crypto(String),
74    
75    #[error("Semver parsing error: {0}")]
76    SemVer(String),
77}
78
79
80impl AuthError {
81    pub fn generic<M: Into<String>>(msg: M) -> Self {
82        AuthError::Generic(msg.into())
83    }
84}
85
86impl From<bech32::primitives::hrp::Error> for AuthError {
87    fn from(err: bech32::primitives::hrp::Error) -> Self {
88        Self::Crypto(err.to_string())
89    }
90}
91
92
93impl From<bech32::EncodeError> for AuthError {
94    fn from(err: bech32::EncodeError) -> Self {
95        Self::Crypto(err.to_string())
96    }
97}
98
99
100#[cfg(feature = "std")]
101impl From<FromUtf8Error> for AuthError {
102    fn from(err: FromUtf8Error) -> Self {
103        Self::Recovery(err.to_string())
104    }
105}
106
107
108#[cfg(feature = "native")] 
109impl From<cosmwasm_crypto::CryptoError> for AuthError {
110    fn from(err: cosmwasm_crypto::CryptoError) -> Self {
111        Self::Crypto(err.to_string())
112    }
113}
114
115#[cfg(feature = "wasm")] 
116mod implementation{
117    use crate::AuthError;
118
119    impl From<crate::cosmwasm::RecoverPubkeyError> for AuthError {
120        fn from(err: crate::cosmwasm::RecoverPubkeyError) -> Self {
121            Self::Recovery(err.to_string())
122        }
123    }
124
125    impl From<crate::cosmwasm::StdError> for AuthError {
126        fn from(err: crate::cosmwasm::StdError) -> Self {
127            Self::Generic(err.to_string())
128        }
129    }
130
131    impl From<crate::cosmwasm::VerificationError> for AuthError {
132        fn from(err: crate::cosmwasm::VerificationError) -> Self {
133            Self::Crypto(err.to_string())
134        }
135    }
136}