Skip to main content

second_brain_sync/keyring/
handover.rs

1use age::x25519::Recipient;
2use anyhow::{Result, anyhow, ensure};
3use second_brain_core::keys::{every_held_epoch, hand_over};
4
5use super::Keyring;
6use crate::pairing::{KeyHandover, KeyRequest};
7
8impl Keyring<'_> {
9    /// Every epoch this device holds, of its current identity and of every
10    /// earlier one, wrapped for a device restoring a backup. A removed
11    /// device still hands over: a thief's revocation must not keep the user
12    /// from restoring with keys from a device they hold.
13    pub fn hand_over_keys(&self, request: &KeyRequest, name: &str) -> Result<KeyHandover> {
14        let recipient = request
15            .age_recipient
16            .parse::<Recipient>()
17            .map_err(|e| anyhow!("the restoring device sent an invalid age recipient: {e}"))?;
18        let held = every_held_epoch(self.store, &self.device)?;
19        ensure!(
20            !held.is_empty(),
21            "this device holds no key epoch, so it has nothing to hand over"
22        );
23        Ok(KeyHandover {
24            machine_id: self.own().to_string(),
25            name: name.to_string(),
26            envelopes: hand_over(&held, &recipient)?,
27        })
28    }
29}