smart_account_auth/
wrapper.rs1#[cfg(feature = "replay")]
2use saa_crypto::ReplayProtection;
3#[cfg(feature = "wasm")]
4use saa_common::wasm::{Deps, Env, MessageInfo};
5use saa_common::{AuthError, CredentialId, CredentialName, Identifiable, Vec};
6
7
8pub trait CredentialsWrapper {
9 #[cfg(not(feature = "replay"))]
10 type Credential : saa_common::Verifiable + Clone;
11 #[cfg(feature = "replay")]
12 type Credential : ReplayProtection + Clone;
13
14 fn credentials(&self) -> &Vec<Self::Credential>;
15
16 fn primary_index(&self) -> Option<usize> {
17 None
18 }
19
20 fn validate(&self, sender: impl AsRef<str> )-> Result<(), AuthError>;
21
22
23 #[cfg(any(feature = "native", feature = "wasm"))]
24 fn verify(&self,
25 #[cfg(feature = "wasm")]
26 deps: Deps, env: &Env, info: &MessageInfo,
27 #[cfg(not(feature = "wasm"))]
28 sender: String,
29 #[cfg(feature = "replay")]
30 params: saa_crypto::ReplayParams,
31 ) -> Result<crate::VerifiedData, AuthError>;
32
33
34 fn primary(&self) -> &Self::Credential {
35 let creds = self.credentials();
36 if let Some(index) = self.primary_index() {
37 return &creds[index];
38 } else {
39 return &creds[0];
40 }
41 }
42
43 fn primary_id(&self) -> CredentialId {
44 self.primary().cred_id()
45 }
46
47
48 #[cfg(feature = "utils")]
49 fn count(&self) -> usize {
50 self.credentials().len()
51 }
52
53 #[cfg(feature = "utils")]
54 fn names(&self) -> Vec<String> {
55 self.credentials()
56 .iter()
57 .map(|c| c.name().to_string())
58 .collect()
59 }
60
61 #[cfg(feature = "utils")]
62 fn secondaries(&self) -> Vec<&Self::Credential> {
63 use saa_common::vec;
64 if self.count() <= 1 { return vec![] };
65 let primary_id = self.primary_id().to_lowercase();
66 self.credentials()
67 .into_iter()
68 .filter(|c| c.cred_id() != primary_id)
69 .collect()
70 }
71
72
73 fn cred_index(
74 &self,
75 id: &CredentialId,
76 name: CredentialName
77 ) -> Option<usize> {
78 self.credentials().iter()
79 .position(|c| c.name() == name && *id == c.cred_id())
80 }
81
82
83}