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
80    #[saa_error]
81    pub enum StorageError {
82        #[error("Error reading {0} from storage: {1}")]
83        Read(String, String),
84
85        #[error("Error writing {0} to storage: {1}")]
86        Write(String, String),
87
88        #[error("The given credential already exists on this account. Failed ID: {0}")]
89        AlreadyExists(String),
90
91        #[error("The given credential was not found on this account")]
92        NotFound, 
93
94        #[cfg(feature = "wasm")]
95        #[error("Standard error: {0}")]
96        Std(#[from] crate::wasm::StdError),
97
98        #[error("Generic error: {0}")]
99        Generic(String)
100    }
101
102    #[saa_error]
103    pub enum CredentialError {
104        #[error("Not provided or partially missing")]
105        NoCredentials,
106
107        #[error("At least one credential must be kept for authorization checks")]
108        NoneLeft,
109
110        #[error("Too many credentials provided: {0}. Maximum allowed is 255")]
111        TooManyCredentials(usize),
112
113        #[error("Invalid primary index: {0}. There are only {1} credentials. (Max index is {1}-1)")]
114        IndexOutOfBounds(usize, usize),
115
116        #[error("Client requested to use a native address that called the environment as a credential, but it hasn't been set")]
117        NoNativeCaller,
118
119        #[error("One of the main properties of the credential '{0}' are missing")]
120        MissingData(CredentialName),
121
122        #[error("Error while processing the credential '{0}', One of the properties is invalid")]
123        IncorrectData(CredentialName),
124
125        #[error("The credential '{0}' is not valid. Error in property '{1}': {2}")]
126        InvalidProperty(CredentialName, String, String),
127
128        #[error("Passed only (native) credentials that aren't validated by the environment. Need to supply at least one verifyable credential")]
129        OnlyCustomNatives,
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        Recovery(String),
167
168        #[error("{0}")]
169        Generic(String),
170
171        #[error("{0}")]
172        Crypto(String),
173
174        #[error("Error converting binary to {0}")]
175        Convertion(String),
176
177        #[error("Credential: {0}")]
178        Credential(#[from] CredentialError),
179        
180        #[cfg(feature = "replay")]
181        #[error("Replay: {0}")]
182        Replay(#[from] ReplayError),
183
184        #[cfg(feature = "session")]
185        #[error("Session: {0}")]
186        Session(#[from] SessionError),
187
188        #[cfg(feature = "wasm")]
189        #[error("Storage: {0}")]
190        Storage(#[from] StorageError),
191    }
192
193
194    impl From<std::string::FromUtf8Error> for AuthError {
195        fn from(err: std::string::FromUtf8Error) -> Self {
196            Self::Recovery(err.to_string())
197        }
198    }
199
200
201    #[cfg(feature = "eth_typed_data")] 
202    impl From<serde_json::DeserializerError> for AuthError {
203        fn from(err: serde_json::DeserializerError) -> Self {
204            Self::Generic(err.to_string())
205        }
206    }
207
208
209    #[cfg(feature = "wasm")] 
210    mod wasm {
211        use super::AuthError;
212
213        impl From<crate::wasm::RecoverPubkeyError> for AuthError {
214            fn from(err: crate::wasm::RecoverPubkeyError) -> Self {
215                Self::Recovery(err.to_string())
216            }
217        }
218
219        impl From<crate::wasm::StdError> for AuthError {
220            fn from(err: crate::wasm::StdError) -> Self {
221                Self::Generic(err.to_string())
222            }
223        }
224
225        impl From<crate::wasm::VerificationError> for AuthError {
226            fn from(err: crate::wasm::VerificationError) -> Self {
227                Self::Crypto(err.to_string())
228            }
229        }
230    }
231
232
233    #[cfg(feature = "native")] 
234    impl From<cosmwasm_crypto::CryptoError> for AuthError {
235        fn from(err: cosmwasm_crypto::CryptoError) -> Self {
236            Self::Crypto(err.to_string())
237        }
238    }
239
240   
241
242}
243
244
245
246#[cfg(not(feature = "std"))]
247mod no_std_mod {
248    use crate::String;
249    use saa_schema::{saa_error, saa_type, strum_macros};
250
251    
252    #[cfg(feature = "replay")]
253    pub enum ReplayError {
254        DifferentNonce(u64, u64),
255        ChainIdMismatch,
256        ContractMismatch,
257    }
258
259    #[cfg(feature = "session")]
260    pub enum SessionError {
261        Expired,
262        InvalidGrantee,
263        InvalidGranter,
264        EmptyCreateActions,
265        EmptyPassedActions,
266        DerivationError,
267        InvalidActions,
268        InnerSessionAction,
269        NotAllowedAction,
270    }
271
272
273    #[saa_error]
274    pub enum AuthError {
275        NoCredentials,
276        MissingData(String),
277        InvalidLength(String, u16, u16),
278        RecoveryParam,
279        RecoveryMismatch,
280        InvalidSignedData,
281        PasskeyChallenge,
282        Unauthorized(String),
283        Signature(String),
284        Recovery(String),
285        Generic(String),
286        Convertation(String),
287        Crypto(String),
288        SemVer(String),
289        #[cfg(feature = "replay")]
290        Replay(String),
291        #[cfg(feature = "session")]
292        Session(String),
293    }
294
295    #[cfg(feature = "replay")]
296    impl From<ReplayError> for AuthError {
297        fn from(err: ReplayError) -> Self {
298            Self::Replay(err.to_string())
299        }
300    }
301
302    #[cfg(feature = "session")]
303    impl From<SessionError> for AuthError {
304        fn from(err: SessionError) -> Self {
305            Self::Session(err.to_string())
306        }
307    }
308
309}    
310
311
312#[cfg(feature = "std")]
313pub use std_mod::*;
314
315
316#[cfg(not(feature = "std"))]
317pub use no_std_mod::*;
318
319
320
321
322
323impl AuthError {
324    pub fn generic<M: Into<String>>(msg: M) -> Self {
325        AuthError::Generic(msg.into())
326    }
327}
328