1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use keri_core::{
    event::sections::seal::{EventSeal, Seal},
    prefix::{IdentifierPrefix, SelfSigningPrefix},
};
use teliox::{
    event::verifiable_event::VerifiableEvent,
    seal::{AttachedSourceSeal, EventSourceSeal},
};

use crate::{error::ControllerError, identifier::Identifier};

use super::MechanicsError;

impl Identifier {
    /// Generate `vcp` event and `ixn` event with  seal to `vcp`. To finalize
    /// the process, `ixn` need to be signed confirmed with `finalize_event`
    /// function.
    pub fn incept_registry(&mut self) -> Result<(IdentifierPrefix, Vec<u8>), ControllerError> {
        // Create tel
        let tel = self.known_events.tel.clone();

        let vcp = tel.make_inception_event(
            self.id.clone(),
            vec![teliox::event::manager_event::Config::NoBackers],
            0,
            vec![],
        )?;
        let id = vcp.get_prefix();
        let seal = Seal::Event(EventSeal {
            prefix: vcp.get_prefix(),
            sn: vcp.get_sn(),
            event_digest: vcp.get_digest()?,
        });
        let ixn = self.anchor_with_seal(&[seal]).unwrap();
        let source_seal = EventSourceSeal {
            sn: ixn.data.sn,
            digest: ixn.digest()?,
        };
        let encoded = ixn.encode()?;

        let verifiable_event = VerifiableEvent {
            event: vcp,
            seal: AttachedSourceSeal { seal: source_seal },
        };

        tel.processor.process(verifiable_event)?;
        self.registry_id = Some(id.clone());

        Ok((id, encoded))
    }

    pub async fn finalize_incept_registry(
        &mut self,
        event: &[u8],
        sig: SelfSigningPrefix,
    ) -> Result<(), MechanicsError> {
        self.finalize_anchor(event, sig).await
    }

    pub async fn notify_backers(&self) -> Result<(), MechanicsError> {
        let to_notify = self.known_events.tel.recently_added_events.get();
        let backers = self.known_events.get_current_witness_list(&self.id)?;
        for backer in backers {
            let location = self
                .known_events
                .get_loc_schemas(&IdentifierPrefix::Basic(backer))
                .unwrap()[0]
                .clone();
            for event in &to_notify {
                self.communication
                    .tel_transport
                    .send_tel_event(event.clone(), location.clone())
                    .await
                    .map_err(|e| MechanicsError::OtherError(e.to_string()))?;
            }
        }
        Ok(())
    }
}