Skip to main content

keri_controller/identifier/mechanics/
tel_managing.rs

1use keri_core::{
2    event::{
3        sections::seal::{EventSeal, Seal},
4        KeyEvent,
5    },
6    event_message::{msg::TypedEvent, EventTypeTag},
7    prefix::{IdentifierPrefix, SelfSigningPrefix},
8};
9use teliox::{
10    event::verifiable_event::VerifiableEvent,
11    seal::{AttachedSourceSeal, EventSourceSeal},
12};
13
14use crate::{error::ControllerError, identifier::Identifier};
15
16use super::MechanicsError;
17
18impl Identifier {
19    /// Generate `vcp` event and `ixn` event with  seal to `vcp`. To finalize
20    /// the process, `ixn` need to be signed confirmed with `finalize_event`
21    /// function.
22    pub fn incept_registry(
23        &mut self,
24    ) -> Result<(IdentifierPrefix, TypedEvent<EventTypeTag, KeyEvent>), ControllerError> {
25        // Create tel
26        let tel = self.known_events.tel.clone();
27
28        let vcp = tel.make_inception_event(
29            self.id.clone(),
30            vec![teliox::event::manager_event::Config::NoBackers],
31            0,
32            vec![],
33        )?;
34        let id = vcp.get_prefix();
35        let seal = Seal::Event(EventSeal::new(
36            vcp.get_prefix(),
37            vcp.get_sn(),
38            vcp.get_digest()?,
39        ));
40        let ixn = self.anchor_with_seal(&[seal]).unwrap();
41        let source_seal = EventSourceSeal {
42            sn: ixn.data.sn,
43            digest: ixn.digest()?,
44        };
45
46        let verifiable_event = VerifiableEvent {
47            event: vcp,
48            seal: AttachedSourceSeal { seal: source_seal },
49        };
50
51        tel.processor.process(verifiable_event)?;
52        self.registry_id = Some(id.clone());
53
54        Ok((id, ixn))
55    }
56
57    pub async fn finalize_incept_registry(
58        &mut self,
59        event: &[u8],
60        sig: SelfSigningPrefix,
61    ) -> Result<(), MechanicsError> {
62        self.finalize_anchor(event, sig).await
63    }
64
65    pub async fn notify_backers(&self) -> Result<(), MechanicsError> {
66        let to_notify = self.known_events.tel.recently_added_events.get();
67        let backers = self.known_events.get_current_witness_list(&self.id)?;
68        for backer in backers {
69            let location = self
70                .known_events
71                .get_loc_schemas(&IdentifierPrefix::Basic(backer))
72                .unwrap()[0]
73                .clone();
74            for event in &to_notify {
75                self.communication
76                    .send_tel_event(event.clone(), location.clone())
77                    .await
78                    .map_err(|e| MechanicsError::OtherError(e.to_string()))?;
79            }
80        }
81        Ok(())
82    }
83}