saa_common/types/
errors.rs

1
2mod std_mod {
3    use saa_schema::saa_error;
4
5    #[cfg(feature = "session")]
6    #[saa_error]
7    pub enum SessionError {
8        #[error("The session key has already expired")]
9        Expired,
10
11        #[error("No session key found")]
12        NotFound,
13
14        #[error("Only the owner or session key granter can perform this operation")]
15        NotOwner,
16
17        #[error("This session key wasn't granted to the given grantee")]
18        NotGrantee,
19
20        #[error("Must have both id and name specified")]
21        InvalidGrantee,
22
23        #[error("Invalid data or indifferent from the grantee")]
24        InvalidGranter,
25
26        #[error("Passed a list with no actions. Use AllowedActions::All() if you want to allow all of them")]
27        EmptyCreateActions,
28
29        #[error("No actions passed to execute")]
30        EmptyPassedActions,
31
32        #[error("Couldn't derivate a String result from given message and method")]
33        DerivationError,
34
35        #[error("Invalid actions provided. Check that there are no empty results not dublicates")]
36        InvalidActions,
37
38        #[error("Session creation messages aren't allowed to be in allowed message list")]
39        InnerSessionAction,
40
41        #[error("Current item cant't be used with the given session key")]
42        NotAllowedAction,
43    }
44
45
46    #[cfg(feature = "replay")]
47    #[saa_error]
48    pub enum ReplayError {
49        #[error("{0} is invalid as nonce. Expected: {1}")]
50        DifferentNonce(u64, u64),
51
52        #[error("The provided credential was meant for a different chain")]
53        ChainIdMismatch,
54
55        #[error("The provided credential was meant for a different contract address")]
56        ContractMismatch,
57
58        #[error("Error converting binary to {0}")]
59        Convertion(String),
60    }
61
62
63
64    #[saa_error]
65    pub enum StorageError {
66        #[error("Error reading {0} from storage: {1}")]
67        Read(String, String),
68
69        #[error("Error writing {0} to storage: {1}")]
70        Write(String, String),
71
72        #[error("The given credential already exists on this account")]
73        AlreadyExists,
74
75        #[error("The given credential was not found on this account")]
76        NotFound, 
77
78        #[cfg(feature = "wasm")]
79        #[error("Standard error: {0}")]
80        Std(#[from] crate::wasm::StdError),
81
82        #[error("Generic error: {0}")]
83        Generic(String)
84    }
85
86
87
88
89    #[saa_error]
90    pub enum AuthError {
91
92        #[error("No credentials provided or credentials are partially missing")]
93        NoCredentials,
94
95        #[error("{0}")]
96        MissingData(String),
97
98        #[error("Invalid length of {0}.  Expected: {1};  Received: {2}")]
99        InvalidLength(String, u16, u16),
100
101        #[error("Values of v other than 27 and 28 not supported. Replay protection (EIP-155) cannot be used here.")]
102        RecoveryParam,
103        
104        #[error("Error recovering from the signature: Addresses do not match")]
105        RecoveryMismatch,
106
107        #[error("The signed data is expected to be a replay attach protection envelope")]
108        InvalidSignedData,
109
110        #[error("Passkey challenge must be base64url to base64 encoded string")]
111        PasskeyChallenge,
112
113        #[error("Unauthorized: {0}")]
114        Unauthorized(String),
115
116        #[error("{0}")]
117        Signature(String),
118
119        #[error("{0}")]
120        Recovery(String),
121
122        #[error("{0}")]
123        Generic(String),
124
125        #[error("{0}")]
126        Crypto(String),
127
128        #[error("Error converting binary to {0}")]
129        Convertation(String),
130        
131        #[error("Semver parsing error: {0}")]
132        SemVer(String),
133        
134        #[cfg(feature = "replay")]
135        #[error("Replay Protection Error: {0}")]
136        Replay(#[from] ReplayError),
137
138        #[cfg(feature = "session")]
139        #[error("Session Error: {0}")]
140        Session(#[from] SessionError),
141
142        #[cfg(feature = "wasm")]
143        #[error("{0}")]
144        Storage(#[from] StorageError),
145    }
146
147
148    impl From<std::string::FromUtf8Error> for AuthError {
149        fn from(err: std::string::FromUtf8Error) -> Self {
150            Self::Recovery(err.to_string())
151        }
152    }
153
154    #[cfg(feature = "eth_typed_data")]
155    impl From<ethers_core::types::transaction::eip712::Eip712Error> for AuthError {
156        fn from(err: ethers_core::types::transaction::eip712::Eip712Error) -> Self {
157            Self::Generic(err.to_string())
158        }
159        
160    }
161
162
163    #[cfg(feature = "wasm")] 
164    mod wasm {
165        use super::AuthError;
166
167        impl From<crate::wasm::RecoverPubkeyError> for AuthError {
168            fn from(err: crate::wasm::RecoverPubkeyError) -> Self {
169                Self::Recovery(err.to_string())
170            }
171        }
172
173        impl From<crate::wasm::StdError> for AuthError {
174            fn from(err: crate::wasm::StdError) -> Self {
175                Self::Generic(err.to_string())
176            }
177        }
178
179        impl From<crate::wasm::VerificationError> for AuthError {
180            fn from(err: crate::wasm::VerificationError) -> Self {
181                Self::Crypto(err.to_string())
182            }
183        }
184
185         impl From<bech32::primitives::hrp::Error> for AuthError {
186            fn from(err: bech32::primitives::hrp::Error) -> Self {
187                Self::Crypto(err.to_string())
188            }
189        }
190
191        impl From<bech32::EncodeError> for AuthError {
192            fn from(err: bech32::EncodeError) -> Self {
193                Self::Crypto(err.to_string())
194            }
195        }
196    }
197
198
199    #[cfg(feature = "native")] 
200    impl From<cosmwasm_crypto::CryptoError> for AuthError {
201        fn from(err: cosmwasm_crypto::CryptoError) -> Self {
202            Self::Crypto(err.to_string())
203        }
204    }
205
206   
207
208}
209
210
211
212#[cfg(not(feature = "std"))]
213mod no_std_mod {
214    use crate::String;
215    use saa_schema::{saa_error, saa_type, strum_macros};
216
217    
218    #[cfg(feature = "replay")]
219    pub enum ReplayError {
220        DifferentNonce(u64, u64),
221        ChainIdMismatch,
222        ContractMismatch,
223    }
224
225    #[cfg(feature = "session")]
226    pub enum SessionError {
227        Expired,
228        InvalidGrantee,
229        InvalidGranter,
230        EmptyCreateActions,
231        EmptyPassedActions,
232        DerivationError,
233        InvalidActions,
234        InnerSessionAction,
235        NotAllowedAction,
236    }
237
238
239    #[saa_error]
240    pub enum AuthError {
241        NoCredentials,
242        MissingData(String),
243        InvalidLength(String, u16, u16),
244        RecoveryParam,
245        RecoveryMismatch,
246        InvalidSignedData,
247        PasskeyChallenge,
248        Unauthorized(String),
249        Signature(String),
250        Recovery(String),
251        Generic(String),
252        Convertation(String),
253        Crypto(String),
254        SemVer(String),
255        #[cfg(feature = "replay")]
256        Replay(String),
257        #[cfg(feature = "session")]
258        Session(String),
259    }
260
261    #[cfg(feature = "replay")]
262    impl From<ReplayError> for AuthError {
263        fn from(err: ReplayError) -> Self {
264            Self::Replay(err.to_string())
265        }
266    }
267
268    #[cfg(feature = "session")]
269    impl From<SessionError> for AuthError {
270        fn from(err: SessionError) -> Self {
271            Self::Session(err.to_string())
272        }
273    }
274
275}    
276
277
278#[cfg(feature = "std")]
279pub use std_mod::*;
280
281
282#[cfg(not(feature = "std"))]
283pub use no_std_mod::*;
284
285
286
287
288
289impl AuthError {
290    pub fn generic<M: Into<String>>(msg: M) -> Self {
291        AuthError::Generic(msg.into())
292    }
293}
294