saa_auth/
caller.rs

1#[cfg(feature = "wasm")]
2use saa_common::{wasm::{Api, MessageInfo}, utils::prefix_from_address};
3use saa_common::{AuthError, CredentialId, Verifiable};
4use saa_schema::saa_type;
5
6
7#[saa_type]
8pub struct Caller(
9    #[cfg_attr(feature = "wasm", schemars(with = "String"))]
10    pub CredentialId
11);
12
13
14
15impl From<&str> for Caller {
16    fn from(s: &str) -> Self {
17        Caller(s.to_string())
18    }
19}
20
21#[cfg(feature = "wasm")]
22impl From<&MessageInfo> for Caller {
23    fn from(info: &MessageInfo) -> Self {
24        Caller(info.sender.to_string())
25    }
26}
27
28
29
30impl Verifiable for Caller {
31
32    fn id(&self) -> CredentialId {
33        self.0.clone()
34    }
35
36    fn hrp(&self) -> Option<String> {
37        #[cfg(feature = "wasm")]
38        {
39            return Some(prefix_from_address(&self.0))
40        }
41        None
42    }
43
44    fn validate(&self) -> Result<(), AuthError> {
45        saa_common::ensure!(
46            self.0.len() > 0,
47            AuthError::MissingData("Missing calling address".to_string())
48        );
49        Ok(())
50    }
51
52    #[cfg(feature = "native")]
53    fn verify(&self) -> Result<(), AuthError> {
54        self.validate()
55    }
56
57    #[cfg(feature = "wasm")]
58    fn verify_cosmwasm(&self, api: &dyn Api) -> Result<(), AuthError> {
59        api.addr_validate(self.0.as_str())?;
60        Ok(())
61    }
62
63}