Skip to content

Commit 0a078b0

Browse files
authored
Lowercase address when checking association state (#1362)
* Lowercase address when checking assoc state * Fix test helper types * Prepare release
1 parent e747eec commit 0a078b0

File tree

6 files changed

+94
-93
lines changed

6 files changed

+94
-93
lines changed

bindings_node/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# @xmtp/node-bindings
22

3+
## 0.0.28
4+
5+
- Removed `is_installation_authorized` and `is_address_authorized` from `Client`
6+
- Lowercased `address` passed to `is_address_authorized`
7+
38
## 0.0.27
49

510
- Switched to Ubuntu 22.04 for builds

bindings_node/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xmtp/node-bindings",
3-
"version": "0.0.27",
3+
"version": "0.0.28",
44
"repository": {
55
"type": "git",
66
"url": "git+https://git@github.com/xmtp/libxmtp.git",

bindings_node/src/client.rs

-89
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ use tracing_subscriber::{fmt, prelude::*};
1313
pub use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
1414
use xmtp_cryptography::signature::ed25519_public_key_to_address;
1515
use xmtp_id::associations::builder::SignatureRequest;
16-
use xmtp_id::associations::MemberIdentifier;
17-
use xmtp_mls::api::ApiClientWrapper;
1816
use xmtp_mls::builder::ClientBuilder;
1917
use xmtp_mls::groups::scoped_client::LocalScopedGroupClient;
2018
use xmtp_mls::identity::IdentityStrategy;
21-
use xmtp_mls::retry::Retry;
2219
use xmtp_mls::storage::{EncryptedMessageStore, EncryptionKey, StorageOption};
2320
use xmtp_mls::Client as MlsClient;
2421
use xmtp_proto::xmtp::mls::message_contents::DeviceSyncKind;
@@ -303,90 +300,4 @@ impl Client {
303300
.map_err(ErrorWrapper::from)?;
304301
Ok(state.into_iter().map(Into::into).collect())
305302
}
306-
307-
#[napi]
308-
pub async fn is_address_authorized(&self, inbox_id: String, address: String) -> Result<bool> {
309-
self
310-
.is_member_of_association_state(&inbox_id, &MemberIdentifier::Address(address))
311-
.await
312-
}
313-
314-
#[napi]
315-
pub async fn is_installation_authorized(
316-
&self,
317-
inbox_id: String,
318-
installation_id: Uint8Array,
319-
) -> Result<bool> {
320-
self
321-
.is_member_of_association_state(
322-
&inbox_id,
323-
&MemberIdentifier::Installation(installation_id.to_vec()),
324-
)
325-
.await
326-
}
327-
328-
async fn is_member_of_association_state(
329-
&self,
330-
inbox_id: &str,
331-
identifier: &MemberIdentifier,
332-
) -> Result<bool> {
333-
let client = &self.inner_client;
334-
let conn = self
335-
.inner_client
336-
.store()
337-
.conn()
338-
.map_err(ErrorWrapper::from)?;
339-
340-
let association_state = client
341-
.get_association_state(&conn, inbox_id, None)
342-
.await
343-
.map_err(ErrorWrapper::from)?;
344-
345-
Ok(association_state.get(identifier).is_some())
346-
}
347-
}
348-
349-
#[napi]
350-
pub async fn is_installation_authorized(
351-
host: String,
352-
inbox_id: String,
353-
installation_id: Uint8Array,
354-
) -> Result<bool> {
355-
is_member_of_association_state(
356-
&host,
357-
&inbox_id,
358-
&MemberIdentifier::Installation(installation_id.to_vec()),
359-
)
360-
.await
361-
}
362-
363-
#[napi]
364-
pub async fn is_address_authorized(
365-
host: String,
366-
inbox_id: String,
367-
address: String,
368-
) -> Result<bool> {
369-
is_member_of_association_state(&host, &inbox_id, &MemberIdentifier::Address(address)).await
370-
}
371-
372-
async fn is_member_of_association_state(
373-
host: &str,
374-
inbox_id: &str,
375-
identifier: &MemberIdentifier,
376-
) -> Result<bool> {
377-
let api_client = TonicApiClient::create(host, true)
378-
.await
379-
.map_err(ErrorWrapper::from)?;
380-
let api_client = ApiClientWrapper::new(Arc::new(api_client), Retry::default());
381-
382-
let is_member = xmtp_mls::identity_updates::is_member_of_association_state(
383-
&api_client,
384-
inbox_id,
385-
identifier,
386-
None,
387-
)
388-
.await
389-
.map_err(ErrorWrapper::from)?;
390-
391-
Ok(is_member)
392303
}

bindings_node/src/inbox_id.rs

+53
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use crate::ErrorWrapper;
22
use napi::bindgen_prelude::Result;
3+
use napi::bindgen_prelude::Uint8Array;
34
use napi_derive::napi;
5+
use std::sync::Arc;
46
use xmtp_api_grpc::grpc_api_helper::Client as TonicApiClient;
57
use xmtp_id::associations::generate_inbox_id as xmtp_id_generate_inbox_id;
8+
use xmtp_id::associations::MemberIdentifier;
69
use xmtp_mls::api::ApiClientWrapper;
710
use xmtp_mls::retry::Retry;
811

@@ -37,3 +40,53 @@ pub fn generate_inbox_id(account_address: String) -> Result<String> {
3740
let result = xmtp_id_generate_inbox_id(&account_address, &1).map_err(ErrorWrapper::from)?;
3841
Ok(result)
3942
}
43+
44+
#[napi]
45+
pub async fn is_installation_authorized(
46+
host: String,
47+
inbox_id: String,
48+
installation_id: Uint8Array,
49+
) -> Result<bool> {
50+
is_member_of_association_state(
51+
&host,
52+
&inbox_id,
53+
&MemberIdentifier::Installation(installation_id.to_vec()),
54+
)
55+
.await
56+
}
57+
58+
#[napi]
59+
pub async fn is_address_authorized(
60+
host: String,
61+
inbox_id: String,
62+
address: String,
63+
) -> Result<bool> {
64+
is_member_of_association_state(
65+
&host,
66+
&inbox_id,
67+
&MemberIdentifier::Address(address.to_lowercase()),
68+
)
69+
.await
70+
}
71+
72+
async fn is_member_of_association_state(
73+
host: &str,
74+
inbox_id: &str,
75+
identifier: &MemberIdentifier,
76+
) -> Result<bool> {
77+
let api_client = TonicApiClient::create(host, true)
78+
.await
79+
.map_err(ErrorWrapper::from)?;
80+
let api_client = ApiClientWrapper::new(Arc::new(api_client), Retry::default());
81+
82+
let is_member = xmtp_mls::identity_updates::is_member_of_association_state(
83+
&api_client,
84+
inbox_id,
85+
identifier,
86+
None,
87+
)
88+
.await
89+
.map_err(ErrorWrapper::from)?;
90+
91+
Ok(is_member)
92+
}

bindings_node/test/helpers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
createClient as create,
99
generateInboxId,
1010
getInboxIdForAddress,
11+
LogLevel,
1112
SignatureRequestType,
1213
} from '../dist/index'
1314

@@ -44,7 +45,7 @@ export const createClient = async (user: User) => {
4445
user.account.address,
4546
undefined,
4647
undefined,
47-
{ level: 'info' }
48+
{ level: LogLevel.info }
4849
)
4950
}
5051

@@ -81,7 +82,7 @@ export const encodeTextMessage = (text: string) => {
8182
}
8283
}
8384

84-
export function sleep(ms) {
85+
export function sleep(ms: number) {
8586
return new Promise((resolve) => {
8687
setTimeout(resolve, ms)
8788
})

bindings_node/test/inboxId.test.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { describe, expect, it } from 'vitest'
22
import { createRegisteredClient, createUser, TEST_API_URL } from '@test/helpers'
3-
import { generateInboxId, getInboxIdForAddress } from '../dist/index'
3+
import {
4+
generateInboxId,
5+
getInboxIdForAddress,
6+
isAddressAuthorized,
7+
isInstallationAuthorized,
8+
} from '../dist/index'
49

510
describe('generateInboxId', () => {
611
it('should generate an inbox id', () => {
@@ -32,3 +37,29 @@ describe('getInboxIdForAddress', () => {
3237
expect(inboxId).toBe(client.inboxId())
3338
})
3439
})
40+
41+
describe('isInstallationAuthorized', () => {
42+
it('should return true if installation is authorized', async () => {
43+
const user = createUser()
44+
const client = await createRegisteredClient(user)
45+
const isAuthorized = await isInstallationAuthorized(
46+
TEST_API_URL,
47+
client.inboxId(),
48+
client.installationIdBytes()
49+
)
50+
expect(isAuthorized).toBe(true)
51+
})
52+
})
53+
54+
describe('isAddressAuthorized', () => {
55+
it('should return true if address is authorized', async () => {
56+
const user = createUser()
57+
const client = await createRegisteredClient(user)
58+
const isAuthorized = await isAddressAuthorized(
59+
TEST_API_URL,
60+
client.inboxId(),
61+
user.account.address
62+
)
63+
expect(isAuthorized).toBe(true)
64+
})
65+
})

0 commit comments

Comments
 (0)