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
use crate::ProteusAutoPrekeyBundle;
use crate::context::CoreCryptoContext;
use crate::generic::CoreCryptoResult;
use crate::proteus_impl;

#[uniffi::export]
impl CoreCryptoContext {
    /// See [core_crypto::proteus::ProteusCentral::try_new]
    pub async fn proteus_init(&self) -> CoreCryptoResult<()> {
        proteus_impl!({
            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.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!({
            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!({ 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!({ 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!({ 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!({ 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!({ 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!({ 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!({ 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!({
            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!({ 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!({ 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!({ 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!({ 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!({ Ok(self.context.proteus_cryptobox_migrate(&path).await?) })
    }
}