Skip to main content

weirdnet_device_link/
lib.rs

1//! `weirdnet-device-link/v1`: move a Weirdnet user identity to another device of
2//! the same person without typing its recovery words.
3//!
4//! The **new device** (no identity yet) shows an [`Offer`] as a QR code or link:
5//! its node key, a fresh X25519 key, its addresses and a 120 s expiry, signed by
6//! the node key. The **existing device** (holds the identity) scans it, connects
7//! over Weirdnet on `/weirdnet/link/2`, and both derive a key and a 6-digit
8//! short authentication string (SAS) from the X25519 agreement and a transcript
9//! hash. Only after the user confirms the SAS matches on the existing device does
10//! it send the [`Transfer`] (seed, profile name, a node certificate for the new
11//! device), sealed with that key. The offer is single use and dies after three
12//! pairing attempts, a cancel, a transfer or its expiry.
13//!
14//! A server that must not hold the seed gets a [`Credential`] instead (a *service
15//! link*): the user's public key, a node certificate and an opaque sync bundle.
16//!
17//! Nothing secret is in the offer, and nothing here logs secrets.
18mod bytes;
19pub mod compact;
20pub mod invite;
21pub mod net;
22pub mod offer;
23pub mod reach;
24pub mod relays;
25pub mod session;
26pub mod wire;
27
28pub use compact::KNOWN_RELAYS;
29pub use invite::{Invite, InviteHost, answer_invite, decode_invite, encode_invite, is_invite};
30pub use net::{HostNode, Pairing};
31pub use offer::{Offer, VerifiedOffer, decode_link, encode_link, is_offer, offer_addrs, offer_scheme};
32pub use reach::{known_relays, link_node_config, parse_relays, relay_groups, relays_from_env, with_link_limits};
33pub use relays::{RelayProbe, RelayState, relay_states};
34pub use session::make_credential;
35pub use session::{Host, HostStatus, Sender};
36pub use wire::{CREDENTIAL_VERSION, Credential, LinkReply, LinkRequest, MAX_BUNDLE, Received, Transfer};
37
38use weirdnet_net::profile::{Profile, WEIRDNET};
39
40/// Protocol version in offers, invites and transfers. 2: compact offers and
41/// invites (CBOR arrays, binary addresses, the known relay by index).
42pub const VERSION: u8 = 2;
43/// How long an offer lives.
44pub const OFFER_TTL_MS: i64 = 120_000;
45/// Pairing attempts an offer accepts before it dies.
46pub const MAX_ATTEMPTS: u32 = 3;
47
48/// The Weirdnet network with this protocol's own stream id, so other nodes
49/// ignore it (and it ignores them).
50pub static LINK_PROFILE: Profile = Profile { protocol: "/weirdnet/link/2", thread: "weirdnet-link", ..WEIRDNET };
51
52#[derive(Debug, thiserror::Error)]
53pub enum Error {
54    #[error("{0}")]
55    Invalid(&'static str),
56    /// The code comes from an older protocol version. Apps that want their
57    /// own name in the message (`an older <App>`) match on this variant.
58    #[error("this code is from an older version; update both devices")]
59    OlderCode,
60    /// The code comes from a newer protocol version (see [`Error::OlderCode`]).
61    #[error("this code is from a newer version; update this device")]
62    NewerCode,
63    #[error("this code expired; show a new one on the other device")]
64    Expired,
65    #[error("the other device refused: {0}")]
66    Refused(String),
67    /// Already reads "network: …" (or "crypto: …"); weirdnet-net errors
68    /// carry their own category, so it is not prefixed again.
69    #[error("{0}")]
70    Net(String),
71    #[error("keys: {0}")]
72    Keys(String),
73}
74
75impl From<weirdnet_keys::KeysError> for Error {
76    fn from(e: weirdnet_keys::KeysError) -> Self {
77        Error::Keys(e.to_string())
78    }
79}
80
81impl From<weirdnet_net::NetError> for Error {
82    fn from(e: weirdnet_net::NetError) -> Self {
83        Error::Net(e.to_string())
84    }
85}
86
87pub type Result<T, E = Error> = std::result::Result<T, E>;
88
89#[cfg(test)]
90mod relay_tests;
91
92#[cfg(test)]
93mod error_tests {
94    use super::*;
95
96    #[test]
97    fn network_errors_are_not_prefixed_twice() {
98        let e = Error::from(weirdnet_net::NetError::Network("the node did not start in time".into()));
99        assert_eq!(e.to_string(), "network: the node did not start in time");
100    }
101}