The pieces / weirdnet-device-link

weirdnet-device-link

Move a Weirdnet identity to another device over a QR offer, a 6-digit code check and a sealed transfer.

v0.4.2 GitHub crates.io docs.rs weirdnet-device-link = "0.4.2"

What it brings

Move a person's Weirdnet identity to their new device by scanning a QR code, without typing the 24 recovery words and without anyone else on the network being able to take it. Built in Morph as "Login over QR" and moved here; 0.3.0 brings over the rest of Morph's copy, so apps use this crate instead of their own.

What it is

weirdnet-device-link/v1: move a user identity to another device of the same person without typing its recovery words. Built in Morph as “Login over QR” and moved here; 0.3.0 folds in the rest of Morph’s copy: compact codes, relay reach and relay status.

How it works

  1. Offer: the new device (no identity yet) shows a QR code or link: its node key, a fresh X25519 key, its addresses (up to three direct ones, one per host first, and a circuit for each of up to two relays, so devices on other networks connect) and a 120-second expiry, signed by its node key. Nothing secret is in it. Or the other way round: the existing device shows an Invite and the new one scans it. Codes are compact CBOR arrays: addresses are binary, and a circuit through the WeirdBrains public relay (KNOWN_RELAYS) is one byte, so the QR stays easy to scan off a laptop screen.
  2. Pairing: the existing device connects on /weirdnet/link/2 (invites arrive on /weirdnet/link-invite/2). Both derive a key and a 6-digit short authentication string from the X25519 agreement and a transcript hash, and show the code.
  3. Transfer: only after the user confirms the codes match on the existing device does it send the seed, the profile name, the sync types the user opted in to (Pairing::send_with) and a node certificate for the new device, sealed with that key.
  4. Service link (0.4.0): a server that must never hold the seed (a model or inference node) gets a Credential instead: the user's public key, a node certificate the existing device signs with the user key (which never leaves it), the sync types and an opaque sync bundle (weirdnet-sync's Identity::service_bundle: the device's grant, the roster and, if it syncs types, keys for those types only since weirdnet-sync 0.8.3), sealed the same way (Pairing::send_credential). The new device takes it with take_any() and adopts it with Identity::adopt_service. take() still returns only a full transfer, and a 0.3 device refuses a credential.
  5. Relays: link nodes start with the relays in their NodeConfig and link-sized limits (with_link_limits: 1 MiB requests since 0.4.0, 16 KiB responses, a 30-second timeout). RelayProbe reserves on the configured relays and measures each one's round trip, and relay_states turns node statuses into connected, reserved and round-trip state per relay, for a settings page.
  6. An offer is single use and dies after three pairing attempts, a cancel, a transfer or its expiry. Nothing logs secrets.

Modules: offer (offers and links), invite, session (both sides without the network: Host, Sender), net (HostNode, Pairing over weirdnet-net), compact, reach, relays and wire (CBOR messages).

0.3.1 and 0.4.0

0.3.1 depends on weirdnet-net = "0.8" and weirdnet-keys = "0.1" with caret ranges, so it shares a tree with weirdnet-keys 0.1.1 and net 0.8.1. 0.4.0 adds service links (above) and raises link requests to 1 MiB; the rest of the API and the full transfer are unchanged.

0.4.2 and 0.3.3

0.4.2 (and 0.3.3 on the 0.3 line) adds what Morph had built on top of the crate, without changing anything already there:

  • offer_scheme(schemes, text): which of several schemes a new device's code is on (its <scheme>link/ prefix in any case) and the code normalized for decode_link. The prefix is not signed, so an app can pair with codes another app made with this crate under its own scheme. is_offer(scheme, text) is the counterpart of is_invite.
  • link_node_config(label, relays): a link node's NodeConfig, listening on TCP and QUIC on every interface with the link limits. known_relays() is KNOWN_RELAYS as multiaddrs, parse_relays reads a comma-separated relay list, and relays_from_env(VAR) reads one from the app's own environment variable (set but empty means no relays; unset means the known relays).

Moving to 0.3.0

Codes changed: offers and invites are compact CBOR arrays (protocol version 2). A 0.2 code read by a 0.3 device, or the other way round, says to update rather than that the code is damaged, so put both devices on apps with the same version. The stream ids stay /weirdnet/link/2 and /weirdnet/link-invite/2. Version mismatches are now Error::OlderCode and Error::NewerCode (were Error::Invalid); match them to word the message with your app's name. Link nodes now apply with_link_limits to the NodeConfig you pass. Everything else is added: compact, reach, relays, Pairing::send_with, session::make_transfer_with and net_status() on HostNode, Pairing and InviteHost.

Moving to 0.2.0

0.2.0 is built on weirdnet-net 0.8.0. Its streams moved to /weirdnet/link/2 and /weirdnet/link-invite/2 (were /1), so a 0.1 device and a 0.2 device fail to connect rather than misread each other: put both devices on apps with the same version. Offers, links, codes and the sealed transfer are unchanged, and so is the API.

Key types and modules

  • offer: Offer, VerifiedOffer, encode_link, decode_link, offer_addrs.
  • compact: KNOWN_RELAYS, pack_addr, unpack_addr: the compact (version 2) code encoding.
  • invite: the reverse direction, where the existing device shows the QR.
  • session: Host and Sender, both sides without the network, and make_transfer / make_transfer_with.
  • net: HostNode and Pairing over weirdnet-net on /weirdnet/link/2 (and invites on /weirdnet/link-invite/2).
  • reach: with_link_limits and relay_groups.
  • relays: RelayProbe, RelayState, relay_states: real relay state for a settings page.
  • wire: LinkRequest, LinkReply, Transfer, Credential (CBOR).
  • Pairing::send_credential, take_any, Received (0.4.0): service links that carry a certificate and a sync bundle, never the seed.
  • offer_scheme, is_offer, known_relays, parse_relays, relays_from_env, link_node_config (0.4.2, and 0.3.3 on the 0.3 line): pair with several apps’ codes, and set up a link node’s relays and configuration.

Feature flags

None: everything is on by default.

Usage

In Cargo.toml:

[dependencies]
weirdnet-device-link = "0.4.2"
weirdnet-keys = "0.1.2"
weirdnet-net = "0.8.2"

Then (built and run against the published crate when this page was made):

// Both sides of a device link without the network (HostNode and Pairing carry it over
// weirdnet-net): offer, matching 6-digit codes, confirm, then the sealed transfer.
use weirdnet_device_link::{Host, HostStatus, LinkReply, Sender, decode_link};
use weirdnet_keys::Key32;
use weirdnet_net::Keypair;

const SCHEME: &str = "weirdnet-morph://";

fn main() {
    // The new device shows the offer as a QR code.
    let node = Keypair::generate_ed25519();
    let host = Host::new(SCHEME, &node, vec!["/ip4/192.0.2.5/tcp/4747".into()], "Morph on desk", 0).unwrap();

    // The existing device scans it and says hello.
    let phone = Keypair::generate_ed25519().public().to_peer_id();
    let (sender, hello) = Sender::start(decode_link(SCHEME, host.link()).unwrap(), 1).unwrap();
    assert_eq!(host.handle(&phone, hello, 2), LinkReply::Ok);
    let HostStatus::Paired { sas } = host.status(3) else { panic!("not paired") };
    assert_eq!(sas, sender.sas()); // the user compares the codes

    // After the user confirms, the identity moves, sealed.
    let seed = Key32::random().unwrap();
    let sync = vec!["notes".to_string()]; // the types the user opted in to sync
    let t = weirdnet_device_link::session::make_transfer_with(&seed, "Work", "{}", &sync, sender.offer(), 4).unwrap();
    assert_eq!(host.handle(&phone, sender.transfer(&t).unwrap(), 5), LinkReply::Ok);
    host.confirm();
    let moved = host.take().unwrap();
    assert_eq!(moved.profile_name, "Work");
    assert_eq!(moved.sync, sync);
}

Where it fits