Skip to main content

sew_e2ee/
ids.rs

1//! Ids and small value types. The string names (`r_…` room, 32-hex
2//! join id, `p_…` engine identity) are `sew_proto::e2ee`'s types, re-exported under the
3//! names this crate's API uses; the slot formula and the labels come from
4//! `sew_proto::e2ee::tls`.
5
6use std::fmt;
7
8pub use sew_proto::e2ee::names::{CallId as RoomName, JoinIdHex as JoinId, LkIdentity};
9pub use sew_proto::e2ee::{CallScope, ConsentState as Consent};
10pub use sew_proto::id::Id;
11
12/// A member (Sew id).
13pub type MemberId = Id;
14/// A recording (Sew id).
15pub type RecordingId = Id;
16/// An MLS epoch.
17pub type Epoch = u64;
18/// The caller's clock in **unix milliseconds**. The core never reads a clock: every input
19/// carries `now`, and certificate validity, key-package lifetimes and timers all use it.
20pub type Millis = u64;
21/// Unix milliseconds (the same clock as [`Millis`]; the alias names wall-clock uses).
22pub type UnixMs = u64;
23
24/// The key-ring size every bridge uses: the only accepted value.
25pub const KEY_RING_SIZE: u8 = sew_proto::e2ee::tls::KEY_RING_SIZE;
26
27/// A key slot (`0..64`), the SDK's key index.
28#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct Slot(u8);
30
31impl Slot {
32    /// A slot, if `< 64`.
33    pub const fn new(v: u8) -> Option<Slot> {
34        if v < KEY_RING_SIZE {
35            Some(Slot(v))
36        } else {
37            None
38        }
39    }
40
41    /// `slot(e) = (slot_base + e) mod 64`.
42    pub fn of(slot_base: u8, epoch: Epoch) -> Slot {
43        Slot(sew_proto::e2ee::tls::slot(slot_base, epoch, KEY_RING_SIZE))
44    }
45
46    /// The raw index.
47    pub const fn get(self) -> u8 {
48        self.0
49    }
50
51    /// The SDK's `i32` key index.
52    pub fn as_i32(self) -> i32 {
53        i32::from(self.0)
54    }
55
56    /// The next slot round the ring.
57    pub fn next(self) -> Slot {
58        Slot((self.0 + 1) % KEY_RING_SIZE)
59    }
60}
61
62impl fmt::Debug for Slot {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(f, "Slot({})", self.0)
65    }
66}
67
68impl fmt::Display for Slot {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        write!(f, "{}", self.0)
71    }
72}
73
74macro_rules! counter_id {
75    ($(#[$m:meta])* $name:ident) => {
76        $(#[$m])*
77        #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
78        pub struct $name(pub u64);
79
80        impl fmt::Display for $name {
81            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82                write!(f, "{}", self.0)
83            }
84        }
85    };
86}
87
88counter_id!(
89    /// A bridge fence: `Input::Installed { fence }` answers `KeyEvent::Fence { id }`.
90    FenceId
91);
92counter_id!(
93    /// A device-signature request: `Input::DeviceSignature { request }` answers
94    /// `Output::NeedDeviceSignature { request }`.
95    SignId
96);
97counter_id!(
98    /// A key-route request: `Input::Reply(id, …)` answers `Output::Send(id, …)`.
99    RequestId
100);
101
102/// `"p_" + base32(join_id)` (equal to the media engine's `identity_for`).
103pub fn lk_identity_for(join_id: &JoinId) -> LkIdentity {
104    join_id.lk_identity()
105}
106
107/// Whether `id` is exactly `"p_" + base32(join_id)`.
108pub fn identity_matches(join_id: &JoinId, id: &LkIdentity) -> bool {
109    &join_id.lk_identity() == id
110}
111
112/// The UTF-8 decimal form of an install id, as certificates carry it.
113pub fn install_id_bytes(install_id: u64) -> Vec<u8> {
114    install_id.to_string().into_bytes()
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn slot_formula_wraps() {
123        assert_eq!(Slot::of(60, 0).get(), 60);
124        assert_eq!(Slot::of(60, 4).get(), 0);
125        assert_eq!(Slot::of(0, 64).get(), 0);
126        assert_eq!(Slot::of(63, 1), Slot::new(0).unwrap_or(Slot(0)));
127        assert!(Slot::new(64).is_none());
128        assert_eq!(Slot(63).next().get(), 0);
129    }
130}