saa_common/types/
errors.rs

1
2mod std_mod {
3    use saa_schema::saa_error;
4    use crate::CredentialName;
5
6    #[cfg(feature = "session")]
7    #[saa_error]
8    pub enum SessionError {
9        #[error("The session key has already expired")]
10        Expired,
11
12        #[error("No session key found")]
13        NotFound,
14
15        #[error("Only the owner or session key granter can perform this operation")]
16        NotOwner,
17
18        #[error("This session key wasn't granted to the given grantee")]
19        NotGrantee,
20
21        #[error("Must have both id and name specified")]
22        InvalidGrantee,
23
24        #[error("Invalid data or indifferent from the grantee")]
25        InvalidGranter,
26
27        #[error("Passed a list with no actions. Use AllowedActions::All() if you want to allow all of them")]
28        EmptyCreateActions,
29
30        #[error("No actions passed to execute")]
31        EmptyPassedActions,
32
33        #[error("Couldn't derivate a String result from given message and method")]
34        DerivationError,
35
36        #[error("Invalid actions provided. Check that there are no empty results not dublicates")]
37        InvalidActions,
38
39        #[error("Session creation messages aren't allowed to be in allowed message list")]
40        InnerSessionAction,
41
42        #[error("Current item cant't be used with the given session key")]
43        NotAllowedAction,
44    }
45
46
47    #[saa_error]
48    pub enum ReplayError {
49        #[error("Invalid nonce. Expected: '{0}'")]
50        InvalidNonce(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        AddressMismatch,
57
58        #[error("Invalid messages. Expected to be signing the following inner message: '{0}'")]
59        MessageMismatch(String),
60
61        #[error("Error converting from binary to {0}")]
62        FromBin(String),
63
64        #[error("Error serializing to binary from: {0}")]
65        ToBin(String),
66
67        #[error("Signed too many messages. Expected: {0}; Received: {1}")]
68        ManyMessages(u8, u8),
69
70        #[error("Invalid envelope: (chain_id, contract_address, nonce or messages)`. Expected: '{0}'; Received: '{1}'")]
71        InvalidEnvelope(String, String),
72
73        #[error("The data for the replay protection is missing or invalid. Item: {0}")]
74        MissingData(String),
75    }
76
77
78
79    #[saa_error]
80    pub enum StorageError {
81        #[error("Error reading {0} from storage: {1}")]
82        Read(String, String),
83
84        #[error("Error writing {0} to storage: {1}")]
85        Write(String, String),
86
87        #[error("The given credential already exists on this account. Failed ID: {0}")]
88        AlreadyExists(String),
89
90        #[error("The given credential was not found on this account")]
91        NotFound, 
92
93        #[cfg(feature = "wasm")]
94        #[error("Standard error: {0}")]
95        Std(#[from] crate::wasm::StdError),
96
97        #[error("Generic error: {0}")]
98        Generic(String)
99    }
100
101    #[saa_error]
102    pub enum CredentialError {
103        #[error("Not provided or partially missing")]
104        NoCredentials,
105
106        #[error("At least one credential must be kept for authorization checks")]
107        NoneLeft,
108
109        #[error("Too many credentials provided: {0}. Maximum allowed is 255")]
110        TooManyCredentials(usize),
111
112        #[error("Invalid primary index: {0}. There are only {1} credentials. (Max index is {1}-1)")]
113        IndexOutOfBounds(usize, usize),
114
115        #[error("A native address is requested but not provided")]
116        NoNativeCaller,
117
118        #[error("One of the main properties of the credential '{0}' are missing")]
119        MissingData(CredentialName),
120
121        #[error("Error while processing the credential '{0}', One of the properties is invalid")]
122        IncorrectData(CredentialName),
123
124        #[error("The credential '{0}' is not valid. Error in property '{1}': {2}")]
125        InvalidProperty(CredentialName, String, String),
126
127        #[error("Must supply at least one non-native credential to be validated")]
128        OnlyCustomNatives,
129
130/*
131        #[error("The credential '{0}' was expecting an info object with a property '{1}', however it wasn't provided or was empty")]
132        NoInfoProperty(CredentialName, String),
133
134         #[error("The credential '{0}' needs an extended info to be passed. It hasn't been done or there was an error")]
135        NoInfoExt(CredentialName),
136 */
137        #[cfg(feature = "wasm")]
138        #[error("(Std) Serialization error: {0}")]
139        Std(#[from] crate::wasm::StdError),
140    }
141
142
143
144    #[saa_error]
145    pub enum AuthError {
146
147        #[error("Missing {0}")]
148        MissingData(String),
149
150        #[error("Values of v other than 27 and 28 not supported. Replay protection (EIP-155) cannot be used here.")]
151        RecoveryParam,
152        
153        #[error("Error recovering from the signature: Addresses do not match")]
154        RecoveryMismatch,
155
156        #[error("Unauthorized: {0}")]
157        Unauthorized(String),
158
159        #[error("Invalid length for type '{0}'. Expected: {1}, Received: {2}")]
160        InvalidLength(String, u16, u16),
161
162        #[error("Signature verification error for {0} with id of '{1}'")]
163        Signature(CredentialName, String),
164
165        #[error("{0}")]
166        Generic(String),
167
168        #[error("{0}")]
169        Crypto(String),
170
171        #[error("Error converting binary to {0}")]
172        Convertion(String),
173
174        #[error("Credential: {0}")]
175        Credential(#[from] CredentialError),
176        
177        #[cfg(feature = "replay")]
178        #[error("Replay: {0}")]
179        Replay(#[from] ReplayError),
180
181        #[cfg(feature = "session")]
182        #[error("Session: {0}")]
183        Session(#[from] SessionError),
184
185        #[cfg(feature = "wasm")]
186        #[error("Storage: {0}")]
187        Storage(#[from] StorageError),
188    }
189
190
191    impl From<std::string::FromUtf8Error> for AuthError {
192        fn from(err: std::string::FromUtf8Error) -> Self {
193            Self::Crypto(err.to_string())
194        }
195    }
196
197
198    #[cfg(feature = "eth_typed_data")] 
199    impl From<serde_json::DeserializerError> for AuthError {
200        fn from(err: serde_json::DeserializerError) -> Self {
201            Self::Generic(err.to_string())
202        }
203    }
204
205
206    #[cfg(feature = "wasm")] 
207    mod wasm {
208        use super::AuthError;
209
210        impl From<crate::wasm::RecoverPubkeyError> for AuthError {
211            fn from(err: crate::wasm::RecoverPubkeyError) -> Self {
212                Self::Crypto(err.to_string())
213            }
214        }
215
216        impl From<crate::wasm::StdError> for AuthError {
217            fn from(err: crate::wasm::StdError) -> Self {
218                Self::Crypto(err.to_string())
219            }
220        }
221
222        impl From<crate::wasm::VerificationError> for AuthError {
223            fn from(err: crate::wasm::VerificationError) -> Self {
224                Self::Crypto(err.to_string())
225            }
226        }
227    }
228
229
230    #[cfg(feature = "native")] 
231    impl From<cosmwasm_crypto::CryptoError> for AuthError {
232        fn from(err: cosmwasm_crypto::CryptoError) -> Self {
233            Self::Crypto(err.to_string())
234        }
235    }
236
237   
238
239}
240
241
242
243#[cfg(not(feature = "std"))]
244mod no_std_mod {
245    use crate::String;
246    use saa_schema::{saa_error, saa_type, strum_macros};
247
248    
249    #[cfg(feature = "replay")]
250    pub enum ReplayError {
251        DifferentNonce(u64, u64),
252        ChainIdMismatch,
253        AddressMismatch,
254        MessageMismatch(String),
255        FromBin(String),
256        ToBin(String),
257        ManyMessages(u8, u8),
258        InvalidEnvelope(String, String),
259        MissingData(String),
260    }
261
262    #[cfg(feature = "session")]
263    pub enum SessionError {
264        Expired,
265        InvalidGrantee,
266        InvalidGranter,
267        EmptyCreateActions,
268        EmptyPassedActions,
269        DerivationError,
270        InvalidActions,
271        InnerSessionAction,
272        NotAllowedAction,
273    }
274
275
276    #[saa_error]
277    pub enum AuthError {
278        NoCredentials,
279        MissingData(String),
280        InvalidLength(String, u16, u16),
281        RecoveryParam,
282        RecoveryMismatch,
283        InvalidSignedData,
284        PasskeyChallenge,
285        Unauthorized(String),
286        Signature(String),
287        Generic(String),
288        Convertation(String),
289        Crypto(String),
290        SemVer(String),
291        #[cfg(feature = "replay")]
292        Replay(String),
293        #[cfg(feature = "session")]
294        Session(String),
295    }
296
297    #[cfg(feature = "replay")]
298    impl From<ReplayError> for AuthError {
299        fn from(err: ReplayError) -> Self {
300            Self::Replay(err.to_string())
301        }
302    }
303
304    #[cfg(feature = "session")]
305    impl From<SessionError> for AuthError {
306        fn from(err: SessionError) -> Self {
307            Self::Session(err.to_string())
308        }
309    }
310
311}    
312
313
314#[cfg(feature = "std")]
315pub use std_mod::*;
316
317
318#[cfg(not(feature = "std"))]
319pub use no_std_mod::*;
320
321
322
323
324
325impl AuthError {
326    pub fn generic<M: Into<String>>(msg: M) -> Self {
327        AuthError::Generic(msg.into())
328    }
329}
330