core_crypto_ffi/core_crypto_context/
proteus.rs1#[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 pub async fn proteus_init(&self) -> CoreCryptoResult<()> {
17 proteus_impl!({
18 self.inner.proteus_init().await?;
19 Ok(())
20 })
21 }
22
23 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 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 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 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 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 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 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 #[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 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 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 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 pub async fn proteus_fingerprint(&self) -> CoreCryptoResult<String> {
119 proteus_impl!({ self.inner.proteus_fingerprint().await.map_err(Into::into) })
120 }
121
122 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 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 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 pub async fn proteus_reload_sessions(&self) -> CoreCryptoResult<()> {
149 proteus_impl!({ self.inner.proteus_reload_sessions().await.map_err(Into::into) })
150 }
151}
152
153fn last_resort_prekey_id_inner() -> CoreCryptoResult<u16> {
157 proteus_impl!({ Ok(core_crypto::CoreCrypto::proteus_last_resort_prekey_id()) })
158}
159
160fn 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 pub fn proteus_last_resort_prekey_id() -> CoreCryptoResult<u16> {
170 last_resort_prekey_id_inner()
171 }
172
173 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 pub fn proteus_last_resort_prekey_id(&self) -> CoreCryptoResult<u16> {
184 last_resort_prekey_id_inner()
185 }
186
187 pub fn proteus_fingerprint_prekeybundle(&self, prekey: Vec<u8>) -> CoreCryptoResult<String> {
189 fingerprint_prekeybundle_inner(prekey)
190 }
191}