Skip to content

Commit

Permalink
Add static authorization methods to Node SDK Client (#740)
Browse files Browse the repository at this point in the history
* Upgrade node bindings

* Add static methods to client

* Create clean-students-sell.md

* Add more tests
  • Loading branch information
rygine authored Dec 2, 2024
1 parent ee08db4 commit 7338e0e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-students-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/node-sdk": patch
---

Add static authorization methods to Node SDK Client
2 changes: 1 addition & 1 deletion sdks/node-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@xmtp/content-type-group-updated": "^1.0.1",
"@xmtp/content-type-primitives": "^1.0.3",
"@xmtp/content-type-text": "^1.0.1",
"@xmtp/node-bindings": "^0.0.27",
"@xmtp/node-bindings": "^0.0.28",
"@xmtp/proto": "^3.72.3"
},
"devDependencies": {
Expand Down
34 changes: 20 additions & 14 deletions sdks/node-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
generateInboxId,
getInboxIdForAddress,
GroupMessageKind,
isAddressAuthorized as isAddressAuthorizedBinding,
isInstallationAuthorized as isInstallationAuthorizedBinding,
LogLevel,
SignatureRequestType,
verifySignedWithPublicKey as verifySignedWithPublicKeyBinding,
Expand Down Expand Up @@ -172,20 +174,6 @@ export class Client {
return this.#innerClient.isRegistered();
}

async isAddressAuthorized(
inboxId: string,
address: string,
): Promise<boolean> {
return this.#innerClient.isAddressAuthorized(inboxId, address);
}

async isInstallationAuthorized(
inboxId: string,
installationId: Uint8Array,
): Promise<boolean> {
return this.#innerClient.isInstallationAuthorized(inboxId, installationId);
}

async #createInboxSignatureText() {
try {
const signatureText = await this.#innerClient.createInboxSignatureText();
Expand Down Expand Up @@ -452,4 +440,22 @@ export class Client {
return false;
}
}

static async isAddressAuthorized(
inboxId: string,
address: string,
options?: NetworkOptions,
): Promise<boolean> {
const host = options?.apiUrl || ApiUrls[options?.env || "dev"];
return await isAddressAuthorizedBinding(host, inboxId, address);
}

static async isInstallationAuthorized(
inboxId: string,
installation: Uint8Array,
options?: NetworkOptions,
): Promise<boolean> {
const host = options?.apiUrl || ApiUrls[options?.env || "dev"];
return await isInstallationAuthorizedBinding(host, inboxId, installation);
}
}
36 changes: 36 additions & 0 deletions sdks/node-sdk/test/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,40 @@ describe("Client", () => {
);
expect(verified4).toBe(false);
});

it("should check if an address is authorized", async () => {
const user = createUser();
const client = await createRegisteredClient(user);
const authorized = await Client.isAddressAuthorized(
client.inboxId,
user.account.address.toLowerCase(),
{ env: "local" },
);
expect(authorized).toBe(true);

const notAuthorized = await Client.isAddressAuthorized(
client.inboxId,
"0x1234567890123456789012345678901234567890",
{ env: "local" },
);
expect(notAuthorized).toBe(false);
});

it("should check if an installation is authorized", async () => {
const user = createUser();
const client = await createRegisteredClient(user);
const authorized = await Client.isInstallationAuthorized(
client.inboxId,
client.installationIdBytes,
{ env: "local" },
);
expect(authorized).toBe(true);

const notAuthorized = await Client.isInstallationAuthorized(
client.inboxId,
new Uint8Array(32),
{ env: "local" },
);
expect(notAuthorized).toBe(false);
});
});
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5086,10 +5086,10 @@ __metadata:
languageName: node
linkType: hard

"@xmtp/node-bindings@npm:^0.0.27":
version: 0.0.27
resolution: "@xmtp/node-bindings@npm:0.0.27"
checksum: 10/e5c737f41169caa58b55241e7f8e544076e8f7d7e3ba9703703885bda73d352c26984a8a4a4d1277a30d3837da9a0023a94dfadf40b5c8f8f3b6366260f3d20e
"@xmtp/node-bindings@npm:^0.0.28":
version: 0.0.28
resolution: "@xmtp/node-bindings@npm:0.0.28"
checksum: 10/0ceea72582926dcce03c8e3839b7101d7ce4aef56a55b46421c2e46e96f65c90a224b1649ff765c1f3cb1e2e21a730d1891bb369260c94712066b85d358fb6f4
languageName: node
linkType: hard

Expand Down Expand Up @@ -5117,7 +5117,7 @@ __metadata:
"@xmtp/content-type-group-updated": "npm:^1.0.1"
"@xmtp/content-type-primitives": "npm:^1.0.3"
"@xmtp/content-type-text": "npm:^1.0.1"
"@xmtp/node-bindings": "npm:^0.0.27"
"@xmtp/node-bindings": "npm:^0.0.28"
"@xmtp/proto": "npm:^3.72.3"
"@xmtp/xmtp-js": "workspace:^"
fast-glob: "npm:^3.3.2"
Expand Down

0 comments on commit 7338e0e

Please sign in to comment.