Skip to main content

quartz_contract_core/msg/execute/
session_create.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{HexBinary, StdError};
3use sha2::{Digest, Sha256};
4
5use crate::{
6    msg::{execute::attested::HasUserData, HasDomainType},
7    state::{Nonce, UserData},
8};
9
10#[derive(Clone, Debug, PartialEq)]
11pub struct SessionCreate {
12    nonce: Nonce,
13    contract: String,
14}
15
16impl SessionCreate {
17    pub fn new(nonce: Nonce, contract: String) -> Self {
18        Self { nonce, contract }
19    }
20
21    pub fn nonce(&self) -> Nonce {
22        self.nonce
23    }
24
25    pub fn contract(&self) -> &str {
26        self.contract.as_str()
27    }
28}
29
30#[cw_serde]
31pub struct RawSessionCreate {
32    nonce: HexBinary,
33    contract: String,
34}
35
36impl TryFrom<RawSessionCreate> for SessionCreate {
37    type Error = StdError;
38
39    fn try_from(value: RawSessionCreate) -> Result<Self, Self::Error> {
40        let nonce = value.nonce.to_array()?;
41        let contract = value.contract;
42        Ok(Self { nonce, contract })
43    }
44}
45
46impl From<SessionCreate> for RawSessionCreate {
47    fn from(value: SessionCreate) -> Self {
48        Self {
49            nonce: value.nonce.into(),
50            contract: value.contract,
51        }
52    }
53}
54
55impl HasDomainType for RawSessionCreate {
56    type DomainType = SessionCreate;
57}
58
59impl HasUserData for SessionCreate {
60    fn user_data(&self) -> UserData {
61        let mut hasher = Sha256::new();
62        hasher.update(
63            serde_json::to_string(&RawSessionCreate::from(self.clone()))
64                .expect("infallible serializer"),
65        );
66        let digest: [u8; 32] = hasher.finalize().into();
67
68        let mut user_data = [0u8; 64];
69        user_data[0..32].copy_from_slice(&digest);
70        user_data
71    }
72}