keri_controller/controller/
mod.rs1use std::sync::Arc;
2
3use keri_core::{
4 event_message::signature::Signature,
5 oobi::LocationScheme,
6 prefix::{BasicPrefix, IdentifierPrefix, SelfSigningPrefix},
7 processor::validator::VerificationError,
8 state::IdentifierState,
9};
10
11#[cfg(feature = "query_cache")]
12use crate::identifier::mechanics::cache::IdentifierCache;
13use crate::{
14 communication::Communication,
15 config::ControllerConfig,
16 error::ControllerError,
17 identifier::{mechanics::MechanicsError, Identifier},
18 known_events::KnownEvents,
19};
20pub mod verifying;
21
22pub struct Controller {
23 pub known_events: Arc<KnownEvents>,
24 pub communication: Arc<Communication>,
25 #[cfg(feature = "query_cache")]
26 pub cache: Arc<IdentifierCache>,
27}
28
29impl Controller {
30 pub fn new(config: ControllerConfig) -> Result<Self, ControllerError> {
31 let ControllerConfig {
32 db_path,
33 initial_oobis,
34 escrow_config,
35 transport,
36 tel_transport,
37 } = config;
38 std::fs::create_dir_all(&db_path).unwrap();
39 let mut query_db_path = db_path.clone();
40 query_db_path.push("query_cache");
41
42 let events = Arc::new(KnownEvents::new(db_path, escrow_config)?);
43
44 #[cfg(feature = "query_cache")]
45 let query_cache = Arc::new(IdentifierCache::new(&query_db_path)?);
46 let comm = Arc::new(Communication {
47 events: events.clone(),
48 transport,
49 tel_transport,
50 });
51
52 let controller = Self {
53 known_events: events.clone(),
54 communication: comm,
55 #[cfg(feature = "query_cache")]
56 cache: query_cache,
57 };
58 if !initial_oobis.is_empty() {
59 async_std::task::block_on(controller.setup_witnesses(&initial_oobis)).unwrap();
60 }
61 Ok(controller)
62 }
63
64 pub async fn incept(
65 &self,
66 public_keys: Vec<BasicPrefix>,
67 next_pub_keys: Vec<BasicPrefix>,
68 witnesses: Vec<LocationScheme>,
69 witness_threshold: u64,
70 ) -> Result<String, MechanicsError> {
71 self.setup_witnesses(&witnesses).await?;
72 self.known_events
73 .incept(public_keys, next_pub_keys, witnesses, witness_threshold)
74 }
75
76 pub fn finalize_incept(
77 &self,
78 event: &[u8],
79 sig: &SelfSigningPrefix,
80 ) -> Result<Identifier, ControllerError> {
81 let initialized_id = self.known_events.finalize_inception(event, sig).unwrap();
82 Ok(Identifier::new(
83 initialized_id,
84 None,
85 self.known_events.clone(),
86 self.communication.clone(),
87 #[cfg(feature = "query_cache")]
88 self.cache.clone(),
89 ))
90 }
91
92 async fn setup_witnesses(&self, oobis: &[LocationScheme]) -> Result<(), MechanicsError> {
93 for lc in oobis {
94 self.communication.resolve_loc_schema(lc).await?;
95 }
96 Ok(())
97 }
98
99 pub fn get_kel_with_receipts(
100 &self,
101 id: &IdentifierPrefix,
102 ) -> Option<Vec<keri_core::event_message::signed_event_message::Notice>> {
103 self.known_events.find_kel_with_receipts(id)
104 }
105
106 pub fn verify(&self, data: &[u8], signature: &Signature) -> Result<(), VerificationError> {
107 self.known_events.verify(data, signature)
108 }
109
110 pub fn find_state(&self, id: &IdentifierPrefix) -> Result<IdentifierState, MechanicsError> {
111 self.known_events.get_state(id)
112 }
113}