|
| 1 | +use std::collections::HashMap; |
1 | 2 | use std::ops::Deref;
|
2 | 3 | use std::sync::Arc;
|
3 | 4 | use std::vec;
|
4 | 5 |
|
5 |
| -use napi::bindgen_prelude::{Error, Result, Uint8Array}; |
| 6 | +use napi::bindgen_prelude::{BigInt, Error, Result, Uint8Array}; |
6 | 7 | use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode};
|
7 | 8 | use napi::JsFunction;
|
8 | 9 | use napi_derive::napi;
|
9 |
| -use xmtp_mls::groups::{GroupMetadataOptions, PreconfiguredPolicies}; |
| 10 | +use xmtp_mls::groups::{GroupMetadataOptions, HmacKey as XmtpHmacKey, PreconfiguredPolicies}; |
10 | 11 | use xmtp_mls::storage::group::ConversationType as XmtpConversationType;
|
11 | 12 | use xmtp_mls::storage::group::GroupMembershipState as XmtpGroupMembershipState;
|
12 | 13 | use xmtp_mls::storage::group::GroupQueryArgs;
|
@@ -97,6 +98,21 @@ impl From<ListConversationsOptions> for GroupQueryArgs {
|
97 | 98 | }
|
98 | 99 | }
|
99 | 100 |
|
| 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 | + |
100 | 116 | #[napi(object)]
|
101 | 117 | #[derive(Clone)]
|
102 | 118 | pub struct CreateGroupOptions {
|
@@ -325,6 +341,31 @@ impl Conversations {
|
325 | 341 | .await
|
326 | 342 | }
|
327 | 343 |
|
| 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 | + |
328 | 369 | #[napi(ts_args_type = "callback: (err: null | Error, result: Conversation | undefined) => void")]
|
329 | 370 | pub fn stream(
|
330 | 371 | &self,
|
|
0 commit comments