rings_node/mail/
descriptor.rs1use std::borrow::Borrow;
21
22use rings_core::delegation::DelegateeKey;
23use rings_core::dht::Did;
24use rings_core::domain_tag;
25use rings_core::ecc::PublicKey;
26use rings_core::ecc::VerificationPublicKey;
27use rings_core::error::Error;
28use rings_core::error::Result;
29use rings_core::message::DomainTag;
30use rings_core::message::MessageSigner;
31use rings_core::message::MessageVerification;
32use rings_core::message::SigningDomain;
33use serde::Deserialize;
34use serde::Serialize;
35
36use crate::descriptor::latest_valid_by_did;
37use crate::descriptor::sign_descriptor_body;
38use crate::descriptor::RegistryElement;
39use crate::descriptor::SignedDescriptor;
40use crate::descriptor::SignedDescriptorBody;
41
42const MAIL_DESCRIPTOR_DOMAIN_TAG: DomainTag = domain_tag!("rings-node:mail-descriptor");
44
45pub fn descriptor_topic(did: Did) -> String {
50 format!("mail_descriptor:{did}")
51}
52
53#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub struct MailRecipient {
57 pub did: Did,
59 pub encryption_key: PublicKey<33>,
61}
62
63#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
65pub struct MailDescriptorBody {
66 pub did: Did,
68 pub public_key: VerificationPublicKey,
70 pub network_id: u32,
72 pub fresh_for_ms: u64,
74 pub expires_at_ms: u128,
76}
77
78impl MailDescriptorBody {
79 fn signing_data(&self) -> Result<Vec<u8>> {
81 rings_codec::serialize(self).map_err(Error::CodecSerialize)
82 }
83}
84
85impl SignedDescriptorBody for MailDescriptorBody {
86 type Descriptor = MailDescriptor;
87
88 const DOMAIN_TAG: DomainTag = MAIL_DESCRIPTOR_DOMAIN_TAG;
89
90 fn body_did(&self) -> Did {
91 self.did
92 }
93
94 fn body_public_key(&self) -> &VerificationPublicKey {
95 &self.public_key
96 }
97
98 fn body_network_id(&self) -> u32 {
99 self.network_id
100 }
101
102 fn body_signing_data(&self) -> Result<Vec<u8>> {
103 self.signing_data()
104 }
105
106 fn into_signed_descriptor(self, signature: MessageVerification) -> Self::Descriptor {
107 MailDescriptor {
108 body: self,
109 signature,
110 heartbeat_at_ms: 0,
111 }
112 }
113}
114
115#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
117pub struct MailDescriptor {
118 pub body: MailDescriptorBody,
120 pub signature: MessageVerification,
122 #[serde(skip)]
126 pub heartbeat_at_ms: u128,
127}
128
129impl MailDescriptor {
130 pub fn new_signed(
132 body: MailDescriptorBody,
133 signer: MessageSigner<impl Borrow<DelegateeKey>>,
134 ) -> Result<Self> {
135 sign_descriptor_body(
136 body,
137 signer,
138 "mail descriptor DID/public key/session mismatch",
139 )
140 }
141
142 pub fn encryption_key_at(&self, network_id: u32, now_ms: u128) -> Option<PublicKey<33>> {
145 let domain = SigningDomain::new(MAIL_DESCRIPTOR_DOMAIN_TAG, network_id);
146 let data = self.body.signing_data().ok()?;
147 self.signature.signer_key_at(domain, &data, now_ms)
148 }
149
150 pub fn live_recipient(
154 descriptors: impl IntoIterator<Item = Self>,
155 did: Did,
156 now_ms: u128,
157 network_id: u32,
158 ) -> Option<MailRecipient> {
159 let descriptor = latest_valid_by_did(
160 descriptors
161 .into_iter()
162 .filter(|descriptor| descriptor.body.did == did),
163 now_ms,
164 network_id,
165 false,
166 )
167 .into_iter()
168 .next()?;
169 let encryption_key = descriptor.encryption_key_at(network_id, now_ms)?;
170 Some(MailRecipient {
171 did,
172 encryption_key,
173 })
174 }
175}
176
177impl SignedDescriptor for MailDescriptor {
178 type Body = MailDescriptorBody;
179
180 fn descriptor_did(&self) -> Did {
181 self.body.did
182 }
183
184 fn descriptor_public_key(&self) -> &VerificationPublicKey {
185 &self.body.public_key
186 }
187
188 fn descriptor_network_id(&self) -> u32 {
189 self.body.network_id
190 }
191
192 fn descriptor_signature(&self) -> &MessageVerification {
193 &self.signature
194 }
195
196 fn descriptor_heartbeat_at_ms(&self) -> u128 {
197 self.heartbeat_at_ms
198 }
199
200 fn descriptor_expires_at_ms(&self) -> u128 {
201 self.body.expires_at_ms
202 }
203
204 fn descriptor_fresh_for_ms(&self) -> u64 {
205 self.body.fresh_for_ms
206 }
207
208 fn descriptor_signing_data(&self) -> Result<Vec<u8>> {
209 self.body.signing_data()
210 }
211
212 fn heard_at(self, heartbeat_at_ms: u128) -> Self {
213 Self {
214 heartbeat_at_ms,
215 ..self
216 }
217 }
218}
219
220impl RegistryElement for MailDescriptor {}