Skip to content

Commit

Permalink
Provide hmac keys on the napi (#1450)
Browse files Browse the repository at this point in the history
* provide hmac keys on the napi

* alias the import instead

* use BigInt
  • Loading branch information
codabrink authored Jan 2, 2025
1 parent 39480a0 commit fcdce97
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
5 changes: 4 additions & 1 deletion bindings_ffi/src/mls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,10 @@ impl FfiConversations {

pub fn get_hmac_keys(&self) -> Result<HashMap<Vec<u8>, Vec<FfiHmacKey>>, GenericError> {
let inner = self.inner_client.as_ref();
let conversations = inner.find_groups(GroupQueryArgs::default())?;
let conversations = inner.find_groups(GroupQueryArgs {
include_duplicate_dms: true,
..GroupQueryArgs::default()
})?;

let mut hmac_map = HashMap::new();
for conversation in conversations {
Expand Down
45 changes: 43 additions & 2 deletions bindings_node/src/conversations.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;
use std::vec;

use napi::bindgen_prelude::{Error, Result, Uint8Array};
use napi::bindgen_prelude::{BigInt, Error, Result, Uint8Array};
use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode};
use napi::JsFunction;
use napi_derive::napi;
use xmtp_mls::groups::{GroupMetadataOptions, PreconfiguredPolicies};
use xmtp_mls::groups::{GroupMetadataOptions, HmacKey as XmtpHmacKey, PreconfiguredPolicies};
use xmtp_mls::storage::group::ConversationType as XmtpConversationType;
use xmtp_mls::storage::group::GroupMembershipState as XmtpGroupMembershipState;
use xmtp_mls::storage::group::GroupQueryArgs;
Expand Down Expand Up @@ -97,6 +98,21 @@ impl From<ListConversationsOptions> for GroupQueryArgs {
}
}

#[napi(object)]
pub struct HmacKey {
pub key: Vec<u8>,
pub epoch: BigInt,
}

impl From<XmtpHmacKey> for HmacKey {
fn from(value: XmtpHmacKey) -> Self {
Self {
epoch: BigInt::from(value.epoch),
key: value.key.to_vec(),
}
}
}

#[napi(object)]
#[derive(Clone)]
pub struct CreateGroupOptions {
Expand Down Expand Up @@ -325,6 +341,31 @@ impl Conversations {
.await
}

#[napi]
pub fn get_hmac_keys(&self) -> Result<HashMap<String, Vec<HmacKey>>> {
let inner = self.inner_client.as_ref();
let conversations = inner
.find_groups(GroupQueryArgs {
include_duplicate_dms: true,
..Default::default()
})
.map_err(ErrorWrapper::from)?;

let mut hmac_map = HashMap::new();
for conversation in conversations {
let id = hex::encode(&conversation.group_id);
let keys = conversation
.hmac_keys(-1..=1)
.map_err(ErrorWrapper::from)?
.into_iter()
.map(Into::into)
.collect::<Vec<_>>();
hmac_map.insert(id, keys);
}

Ok(hmac_map)
}

#[napi(ts_args_type = "callback: (err: null | Error, result: Conversation | undefined) => void")]
pub fn stream(
&self,
Expand Down

0 comments on commit fcdce97

Please sign in to comment.