core_crypto/transaction_context/
key_package.rs

1//! This module contains all transactional behavior related to key packages
2
3use std::time::Duration;
4
5use super::{Result, TransactionContext};
6use crate::{CredentialRef, Keypackage, KeypackageRef, RecursiveError};
7
8impl TransactionContext {
9    /// Generate a [Keypackage] from the referenced credential.
10    ///
11    /// Makes no attempt to look up or prune existing keypackges.
12    ///
13    /// If `lifetime` is set, the keypackages will expire that span into the future.
14    /// If it is unset, [`KEYPACKAGE_DEFAULT_LIFETIME`][crate::mls::session::key_package::KEYPACKAGE_DEFAULT_LIFETIME]
15    /// is used.
16    pub async fn generate_keypackage(
17        &self,
18        credential_ref: &CredentialRef,
19        lifetime: Option<Duration>,
20    ) -> Result<Keypackage> {
21        let session = self.session().await?;
22        session
23            .generate_keypackage(credential_ref, lifetime)
24            .await
25            .map_err(RecursiveError::mls_client("generating keypackage for transaction"))
26            .map_err(Into::into)
27    }
28
29    /// Get all [`KeypackageRef`]s known to the keystore.
30    pub async fn get_keypackage_refs(&self) -> Result<Vec<KeypackageRef>> {
31        let session = self.session().await?;
32        session
33            .get_keypackage_refs()
34            .await
35            .map_err(RecursiveError::mls_client(
36                "getting all key package refs for transaction",
37            ))
38            .map_err(Into::into)
39    }
40
41    /// Remove a [`Keypackage`] from the keystore.
42    pub async fn remove_keypackage(&self, kp_ref: &KeypackageRef) -> Result<()> {
43        let session = self.session().await?;
44        session
45            .remove_keypackage(kp_ref)
46            .await
47            .map_err(RecursiveError::mls_client("removing a keypackage for transaction"))
48            .map_err(Into::into)
49    }
50
51    /// Remove all [`Keypackage`]s associated with this ref.
52    pub async fn remove_keypackages_for(&self, credential_ref: &CredentialRef) -> Result<()> {
53        let session = self.session().await?;
54        session
55            .remove_keypackages_for(credential_ref)
56            .await
57            .map_err(RecursiveError::mls_client(
58                "removing all keypackages for credential ref for transaction",
59            ))
60            .map_err(Into::into)
61    }
62}