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("Error converting from binary to {0}")]
59        FromBin(String),
60
61        #[error("Error serializing to binary from: {0}")]
62        ToBin(String),
63
64        #[error("Signed too many messages. Expected: {0}; Received: {1}")]
65        ManyMessages(u8, u8),
66
67        #[error("Invalid signed envelope. Check that chain_id`s, contract_address`s, nonce`s amd messages` match")]
68        InvalidEnvelope,
69
70        #[error("The data for the replay protection is missing or invalid. Item: {0}")]
71        MissingData(String),
72
73    }
74
75
76
77    #[saa_error]
78    pub enum StorageError {
79        #[error("Error reading {0} from storage: {1}")]
80        Read(String, String),
81
82        #[error("Error writing {0} to storage: {1}")]
83        Write(String, String),
84
85        #[error("The given credential already exists on this account. Failed ID: {0}")]
86        AlreadyExists(String),
87
88        #[error("The given credential was not found on this account")]
89        NotFound, 
90
91        #[cfg(feature = "wasm")]
92        #[error("Standard error: {0}")]
93        Std(#[from] crate::wasm::StdError),
94
95        #[error("Generic error: {0}")]
96        Generic(String)
97    }
98
99    #[saa_error]
100    pub enum CredentialError {
101        #[error("Not provided or partially missing")]
102        NoCredentials,
103
104        #[error("At least one credential must be kept for authorization checks")]
105        NoneLeft,
106
107        #[error("Too many credentials provided: {0}. Maximum allowed is 255")]
108        TooManyCredentials(usize),
109
110        #[error("Invalid primary index: {0}. There are only {1} credentials. (Max index is {1}-1)")]
111        IndexOutOfBounds(usize, usize),
112
113        #[error("Client requested to use a native address that called the environment as a credential, but it hasn't been set")]
114        NoNativeCaller,
115
116        #[error("One of the main properties of the credential '{0}' are missing")]
117        MissingData(CredentialName),
118
119        #[error("Error while processing the credential '{0}', One of the properties is invalid")]
120        IncorrectData(CredentialName),
121
122        #[error("The credential '{0}' is not valid. Error in property '{1}': {2}")]
123        InvalidProperty(CredentialName, String, String),
124
125        #[error("Passed only (native) credentials that aren't validated by the environment. Need to supply at least one verifyable credential")]
126        OnlyCustomNatives,
127
128        #[error("The credential '{0}' was expecting an info object with a property '{1}', however it wasn't provided or was empty")]
129        NoInfoProperty(CredentialName, String),
130
131        #[error("The credential '{0}' needs an extended info to be passed. It hasn't been done or there was an error")]
132        NoInfoExt(CredentialName),
133
134        #[cfg(feature = "wasm")]
135        #[error("(Std) Serialization error: {0}")]
136        Std(#[from] crate::wasm::StdError),
137    }
138
139
140
141    #[saa_error]
142    pub enum AuthError {
143
144        #[error("Missing {0}")]
145        MissingData(String),
146
147        #[error("Values of v other than 27 and 28 not supported. Replay protection (EIP-155) cannot be used here.")]
148        RecoveryParam,
149        
150        #[error("Error recovering from the signature: Addresses do not match")]
151        RecoveryMismatch,
152
153        #[error("Unauthorized: {0}")]
154        Unauthorized(String),
155
156        #[error("Invalid length for type '{0}'. Expected: {1}, Received: {2}")]
157        InvalidLength(String, u16, u16),
158
159        #[error("Signature verification error for {0} with id of '{1}'")]
160        Signature(CredentialName, String),
161
162        #[error("{0}")]
163        Recovery(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::Recovery(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::Recovery(err.to_string())
213            }
214        }
215
216        impl From<crate::wasm::StdError> for AuthError {
217            fn from(err: crate::wasm::StdError) -> Self {
218                Self::Generic(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        ContractMismatch,
254    }
255
256    #[cfg(feature = "session")]
257    pub enum SessionError {
258        Expired,
259        InvalidGrantee,
260        InvalidGranter,
261        EmptyCreateActions,
262        EmptyPassedActions,
263        DerivationError,
264        InvalidActions,
265        InnerSessionAction,
266        NotAllowedAction,
267    }
268
269
270    #[saa_error]
271    pub enum AuthError {
272        NoCredentials,
273        MissingData(String),
274        InvalidLength(String, u16, u16),
275        RecoveryParam,
276        RecoveryMismatch,
277        InvalidSignedData,
278        PasskeyChallenge,
279        Unauthorized(String),
280        Signature(String),
281        Recovery(String),
282        Generic(String),
283        Convertation(String),
284        Crypto(String),
285        SemVer(String),
286        #[cfg(feature = "replay")]
287        Replay(String),
288        #[cfg(feature = "session")]
289        Session(String),
290    }
291
292    #[cfg(feature = "replay")]
293    impl From<ReplayError> for AuthError {
294        fn from(err: ReplayError) -> Self {
295            Self::Replay(err.to_string())
296        }
297    }
298
299    #[cfg(feature = "session")]
300    impl From<SessionError> for AuthError {
301        fn from(err: SessionError) -> Self {
302            Self::Session(err.to_string())
303        }
304    }
305
306}    
307
308
309#[cfg(feature = "std")]
310pub use std_mod::*;
311
312
313#[cfg(not(feature = "std"))]
314pub use no_std_mod::*;
315
316
317
318
319
320impl AuthError {
321    pub fn generic<M: Into<String>>(msg: M) -> Self {
322        AuthError::Generic(msg.into())
323    }
324}
325