1use std::sync::Arc;
2
3use keri_core::{database::EventDatabase, processor::event_storage::EventStorage};
4
5use crate::{
6 database::TelEventDatabase,
7 error::Error,
8 event::{verifiable_event::VerifiableEvent, Event},
9 query::SignedTelQuery,
10};
11
12use self::{
13 notification::{TelNotification, TelNotificationBus, TelNotificationKind, TelNotifier},
14 storage::TelEventStorage,
15 validator::TelEventValidator,
16};
17
18pub mod escrow;
19pub mod notification;
20pub mod storage;
21pub mod validator;
22
23pub struct TelEventProcessor<D: TelEventDatabase, K: EventDatabase> {
24 kel_reference: Arc<EventStorage<K>>,
25 pub tel_reference: Arc<TelEventStorage<D>>,
26 pub publisher: TelNotificationBus,
27}
28
29impl<D: TelEventDatabase, K: EventDatabase> TelEventProcessor<D, K> {
30 pub fn new(
31 kel_reference: Arc<EventStorage<K>>,
32 tel_reference: Arc<TelEventStorage<D>>,
33 tel_publisher: Option<TelNotificationBus>,
34 ) -> Self {
35 Self {
36 kel_reference,
37 tel_reference,
38 publisher: tel_publisher.unwrap_or_default(),
39 }
40 }
41
42 pub fn register_observer(
43 &mut self,
44 observer: Arc<dyn TelNotifier + Send + Sync>,
45 notifications: Vec<TelNotificationKind>,
46 ) -> Result<(), Error> {
47 self.publisher.register_observer(observer, notifications)?;
48 Ok(())
49 }
50
51 pub fn process(&self, event: VerifiableEvent) -> Result<(), Error> {
53 let validator =
54 TelEventValidator::new(self.tel_reference.clone(), self.kel_reference.clone());
55 match &event.event.clone() {
56 Event::Management(ref man) => match validator.validate_management(man, &event.seal) {
57 Ok(_) => {
58 self.tel_reference
59 .db
60 .add_new_event(event.clone(), &man.data.prefix)
61 .unwrap();
62 self.publisher
63 .notify(&TelNotification::TelEventAdded(event))?;
64 Ok(())
65 }
66 Err(e) => match e {
67 Error::OutOfOrderError => {
68 self.publisher.notify(&TelNotification::OutOfOrder(event))
69 }
70 Error::MissingIssuerEventError => self
71 .publisher
72 .notify(&TelNotification::MissingIssuer(event)),
73 Error::MissingRegistryError => self
74 .publisher
75 .notify(&TelNotification::MissingRegistry(event)),
76 Error::EventAlreadySavedError => {
77 Ok(())
79 }
80 e => Err(e),
81 },
82 },
83 Event::Vc(ref vc_ev) => {
84 match validator.validate_vc(vc_ev, &event.seal) {
85 Ok(_) => {
86 self.tel_reference
87 .db
88 .add_new_event(event.clone(), &vc_ev.data.data.prefix)
89 .unwrap();
90 self.publisher
91 .notify(&TelNotification::TelEventAdded(event))
92 }
93 Err(Error::MissingIssuerEventError) => self
94 .publisher
95 .notify(&TelNotification::MissingIssuer(event)),
96 Err(Error::MissingRegistryError) => self
97 .publisher
98 .notify(&TelNotification::MissingRegistry(event)),
99 Err(Error::OutOfOrderError) => {
100 self.publisher.notify(&TelNotification::OutOfOrder(event))
101 }
102 Err(Error::EventAlreadySavedError) => {
103 Ok(())
105 }
106 Err(e) => Err(e),
107 }
108 }
109 }
110 }
111
112 pub fn process_signed_query(&self, qr: SignedTelQuery) -> Result<TelReplyType, Error> {
113 let signature = qr.signature;
114 let ver_result = signature.verify(&(qr.query.encode()?), &self.kel_reference)?;
116
117 if !ver_result {
118 return Err(Error::Generic("Wrong query event signature".to_string()));
119 };
120
121 self.tel_reference.process_query(&qr.query.data.data)
123 }
124}
125
126pub enum TelReplyType {
127 Tel(Vec<u8>),
128}
129
130impl ToString for TelReplyType {
131 fn to_string(&self) -> String {
132 match self {
133 TelReplyType::Tel(tel) => String::from_utf8(tel.to_vec()).unwrap(),
134 }
135 }
136}