core_crypto_ffi/
ephemeral.rs

1#[cfg(target_family = "wasm")]
2use wasm_bindgen::prelude::*;
3
4use crate::{CoreCrypto, CoreCryptoError, CoreCryptoResult};
5use core_crypto::prelude::{CoreCrypto as CoreCryptoFfi, HistorySecret as HistorySecretFfi};
6
7/// A `HistorySecret` encodes sufficient client state that it can be used to instantiate an
8/// ephemeral client.
9pub type HistorySecret = Vec<u8>;
10
11async fn history_client_inner(history_secret: HistorySecret) -> CoreCryptoResult<CoreCrypto> {
12    let secret = rmp_serde::from_slice::<HistorySecretFfi>(&history_secret).map_err(CoreCryptoError::generic())?;
13    CoreCryptoFfi::history_client(secret)
14        .await
15        .map(|inner| CoreCrypto { inner })
16        .map_err(Into::into)
17}
18
19#[cfg(target_family = "wasm")]
20#[wasm_bindgen]
21impl CoreCrypto {
22    /// Instantiate a history client.
23    ///
24    /// This client exposes the full interface of `CoreCrypto`, but it should only be used to decrypt messages.
25    /// Other use is a logic error.
26    pub async fn history_client(history_secret: HistorySecret) -> CoreCryptoResult<Self> {
27        history_client_inner(history_secret).await
28    }
29}
30
31/// Instantiate a history client.
32///
33/// This client exposes the full interface of `CoreCrypto`, but it should only be used to decrypt messages.
34/// Other use is a logic error.
35#[cfg(not(target_family = "wasm"))]
36#[uniffi::export]
37pub async fn core_crypto_history_client(history_secret: HistorySecret) -> CoreCryptoResult<CoreCrypto> {
38    history_client_inner(history_secret).await
39}