core_crypto_ffi/generic/context/
proteus.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::context::CoreCryptoContext;
use crate::generic::CoreCryptoResult;
use crate::proteus_impl;
use crate::{CoreCryptoError, ProteusAutoPrekeyBundle};

#[uniffi::export]
impl CoreCryptoContext {
    /// See [core_crypto::proteus::ProteusCentral::try_new]
    pub async fn proteus_init(&self) -> CoreCryptoResult<()> {
        proteus_impl! { self.proteus_last_error_code => {
            self.context
                .proteus_init()
                .await?;
            Ok(())
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_session_from_prekey]
    pub async fn proteus_session_from_prekey(&self, session_id: String, prekey: Vec<u8>) -> CoreCryptoResult<()> {
        proteus_impl! { self.proteus_last_error_code => {
            self.context
                .proteus_session_from_prekey(&session_id, &prekey)
                .await?;
            Ok(())
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_session_from_message]
    pub async fn proteus_session_from_message(
        &self,
        session_id: String,
        envelope: Vec<u8>,
    ) -> CoreCryptoResult<Vec<u8>> {
        proteus_impl! { self.proteus_last_error_code => {
            let (_, payload) = self.context
                .proteus_session_from_message(&session_id, &envelope)
                .await?;
            Ok(payload)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_session_save]
    /// **Note**: This isn't usually needed as persisting sessions happens automatically when decrypting/encrypting messages and initializing Sessions
    pub async fn proteus_session_save(&self, session_id: String) -> CoreCryptoResult<()> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.
            context
                .proteus_session_save(&session_id)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_session_delete]
    pub async fn proteus_session_delete(&self, session_id: String) -> CoreCryptoResult<()> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_session_delete(&session_id)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_session_exists]
    pub async fn proteus_session_exists(&self, session_id: String) -> CoreCryptoResult<bool> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_session_exists(&session_id)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_decrypt]
    pub async fn proteus_decrypt(&self, session_id: String, ciphertext: Vec<u8>) -> CoreCryptoResult<Vec<u8>> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok( self.context
                .proteus_decrypt(&session_id, &ciphertext)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_encrypt]
    pub async fn proteus_encrypt(&self, session_id: String, plaintext: Vec<u8>) -> CoreCryptoResult<Vec<u8>> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_encrypt(&session_id, &plaintext)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_encrypt_batched]
    pub async fn proteus_encrypt_batched(
        &self,
        sessions: Vec<String>,
        plaintext: Vec<u8>,
    ) -> CoreCryptoResult<std::collections::HashMap<String, Vec<u8>>> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(
            self.context
                .proteus_encrypt_batched(&sessions, &plaintext)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_new_prekey]
    pub async fn proteus_new_prekey(&self, prekey_id: u16) -> CoreCryptoResult<Vec<u8>> {
        proteus_impl! { self.proteus_last_error_code => {
            CoreCryptoResult::Ok(self.context
                .proteus_new_prekey(prekey_id)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_new_prekey_auto]
    pub async fn proteus_new_prekey_auto(&self) -> CoreCryptoResult<ProteusAutoPrekeyBundle> {
        proteus_impl! { self.proteus_last_error_code => {
            let (id, pkb) = self.context
                .proteus_new_prekey_auto()
                .await?;
            CoreCryptoResult::Ok(ProteusAutoPrekeyBundle { id, pkb })
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_last_resort_prekey]
    pub async fn proteus_last_resort_prekey(&self) -> CoreCryptoResult<Vec<u8>> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_last_resort_prekey()
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_last_resort_prekey_id]
    pub fn proteus_last_resort_prekey_id(&self) -> CoreCryptoResult<u16> {
        proteus_impl!({ Ok(core_crypto::CoreCrypto::proteus_last_resort_prekey_id()) })
    }

    /// See [core_crypto::context::CentralContext::proteus_fingerprint]
    pub async fn proteus_fingerprint(&self) -> CoreCryptoResult<String> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_fingerprint().await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_fingerprint_local]
    pub async fn proteus_fingerprint_local(&self, session_id: String) -> CoreCryptoResult<String> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_fingerprint_local(&session_id)
                .await?)
        }}
    }

    /// See [core_crypto::context::CentralContext::proteus_fingerprint_remote]
    pub async fn proteus_fingerprint_remote(&self, session_id: String) -> CoreCryptoResult<String> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_fingerprint_remote(&session_id)
                .await?)
        }}
    }

    /// See [core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle]
    /// NOTE: uniffi doesn't support associated functions, so we have to have the self here
    pub fn proteus_fingerprint_prekeybundle(&self, prekey: Vec<u8>) -> CoreCryptoResult<String> {
        proteus_impl!({ Ok(core_crypto::proteus::ProteusCentral::fingerprint_prekeybundle(&prekey)?) })
    }

    /// See [core_crypto::context::CentralContext::proteus_cryptobox_migrate]
    pub async fn proteus_cryptobox_migrate(&self, path: String) -> CoreCryptoResult<()> {
        proteus_impl! { self.proteus_last_error_code => {
            Ok(self.context
                .proteus_cryptobox_migrate(&path)
                .await?)
        }}
    }

    /// Returns the latest proteus error code. If 0, no error has occured
    ///
    /// NOTE: This will clear the last error code.
    pub fn proteus_last_error_code(&self) -> Option<u16> {
        let raw_error_code = self
            .proteus_last_error_code
            .swap(0, std::sync::atomic::Ordering::SeqCst);
        (raw_error_code != 0).then_some(raw_error_code)
    }
}