Skip to content

Commit

Permalink
Add inbox ID helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Oct 30, 2024
1 parent af88215 commit 215d639
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sdks/node-sdk/src/helpers/inboxId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
generateInboxId as generateInboxIdBinding,
getInboxIdForAddress as getInboxIdForAddressBinding,
} from "@xmtp/node-bindings";
import { ApiUrls, type XmtpEnv } from "@/Client";

export const generateInboxId = (accountAddress: string): string => {
return generateInboxIdBinding(accountAddress);
};

export const getInboxIdForAddress = async (
accountAddress: string,
env: XmtpEnv = "dev",
) => {
const host = ApiUrls[env];
const isSecure = host.startsWith("https");
return getInboxIdForAddressBinding(host, isSecure, accountAddress);
};
1 change: 1 addition & 0 deletions sdks/node-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { Conversations } from "./Conversations";
export { DecodedMessage } from "./DecodedMessage";
export type { StreamCallback } from "./AsyncStream";
export type * from "@xmtp/node-bindings";
export { generateInboxId, getInboxIdForAddress } from "./helpers/inboxId";
26 changes: 26 additions & 0 deletions sdks/node-sdk/test/inboxId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { generateInboxId, getInboxIdForAddress } from "@/helpers/inboxId";
import { createRegisteredClient, createUser } from "@test/helpers";

describe("generateInboxId", () => {
it("should generate an inbox id", () => {
const user = createUser();
const inboxId = generateInboxId(user.account.address);
expect(inboxId).toBeDefined();
});
});

describe("getInboxIdForAddress", () => {
it("should return `null` inbox ID for unregistered address", async () => {
const user = createUser();
const inboxId = await getInboxIdForAddress(user.account.address, "local");
expect(inboxId).toBe(null);
});

it("should return inbox ID for registered address", async () => {
const user = createUser();
const client = await createRegisteredClient(user);
const inboxId = await getInboxIdForAddress(user.account.address, "local");
expect(inboxId).toBe(client.inboxId);
});
});

0 comments on commit 215d639

Please sign in to comment.