Skip to content

Commit 8ecffa5

Browse files
erskingardneryukibtc
authored andcommitted
mls: update to use GroupId for mls_group_ids and [u8; 32] for nostr_group_ids
- Updates all the mls crates to use `GroupId` values for the mls_group_ids in all types and method signatures. - Updates all `nostr_group_id` values to use `&[u8; 32]` values instead of a mix of strings and byte arrays. Pull-Request: #849 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent ef273f0 commit 8ecffa5

File tree

18 files changed

+280
-236
lines changed

18 files changed

+280
-236
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/nostr-mls-memory-storage/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ keywords = ["nostr", "mls", "openmls", "memory"]
1515
lru.workspace = true
1616
nostr = { workspace = true, features = ["std"] }
1717
nostr-mls-storage.workspace = true
18+
openmls = { git = "https://github.com/openmls/openmls", rev = "4cc0f594b11262083ad9827b3b2033052c6ef99f", default-features = false }
1819
openmls_memory_storage = { git = "https://github.com/openmls/openmls", rev = "4cc0f594b11262083ad9827b3b2033052c6ef99f", default-features = false }
1920
parking_lot = "0.12"

crates/nostr-mls-memory-storage/src/groups.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@ use nostr_mls_storage::groups::error::{GroupError, InvalidGroupState};
77
use nostr_mls_storage::groups::types::*;
88
use nostr_mls_storage::groups::GroupStorage;
99
use nostr_mls_storage::messages::types::Message;
10+
use openmls::group::GroupId;
1011

1112
use crate::NostrMlsMemoryStorage;
1213

13-
/// Creates a compound key from an MLS group ID and epoch
14-
///
15-
/// The key is created by concatenating the MLS group ID and the epoch as bytes
16-
fn create_compound_key(mls_group_id: &[u8], epoch: u64) -> Vec<u8> {
17-
let mut key = mls_group_id.to_vec();
18-
key.extend_from_slice(&epoch.to_be_bytes());
19-
key
20-
}
21-
2214
impl GroupStorage for NostrMlsMemoryStorage {
2315
fn save_group(&self, group: Group) -> Result<(), GroupError> {
2416
// Store in the MLS group ID cache
@@ -30,7 +22,7 @@ impl GroupStorage for NostrMlsMemoryStorage {
3022
// Store in the Nostr group ID cache
3123
{
3224
let mut cache = self.groups_by_nostr_id_cache.write();
33-
cache.put(group.nostr_group_id.clone(), group);
25+
cache.put(group.nostr_group_id, group);
3426
}
3527

3628
Ok(())
@@ -43,20 +35,23 @@ impl GroupStorage for NostrMlsMemoryStorage {
4335
Ok(groups)
4436
}
4537

46-
fn find_group_by_mls_group_id(&self, mls_group_id: &[u8]) -> Result<Option<Group>, GroupError> {
38+
fn find_group_by_mls_group_id(
39+
&self,
40+
mls_group_id: &GroupId,
41+
) -> Result<Option<Group>, GroupError> {
4742
let cache = self.groups_cache.read();
4843
Ok(cache.peek(mls_group_id).cloned())
4944
}
5045

5146
fn find_group_by_nostr_group_id(
5247
&self,
53-
nostr_group_id: &str,
48+
nostr_group_id: &[u8; 32],
5449
) -> Result<Option<Group>, GroupError> {
5550
let cache = self.groups_by_nostr_id_cache.read();
5651
Ok(cache.peek(nostr_group_id).cloned())
5752
}
5853

59-
fn messages(&self, mls_group_id: &[u8]) -> Result<Vec<Message>, GroupError> {
54+
fn messages(&self, mls_group_id: &GroupId) -> Result<Vec<Message>, GroupError> {
6055
// Check if the group exists first
6156
self.find_group_by_mls_group_id(mls_group_id)?;
6257

@@ -68,14 +63,14 @@ impl GroupStorage for NostrMlsMemoryStorage {
6863
}
6964
}
7065

71-
fn admins(&self, mls_group_id: &[u8]) -> Result<BTreeSet<PublicKey>, GroupError> {
66+
fn admins(&self, mls_group_id: &GroupId) -> Result<BTreeSet<PublicKey>, GroupError> {
7267
match self.find_group_by_mls_group_id(mls_group_id)? {
7368
Some(group) => Ok(group.admin_pubkeys),
7469
None => Err(GroupError::InvalidState(InvalidGroupState::NoAdmins)),
7570
}
7671
}
7772

78-
fn group_relays(&self, mls_group_id: &[u8]) -> Result<BTreeSet<GroupRelay>, GroupError> {
73+
fn group_relays(&self, mls_group_id: &GroupId) -> Result<BTreeSet<GroupRelay>, GroupError> {
7974
// Check if the group exists first
8075
self.find_group_by_mls_group_id(mls_group_id)?;
8176

@@ -114,16 +109,15 @@ impl GroupStorage for NostrMlsMemoryStorage {
114109

115110
fn get_group_exporter_secret(
116111
&self,
117-
mls_group_id: &[u8],
112+
mls_group_id: &GroupId,
118113
epoch: u64,
119114
) -> Result<Option<GroupExporterSecret>, GroupError> {
120115
// Check if the group exists first
121116
self.find_group_by_mls_group_id(mls_group_id)?;
122117

123118
let cache = self.group_exporter_secrets_cache.read();
124-
// Create a compound key from mls_group_id and epoch
125-
let key = create_compound_key(mls_group_id, epoch);
126-
Ok(cache.peek(&key).cloned())
119+
// Use tuple (GroupId, epoch) as key
120+
Ok(cache.peek(&(mls_group_id.clone(), epoch)).cloned())
127121
}
128122

129123
fn save_group_exporter_secret(
@@ -134,9 +128,9 @@ impl GroupStorage for NostrMlsMemoryStorage {
134128
self.find_group_by_mls_group_id(&group_exporter_secret.mls_group_id)?;
135129

136130
let mut cache = self.group_exporter_secrets_cache.write();
137-
// Create a compound key from mls_group_id and epoch
138-
let key = create_compound_key(
139-
&group_exporter_secret.mls_group_id,
131+
// Use tuple (GroupId, epoch) as key
132+
let key = (
133+
group_exporter_secret.mls_group_id.clone(),
140134
group_exporter_secret.epoch,
141135
);
142136
cache.put(key, group_exporter_secret);

0 commit comments

Comments
 (0)