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

Add static authorization methods to Node SDK Client #740

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 .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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The random name generator really picked a good one this time. 😬 😆

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);
}
rygine marked this conversation as resolved.
Show resolved Hide resolved

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);
}
rygine marked this conversation as resolved.
Show resolved Hide resolved
}
22 changes: 22 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,26 @@ 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);
});
rygine marked this conversation as resolved.
Show resolved Hide resolved

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);
});
rygine marked this conversation as resolved.
Show resolved Hide resolved
});
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
Loading