Skip to main content

keri_controller/identifier/
tel.rs

1use keri_core::actor::prelude::{HashFunctionCode, SelfAddressingIdentifier, SerializationFormats};
2use keri_core::event::sections::seal::{EventSeal, Seal};
3use keri_core::event::KeyEvent;
4use keri_core::event_message::msg::{KeriEvent, TypedEvent};
5use keri_core::event_message::timestamped::Timestamped;
6use keri_core::event_message::EventTypeTag;
7use keri_core::prefix::{IdentifierPrefix, IndexedSignature, SelfSigningPrefix};
8use teliox::event::verifiable_event::VerifiableEvent;
9use teliox::query::{SignedTelQuery, TelQueryArgs, TelQueryEvent, TelQueryRoute};
10use teliox::seal::{AttachedSourceSeal, EventSourceSeal};
11
12use crate::error::ControllerError;
13
14use super::mechanics::MechanicsError;
15use super::Identifier;
16
17impl Identifier {
18    /// Generate `iss` event and `ixn` event with  seal to `iss`. To finalize
19    /// the process, `ixn` need to be signed confirmed with `finalize_event`
20    /// function.
21    pub fn issue(
22        &self,
23        credential_digest: SelfAddressingIdentifier,
24    ) -> Result<(IdentifierPrefix, TypedEvent<EventTypeTag, KeyEvent>), ControllerError> {
25        match self.registry_id.as_ref() {
26            Some(registry_id) => {
27                let tel = self.known_events.tel.clone();
28                let iss = tel.make_issuance_event(registry_id, credential_digest)?;
29
30                let vc_hash = iss.get_prefix();
31                let seal = Seal::Event(EventSeal::new(
32                    iss.get_prefix(),
33                    iss.get_sn(),
34                    iss.get_digest()?,
35                ));
36                let ixn = self.anchor_with_seal(&[seal]).unwrap();
37
38                let source_seal = EventSourceSeal {
39                    sn: ixn.data.sn,
40                    digest: ixn.digest()?,
41                };
42
43                let verifiable_event = VerifiableEvent {
44                    event: iss,
45                    seal: AttachedSourceSeal { seal: source_seal },
46                };
47                tel.processor.process(verifiable_event)?;
48
49                Ok((vc_hash, ixn))
50            }
51            None => Err(ControllerError::OtherError("Tel not incepted".into())),
52        }
53    }
54
55    /// Generate `rev` event and `ixn` event with  seal to `rev`. To finalize
56    /// the process, `ixn` need to be signed confirmed with `finalize_event`
57    /// function.
58    pub fn revoke(
59        &self,
60        credential_sai: &SelfAddressingIdentifier,
61    ) -> Result<Vec<u8>, ControllerError> {
62        match &self.registry_id {
63            Some(registry_id) => {
64                let tel = self.known_events.tel.clone();
65                let rev = tel.make_revoke_event(registry_id, credential_sai)?;
66
67                let seal = Seal::Event(EventSeal::new(
68                    rev.get_prefix(),
69                    rev.get_sn(),
70                    rev.get_digest()?,
71                ));
72                let ixn = self.anchor_with_seal(&[seal]).unwrap();
73
74                let source_seal = EventSourceSeal {
75                    sn: ixn.data.sn,
76                    digest: ixn.digest()?,
77                };
78                let encoded_ixn = ixn.encode()?;
79
80                let verifiable_event = VerifiableEvent {
81                    event: rev,
82                    seal: AttachedSourceSeal { seal: source_seal },
83                };
84                tel.processor.process(verifiable_event)?;
85
86                Ok(encoded_ixn)
87            }
88            None => Err(ControllerError::OtherError("Tel not incepted".into())),
89        }
90    }
91
92    pub async fn finalize_issue(
93        &mut self,
94        event: &[u8],
95        sig: SelfSigningPrefix,
96    ) -> Result<(), MechanicsError> {
97        self.finalize_anchor(event, sig).await
98    }
99
100    pub async fn finalize_revoke(
101        &mut self,
102        event: &[u8],
103        sig: SelfSigningPrefix,
104    ) -> Result<(), MechanicsError> {
105        self.finalize_anchor(event, sig).await
106    }
107
108    pub fn query_tel(
109        &self,
110        registry_id: IdentifierPrefix,
111        vc_identifier: IdentifierPrefix,
112    ) -> Result<TelQueryEvent, ControllerError> {
113        let route = TelQueryRoute::Tels {
114            reply_route: "".into(),
115            args: TelQueryArgs {
116                i: Some(vc_identifier),
117                ri: Some(registry_id),
118            },
119        };
120        let env = Timestamped::new(route);
121        Ok(KeriEvent::new(
122            SerializationFormats::JSON,
123            HashFunctionCode::Blake3_256.into(),
124            env,
125        ))
126    }
127
128    pub async fn finalize_query_tel(
129        &self,
130        qry: TelQueryEvent,
131        sig: SelfSigningPrefix,
132    ) -> Result<(), MechanicsError> {
133        let query = match &self.id {
134            IdentifierPrefix::Basic(bp) => {
135                SignedTelQuery::new_nontrans(qry.clone(), bp.clone(), sig)
136            }
137            _ => {
138                let signatures = vec![IndexedSignature::new_both_same(sig, 0)];
139                SignedTelQuery::new_trans(qry.clone(), self.id.clone(), signatures)
140            }
141        };
142        let watcher = self.watchers().unwrap()[0].clone();
143        let location = self.known_events.get_loc_schemas(&watcher).unwrap()[0].clone();
144        let tel_res = self
145            .communication
146            .send_tel_query(query, location)
147            .await
148            .map_err(|e| MechanicsError::OtherError(e.to_string()))?;
149        self.known_events
150            .tel
151            .parse_and_process_tel_stream(tel_res.as_bytes())
152            .map_err(|e| MechanicsError::OtherError(e.to_string()))?;
153
154        Ok(())
155    }
156}