From 8ad235a76b64c99bbfb7bc315ab7261110a106ae Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Thu, 6 Jun 2024 17:49:29 -0500 Subject: [PATCH 1/4] Ensure lowercase address for inbox IDs --- bindings_node/src/mls_client.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bindings_node/src/mls_client.rs b/bindings_node/src/mls_client.rs index aaaac0b62..0c61718f6 100644 --- a/bindings_node/src/mls_client.rs +++ b/bindings_node/src/mls_client.rs @@ -82,6 +82,7 @@ pub async fn get_inbox_id_for_address( is_secure: bool, account_address: String, ) -> Result> { + let account_address = account_address.to_lowercase(); let api_client = ApiClientWrapper::new( TonicApiClient::create(host.clone(), is_secure) .await @@ -99,6 +100,7 @@ pub async fn get_inbox_id_for_address( #[napi] pub fn generate_inbox_id(account_address: String) -> String { + let account_address = account_address.to_lowercase(); // ensure that the nonce is always 1 for now since this will only be used for the // create_client function above, which also has a hard-coded nonce of 1 xmtp_id_generate_inbox_id(&account_address, &1) From 763f774de04f41ba696706b8ce3a2b3d06338b3e Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Thu, 6 Jun 2024 17:50:12 -0500 Subject: [PATCH 2/4] Add permissions functions to groups --- bindings_node/src/groups.rs | 141 +++++++++++++++++++++++++++++++++++- 1 file changed, 140 insertions(+), 1 deletion(-) diff --git a/bindings_node/src/groups.rs b/bindings_node/src/groups.rs index cbb0183a7..adfaea54b 100644 --- a/bindings_node/src/groups.rs +++ b/bindings_node/src/groups.rs @@ -9,7 +9,8 @@ use xmtp_cryptography::signature::ed25519_public_key_to_address; use xmtp_mls::groups::{ group_metadata::{ConversationType, GroupMetadata}, group_permissions::GroupMutablePermissions, - MlsGroup, PreconfiguredPolicies, + members::PermissionLevel, + MlsGroup, PreconfiguredPolicies, UpdateAdminListType, }; use xmtp_proto::xmtp::mls::message_contents::EncodedContent; @@ -70,11 +71,19 @@ impl NapiGroupMetadata { } } +#[napi] +pub enum NapiPermissionLevel { + Member, + Admin, + SuperAdmin, +} + #[napi] pub struct NapiGroupMember { pub inbox_id: String, pub account_addresses: Vec, pub installation_ids: Vec, + pub permission_level: NapiPermissionLevel, } #[napi] @@ -231,12 +240,63 @@ impl NapiGroup { .into_iter() .map(|id| ed25519_public_key_to_address(id.as_slice())) .collect(), + permission_level: match member.permission_level { + PermissionLevel::Member => NapiPermissionLevel::Member, + PermissionLevel::Admin => NapiPermissionLevel::Admin, + PermissionLevel::SuperAdmin => NapiPermissionLevel::SuperAdmin, + }, }) .collect(); Ok(members) } + #[napi] + pub fn admin_list(&self) -> Result> { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + + let admin_list = group + .admin_list() + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(admin_list) + } + + #[napi] + pub fn super_admin_list(&self) -> Result> { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + + let super_admin_list = group + .super_admin_list() + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(super_admin_list) + } + + #[napi] + pub fn is_admin(&self, inbox_id: String) -> Result { + let admin_list = self + .admin_list() + .map_err(|e| Error::from_reason(format!("{}", e)))?; + Ok(admin_list.contains(&inbox_id)) + } + + #[napi] + pub fn is_super_admin(&self, inbox_id: String) -> Result { + let super_admin_list = self + .super_admin_list() + .map_err(|e| Error::from_reason(format!("{}", e)))?; + Ok(super_admin_list.contains(&inbox_id)) + } + #[napi] pub async fn add_members(&self, account_addresses: Vec) -> Result<()> { let group = MlsGroup::new( @@ -253,6 +313,85 @@ impl NapiGroup { Ok(()) } + #[napi] + pub async fn add_admin(&self, inbox_id: String) -> Result<()> { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + group + .update_admin_list(&self.inner_client, UpdateAdminListType::Add, inbox_id) + .await + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(()) + } + + #[napi] + pub async fn remove_admin(&self, inbox_id: String) -> Result<()> { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + group + .update_admin_list(&self.inner_client, UpdateAdminListType::Remove, inbox_id) + .await + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(()) + } + + #[napi] + pub async fn add_super_admin(&self, inbox_id: String) -> Result<()> { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + group + .update_admin_list(&self.inner_client, UpdateAdminListType::AddSuper, inbox_id) + .await + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(()) + } + + #[napi] + pub async fn remove_super_admin(&self, inbox_id: String) -> Result<()> { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + group + .update_admin_list( + &self.inner_client, + UpdateAdminListType::RemoveSuper, + inbox_id, + ) + .await + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(()) + } + + #[napi] + pub fn group_permissions(&self) -> Result { + let group = MlsGroup::new( + self.inner_client.context().clone(), + self.group_id.clone(), + self.created_at_ns, + ); + + let permissions = group + .permissions() + .map_err(|e| Error::from_reason(format!("{}", e)))?; + + Ok(NapiGroupPermissions { inner: permissions }) + } + #[napi] pub async fn add_members_by_inbox_id(&self, inbox_ids: Vec) -> Result<()> { let group = MlsGroup::new( From 99c9b85da0ec4e9f7facc11b38402de8433b5bce Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Thu, 6 Jun 2024 17:50:24 -0500 Subject: [PATCH 3/4] Update test utils --- bindings_node/scripts/streamTest.mjs | 3 ++- bindings_node/scripts/utils.mjs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bindings_node/scripts/streamTest.mjs b/bindings_node/scripts/streamTest.mjs index ea3e2a86f..c04a7845d 100644 --- a/bindings_node/scripts/streamTest.mjs +++ b/bindings_node/scripts/streamTest.mjs @@ -1,6 +1,7 @@ import process from "node:process"; import { AsyncStream } from "./AsyncStream.mjs"; -import { initEcdsaClient } from "./utils.mjs"; +import { initEcdsaClient, syncGroups } from "./utils.mjs"; +import { wallets } from "./users.mjs"; const client1 = await initEcdsaClient(wallets[0]); await syncGroups(client1); diff --git a/bindings_node/scripts/utils.mjs b/bindings_node/scripts/utils.mjs index b7251fa1d..6459afd23 100644 --- a/bindings_node/scripts/utils.mjs +++ b/bindings_node/scripts/utils.mjs @@ -2,15 +2,27 @@ import { toBytes } from "viem"; import { join } from "node:path"; import process from "node:process"; import { TextEncoder } from "node:util"; -import { createClient } from "../dist/index.js"; +import { + createClient, + getInboxIdForAddress, + generateInboxId, +} from "../dist/index.js"; export const initEcdsaClient = async (wallet) => { const dbPath = join(process.cwd(), `${wallet.account.address}.db3`); + const inboxId = + (await getInboxIdForAddress( + "http://localhost:5556", + false, + wallet.account.address + )) || generateInboxId(wallet.account.address); + const client = await createClient( "http://localhost:5556", false, dbPath, + inboxId, wallet.account.address ); From fb4044d7f78c9efa1a4830e0aeaed653fddf60b6 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Thu, 6 Jun 2024 17:56:27 -0500 Subject: [PATCH 4/4] Updated version and changelog --- bindings_node/CHANGELOG.md | 6 ++++++ bindings_node/package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bindings_node/CHANGELOG.md b/bindings_node/CHANGELOG.md index d80ccb8d0..c8f24766b 100644 --- a/bindings_node/CHANGELOG.md +++ b/bindings_node/CHANGELOG.md @@ -1,5 +1,11 @@ # @xmtp/mls-client-bindings-node +## 0.0.2 + +- Added inbox ID helpers +- Refactored identity strategy creation +- Added permissions functions to groups + ## 0.0.1 Initial release diff --git a/bindings_node/package.json b/bindings_node/package.json index 50bc131d5..0e8cd240f 100644 --- a/bindings_node/package.json +++ b/bindings_node/package.json @@ -1,6 +1,6 @@ { "name": "@xmtp/mls-client-bindings-node", - "version": "0.0.1", + "version": "0.0.2", "main": "dist/index.js", "types": "dist/index.d.ts", "exports": {