Skip to content

Commit

Permalink
Add HMAC support to WASM bindings (#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine authored Jan 10, 2025
1 parent e924f9e commit 148bc7d
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion bindings_wasm/src/conversations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;
use std::sync::Arc;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError, JsValue};
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 @@ -175,6 +176,22 @@ impl CreateGroupOptions {
}
}

#[wasm_bindgen(getter_with_clone)]
#[derive(serde::Serialize)]
pub struct HmacKey {
pub key: Vec<u8>,
pub epoch: i64,
}

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

#[wasm_bindgen]
pub struct Conversations {
inner_client: Arc<RustXmtpClient>,
Expand Down Expand Up @@ -369,4 +386,29 @@ impl Conversations {
..opts.unwrap_or_default()
}))
}

#[wasm_bindgen(js_name = getHmacKeys)]
pub fn get_hmac_keys(&self) -> Result<JsValue, JsError> {
let inner = self.inner_client.as_ref();
let conversations = inner
.find_groups(GroupQueryArgs {
include_duplicate_dms: true,
..Default::default()
})
.map_err(|e| JsError::new(format!("{}", e).as_str()))?;

let mut hmac_map: HashMap<String, Vec<HmacKey>> = HashMap::new();
for conversation in conversations {
let id = hex::encode(&conversation.group_id);
let keys = conversation
.hmac_keys(-1..=1)
.map_err(|e| JsError::new(format!("{}", e).as_str()))?
.into_iter()
.map(Into::into)
.collect::<Vec<_>>();
hmac_map.insert(id, keys);
}

Ok(serde_wasm_bindgen::to_value(&hmac_map)?)
}
}

0 comments on commit 148bc7d

Please sign in to comment.