Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lowercase address when checking association state #1362

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bindings_node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# @xmtp/node-bindings

## 0.0.28

- Removed `is_installation_authorized` and `is_address_authorized` from `Client`
- Lowercased `address` passed to `is_address_authorized`

## 0.0.27

- Switched to Ubuntu 22.04 for builds
Expand Down
2 changes: 1 addition & 1 deletion bindings_node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xmtp/node-bindings",
"version": "0.0.27",
"version": "0.0.28",
"repository": {
"type": "git",
"url": "git+https://git@github.com/xmtp/libxmtp.git",
Expand Down
89 changes: 0 additions & 89 deletions bindings_node/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ use tracing_subscriber::{fmt, prelude::*};
pub use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_cryptography::signature::ed25519_public_key_to_address;
use xmtp_id::associations::builder::SignatureRequest;
use xmtp_id::associations::MemberIdentifier;
use xmtp_mls::api::ApiClientWrapper;
use xmtp_mls::builder::ClientBuilder;
use xmtp_mls::groups::scoped_client::LocalScopedGroupClient;
use xmtp_mls::identity::IdentityStrategy;
use xmtp_mls::retry::Retry;
use xmtp_mls::storage::{EncryptedMessageStore, EncryptionKey, StorageOption};
use xmtp_mls::Client as MlsClient;
use xmtp_proto::xmtp::mls::message_contents::DeviceSyncKind;
Expand Down Expand Up @@ -303,90 +300,4 @@ impl Client {
.map_err(ErrorWrapper::from)?;
Ok(state.into_iter().map(Into::into).collect())
}

#[napi]
pub async fn is_address_authorized(&self, inbox_id: String, address: String) -> Result<bool> {
self
.is_member_of_association_state(&inbox_id, &MemberIdentifier::Address(address))
.await
}

#[napi]
pub async fn is_installation_authorized(
&self,
inbox_id: String,
installation_id: Uint8Array,
) -> Result<bool> {
self
.is_member_of_association_state(
&inbox_id,
&MemberIdentifier::Installation(installation_id.to_vec()),
)
.await
}

async fn is_member_of_association_state(
&self,
inbox_id: &str,
identifier: &MemberIdentifier,
) -> Result<bool> {
let client = &self.inner_client;
let conn = self
.inner_client
.store()
.conn()
.map_err(ErrorWrapper::from)?;

let association_state = client
.get_association_state(&conn, inbox_id, None)
.await
.map_err(ErrorWrapper::from)?;

Ok(association_state.get(identifier).is_some())
}
}

#[napi]
pub async fn is_installation_authorized(
host: String,
inbox_id: String,
installation_id: Uint8Array,
) -> Result<bool> {
is_member_of_association_state(
&host,
&inbox_id,
&MemberIdentifier::Installation(installation_id.to_vec()),
)
.await
}

#[napi]
pub async fn is_address_authorized(
host: String,
inbox_id: String,
address: String,
) -> Result<bool> {
is_member_of_association_state(&host, &inbox_id, &MemberIdentifier::Address(address)).await
}

async fn is_member_of_association_state(
host: &str,
inbox_id: &str,
identifier: &MemberIdentifier,
) -> Result<bool> {
let api_client = TonicApiClient::create(host, true)
.await
.map_err(ErrorWrapper::from)?;
let api_client = ApiClientWrapper::new(Arc::new(api_client), Retry::default());

let is_member = xmtp_mls::identity_updates::is_member_of_association_state(
&api_client,
inbox_id,
identifier,
None,
)
.await
.map_err(ErrorWrapper::from)?;

Ok(is_member)
}
53 changes: 53 additions & 0 deletions bindings_node/src/inbox_id.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::ErrorWrapper;
use napi::bindgen_prelude::Result;
use napi::bindgen_prelude::Uint8Array;
use napi_derive::napi;
use std::sync::Arc;
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
use xmtp_id::associations::generate_inbox_id as xmtp_id_generate_inbox_id;
use xmtp_id::associations::MemberIdentifier;
use xmtp_mls::api::ApiClientWrapper;
use xmtp_mls::retry::Retry;

Expand Down Expand Up @@ -37,3 +40,53 @@ pub fn generate_inbox_id(account_address: String) -> Result<String> {
let result = xmtp_id_generate_inbox_id(&account_address, &1).map_err(ErrorWrapper::from)?;
Ok(result)
}

#[napi]
pub async fn is_installation_authorized(
host: String,
inbox_id: String,
installation_id: Uint8Array,
) -> Result<bool> {
is_member_of_association_state(
&host,
&inbox_id,
&MemberIdentifier::Installation(installation_id.to_vec()),
)
.await
}

#[napi]
pub async fn is_address_authorized(
host: String,
inbox_id: String,
address: String,
) -> Result<bool> {
is_member_of_association_state(
&host,
&inbox_id,
&MemberIdentifier::Address(address.to_lowercase()),
)
.await
}

async fn is_member_of_association_state(
host: &str,
inbox_id: &str,
identifier: &MemberIdentifier,
) -> Result<bool> {
let api_client = TonicApiClient::create(host, true)
.await
.map_err(ErrorWrapper::from)?;
let api_client = ApiClientWrapper::new(Arc::new(api_client), Retry::default());

let is_member = xmtp_mls::identity_updates::is_member_of_association_state(
&api_client,
inbox_id,
identifier,
None,
)
.await
.map_err(ErrorWrapper::from)?;

Ok(is_member)
}
5 changes: 3 additions & 2 deletions bindings_node/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
createClient as create,
generateInboxId,
getInboxIdForAddress,
LogLevel,
SignatureRequestType,
} from '../dist/index'

Expand Down Expand Up @@ -44,7 +45,7 @@ export const createClient = async (user: User) => {
user.account.address,
undefined,
undefined,
{ level: 'info' }
{ level: LogLevel.info }
)
}

Expand Down Expand Up @@ -81,7 +82,7 @@ export const encodeTextMessage = (text: string) => {
}
}

export function sleep(ms) {
export function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
Expand Down
33 changes: 32 additions & 1 deletion bindings_node/test/inboxId.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, it } from 'vitest'
import { createRegisteredClient, createUser, TEST_API_URL } from '@test/helpers'
import { generateInboxId, getInboxIdForAddress } from '../dist/index'
import {
generateInboxId,
getInboxIdForAddress,
isAddressAuthorized,
isInstallationAuthorized,
} from '../dist/index'

describe('generateInboxId', () => {
it('should generate an inbox id', () => {
Expand Down Expand Up @@ -32,3 +37,29 @@ describe('getInboxIdForAddress', () => {
expect(inboxId).toBe(client.inboxId())
})
})

describe('isInstallationAuthorized', () => {
it('should return true if installation is authorized', async () => {
const user = createUser()
const client = await createRegisteredClient(user)
const isAuthorized = await isInstallationAuthorized(
TEST_API_URL,
client.inboxId(),
client.installationIdBytes()
)
expect(isAuthorized).toBe(true)
})
})

describe('isAddressAuthorized', () => {
it('should return true if address is authorized', async () => {
const user = createUser()
const client = await createRegisteredClient(user)
const isAuthorized = await isAddressAuthorized(
TEST_API_URL,
client.inboxId(),
user.account.address
)
expect(isAuthorized).toBe(true)
})
})
Loading