1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // Copyright by contributors to this project.
3 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
4 
5 use alloc::vec;
6 use alloc::vec::Vec;
7 use core::fmt::{self, Debug};
8 use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
9 
10 use crate::{cipher_suite::CipherSuite, protocol_version::ProtocolVersion, ExtensionList};
11 
12 use super::ConfirmedTranscriptHash;
13 
14 #[derive(Clone, PartialEq, MlsSize, MlsEncode, MlsDecode)]
15 #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
16 #[cfg_attr(
17     all(feature = "ffi", not(test)),
18     safer_ffi_gen::ffi_type(clone, opaque)
19 )]
20 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21 pub struct GroupContext {
22     pub(crate) protocol_version: ProtocolVersion,
23     pub(crate) cipher_suite: CipherSuite,
24     #[mls_codec(with = "mls_rs_codec::byte_vec")]
25     #[cfg_attr(feature = "serde", serde(with = "mls_rs_core::vec_serde"))]
26     pub(crate) group_id: Vec<u8>,
27     pub(crate) epoch: u64,
28     #[cfg_attr(feature = "serde", serde(with = "mls_rs_core::vec_serde"))]
29     #[mls_codec(with = "mls_rs_codec::byte_vec")]
30     pub(crate) tree_hash: Vec<u8>,
31     pub(crate) confirmed_transcript_hash: ConfirmedTranscriptHash,
32     pub(crate) extensions: ExtensionList,
33 }
34 
35 impl Debug for GroupContext {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result36     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37         f.debug_struct("GroupContext")
38             .field("protocol_version", &self.protocol_version)
39             .field("cipher_suite", &self.cipher_suite)
40             .field(
41                 "group_id",
42                 &mls_rs_core::debug::pretty_group_id(&self.group_id),
43             )
44             .field("epoch", &self.epoch)
45             .field(
46                 "tree_hash",
47                 &mls_rs_core::debug::pretty_bytes(&self.tree_hash),
48             )
49             .field("confirmed_transcript_hash", &self.confirmed_transcript_hash)
50             .field("extensions", &self.extensions)
51             .finish()
52     }
53 }
54 
55 #[cfg_attr(all(feature = "ffi", not(test)), ::safer_ffi_gen::safer_ffi_gen)]
56 impl GroupContext {
new_group( protocol_version: ProtocolVersion, cipher_suite: CipherSuite, group_id: Vec<u8>, tree_hash: Vec<u8>, extensions: ExtensionList, ) -> Self57     pub(crate) fn new_group(
58         protocol_version: ProtocolVersion,
59         cipher_suite: CipherSuite,
60         group_id: Vec<u8>,
61         tree_hash: Vec<u8>,
62         extensions: ExtensionList,
63     ) -> Self {
64         GroupContext {
65             protocol_version,
66             cipher_suite,
67             group_id,
68             epoch: 0,
69             tree_hash,
70             confirmed_transcript_hash: ConfirmedTranscriptHash::from(vec![]),
71             extensions,
72         }
73     }
74 
75     /// Get the current protocol version in use by the group.
version(&self) -> ProtocolVersion76     pub fn version(&self) -> ProtocolVersion {
77         self.protocol_version
78     }
79 
80     /// Get the current cipher suite in use by the group.
cipher_suite(&self) -> CipherSuite81     pub fn cipher_suite(&self) -> CipherSuite {
82         self.cipher_suite
83     }
84 
85     /// Get the unique identifier of this group.
group_id(&self) -> &[u8]86     pub fn group_id(&self) -> &[u8] {
87         &self.group_id
88     }
89 
90     /// Get the current epoch number of the group's state.
epoch(&self) -> u6491     pub fn epoch(&self) -> u64 {
92         self.epoch
93     }
94 
extensions(&self) -> &ExtensionList95     pub fn extensions(&self) -> &ExtensionList {
96         &self.extensions
97     }
98 }
99