second_brain_sync/keyring/
handover.rs1use 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 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}