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("Client requested to use a native address that called the environment as a credential, but it hasn't been set")]
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("Passed only (native) credentials that aren't validated by the environment. Need to supply at least one verifyable credential")]
128        OnlyCustomNatives,
129
130        #[error("The credential '{0}' was expecting an info object with a property '{1}', however it wasn't provided or was empty")]
131        NoInfoProperty(CredentialName, String),
132
133        #[error("The credential '{0}' needs an extended info to be passed. It hasn't been done or there was an error")]
134        NoInfoExt(CredentialName),
135
136        #[cfg(feature = "wasm")]
137        #[error("(Std) Serialization error: {0}")]
138        Std(#[from] crate::wasm::StdError),
139    }
140
141
142
143    #[saa_error]
144    pub enum AuthError {
145
146        #[error("Missing {0}")]
147        MissingData(String),
148
149        #[error("Values of v other than 27 and 28 not supported. Replay protection (EIP-155) cannot be used here.")]
150        RecoveryParam,
151        
152        #[error("Error recovering from the signature: Addresses do not match")]
153        RecoveryMismatch,
154
155        #[error("Unauthorized: {0}")]
156        Unauthorized(String),
157
158        #[error("Invalid length for type '{0}'. Expected: {1}, Received: {2}")]
159        InvalidLength(String, u16, u16),
160
161        #[error("Signature verification error for {0} with id of '{1}'")]
162        Signature(CredentialName, String),
163
164        #[error("{0}")]
165        Recovery(String),
166
167        #[error("{0}")]
168        Generic(String),
169
170        #[error("{0}")]
171        Crypto(String),
172
173        #[error("Error converting binary to {0}")]
174        Convertion(String),
175
176        #[error("Credential: {0}")]
177        Credential(#[from] CredentialError),
178        
179        #[cfg(feature = "replay")]
180        #[error("Replay: {0}")]
181        Replay(#[from] ReplayError),
182
183        #[cfg(feature = "session")]
184        #[error("Session: {0}")]
185        Session(#[from] SessionError),
186
187        #[cfg(feature = "wasm")]
188        #[error("Storage: {0}")]
189        Storage(#[from] StorageError),
190    }
191
192
193    impl From<std::string::FromUtf8Error> for AuthError {
194        fn from(err: std::string::FromUtf8Error) -> Self {
195            Self::Recovery(err.to_string())
196        }
197    }
198
199
200    #[cfg(feature = "eth_typed_data")] 
201    impl From<serde_json::DeserializerError> for AuthError {
202        fn from(err: serde_json::DeserializerError) -> Self {
203            Self::Generic(err.to_string())
204        }
205    }
206
207
208    #[cfg(feature = "wasm")] 
209    mod wasm {
210        use super::AuthError;
211
212        impl From<crate::wasm::RecoverPubkeyError> for AuthError {
213            fn from(err: crate::wasm::RecoverPubkeyError) -> Self {
214                Self::Recovery(err.to_string())
215            }
216        }
217
218        impl From<crate::wasm::StdError> for AuthError {
219            fn from(err: crate::wasm::StdError) -> Self {
220                Self::Generic(err.to_string())
221            }
222        }
223
224        impl From<crate::wasm::VerificationError> for AuthError {
225            fn from(err: crate::wasm::VerificationError) -> Self {
226                Self::Crypto(err.to_string())
227            }
228        }
229    }
230
231
232    #[cfg(feature = "native")] 
233    impl From<cosmwasm_crypto::CryptoError> for AuthError {
234        fn from(err: cosmwasm_crypto::CryptoError) -> Self {
235            Self::Crypto(err.to_string())
236        }
237    }
238
239   
240
241}
242
243
244
245#[cfg(not(feature = "std"))]
246mod no_std_mod {
247    use crate::String;
248    use saa_schema::{saa_error, saa_type, strum_macros};
249
250    
251    #[cfg(feature = "replay")]
252    pub enum ReplayError {
253        DifferentNonce(u64, u64),
254        ChainIdMismatch,
255        AddressMismatch,
256        MessageMismatch(String),
257        FromBin(String),
258        ToBin(String),
259        ManyMessages(u8, u8),
260        InvalidEnvelope(String, String),
261        MissingData(String),
262    }
263
264    #[cfg(feature = "session")]
265    pub enum SessionError {
266        Expired,
267        InvalidGrantee,
268        InvalidGranter,
269        EmptyCreateActions,
270        EmptyPassedActions,
271        DerivationError,
272        InvalidActions,
273        InnerSessionAction,
274        NotAllowedAction,
275    }
276
277
278    #[saa_error]
279    pub enum AuthError {
280        NoCredentials,
281        MissingData(String),
282        InvalidLength(String, u16, u16),
283        RecoveryParam,
284        RecoveryMismatch,
285        InvalidSignedData,
286        PasskeyChallenge,
287        Unauthorized(String),
288        Signature(String),
289        Recovery(String),
290        Generic(String),
291        Convertation(String),
292        Crypto(String),
293        SemVer(String),
294        #[cfg(feature = "replay")]
295        Replay(String),
296        #[cfg(feature = "session")]
297        Session(String),
298    }
299
300    #[cfg(feature = "replay")]
301    impl From<ReplayError> for AuthError {
302        fn from(err: ReplayError) -> Self {
303            Self::Replay(err.to_string())
304        }
305    }
306
307    #[cfg(feature = "session")]
308    impl From<SessionError> for AuthError {
309        fn from(err: SessionError) -> Self {
310            Self::Session(err.to_string())
311        }
312    }
313
314}    
315
316
317#[cfg(feature = "std")]
318pub use std_mod::*;
319
320
321#[cfg(not(feature = "std"))]
322pub use no_std_mod::*;
323
324
325
326
327
328impl AuthError {
329    pub fn generic<M: Into<String>>(msg: M) -> Self {
330        AuthError::Generic(msg.into())
331    }
332}
333