use age::x25519::Recipient;
use anyhow::{Result, anyhow, ensure};
use second_brain_core::keys::{every_held_epoch, hand_over};
use super::Keyring;
use crate::pairing::{KeyHandover, KeyRequest};
impl Keyring<'_> {
pub fn hand_over_keys(&self, request: &KeyRequest, name: &str) -> Result<KeyHandover> {
let recipient = request
.age_recipient
.parse::<Recipient>()
.map_err(|e| anyhow!("the restoring device sent an invalid age recipient: {e}"))?;
let held = every_held_epoch(self.store, &self.device)?;
ensure!(
!held.is_empty(),
"this device holds no key epoch, so it has nothing to hand over"
);
Ok(KeyHandover {
machine_id: self.own().to_string(),
name: name.to_string(),
envelopes: hand_over(&held, &recipient)?,
})
}
}