Skip to main content

smart_account_auth/
caller.rs

1use saa_common::{
2    AuthError, CredentialError, CredentialId, CredentialName, Identifiable, Verifiable
3};
4
5
6use CredentialName::Native;
7
8
9#[saa_schema::saa_type]
10pub struct Caller(pub CredentialId);
11
12
13impl From<&str> for Caller {
14    fn from(addr: &str) -> Self {
15        Caller(addr.to_string())
16    }
17}
18
19
20impl Identifiable for Caller {
21
22    fn cred_id(&self) -> CredentialId {
23        self.0.to_lowercase()
24    }
25
26    fn name(&self) -> CredentialName {
27        Native
28    }
29}
30
31
32impl Verifiable for Caller {
33
34     fn message(&self) -> std::borrow::Cow<'_, [u8]> {
35        std::borrow::Cow::Owned(vec![])
36    }
37
38    fn validate(&self) -> Result<(), AuthError> {
39        saa_common::ensure!(self.0.len() > 3, CredentialError::MissingData(Native));
40        Ok(())
41    }
42    
43    #[cfg(any(feature = "native", feature = "wasm"))]  
44    fn verify(&self,
45        #[cfg(feature = "wasm")]
46        deps: saa_common::wasm::Deps
47    ) -> Result<saa_common::CredentialInfo, AuthError> {
48        #[cfg(feature = "wasm")]
49        let address = deps.api.addr_validate(self.0.as_str())?;
50        #[cfg(feature = "wasm")]
51        let hrp = address.as_str().split("1").next().map(|s| s.to_string());
52        #[cfg(not(feature = "wasm"))]
53        let address = self.0.clone();
54        #[cfg(not(feature = "wasm"))]
55        let hrp = None;
56        
57        Ok(saa_common::CredentialInfo {
58            hrp,
59            extension: None,
60            name: Native,
61            address: Some(saa_common::CredentialAddress::Bech32(address)),
62        })
63    }
64}
65
66
67#[cfg(feature = "replay")]
68impl saa_crypto::ReplayProtection for Caller {
69
70
71    #[cfg(any(feature = "cosmwasm", feature = "native"))]
72    fn protect_reply(
73        &self,
74        #[cfg(feature = "wasm")]
75        _: &saa_common::wasm::Env, 
76        _: saa_crypto::ReplayParams,
77    ) -> Result<(), saa_common::ReplayError> {
78        return Ok(());
79    }
80}