core_crypto/transaction_context/
key_package.rs

1//! This module contains all transactional behavior related to key packages
2
3use openmls::prelude::{KeyPackage, KeyPackageRef};
4
5use super::{Result, TransactionContext};
6use crate::{Ciphersuite, CredentialType, RecursiveError};
7
8impl TransactionContext {
9    /// Returns `amount_requested` OpenMLS [KeyPackage]s.
10    /// Will always return the requested amount as it will generate the necessary (lacking) amount on-the-fly
11    ///
12    /// Note: Keypackage pruning is performed as a first step
13    ///
14    /// # Arguments
15    /// * `amount_requested` - number of KeyPackages to request and fill the `KeyPackageBundle`
16    ///
17    /// # Return type
18    /// A vector of `KeyPackageBundle`
19    ///
20    /// # Errors
21    /// Errors can happen when accessing the KeyStore
22    pub async fn get_or_create_client_keypackages(
23        &self,
24        ciphersuite: Ciphersuite,
25        credential_type: CredentialType,
26        amount_requested: usize,
27    ) -> Result<Vec<KeyPackage>> {
28        let session = self.session().await?;
29        session
30            .request_key_packages(
31                amount_requested,
32                ciphersuite,
33                credential_type,
34                &self.mls_provider().await?,
35            )
36            .await
37            .map_err(RecursiveError::mls_client("requesting key packages"))
38            .map_err(Into::into)
39    }
40
41    /// Returns the count of valid, non-expired, unclaimed keypackages in store for the given [Ciphersuite] and
42    /// [CredentialType]
43    pub async fn client_valid_key_packages_count(
44        &self,
45        ciphersuite: Ciphersuite,
46        credential_type: CredentialType,
47    ) -> Result<usize> {
48        let session = self.session().await?;
49        session
50            .valid_keypackages_count(&self.mls_provider().await?, ciphersuite, credential_type)
51            .await
52            .map_err(RecursiveError::mls_client("counting valid key packages"))
53            .map_err(Into::into)
54    }
55
56    /// Prunes local KeyPackages after making sure they also have been deleted on the backend side
57    /// You should only use this after [TransactionContext::save_x509_credential]
58    pub async fn delete_keypackages(&self, refs: impl IntoIterator<Item = KeyPackageRef>) -> Result<()> {
59        let mut session = self.session().await?;
60        session
61            .prune_keypackages_and_credential(&self.mls_provider().await?, refs)
62            .await
63            .map_err(RecursiveError::mls_client("pruning key packages and credential"))
64            .map_err(Into::into)
65    }
66}