Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better devX for attested messages in app contracts #55

Merged
merged 2 commits into from
Jun 18, 2024

Conversation

hu55a1n1
Copy link
Member

@hu55a1n1 hu55a1n1 commented Jun 18, 2024

Closes: #54

With these changes the UpdateMsg in the transfer contract is as simple as ->

#[cw_serde]
pub enum ExecuteMsg {
    Update(RawAttested<RawAttestedMsgSansHandler<execute::UpdateMsg>, RawEpidAttestation>),
}

pub mod execute {
    use quartz_cw::{msg::execute::attested::HasUserData, state::UserData};
    use sha2::{Digest, Sha256};

    use super::*;

    #[cw_serde]
    pub struct UpdateMsg {
        pub ciphertext: HexBinary,
        pub quantity: u32,
        pub withdrawals: BTreeMap<Addr, Uint128>,
        // pub proof: π
    }

    impl HasUserData for UpdateMsg {
        fn user_data(&self) -> UserData {
            let mut hasher = Sha256::new();
            hasher.update(serde_json::to_string(&self).expect("infallible serializer"));
            let digest: [u8; 32] = hasher.finalize().into();

            let mut user_data = [0u8; 64];
            user_data[0..32].copy_from_slice(&digest);
            user_data
        }
    }
}

And what that looked like before ->

#[cw_serde]
pub enum ExecuteMsg {
    Update(RawAttested<execute::RawUpdateMsg, RawEpidAttestation>),
}

pub mod execute {
    // use ...

    use super::*;

    #[cw_serde]
    pub struct RawUpdateMsg {
        pub ciphertext: HexBinary,
        pub quantity: u32,
        pub withdrawals: BTreeMap<Addr, Uint128>,
        // pub proof: π
    }

    #[derive(Clone, Debug, PartialEq)]
    pub struct UpdateMsg(pub RawUpdateMsg);

    impl HasUserData for UpdateMsg {
        fn user_data(&self) -> UserData {
            let mut hasher = Sha256::new();
            hasher.update(serde_json::to_string(&self.0).expect("infallible serializer"));
            let digest: [u8; 32] = hasher.finalize().into();

            let mut user_data = [0u8; 64];
            user_data[0..32].copy_from_slice(&digest);
            user_data
        }
    }

    impl HasDomainType for RawUpdateMsg {
        type DomainType = UpdateMsg;
    }

    impl TryFrom<RawUpdateMsg> for UpdateMsg {
        type Error = StdError;

        fn try_from(value: RawUpdateMsg) -> Result<Self, Self::Error> {
            Ok(Self(value))
        }
    }

    impl From<UpdateMsg> for RawUpdateMsg {
        fn from(value: UpdateMsg) -> Self {
            value.0
        }
    }

    impl Handler for UpdateMsg {
        fn handle(
            self,
            _deps: DepsMut<'_>,
            _env: &Env,
            _info: &MessageInfo,
        ) -> Result<Response, Error> {
            // basically handle `transfer_request` here
            Ok(Response::default())
        }
    }
}

@hu55a1n1 hu55a1n1 marked this pull request as ready for review June 18, 2024 20:15
@hu55a1n1 hu55a1n1 merged commit 99f2974 into main Jun 18, 2024
7 checks passed
@hu55a1n1 hu55a1n1 deleted the hu55a1n1/54-attested-app-msg branch June 18, 2024 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better devX for defining attested messages in apps' contracts
1 participant