core_crypto_ffi/core_crypto_context/
proteus.rs

1#[cfg(target_family = "wasm")]
2use wasm_bindgen::prelude::*;
3
4use crate::{CoreCryptoContext, CoreCryptoResult, ProteusAutoPrekeyBundle, proteus_impl};
5
6#[cfg(not(target_family = "wasm"))]
7type BatchedEncryptedMessages = std::collections::HashMap<String, Vec<u8>>;
8
9#[cfg(target_family = "wasm")]
10type BatchedEncryptedMessages = JsValue;
11
12#[cfg_attr(target_family = "wasm", wasm_bindgen)]
13#[cfg_attr(not(target_family = "wasm"), uniffi::export)]
14impl CoreCryptoContext {
15    /// See [core_crypto::proteus::ProteusCentral::try_new]
16    pub async fn proteus_init(&self) -> CoreCryptoResult<()> {
17        proteus_impl!({
18            self.inner.proteus_init().await?;
19            Ok(())
20        })
21    }
22
23    /// See [core_crypto::transaction_context::TransactionContext::proteus_session_from_prekey]
24    pub async fn proteus_session_from_prekey(&self, session_id: String, prekey: Vec<u8>) -> CoreCryptoResult<()> {
25        proteus_impl!({
26            self.inner.proteus_session_from_prekey(&session_id, &prekey).await?;
27            Ok(())
28        })
29    }
30
31    /// See [core_crypto::transaction_context::TransactionContext::proteus_session_from_message]
32    pub async fn proteus_session_from_message(
33        &self,
34        session_id: String,
35        envelope: Vec<u8>,
36    ) -> CoreCryptoResult<Vec<u8>> {
37        proteus_impl!({
38            let (_, payload) = self.inner.proteus_session_from_message(&session_id, &envelope).await?;
39            Ok(payload)
40        })
41    }
42
43    /// See [core_crypto::transaction_context::TransactionContext::proteus_session_save]
44    ///
45    /// **Note**: This isn't usually needed as persisting sessions happens automatically when
46    /// decrypting/encrypting messages and initializing Sessions
47    pub async fn proteus_session_save(&self, session_id: String) -> CoreCryptoResult<()> {
48        proteus_impl!({ self.inner.proteus_session_save(&session_id).await.map_err(Into::into) })
49    }
50
51    /// See [core_crypto::transaction_context::TransactionContext::proteus_session_delete]
52    pub async fn proteus_session_delete(&self, session_id: String) -> CoreCryptoResult<()> {
53        proteus_impl!({ self.inner.proteus_session_delete(&session_id).await.map_err(Into::into) })
54    }
55
56    /// See [core_crypto::transaction_context::TransactionContext::proteus_session_exists]
57    pub async fn proteus_session_exists(&self, session_id: String) -> CoreCryptoResult<bool> {
58        proteus_impl!({ self.inner.proteus_session_exists(&session_id).await.map_err(Into::into) })
59    }
60    /// See [core_crypto::transaction_context::TransactionContext::proteus_decrypt]
61    pub async fn proteus_decrypt(&self, session_id: String, ciphertext: Vec<u8>) -> CoreCryptoResult<Vec<u8>> {
62        proteus_impl!({
63            self.inner
64                .proteus_decrypt(&session_id, &ciphertext)
65                .await
66                .map_err(Into::into)
67        })
68    }
69
70    /// See [core_crypto::transaction_context::TransactionContext::proteus_encrypt]
71    pub async fn proteus_encrypt(&self, session_id: String, plaintext: Vec<u8>) -> CoreCryptoResult<Vec<u8>> {
72        proteus_impl!({
73            self.inner
74                .proteus_encrypt(&session_id, &plaintext)
75                .await
76                .map_err(Into::into)
77        })
78    }
79
80    /// See [core_crypto::transaction_context::TransactionContext::proteus_encrypt_batched]
81    #[cfg_attr(
82        target_family = "wasm",
83        wasm_bindgen(unchecked_return_type = "Map<string, Uint8Array>")
84    )]
85    pub async fn proteus_encrypt_batched(
86        &self,
87        sessions: Vec<String>,
88        plaintext: Vec<u8>,
89    ) -> CoreCryptoResult<BatchedEncryptedMessages> {
90        proteus_impl!({
91            let batched_encrypted_messages = self.inner.proteus_encrypt_batched(&sessions, &plaintext).await?;
92
93            #[cfg(target_family = "wasm")]
94            let batched_encrypted_messages = serde_wasm_bindgen::to_value(&batched_encrypted_messages)?;
95
96            Ok(batched_encrypted_messages)
97        })
98    }
99
100    /// See [core_crypto::transaction_context::TransactionContext::proteus_new_prekey]
101    pub async fn proteus_new_prekey(&self, prekey_id: u16) -> CoreCryptoResult<Vec<u8>> {
102        proteus_impl!({ self.inner.proteus_new_prekey(prekey_id).await.map_err(Into::into) })
103    }
104
105    /// See [core_crypto::transaction_context::TransactionContext::proteus_new_prekey_auto]
106    pub async fn proteus_new_prekey_auto(&self) -> CoreCryptoResult<ProteusAutoPrekeyBundle> {
107        proteus_impl!({
108            let (id, pkb) = self.inner.proteus_new_prekey_auto().await?;
109            Ok(ProteusAutoPrekeyBundle { id, pkb })
110        })
111    }
112
113    /// See [core_crypto::transaction_context::TransactionContext::proteus_last_resort_prekey]
114    pub async fn proteus_last_resort_prekey(&self) -> CoreCryptoResult<Vec<u8>> {
115        proteus_impl!({ self.inner.proteus_last_resort_prekey().await.map_err(Into::into) })
116    }
117
118    /// See [core_crypto::transaction_context::TransactionContext::proteus_fingerprint]
119    pub async fn proteus_fingerprint(&self) -> CoreCryptoResult<String> {
120        proteus_impl!({ self.inner.proteus_fingerprint().await.map_err(Into::into) })
121    }
122
123    /// See [core_crypto::transaction_context::TransactionContext::proteus_fingerprint_local]
124    pub async fn proteus_fingerprint_local(&self, session_id: String) -> CoreCryptoResult<String> {
125        proteus_impl!({
126            self.inner
127                .proteus_fingerprint_local(&session_id)
128                .await
129                .map_err(Into::into)
130        })
131    }
132
133    /// See [core_crypto::transaction_context::TransactionContext::proteus_fingerprint_remote]
134    pub async fn proteus_fingerprint_remote(&self, session_id: String) -> CoreCryptoResult<String> {
135        proteus_impl!({
136            self.inner
137                .proteus_fingerprint_remote(&session_id)
138                .await
139                .map_err(Into::into)
140        })
141    }
142
143    /// See [core_crypto::transaction_context::TransactionContext::proteus_cryptobox_migrate]
144    pub async fn proteus_cryptobox_migrate(&self, path: String) -> CoreCryptoResult<()> {
145        proteus_impl!({ self.inner.proteus_cryptobox_migrate(&path).await.map_err(Into::into) })
146    }
147
148    /// See [core_crypto::transaction_context::TransactionContext::proteus_reload_sessions]
149    pub async fn proteus_reload_sessions(&self) -> CoreCryptoResult<()> {
150        proteus_impl!({ self.inner.proteus_reload_sessions().await.map_err(Into::into) })
151    }
152}
153
154// here are some static members, except that Uniffi doesn't do that, so we insert a pointless `self` param in that context
155
156/// See [core_crypto::proteus::ProteusCentral::last_resort_prekey_id]
157fn last_resort_prekey_id_inner() -> CoreCryptoResult<u16> {
158    proteus_impl!({ Ok(core_crypto::CoreCrypto::proteus_last_resort_prekey_id()) })
159}
160
161/// See [core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle]
162fn fingerprint_prekeybundle_inner(prekey: Vec<u8>) -> CoreCryptoResult<String> {
163    proteus_impl!({ core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle(&prekey).map_err(Into::into) })
164}
165
166#[cfg(target_family = "wasm")]
167#[wasm_bindgen]
168impl CoreCryptoContext {
169    /// See [core_crypto::proteus::ProteusCentral::last_resort_prekey_id]
170    pub fn proteus_last_resort_prekey_id() -> CoreCryptoResult<u16> {
171        last_resort_prekey_id_inner()
172    }
173
174    /// See [core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle]
175    pub fn proteus_fingerprint_prekeybundle(prekey: Vec<u8>) -> CoreCryptoResult<String> {
176        fingerprint_prekeybundle_inner(prekey)
177    }
178}
179
180#[cfg(not(target_family = "wasm"))]
181#[uniffi::export]
182impl CoreCryptoContext {
183    /// See [core_crypto::proteus::ProteusCentral::last_resort_prekey_id]
184    pub fn proteus_last_resort_prekey_id(&self) -> CoreCryptoResult<u16> {
185        last_resort_prekey_id_inner()
186    }
187
188    /// See [core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle]
189    pub fn proteus_fingerprint_prekeybundle(&self, prekey: Vec<u8>) -> CoreCryptoResult<String> {
190        fingerprint_prekeybundle_inner(prekey)
191    }
192}