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::{MlsCiphersuite, MlsCredentialType, RecursiveError};
7
8impl TransactionContext {
9    /// Returns `amount_requested` OpenMLS [openmls::key_packages::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: MlsCiphersuite,
25        credential_type: MlsCredentialType,
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 [MlsCiphersuite] and [MlsCredentialType]
42    pub async fn client_valid_key_packages_count(
43        &self,
44        ciphersuite: MlsCiphersuite,
45        credential_type: MlsCredentialType,
46    ) -> Result<usize> {
47        let session = self.session().await?;
48        session
49            .valid_keypackages_count(&self.mls_provider().await?, ciphersuite, credential_type)
50            .await
51            .map_err(RecursiveError::mls_client("counting valid key packages"))
52            .map_err(Into::into)
53    }
54
55    /// Prunes local KeyPackages after making sure they also have been deleted on the backend side
56    /// You should only use this after [TransactionContext::save_x509_credential]
57    pub async fn delete_keypackages(&self, refs: impl IntoIterator<Item = KeyPackageRef>) -> Result<()> {
58        let mut session = self.session().await?;
59        session
60            .prune_keypackages_and_credential(&self.mls_provider().await?, refs)
61            .await
62            .map_err(RecursiveError::mls_client("pruning key packages and credential"))
63            .map_err(Into::into)
64    }
65}