core_crypto_ffi/generic/context/
proteus.rs

1use crate::context::CoreCryptoContext;
2use crate::generic::CoreCryptoResult;
3use crate::proteus_impl;
4use crate::{CoreCryptoError, ProteusAutoPrekeyBundle};
5
6#[uniffi::export]
7impl CoreCryptoContext {
8    /// See [core_crypto::proteus::ProteusCentral::try_new]
9    pub async fn proteus_init(&self) -> CoreCryptoResult<()> {
10        proteus_impl! { self.proteus_last_error_code => {
11            self.context
12                .proteus_init()
13                .await?;
14            Ok(())
15        }}
16    }
17
18    /// See [core_crypto::context::CentralContext::proteus_session_from_prekey]
19    pub async fn proteus_session_from_prekey(&self, session_id: String, prekey: Vec<u8>) -> CoreCryptoResult<()> {
20        proteus_impl! { self.proteus_last_error_code => {
21            self.context
22                .proteus_session_from_prekey(&session_id, &prekey)
23                .await?;
24            Ok(())
25        }}
26    }
27
28    /// See [core_crypto::context::CentralContext::proteus_session_from_message]
29    pub async fn proteus_session_from_message(
30        &self,
31        session_id: String,
32        envelope: Vec<u8>,
33    ) -> CoreCryptoResult<Vec<u8>> {
34        proteus_impl! { self.proteus_last_error_code => {
35            let (_, payload) = self.context
36                .proteus_session_from_message(&session_id, &envelope)
37                .await?;
38            Ok(payload)
39        }}
40    }
41
42    /// See [core_crypto::context::CentralContext::proteus_session_save]
43    /// **Note**: This isn't usually needed as persisting sessions happens automatically when decrypting/encrypting messages and initializing Sessions
44    pub async fn proteus_session_save(&self, session_id: String) -> CoreCryptoResult<()> {
45        proteus_impl! { self.proteus_last_error_code => {
46            Ok(self.
47            context
48                .proteus_session_save(&session_id)
49                .await?)
50        }}
51    }
52
53    /// See [core_crypto::context::CentralContext::proteus_session_delete]
54    pub async fn proteus_session_delete(&self, session_id: String) -> CoreCryptoResult<()> {
55        proteus_impl! { self.proteus_last_error_code => {
56            Ok(self.context
57                .proteus_session_delete(&session_id)
58                .await?)
59        }}
60    }
61
62    /// See [core_crypto::context::CentralContext::proteus_session_exists]
63    pub async fn proteus_session_exists(&self, session_id: String) -> CoreCryptoResult<bool> {
64        proteus_impl! { self.proteus_last_error_code => {
65            Ok(self.context
66                .proteus_session_exists(&session_id)
67                .await?)
68        }}
69    }
70
71    /// See [core_crypto::context::CentralContext::proteus_decrypt]
72    pub async fn proteus_decrypt(&self, session_id: String, ciphertext: Vec<u8>) -> CoreCryptoResult<Vec<u8>> {
73        proteus_impl! { self.proteus_last_error_code => {
74            Ok( self.context
75                .proteus_decrypt(&session_id, &ciphertext)
76                .await?)
77        }}
78    }
79
80    /// See [core_crypto::context::CentralContext::proteus_encrypt]
81    pub async fn proteus_encrypt(&self, session_id: String, plaintext: Vec<u8>) -> CoreCryptoResult<Vec<u8>> {
82        proteus_impl! { self.proteus_last_error_code => {
83            Ok(self.context
84                .proteus_encrypt(&session_id, &plaintext)
85                .await?)
86        }}
87    }
88
89    /// See [core_crypto::context::CentralContext::proteus_encrypt_batched]
90    pub async fn proteus_encrypt_batched(
91        &self,
92        sessions: Vec<String>,
93        plaintext: Vec<u8>,
94    ) -> CoreCryptoResult<std::collections::HashMap<String, Vec<u8>>> {
95        proteus_impl! { self.proteus_last_error_code => {
96            Ok(
97            self.context
98                .proteus_encrypt_batched(&sessions, &plaintext)
99                .await?)
100        }}
101    }
102
103    /// See [core_crypto::context::CentralContext::proteus_new_prekey]
104    pub async fn proteus_new_prekey(&self, prekey_id: u16) -> CoreCryptoResult<Vec<u8>> {
105        proteus_impl! { self.proteus_last_error_code => {
106            CoreCryptoResult::Ok(self.context
107                .proteus_new_prekey(prekey_id)
108                .await?)
109        }}
110    }
111
112    /// See [core_crypto::context::CentralContext::proteus_new_prekey_auto]
113    pub async fn proteus_new_prekey_auto(&self) -> CoreCryptoResult<ProteusAutoPrekeyBundle> {
114        proteus_impl! { self.proteus_last_error_code => {
115            let (id, pkb) = self.context
116                .proteus_new_prekey_auto()
117                .await?;
118            CoreCryptoResult::Ok(ProteusAutoPrekeyBundle { id, pkb })
119        }}
120    }
121
122    /// See [core_crypto::context::CentralContext::proteus_last_resort_prekey]
123    pub async fn proteus_last_resort_prekey(&self) -> CoreCryptoResult<Vec<u8>> {
124        proteus_impl! { self.proteus_last_error_code => {
125            Ok(self.context
126                .proteus_last_resort_prekey()
127                .await?)
128        }}
129    }
130
131    /// See [core_crypto::context::CentralContext::proteus_last_resort_prekey_id]
132    pub fn proteus_last_resort_prekey_id(&self) -> CoreCryptoResult<u16> {
133        proteus_impl!({ Ok(core_crypto::CoreCrypto::proteus_last_resort_prekey_id()) })
134    }
135
136    /// See [core_crypto::context::CentralContext::proteus_fingerprint]
137    pub async fn proteus_fingerprint(&self) -> CoreCryptoResult<String> {
138        proteus_impl! { self.proteus_last_error_code => {
139            Ok(self.context
140                .proteus_fingerprint().await?)
141        }}
142    }
143
144    /// See [core_crypto::context::CentralContext::proteus_fingerprint_local]
145    pub async fn proteus_fingerprint_local(&self, session_id: String) -> CoreCryptoResult<String> {
146        proteus_impl! { self.proteus_last_error_code => {
147            Ok(self.context
148                .proteus_fingerprint_local(&session_id)
149                .await?)
150        }}
151    }
152
153    /// See [core_crypto::context::CentralContext::proteus_fingerprint_remote]
154    pub async fn proteus_fingerprint_remote(&self, session_id: String) -> CoreCryptoResult<String> {
155        proteus_impl! { self.proteus_last_error_code => {
156            Ok(self.context
157                .proteus_fingerprint_remote(&session_id)
158                .await?)
159        }}
160    }
161
162    /// See [core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle]
163    /// NOTE: uniffi doesn't support associated functions, so we have to have the self here
164    pub fn proteus_fingerprint_prekeybundle(&self, prekey: Vec<u8>) -> CoreCryptoResult<String> {
165        proteus_impl!({ Ok(core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle(&prekey)?) })
166    }
167
168    /// See [core_crypto::context::CentralContext::proteus_cryptobox_migrate]
169    pub async fn proteus_cryptobox_migrate(&self, path: String) -> CoreCryptoResult<()> {
170        proteus_impl! { self.proteus_last_error_code => {
171            Ok(self.context
172                .proteus_cryptobox_migrate(&path)
173                .await?)
174        }}
175    }
176
177    /// Returns the latest proteus error code. If 0, no error has occured
178    ///
179    /// NOTE: This will clear the last error code.
180    pub fn proteus_last_error_code(&self) -> Option<u16> {
181        let raw_error_code = self
182            .proteus_last_error_code
183            .swap(0, std::sync::atomic::Ordering::SeqCst);
184        (raw_error_code != 0).then_some(raw_error_code)
185    }
186}