Skip to content

Commit fcdce97

Browse files
authored
Provide hmac keys on the napi (#1450)
* provide hmac keys on the napi * alias the import instead * use BigInt
1 parent 39480a0 commit fcdce97

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

bindings_ffi/src/mls.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,10 @@ impl FfiConversations {
11191119

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

11241127
let mut hmac_map = HashMap::new();
11251128
for conversation in conversations {

bindings_node/src/conversations.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
use std::collections::HashMap;
12
use std::ops::Deref;
23
use std::sync::Arc;
34
use std::vec;
45

5-
use napi::bindgen_prelude::{Error, Result, Uint8Array};
6+
use napi::bindgen_prelude::{BigInt, Error, Result, Uint8Array};
67
use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode};
78
use napi::JsFunction;
89
use napi_derive::napi;
9-
use xmtp_mls::groups::{GroupMetadataOptions, PreconfiguredPolicies};
10+
use xmtp_mls::groups::{GroupMetadataOptions, HmacKey as XmtpHmacKey, PreconfiguredPolicies};
1011
use xmtp_mls::storage::group::ConversationType as XmtpConversationType;
1112
use xmtp_mls::storage::group::GroupMembershipState as XmtpGroupMembershipState;
1213
use xmtp_mls::storage::group::GroupQueryArgs;
@@ -97,6 +98,21 @@ impl From<ListConversationsOptions> for GroupQueryArgs {
9798
}
9899
}
99100

101+
#[napi(object)]
102+
pub struct HmacKey {
103+
pub key: Vec<u8>,
104+
pub epoch: BigInt,
105+
}
106+
107+
impl From<XmtpHmacKey> for HmacKey {
108+
fn from(value: XmtpHmacKey) -> Self {
109+
Self {
110+
epoch: BigInt::from(value.epoch),
111+
key: value.key.to_vec(),
112+
}
113+
}
114+
}
115+
100116
#[napi(object)]
101117
#[derive(Clone)]
102118
pub struct CreateGroupOptions {
@@ -325,6 +341,31 @@ impl Conversations {
325341
.await
326342
}
327343

344+
#[napi]
345+
pub fn get_hmac_keys(&self) -> Result<HashMap<String, Vec<HmacKey>>> {
346+
let inner = self.inner_client.as_ref();
347+
let conversations = inner
348+
.find_groups(GroupQueryArgs {
349+
include_duplicate_dms: true,
350+
..Default::default()
351+
})
352+
.map_err(ErrorWrapper::from)?;
353+
354+
let mut hmac_map = HashMap::new();
355+
for conversation in conversations {
356+
let id = hex::encode(&conversation.group_id);
357+
let keys = conversation
358+
.hmac_keys(-1..=1)
359+
.map_err(ErrorWrapper::from)?
360+
.into_iter()
361+
.map(Into::into)
362+
.collect::<Vec<_>>();
363+
hmac_map.insert(id, keys);
364+
}
365+
366+
Ok(hmac_map)
367+
}
368+
328369
#[napi(ts_args_type = "callback: (err: null | Error, result: Conversation | undefined) => void")]
329370
pub fn stream(
330371
&self,

0 commit comments

Comments
 (0)