Skip to main content

quartz_contract_core/msg/execute/
signed.rs

1use std::fmt::Debug;
2
3use cosmwasm_schema::cw_serde;
4use cosmwasm_std::StdError;
5
6use super::attested::Noop;
7use crate::{error::Error, msg::HasDomainType};
8
9pub type AnySigned<M, P, S> = Signed<M, AnyAuth<P, S>>;
10
11#[derive(Clone, Debug, PartialEq)]
12pub struct Signed<M, A> {
13    msg: M,
14    auth: A,
15}
16
17impl<M, A> Signed<M, A> {
18    pub fn new(msg: M, auth: A) -> Self {
19        Self { msg, auth }
20    }
21
22    pub fn into_tuple(self) -> (M, A) {
23        let Self { msg, auth } = self;
24        (msg, auth)
25    }
26
27    pub fn msg(&self) -> &M {
28        &self.msg
29    }
30
31    pub fn auth(&self) -> &A {
32        &self.auth
33    }
34}
35
36#[cw_serde]
37pub struct RawSigned<RM, RA> {
38    pub msg: RM,
39    pub auth: RA,
40}
41
42impl<RM, RA> RawSigned<RM, RA> {
43    pub fn new(msg: RM, auth: RA) -> Self {
44        Self { msg, auth }
45    }
46}
47
48impl<RM, RA> HasDomainType for RawSigned<RM, RA>
49where
50    RM: HasDomainType,
51    RA: HasDomainType,
52{
53    type DomainType = Signed<RM::DomainType, RA::DomainType>;
54}
55
56impl<RM, RA> TryFrom<RawSigned<RM, RA>> for Signed<RM::DomainType, RA::DomainType>
57where
58    RM: HasDomainType,
59    RA: HasDomainType,
60{
61    type Error = StdError;
62
63    fn try_from(value: RawSigned<RM, RA>) -> Result<Self, Self::Error> {
64        Ok(Self {
65            msg: value.msg.try_into()?,
66            auth: value.auth.try_into()?,
67        })
68    }
69}
70
71impl<RM, RA> From<Signed<RM::DomainType, RA::DomainType>> for RawSigned<RM, RA>
72where
73    RM: HasDomainType,
74    RA: HasDomainType,
75{
76    fn from(value: Signed<RM::DomainType, RA::DomainType>) -> Self {
77        Self {
78            msg: value.msg.into(),
79            auth: value.auth.into(),
80        }
81    }
82}
83
84pub trait MsgVerifier {
85    type PubKey;
86    type Sig;
87
88    fn verify(&self, pub_key: &Self::PubKey, sig: &Self::Sig) -> Result<(), Error>;
89}
90
91#[derive(Clone, Debug, PartialEq)]
92pub struct AnyAuth<P, S> {
93    pub pub_key: P,
94    pub sig: S,
95}
96
97impl<P, S> AnyAuth<P, S> {
98    pub fn new(pub_key: P, sig: S) -> Self {
99        Self { pub_key, sig }
100    }
101}
102
103pub trait Auth<P, S> {
104    fn pub_key(&self) -> &P;
105    fn sig(&self) -> &S;
106}
107
108impl<P, S> Auth<P, S> for AnyAuth<P, S> {
109    fn pub_key(&self) -> &P {
110        &self.pub_key
111    }
112
113    fn sig(&self) -> &S {
114        &self.sig
115    }
116}
117
118impl<M: MsgVerifier> MsgVerifier for Noop<M> {
119    type PubKey = M::PubKey;
120    type Sig = M::Sig;
121
122    fn verify(&self, pub_key: &Self::PubKey, sig: &Self::Sig) -> Result<(), Error> {
123        self.0.verify(pub_key, sig)
124    }
125}