Skip to main content

weirdnet_device_link/
wire.rs

1//! Messages on `/weirdnet/link/2` and the sealed transfer.
2use serde::{Deserialize, Serialize};
3use weirdnet_keys::Sealed;
4use weirdnet_net::user::NodeCert;
5use zeroize::Zeroize;
6
7/// From the existing device to the new one.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum LinkRequest {
10    /// Start pairing: the existing device's ephemeral X25519 key.
11    Hello {
12        #[serde(with = "crate::bytes")]
13        nonce: Vec<u8>,
14        #[serde(with = "crate::bytes")]
15        eph: Vec<u8>,
16    },
17    /// The sealed [`Transfer`], after the user confirmed the SAS.
18    Transfer {
19        #[serde(with = "crate::bytes")]
20        nonce: Vec<u8>,
21        sealed: Sealed,
22    },
23    /// The user said the codes do not match (or gave up).
24    Cancel {
25        #[serde(with = "crate::bytes")]
26        nonce: Vec<u8>,
27    },
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub enum LinkReply {
32    Ok,
33    Refused { reason: String },
34}
35
36impl LinkReply {
37    pub(crate) fn refused(reason: &str) -> Self {
38        LinkReply::Refused { reason: reason.to_string() }
39    }
40}
41
42/// What moves to the new device. The seed is wiped on drop and never shown by
43/// `Debug`.
44#[derive(Clone, Serialize, Deserialize)]
45pub struct Transfer {
46    pub v: u8,
47    /// The 32-byte user-key seed (what the recovery words encode).
48    #[serde(with = "crate::bytes")]
49    pub seed: Vec<u8>,
50    pub user_id: String,
51    pub profile_name: String,
52    /// App settings worth carrying over (JSON; `{}` when none).
53    pub settings: String,
54    /// The new device's node certificate, signed by the user key.
55    pub node_cert: NodeCert,
56    /// Data types the user chose to sync (none yet).
57    pub sync: Vec<String>,
58}
59
60impl Drop for Transfer {
61    fn drop(&mut self) {
62        self.seed.zeroize();
63    }
64}
65
66impl std::fmt::Debug for Transfer {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        f.debug_struct("Transfer").field("user_id", &self.user_id).field("profile_name", &self.profile_name).finish_non_exhaustive()
69    }
70}