xmtp_proto/
impls.rs

1pub mod update_dedupe;
2
3use crate::types::GlobalCursor;
4/// implementations for some generated types
5use crate::xmtp::mls::api::v1::welcome_message::Version;
6use crate::xmtp::mls::message_contents::{
7    GroupUpdated, WelcomePointeeEncryptionAeadType, WelcomePointeeEncryptionAeadTypesExtension,
8};
9use crate::xmtp::xmtpv4::envelopes::AuthenticatedData;
10use crate::xmtp::xmtpv4::envelopes::client_envelope::Payload;
11use std::hash::Hash;
12
13impl std::fmt::Display for Payload {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Payload::GroupMessage(_) => write!(f, "Payload::GroupMessage"),
17            Payload::WelcomeMessage(_) => write!(f, "Payload::WelcomeMessage"),
18            Payload::UploadKeyPackage(_) => write!(f, "Payload::UploadKeyPackage"),
19            Payload::IdentityUpdate(_) => write!(f, "Payload::IdentityUpdate"),
20            Payload::PayerReport(_) => write!(f, "Payload::PayerReport"),
21            Payload::PayerReportAttestation(_) => write!(f, "Payload::PayerReportAttestation"),
22        }
23    }
24}
25
26impl std::fmt::Display for AuthenticatedData {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        if let Some(d) = &self.depends_on {
29            let cursor: GlobalCursor = d.clone().into();
30            write!(f, "aad[{} -> {}]", hex::encode(&self.target_topic), cursor)?;
31        } else {
32            write!(
33                f,
34                "aad[{} -> (no dependency)]",
35                hex::encode(&self.target_topic)
36            )?;
37        }
38        Ok(())
39    }
40}
41
42impl Hash for GroupUpdated {
43    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
44        self.initiated_by_inbox_id.hash(state);
45        self.added_inboxes.hash(state);
46        self.removed_inboxes.hash(state);
47        self.metadata_field_changes.hash(state);
48        self.left_inboxes.hash(state);
49        self.added_admin_inboxes.hash(state);
50        self.removed_admin_inboxes.hash(state);
51        self.added_super_admin_inboxes.hash(state);
52        self.removed_super_admin_inboxes.hash(state);
53    }
54}
55
56xmtp_common::if_test! {
57    use crate::mls_v1::{group_message, welcome_message};
58    use xmtp_common::Generate;
59
60    impl Generate for welcome_message::V1 {
61        fn generate() -> Self {
62            welcome_message::V1 {
63                id: xmtp_common::rand_u64(),
64                created_ns: xmtp_common::rand_u64(),
65                installation_key: xmtp_common::rand_vec::<32>(),
66                data: xmtp_common::rand_vec::<6>(),
67                hpke_public_key: xmtp_common::rand_vec::<6>(),
68                wrapper_algorithm: 1,
69                welcome_metadata: xmtp_common::rand_vec::<12>(),
70            }
71        }
72    }
73
74    impl Generate for group_message::V1 {
75        fn generate() -> Self {
76            group_message::V1 {
77                id: xmtp_common::rand_u64(),
78                created_ns: xmtp_common::rand_u64(),
79                group_id: xmtp_common::rand_vec::<16>(),
80                data: xmtp_common::rand_vec::<6>(),
81                sender_hmac: xmtp_common::rand_vec::<6>(),
82                should_push: false,
83                is_commit: false,
84            }
85        }
86    }
87}
88
89impl Version {
90    pub fn id(&self) -> u64 {
91        match self {
92            Version::V1(v1) => v1.id,
93            Version::WelcomePointer(w) => w.id,
94        }
95    }
96    pub fn created_ns(&self) -> u64 {
97        match self {
98            Version::V1(v1) => v1.created_ns,
99            Version::WelcomePointer(w) => w.created_ns,
100        }
101    }
102    pub fn installation_key(&self) -> &[u8] {
103        match self {
104            Version::V1(v1) => v1.installation_key.as_slice(),
105            Version::WelcomePointer(w) => w.installation_key.as_slice(),
106        }
107    }
108}
109
110impl WelcomePointeeEncryptionAeadTypesExtension {
111    pub fn available_types() -> Self {
112        Self {
113            supported_aead_types: vec![WelcomePointeeEncryptionAeadType::Chacha20Poly1305.into()],
114        }
115    }
116}