Skip to main content

de_mls/mls_crypto/
types.rs

1//! MLS types and operation results.
2
3use openmls::key_packages::KeyPackage as MlsKeyPackage;
4
5use super::error::{MlsError, MlsServiceError};
6
7/// Serialized key package for joining groups.
8///
9/// Contains the TLS-serialized key package bytes and the owner's wallet identity.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct KeyPackageBytes {
12    bytes: Vec<u8>,
13    identity: Vec<u8>,
14}
15
16impl KeyPackageBytes {
17    pub fn new(bytes: Vec<u8>, identity: Vec<u8>) -> Self {
18        Self { bytes, identity }
19    }
20
21    /// Get the serialized key package bytes.
22    pub fn as_bytes(&self) -> &[u8] {
23        &self.bytes
24    }
25}
26
27/// Membership change for commit.
28#[derive(Clone, Debug)]
29pub enum GroupUpdate {
30    /// Add a new member using their key package.
31    Add(KeyPackageBytes),
32    /// Remove a member by their wallet address (20 bytes).
33    Remove(Vec<u8>),
34}
35
36/// Result of decrypting an inbound message.
37#[derive(Clone, Debug)]
38pub enum DecryptResult {
39    /// Application message decrypted successfully.
40    Application(Vec<u8>),
41    /// We were removed from the group.
42    Removed,
43    /// Proposal stored (no action needed).
44    ProposalStored,
45    /// Commit processed, group updated.
46    CommitProcessed,
47    /// Message ignored (wrong group/epoch).
48    Ignored,
49}
50
51/// Result of creating a commit.
52#[derive(Clone, Debug)]
53pub struct CommitResult {
54    /// Serialized MLS proposal messages.
55    pub proposals: Vec<Vec<u8>>,
56    /// Serialized MLS commit message.
57    pub commit: Vec<u8>,
58    /// Optional welcome message for new members (if any adds).
59    pub welcome: Option<Vec<u8>>,
60}
61
62/// Parse a JSON-serialized key package and extract the identity.
63///
64/// Returns `(key_package_bytes, identity)` where:
65/// - `key_package_bytes` is the original JSON bytes (passed through)
66/// - `identity` is the wallet address extracted from the credential
67pub fn key_package_bytes_from_json(json_bytes: Vec<u8>) -> Result<(Vec<u8>, Vec<u8>), MlsError> {
68    let kp: MlsKeyPackage =
69        serde_json::from_slice(&json_bytes).map_err(MlsServiceError::InvalidKeyPackage)?;
70    let identity = kp.leaf_node().credential().serialized_content().to_vec();
71    Ok((json_bytes, identity))
72}