osmosis_authenticators/
types.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{Addr, Binary, Coin};
3
4use crate::cw_serde_struct_allow_unknown_fields;
5
6cw_serde_struct_allow_unknown_fields! {
7    // --- requests ---
8
9    pub struct OnAuthenticatorAddedRequest {
10        pub account: Addr,
11        pub authenticator_id: String,
12        pub authenticator_params: Option<Binary>,
13    }
14
15    pub struct OnAuthenticatorRemovedRequest {
16        pub account: Addr,
17        pub authenticator_id: String,
18        pub authenticator_params: Option<Binary>,
19    }
20
21    pub struct AuthenticationRequest {
22        pub authenticator_id: String,
23        pub account: Addr,
24        pub fee_payer: Addr,
25        pub fee_granter: Option<Addr>,
26        pub fee: Vec<Coin>,
27        pub msg: Any,
28        pub msg_index: u64,
29        pub signature: Binary,
30        pub sign_mode_tx_data: SignModeTxData,
31        pub tx_data: TxData,
32        pub signature_data: SignatureData,
33        pub simulate: bool,
34        pub authenticator_params: Option<Binary>,
35    }
36
37    pub struct TrackRequest {
38        pub authenticator_id: String,
39        pub account: Addr,
40        pub fee_payer: Addr,
41        pub fee_granter: Option<Addr>,
42        pub fee: Vec<Coin>,
43        pub msg: Any,
44        pub msg_index: u64,
45        pub authenticator_params: Option<Binary>,
46    }
47
48    pub struct ConfirmExecutionRequest {
49        pub authenticator_id: String,
50        pub account: Addr,
51        pub fee_payer: Addr,
52        pub fee_granter: Option<Addr>,
53        pub fee: Vec<Coin>,
54        pub msg: Any,
55        pub msg_index: u64,
56        pub authenticator_params: Option<Binary>,
57    }
58
59    // --- data ---
60
61    pub struct SignModeTxData {
62        pub sign_mode_direct: Binary,
63        pub sign_mode_textual: Option<String>, // Assuming it's a string or null
64    }
65
66    pub struct TxData {
67        pub chain_id: String,
68        pub account_number: u64,
69        pub sequence: u64,
70        pub timeout_height: u64,
71        pub msgs: Vec<Any>,
72        pub memo: String,
73    }
74
75    pub struct SignatureData {
76        pub signers: Vec<Addr>,
77        pub signatures: Vec<Binary>,
78    }
79
80    pub struct Any {
81        pub type_url: String,
82        pub value: cosmwasm_std::Binary,
83    }
84}
85
86#[cw_serde]
87pub enum AuthenticatorSudoMsg {
88    OnAuthenticatorAdded(OnAuthenticatorAddedRequest),
89    OnAuthenticatorRemoved(OnAuthenticatorRemovedRequest),
90    Authenticate(AuthenticationRequest),
91    Track(TrackRequest),
92    ConfirmExecution(ConfirmExecutionRequest),
93}
94
95#[cfg(test)]
96mod tests {
97    use cosmwasm_schema::schemars::JsonSchema;
98    use serde_json::{from_str, to_string, to_value, Value};
99
100    use super::*;
101
102    #[test]
103    fn test_any() {
104        let t = Any {
105            type_url: "type_url".to_string(),
106            value: Binary::from(vec![0x01, 0x02, 0x03]),
107        };
108
109        assert_eq!(t, with_unknown_field(t.clone()));
110        has_json_schema_impl::<Any>();
111    }
112
113    #[test]
114    fn test_on_authenticator_added_request() {
115        let t = OnAuthenticatorAddedRequest {
116            account: Addr::unchecked("account"),
117            authenticator_id: "authenticator_id".to_string(),
118            authenticator_params: Some(Binary::from(vec![0x01, 0x02, 0x03])),
119        };
120
121        assert_eq!(t, with_unknown_field(t.clone()));
122        has_json_schema_impl::<OnAuthenticatorAddedRequest>();
123    }
124
125    #[test]
126    fn test_on_authenticator_removed_request() {
127        let t = OnAuthenticatorRemovedRequest {
128            account: Addr::unchecked("account"),
129            authenticator_id: "authenticator_id".to_string(),
130            authenticator_params: Some(Binary::from(vec![0x01, 0x02, 0x03])),
131        };
132
133        assert_eq!(t, with_unknown_field(t.clone()));
134        has_json_schema_impl::<OnAuthenticatorRemovedRequest>();
135    }
136
137    #[test]
138    fn test_authentication_request() {
139        let t = AuthenticationRequest {
140            authenticator_id: "authenticator_id".to_string(),
141            account: Addr::unchecked("account"),
142            fee_payer: Addr::unchecked("fee_payer"),
143            fee_granter: None,
144            fee: vec![Coin::new(1000, "uosmo")],
145            msg: Any {
146                type_url: "type_url".to_string(),
147                value: Binary::from(vec![0x01, 0x02, 0x03]),
148            },
149            msg_index: 1,
150            signature: Binary::from(vec![0x01, 0x02, 0x03]),
151            sign_mode_tx_data: SignModeTxData {
152                sign_mode_direct: Binary::from(vec![0x01, 0x02, 0x03]),
153                sign_mode_textual: Some("sign_mode_textual".to_string()),
154            },
155            tx_data: TxData {
156                chain_id: "chain_id".to_string(),
157                account_number: 1,
158                sequence: 1,
159                timeout_height: 1,
160                msgs: vec![Any {
161                    type_url: "type_url".to_string(),
162                    value: Binary::from(vec![0x01, 0x02, 0x03]),
163                }],
164                memo: "memo".to_string(),
165            },
166            signature_data: SignatureData {
167                signers: vec![Addr::unchecked("account")],
168                signatures: vec![Binary::from(vec![0x01, 0x02, 0x03])],
169            },
170            simulate: true,
171            authenticator_params: Some(Binary::from(vec![0x01, 0x02, 0x03])),
172        };
173
174        assert_eq!(t, with_unknown_field(t.clone()));
175        has_json_schema_impl::<AuthenticationRequest>();
176    }
177
178    #[test]
179    fn test_sign_mode_tx_data() {
180        let t = SignModeTxData {
181            sign_mode_direct: Binary::from(vec![0x01, 0x02, 0x03]),
182            sign_mode_textual: Some("sign_mode_textual".to_string()),
183        };
184
185        assert_eq!(t, with_unknown_field(t.clone()));
186        has_json_schema_impl::<SignModeTxData>();
187    }
188
189    #[test]
190    fn test_tx_data() {
191        let t = TxData {
192            chain_id: "chain_id".to_string(),
193            account_number: 1,
194            sequence: 1,
195            timeout_height: 1,
196            msgs: vec![Any {
197                type_url: "type_url".to_string(),
198                value: Binary::from(vec![0x01, 0x02, 0x03]),
199            }],
200            memo: "memo".to_string(),
201        };
202
203        assert_eq!(t, with_unknown_field(t.clone()));
204        has_json_schema_impl::<TxData>();
205    }
206
207    #[test]
208    fn test_signature_data() {
209        let t = SignatureData {
210            signers: vec![Addr::unchecked("account")],
211            signatures: vec![Binary::from(vec![0x01, 0x02, 0x03])],
212        };
213
214        assert_eq!(t, with_unknown_field(t.clone()));
215        has_json_schema_impl::<SignatureData>();
216    }
217
218    #[test]
219    fn test_track_request() {
220        let t = TrackRequest {
221            authenticator_id: "authenticator_id".to_string(),
222            account: Addr::unchecked("account"),
223            fee_payer: Addr::unchecked("fee_payer"),
224            fee_granter: None,
225            fee: vec![Coin::new(1000, "uosmo")],
226            msg: Any {
227                type_url: "type_url".to_string(),
228                value: Binary::from(vec![0x01, 0x02, 0x03]),
229            },
230            msg_index: 1,
231            authenticator_params: Some(Binary::from(vec![0x01, 0x02, 0x03])),
232        };
233
234        assert_eq!(t, with_unknown_field(t.clone()));
235        has_json_schema_impl::<TrackRequest>();
236    }
237
238    #[test]
239    fn test_confirm_execution_request() {
240        let t = ConfirmExecutionRequest {
241            authenticator_id: "authenticator_id".to_string(),
242            account: Addr::unchecked("account"),
243            fee_payer: Addr::unchecked("fee_payer"),
244            fee_granter: None,
245            fee: vec![Coin::new(1000, "uosmo")],
246            msg: Any {
247                type_url: "type_url".to_string(),
248                value: Binary::from(vec![0x01, 0x02, 0x03]),
249            },
250            msg_index: 1,
251            authenticator_params: Some(Binary::from(vec![0x01, 0x02, 0x03])),
252        };
253
254        assert_eq!(t, with_unknown_field(t.clone()));
255        has_json_schema_impl::<ConfirmExecutionRequest>();
256    }
257
258    #[test]
259    fn test_sudo_msg() {
260        has_json_schema_impl::<AuthenticatorSudoMsg>();
261    }
262
263    fn with_unknown_field<
264        T: cosmwasm_schema::serde::Serialize + cosmwasm_schema::serde::de::DeserializeOwned,
265    >(
266        t: T,
267    ) -> T {
268        let json = to_value(t).unwrap();
269
270        let json = match json {
271            Value::Object(mut map) => {
272                map.entry("unknown")
273                    .or_insert(Value::String("unknown".to_string()));
274
275                Value::Object(map)
276            }
277            _ => panic!("expected object"),
278        };
279
280        let json_string = to_string(&json).unwrap();
281
282        from_str::<T>(json_string.as_str()).unwrap()
283    }
284
285    fn has_json_schema_impl<T: JsonSchema>() {}
286}