Skip to main content

rings_node/mail/
mod.rs

1//! Mail to `did@domain` through a DHT data topic (#1117): what a mail gateway writes and what a
2//! recipient, native or in a browser, reads.
3//!
4//! A gateway is a stateless SMTP receiver for every domain whose MX names it; its SMTP front is
5//! an application (`examples/mailbox`). This module is what both ends share over the DHT:
6//!
7//! ```text
8//!   recipient d:  μ(d) on descriptor_topic(d), every heartbeat          (registration)
9//!   gateway:      RCPT l@any ─▶ d = addressed_did(l) ─▶ μ(d) live?       (gateway::recipient)
10//!                 DATA m ─▶ seal(m, ek(μ)) ─▶ Extend(mail_topic(d), ttl = MAX_TTL_MS)
11//!                        ─▶ 250 at the first owner's Stored report         (gateway::deliver)
12//!   recipient d:  fetch mail_topic(d) at start and every I ≤ H / 2 ─▶ open ─▶ file, dedup
13//! ```
14//!
15//! - [`address`]: the DID a mail address names.
16//! - [`descriptor`]: the signed mail descriptor `μ(d)`, whose signature gives the key mail is
17//!   sealed to.
18//! - [`registration`]: the heartbeat that keeps `μ(d)` in the DHT under the registry lease.
19//! - [`envelope`]: a mail as DHT elements: a manifest and its chunks (pure).
20//! - [`open`]: the recipient's opening of those elements (pure).
21//! - [`gateway`]: the gateway's DHT side: the RCPT lookup and the write.
22//! - [`recipient`]: the recipient's poll and the records it keeps.
23//!
24//! Storage semantics: a mail is one `Extend` of every element with lifetime
25//! [`MAX_TTL_MS`](rings_core::consts::MAX_TTL_MS), so it lives `H` (100 minutes) after the
26//! write and is then lost if its recipient has not fetched it. A mail is lost too when it is
27//! sealed to a delegatee key its recipient no longer holds: a browser node takes a new key at
28//! every load (`recipient`'s one-key law). The gateway's `250` means that
29//! an owner reported holding the mail (#866); a read-back would be the writer's own cache
30//! (#948, #982), not an owner's statement. The recipient never removes: each element retires at
31//! its horizon.
32//!
33//! Layering: application code over core's public storage API; core knows nothing of mail.
34
35pub mod address;
36pub mod descriptor;
37pub mod envelope;
38pub mod gateway;
39pub mod open;
40pub mod recipient;
41pub mod registration;
42
43use crate::prelude::rings_core::dht::Did;
44
45/// Write bytes in lowercase hexadecimal. Law: each byte contributes exactly two digits in
46/// input order, preserving leading zeroes in every mail digest and record-name key.
47fn write_lowercase_hex(
48    writer: &mut (impl std::fmt::Write + ?Sized),
49    bytes: &[u8],
50) -> std::fmt::Result {
51    bytes
52        .iter()
53        .try_for_each(|byte| write!(writer, "{byte:02x}"))
54}
55
56/// The data topic a recipient's mail is written to: `"mail:" ‖ d`.
57///
58/// Law: injective in `did`, and disjoint from [`descriptor::descriptor_topic`] and from every
59/// registry and DNS relay topic, none of which starts with `mail:`.
60pub fn mail_topic(did: Did) -> String {
61    format!("mail:{did}")
62}
63
64#[cfg(test)]
65mod tests;