Skip to content

Commit

Permalink
provide on the ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink committed Dec 9, 2024
1 parent ccb7bdd commit 5c12d76
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
39 changes: 39 additions & 0 deletions bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use xmtp_id::{
InboxId,
};
use xmtp_mls::groups::scoped_client::LocalScopedGroupClient;
use xmtp_mls::groups::HmacKey;
use xmtp_mls::storage::group::ConversationType;
use xmtp_mls::storage::group_message::MsgQueryArgs;
use xmtp_mls::storage::group_message::SortDirection;
Expand Down Expand Up @@ -527,6 +528,38 @@ impl FfiXmtpClient {
scw_verifier: self.inner_client.scw_verifier().clone().clone(),
}))
}

pub fn get_hmac_keys(&self) -> Result<Vec<FfiHmacKey>, GenericError> {
let inner = self.inner_client.as_ref();
let conversations: Vec<Arc<FfiConversation>> = inner
.find_groups(GroupQueryArgs::default())?
.into_iter()
.map(|group| Arc::new(group.into()))
.collect();

let mut keys = vec![];
for conversation in conversations {
let mut k = conversation
.inner
.hmac_keys(-1..=1)?
.into_iter()
.map(Into::into)
.collect::<Vec<_>>();

keys.append(&mut k);
}

Ok(keys)
}
}

impl Into<FfiHmacKey> for HmacKey {
fn into(self) -> FfiHmacKey {
FfiHmacKey {
epoch: self.epoch,
key: self.key.to_vec(),
}
}
}

#[derive(uniffi::Record)]
Expand All @@ -537,6 +570,12 @@ pub struct FfiInboxState {
pub account_addresses: Vec<String>,
}

#[derive(uniffi::Record)]
pub struct FfiHmacKey {
key: Vec<u8>,
epoch: i64,
}

#[derive(uniffi::Record)]
pub struct FfiInstallation {
pub id: Vec<u8>,
Expand Down
24 changes: 12 additions & 12 deletions xmtp_mls/src/groups/mls_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
UpdateAdminListIntentData, UpdateGroupMembershipIntentData, UpdatePermissionIntentData,
},
validated_commit::{extract_group_membership, CommitValidationError},
GroupError, IntentError, MlsGroup, ScopedGroupClient,
GroupError, HmacKey, IntentError, MlsGroup, ScopedGroupClient,
};
use crate::{
codecs::{group_updated::GroupUpdatedCodec, ContentCodec},
Expand Down Expand Up @@ -61,7 +61,7 @@ use sha2::Sha256;
use std::{
collections::{HashMap, HashSet},
mem::{discriminant, Discriminant},
ops::Range,
ops::{Range, RangeInclusive},
};
use thiserror::Error;
use xmtp_id::{InboxId, InboxIdRef};
Expand Down Expand Up @@ -167,17 +167,17 @@ struct PublishIntentData {
payload_to_publish: Vec<u8>,
}

pub struct HmacKey {
key: [u8; 42],
// # of 30 day periods since unix epoch
epoch: i64,
}

impl<ScopedClient> MlsGroup<ScopedClient>
where
ScopedClient: ScopedGroupClient,
{
fn hmac_keys(&self, epoch_delta_range: Range<i64>) -> Result<Vec<HmacKey>, StorageError> {
/// Provides hmac keys for a range of epochs around current epoch
/// `group.hmac_keys(-1..=1)`` will provide 3 keys consisting of last epoch, current epoch, and next epoch
/// `group.hmac_keys(0..=0) will provide 1 key, consisting of only the current epoch
pub fn hmac_keys(
&self,
epoch_delta_range: RangeInclusive<i64>,
) -> Result<Vec<HmacKey>, StorageError> {
let mut base_okm = [0; 42];

let conn = self.client.store().conn()?;
Expand All @@ -200,12 +200,12 @@ where
Ok(result)
}

pub(super) fn add_hmac_to_messages(
pub(super) fn prepare_group_messages(
&self,
payloads: Vec<&[u8]>,
) -> Result<Vec<GroupMessageInput>, GroupError> {
let hmac_key = self
.hmac_keys(0..1)?
.hmac_keys(0..=0)?
.pop()
.expect("Range of count 1 was provided.");
let sender_hmac =
Expand Down Expand Up @@ -1047,7 +1047,7 @@ where
intent.id
);

let messages = self.add_hmac_to_messages(vec![payload_slice])?;
let messages = self.prepare_group_messages(vec![payload_slice])?;
self.client.api().send_group_messages(messages).await?;

tracing::info!(
Expand Down
8 changes: 7 additions & 1 deletion xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ impl<C> Clone for MlsGroup<C> {
}
}

pub struct HmacKey {
pub key: [u8; 42],
// # of 30 day periods since unix epoch
pub epoch: i64,
}

#[derive(Debug, Clone, PartialEq)]
pub enum UpdateAdminListType {
Add,
Expand Down Expand Up @@ -1677,7 +1683,7 @@ pub(crate) mod tests {
serialized_welcome,
);
let messages = sender_group
.add_hmac_to_messages(vec![serialized_commit.as_slice()])
.prepare_group_messages(vec![serialized_commit.as_slice()])
.unwrap();
sender_client
.api_client
Expand Down

0 comments on commit 5c12d76

Please sign in to comment.