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