core_crypto/transaction_context/
proteus.rs1use super::{Error, Result, TransactionContext};
4use crate::{RecursiveError, proteus::ProteusCentral};
5
6impl TransactionContext {
7 pub async fn proteus_init(&self) -> Result<()> {
9 let inner = self.inner()?;
10 let proteus_client = ProteusCentral::try_new(&inner.transaction)
11 .await
12 .map_err(RecursiveError::context("creating new proteus client"))?;
13
14 let _ = proteus_client
16 .last_resort_prekey(&inner.transaction)
17 .await
18 .map_err(RecursiveError::context("getting last resort prekey"))?;
19
20 let mut guard = inner.core_crypto.proteus.lock().await;
21 *guard = Some(proteus_client);
22 Ok(())
23 }
24
25 pub async fn proteus_session_from_prekey(&self, session_id: &str, prekey: &[u8]) -> Result<()> {
30 let inner = self.inner()?;
31 let mut guard = inner.core_crypto.proteus.lock().await;
32 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
33 let session = proteus
34 .session_from_prekey(session_id, prekey)
35 .map_err(RecursiveError::context("creating proteus session from prekey"))?;
36 ProteusCentral::session_save_by_ref(&inner.transaction, session)
37 .map_err(RecursiveError::context("saving proteus session by ref"))?;
38 Ok(())
39 }
40
41 pub async fn proteus_session_from_message(&self, session_id: &str, envelope: &[u8]) -> Result<Vec<u8>> {
46 let inner = self.inner()?;
47 let mut guard = inner.core_crypto.proteus.lock().await;
48 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
49 let (session, message) = proteus
50 .session_from_message(&inner.transaction, session_id, envelope)
51 .await
52 .map_err(RecursiveError::context("creating proteus sesseion from message"))?;
53 ProteusCentral::session_save_by_ref(&inner.transaction, session)
54 .map_err(RecursiveError::context("saving proteus session by ref"))?;
55 Ok(message)
56 }
57
58 pub async fn proteus_session_save(&self, session_id: &str) -> Result<()> {
63 let inner = self.inner()?;
64 let mut guard = inner.core_crypto.proteus.lock().await;
65 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
66 proteus
67 .session_save(&inner.transaction, session_id)
68 .await
69 .map_err(RecursiveError::context("saving proteus session"))
70 .map_err(Into::into)
71 }
72
73 pub async fn proteus_session_delete(&self, session_id: &str) -> Result<()> {
78 let inner = self.inner()?;
79 let mut guard = inner.core_crypto.proteus.lock().await;
80 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
81 proteus
82 .session_delete(&inner.transaction, session_id)
83 .map_err(RecursiveError::context("deleting proteus session"))
84 .map_err(Into::into)
85 }
86
87 pub async fn proteus_session_exists(&self, session_id: &str) -> Result<bool> {
92 let inner = self.inner()?;
93 let mut guard = inner.core_crypto.proteus.lock().await;
94 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
95 proteus
96 .session_exists(session_id, &inner.transaction)
97 .await
98 .map_err(RecursiveError::context("checking whether proteus session exists"))
99 .map_err(Into::into)
100 }
101
102 pub async fn proteus_decrypt(&self, session_id: &str, ciphertext: &[u8]) -> Result<Vec<u8>> {
107 let inner = self.inner()?;
108 let mut guard = inner.core_crypto.proteus.lock().await;
109 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
110 proteus
111 .decrypt(&inner.transaction, session_id, ciphertext)
112 .await
113 .map_err(RecursiveError::context("decrypting proteus message"))
114 .map_err(Into::into)
115 }
116
117 pub async fn proteus_encrypt(&self, session_id: &str, plaintext: &[u8]) -> Result<Vec<u8>> {
122 let inner = self.inner()?;
123 let mut guard = inner.core_crypto.proteus.lock().await;
124 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
125 proteus
126 .encrypt(&inner.transaction, session_id, plaintext)
127 .await
128 .map_err(RecursiveError::context("encrypting proteus message"))
129 .map_err(Into::into)
130 }
131
132 pub async fn proteus_encrypt_batched(
138 &self,
139 sessions: &[impl AsRef<str>],
140 plaintext: &[u8],
141 ) -> Result<std::collections::HashMap<String, Vec<u8>>> {
142 let inner = self.inner()?;
143 let mut guard = inner.core_crypto.proteus.lock().await;
144 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
145 proteus
146 .encrypt_batched(&inner.transaction, sessions, plaintext)
147 .await
148 .map_err(RecursiveError::context("batch encrypting proteus message"))
149 .map_err(Into::into)
150 }
151
152 pub async fn proteus_new_prekey(&self, prekey_id: u16) -> Result<Vec<u8>> {
163 let inner = self.inner()?;
164 let mut guard = inner.core_crypto.proteus.lock().await;
165 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
166 proteus
167 .new_prekey(prekey_id, &inner.transaction)
168 .await
169 .map_err(RecursiveError::context("new proteus prekey"))
170 .map_err(Into::into)
171 }
172
173 pub async fn proteus_new_prekey_auto(&self) -> Result<(u16, Vec<u8>)> {
179 let inner = self.inner()?;
180 let mut guard = inner.core_crypto.proteus.lock().await;
181 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
182 proteus
183 .new_prekey_auto(&inner.transaction)
184 .await
185 .map_err(RecursiveError::context("proteus new prekey auto"))
186 .map_err(Into::into)
187 }
188
189 pub async fn proteus_last_resort_prekey(&self) -> Result<Vec<u8>> {
191 let inner = self.inner()?;
192 let mut guard = inner.core_crypto.proteus.lock().await;
193 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
194
195 proteus
196 .last_resort_prekey(&inner.transaction)
197 .await
198 .map_err(RecursiveError::context("getting proteus last resort prekey"))
199 .map_err(Into::into)
200 }
201
202 pub fn proteus_last_resort_prekey_id() -> u16 {
204 ProteusCentral::last_resort_prekey_id()
205 }
206
207 pub async fn proteus_fingerprint(&self) -> Result<String> {
212 let inner = self.inner()?;
213 let mut guard = inner.core_crypto.proteus.lock().await;
214 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
215 Ok(proteus.fingerprint())
216 }
217
218 pub async fn proteus_fingerprint_local(&self, session_id: &str) -> Result<String> {
223 let inner = self.inner()?;
224 let mut guard = inner.core_crypto.proteus.lock().await;
225 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
226 proteus
227 .fingerprint_local(session_id, &inner.transaction)
228 .await
229 .map_err(RecursiveError::context("getting proteus fingerprint local"))
230 .map_err(Into::into)
231 }
232
233 pub async fn proteus_fingerprint_remote(&self, session_id: &str) -> Result<String> {
238 let inner = self.inner()?;
239 let mut guard = inner.core_crypto.proteus.lock().await;
240 let proteus = guard.as_mut().ok_or(Error::ProteusNotInitialized)?;
241 proteus
242 .fingerprint_remote(session_id, &inner.transaction)
243 .await
244 .map_err(RecursiveError::context("geeting proteus fingerprint remote"))
245 .map_err(Into::into)
246 }
247}