coven_sync/pairing_types.rs
1//! What the screen that adds a device sees of a pairing (§13.2, E10).
2
3use coven_crypto::ConfirmationNumber;
4use coven_foundation::id_source::{DeviceId, InviteId};
5use serde::{Deserialize, Serialize};
6use std::time::SystemTime;
7use uuid::Uuid;
8
9/// A pairing's UUID, which names its request's path in an invite's place
10/// (§13.2, format.md D10).
11#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct AddDeviceId(
14 /// The pairing's UUID, supplied by the id source.
15 pub Uuid,
16);
17
18impl From<InviteId> for AddDeviceId {
19 fn from(id: InviteId) -> Self {
20 Self(id.0)
21 }
22}
23
24impl From<AddDeviceId> for InviteId {
25 fn from(id: AddDeviceId) -> Self {
26 Self(id.0)
27 }
28}
29
30/// A pairing `add_device` made: what the screen that adds a device shows.
31#[derive(Clone, Debug, PartialEq, Eq)]
32pub struct AddDevice {
33 /// Names the pairing in every later call.
34 pub id: AddDeviceId,
35 /// The code to show as a QR code, behind a tap. On S3 it carries the key
36 /// this device connects with, so the new device asks for none: whoever
37 /// sees it reaches the storage as this member until the key is replaced
38 /// (§14.3).
39 pub qr_code: String,
40 /// The code to copy or share as text. It carries no key; on S3 the new
41 /// device asks the person for theirs. On other providers it is `qr_code`.
42 pub shared_code: String,
43 /// 30 minutes after it was made, by this device's clock.
44 pub expires_at: SystemTime,
45}
46
47/// Where a pairing is (§13.2). The app offers approving and declining only
48/// for a request that is `Waiting`, and ends the pairing when its screen
49/// closes, whatever its status.
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub enum AddDeviceStatus {
52 /// Waiting for a request, then with the checked request, for the person
53 /// to compare its number and decide.
54 Waiting {
55 /// The checked request, once one has come.
56 request: Option<AddDeviceRequest>,
57 },
58 /// The person approved `request`: its handover is on its way to the
59 /// request's place, or there, waiting for the new device to register.
60 Approved {
61 /// The approved request.
62 request: AddDeviceRequest,
63 },
64 /// The pairing has ended, its marker in place or on its way; the app
65 /// shows how until it ends the pairing and forgets it.
66 Ended(AddDeviceEnd),
67}
68
69/// How a pairing ended (§13.2).
70#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
71pub enum AddDeviceEnd {
72 /// This device applied the new device's add-device entry.
73 Added {
74 /// The device the request named.
75 device: DeviceId,
76 /// The name the new device gave itself.
77 name: String,
78 },
79 /// The person declined the request.
80 Declined,
81 /// The request failed a check, such as answering for another member or
82 /// carrying keys history knows.
83 Refused,
84 /// 30 minutes passed by this device's clock first.
85 Expired,
86}
87
88/// A checked pairing request, as `AddDeviceStatus` shows it (§13.2).
89#[derive(Clone, Debug, PartialEq, Eq)]
90pub struct AddDeviceRequest {
91 /// The pairing it answers.
92 pub pairing: AddDeviceId,
93 /// The name the new device gave itself, for the person to check.
94 pub device_name: String,
95 /// Computed from the pairing id and the request's keys; the new device
96 /// shows the same number (format.md D11).
97 pub confirmation: ConfirmationNumber,
98 /// The checked request's exact bytes, which approving compares with the
99 /// pairing's own.
100 pub(crate) record: Vec<u8>,
101}