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//! Nothing secret is in the offer, and nothing here logs secrets.
15mod bytes;
16pub mod compact;
17pub mod invite;
18pub mod net;
19pub mod offer;
20pub mod reach;
21pub mod relays;
22pub mod session;
23pub mod wire;
24
25pub use compact::KNOWN_RELAYS;
26pub use invite::{Invite, InviteHost, answer_invite, decode_invite, encode_invite, is_invite};
27pub use net::{HostNode, Pairing};
28pub use offer::{Offer, VerifiedOffer, decode_link, encode_link, is_offer, offer_addrs, offer_scheme};
29pub use reach::{known_relays, link_node_config, parse_relays, relay_groups, relays_from_env, with_link_limits};
30pub use relays::{RelayProbe, RelayState, relay_states};
31pub use session::{Host, HostStatus, Sender};
32pub use wire::{LinkReply, LinkRequest, Transfer};
33
34use weirdnet_net::profile::{Profile, WEIRDNET};
35
36/// Protocol version in offers, invites and transfers. 2: compact offers and
37/// invites (CBOR arrays, binary addresses, the known relay by index).
38pub const VERSION: u8 = 2;
39/// How long an offer lives.
40pub const OFFER_TTL_MS: i64 = 120_000;
41/// Pairing attempts an offer accepts before it dies.
42pub const MAX_ATTEMPTS: u32 = 3;
43
44/// The Weirdnet network with this protocol's own stream id, so other nodes
45/// ignore it (and it ignores them).
46pub static LINK_PROFILE: Profile = Profile { protocol: "/weirdnet/link/2", thread: "weirdnet-link", ..WEIRDNET };
47
48#[derive(Debug, thiserror::Error)]
49pub enum Error {
50    #[error("{0}")]
51    Invalid(&'static str),
52    /// The code comes from an older protocol version. Apps that want their
53    /// own name in the message (`an older <App>`) match on this variant.
54    #[error("this code is from an older version; update both devices")]
55    OlderCode,
56    /// The code comes from a newer protocol version (see [`Error::OlderCode`]).
57    #[error("this code is from a newer version; update this device")]
58    NewerCode,
59    #[error("this code expired; show a new one on the other device")]
60    Expired,
61    #[error("the other device refused: {0}")]
62    Refused(String),
63    /// Already reads "network: …" (or "crypto: …"); weirdnet-net errors
64    /// carry their own category, so it is not prefixed again.
65    #[error("{0}")]
66    Net(String),
67    #[error("keys: {0}")]
68    Keys(String),
69}
70
71impl From<weirdnet_keys::KeysError> for Error {
72    fn from(e: weirdnet_keys::KeysError) -> Self {
73        Error::Keys(e.to_string())
74    }
75}
76
77impl From<weirdnet_net::NetError> for Error {
78    fn from(e: weirdnet_net::NetError) -> Self {
79        Error::Net(e.to_string())
80    }
81}
82
83pub type Result<T, E = Error> = std::result::Result<T, E>;
84
85#[cfg(test)]
86mod relay_tests;
87
88#[cfg(test)]
89mod error_tests {
90    use super::*;
91
92    #[test]
93    fn network_errors_are_not_prefixed_twice() {
94        let e = Error::from(weirdnet_net::NetError::Network("the node did not start in time".into()));
95        assert_eq!(e.to_string(), "network: the node did not start in time");
96    }
97}