saa_custom/
caller.rs

1#[cfg(feature = "wasm")]
2use saa_common::{cosmwasm::{Api, MessageInfo}, utils::prefix_from_address};
3use saa_common::{ensure, AuthError, CredentialId, ToString, Verifiable};
4use saa_schema::wasm_serde;
5
6
7#[wasm_serde]
8pub struct Caller {
9    pub id: CredentialId
10}
11
12
13#[cfg(feature = "substrate")]
14impl From<&[u8]> for Caller {
15    fn from(bytes: &[u8]) -> Self {
16        Caller {
17            id: bytes.to_vec()
18        }
19    }
20}
21
22
23#[cfg(feature = "wasm")]
24impl From<&MessageInfo> for Caller {
25    fn from(info: &MessageInfo) -> Self {
26        Caller {
27            id: info.sender.as_bytes().to_vec()
28        }
29    }
30}
31
32
33
34impl Verifiable for Caller {
35
36    fn id(&self) -> CredentialId {
37        self.id.clone()
38    }
39
40    fn hrp(&self) -> Option<String> {
41        #[cfg(feature = "wasm")]
42        {
43            let res = String::from_utf8(self.id.clone());
44            if res.is_err() {
45                return None;
46            }
47            return Some(prefix_from_address(res.unwrap().as_str()));
48        }
49        None
50    }
51
52    fn validate(&self) -> Result<(), AuthError> {
53        let id = self.id();
54        if !(id.len() > 3) {
55            return Err(AuthError::MissingData("Caller must have an id".to_string()));
56        }
57        ensure!(String::from_utf8(id).is_ok(), AuthError::generic("Can't derive calling address"));
58        Ok(())
59    }
60
61    #[cfg(feature = "native")]
62    fn verify(&self) -> Result<(), AuthError> {
63        self.validate()
64    }
65
66
67    #[cfg(feature = "wasm")]
68    fn verify_cosmwasm(& self, _: &dyn Api) -> Result<(), AuthError> {
69        self.validate()
70    }
71
72}