keri_controller/identifier/
mod.rs1use std::{
2 collections::HashMap,
3 sync::{Arc, Mutex},
4};
5
6use keri_core::{
7 actor::prelude::SelfAddressingIdentifier,
8 event::{event_data::EventData, sections::seal::EventSeal},
9 event_message::signed_event_message::{Notice, SignedEventMessage},
10 oobi::Oobi,
11 prefix::{BasicPrefix, IdentifierPrefix},
12 state::IdentifierState,
13};
14#[cfg(feature = "query_cache")]
15use mechanics::cache::IdentifierCache;
16use teliox::state::{vc_state::TelState, ManagerTelState};
17
18use crate::{communication::Communication, error::ControllerError, known_events::KnownEvents};
19
20use self::mechanics::MechanicsError;
21
22pub mod mechanics;
23pub mod nontransferable;
24pub mod query;
25pub mod signing;
26pub mod tel;
27
28pub struct Identifier {
29 id: IdentifierPrefix,
30 registry_id: Option<IdentifierPrefix>,
31 pub known_events: Arc<KnownEvents>,
32 communication: Arc<Communication>,
33 pub to_notify: Vec<SignedEventMessage>,
34 #[cfg(feature = "query_cache")]
35 query_cache: Arc<IdentifierCache>,
36 cached_state: IdentifierState,
40 cached_identifiers: Mutex<HashMap<IdentifierPrefix, IdentifierState>>,
41}
42
43impl Identifier {
44 pub fn new(
45 id: IdentifierPrefix,
46 registry_id: Option<IdentifierPrefix>,
47 known_events: Arc<KnownEvents>,
48 communication: Arc<Communication>,
49 #[cfg(feature = "query_cache")] db: Arc<IdentifierCache>,
50 ) -> Self {
51 let events_to_notice: Vec<_> = known_events
53 .partially_witnessed_escrow
54 .get_partially_witnessed_events(&id)
55 .unwrap()
56 .collect();
57 let state = if let Ok(state) = known_events.get_state(&id) {
59 state
60 } else {
61 let not_accepted_incept = events_to_notice.iter().find_map(|ev| {
62 if let EventData::Icp(_icp) = &ev.event_message.data.event_data {
63 Some(ev.event_message.clone())
64 } else {
65 None
66 }
67 });
68 if let Some(incept) = not_accepted_incept {
69 IdentifierState::default().apply(&incept).unwrap()
70 } else {
71 IdentifierState::default()
72 }
73 };
74 Self {
75 id,
76 known_events,
77 communication,
78 to_notify: events_to_notice,
79 #[cfg(feature = "query_cache")]
80 query_cache: db,
81 cached_state: state,
82 registry_id,
83 cached_identifiers: Mutex::new(HashMap::new()),
84 }
85 }
86
87 pub async fn resolve_oobi(&self, oobi: &Oobi) -> Result<(), MechanicsError> {
88 self.communication.resolve_oobi(oobi).await
89 }
90
91 pub async fn send_oobi_to_watcher(
92 &self,
93 id: &IdentifierPrefix,
94 oobi: &Oobi,
95 ) -> Result<(), ControllerError> {
96 self.communication.send_oobi_to_watcher(id, oobi).await
97 }
98
99 pub fn id(&self) -> &IdentifierPrefix {
100 &self.id
101 }
102
103 pub fn registry_id(&self) -> Option<&IdentifierPrefix> {
104 self.registry_id.as_ref()
105 }
106
107 pub fn find_state(&self, id: &IdentifierPrefix) -> Result<IdentifierState, MechanicsError> {
109 self.known_events.get_state(id)
110 }
111
112 pub fn find_management_tel_state(
113 &self,
114 id: &IdentifierPrefix,
115 ) -> Result<Option<ManagerTelState>, ControllerError> {
116 Ok(self.known_events.tel.get_management_tel_state(id)?)
117 }
118
119 pub fn find_vc_state(
120 &self,
121 vc_hash: &SelfAddressingIdentifier,
122 ) -> Result<Option<TelState>, ControllerError> {
123 Ok(self.known_events.tel.get_vc_state(vc_hash)?)
124 }
125
126 pub fn current_public_keys(&self) -> Result<Vec<BasicPrefix>, ControllerError> {
127 Ok(self.known_events.current_public_keys(&self.id).unwrap())
128 }
129
130 pub fn witnesses(&self) -> impl Iterator<Item = BasicPrefix> {
131 self.cached_state
132 .witness_config
133 .witnesses
134 .clone()
135 .into_iter()
136 }
137
138 pub fn watchers(&self) -> Result<Vec<IdentifierPrefix>, ControllerError> {
139 self.known_events.get_watchers(&self.id)
140 }
141
142 pub fn get_own_kel(&self) -> Option<Vec<Notice>> {
144 self.known_events.find_kel_with_receipts(&self.id)
145 }
146
147 pub fn get_kel(&self, id: &IdentifierPrefix) -> Option<Vec<Notice>> {
149 self.known_events.find_kel_with_receipts(id)
150 }
151
152 pub fn get_last_establishment_event_seal(&self) -> Result<EventSeal, ControllerError> {
153 self.known_events
154 .storage
155 .get_last_establishment_event_seal(&self.id)
156 .ok_or(ControllerError::UnknownIdentifierError)
157 }
158
159 pub fn get_last_event_seal(&self) -> Result<EventSeal, MechanicsError> {
160 let state = self.known_events.get_state(self.id())?;
161 Ok(EventSeal::new(
162 state.prefix,
163 state.sn,
164 state.last_event_digest.said,
165 ))
166 }
167}