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

Update Node SDK #715

Merged
merged 7 commits into from
Nov 13, 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 .changeset/ninety-moons-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/node-sdk": patch
---

Update Node SDK
2 changes: 1 addition & 1 deletion sdks/node-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@xmtp/content-type-group-updated": "^1.0.0",
"@xmtp/content-type-primitives": "^1.0.2",
"@xmtp/content-type-text": "^1.0.0",
"@xmtp/node-bindings": "^0.0.17",
"@xmtp/node-bindings": "^0.0.18",
rygine marked this conversation as resolved.
Show resolved Hide resolved
"@xmtp/proto": "^3.62.1"
},
"devDependencies": {
Expand Down
41 changes: 31 additions & 10 deletions sdks/node-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import {
generateInboxId,
getInboxIdForAddress,
GroupMessageKind,
Level,
type Consent,
type ConsentEntityType,
type LogOptions,
type Message,
type Client as NodeClient,
type SignatureRequestType,
Expand Down Expand Up @@ -50,10 +52,6 @@ export type NetworkOptions = {
* Storage options
*/
export type StorageOptions = {
/**
* Encryption key to use for the local DB
*/
encryptionKey?: Uint8Array | null;
/**
* Path to the local DB
*/
Expand All @@ -73,9 +71,13 @@ export type OtherOptions = {
*/
requestHistorySync?: string;
/**
* Optionally set the logging level (default: 'off')
* Enable structured JSON logging
*/
logging?: "debug" | "info" | "warn" | "error" | "off";
structuredLogging?: boolean;
/**
* Logging level
*/
loggingLevel?: Level;
};

export type ClientOptions = NetworkOptions &
Expand All @@ -96,7 +98,11 @@ export class Client {
);
}

static async create(accountAddress: string, options?: ClientOptions) {
static async create(
accountAddress: string,
encryptionKey: Uint8Array,
options?: ClientOptions,
) {
const host = options?.apiUrl ?? ApiUrls[options?.env ?? "dev"];
const isSecure = host.startsWith("https");
const dbPath =
Expand All @@ -106,16 +112,21 @@ export class Client {
(await getInboxIdForAddress(host, isSecure, accountAddress)) ||
generateInboxId(accountAddress);

const logOptions: LogOptions = {
structured: options?.structuredLogging ?? false,
level: options?.loggingLevel ?? Level.off,
};

return new Client(
await createClient(
host,
isSecure,
dbPath,
inboxId,
accountAddress,
options?.encryptionKey,
encryptionKey,
options?.requestHistorySync,
options?.logging ?? "off",
logOptions,
),
[new GroupUpdatedCodec(), new TextCodec(), ...(options?.codecs ?? [])],
);
Expand Down Expand Up @@ -182,7 +193,8 @@ export class Client {
}

async canMessage(accountAddresses: string[]) {
return this.#innerClient.canMessage(accountAddresses);
const canMessage = await this.#innerClient.canMessage(accountAddresses);
return new Map(Object.entries(canMessage));
}

addSignature(
Expand All @@ -192,6 +204,15 @@ export class Client {
void this.#innerClient.addSignature(signatureType, signatureBytes);
}

async addScwSignature(
type: SignatureRequestType,
bytes: Uint8Array,
chainId: bigint,
blockNumber?: bigint,
) {
return this.#innerClient.addScwSignature(type, bytes, chainId, blockNumber);
}

async applySignatures() {
return this.#innerClient.applySignatureRequests();
}
Expand Down
32 changes: 31 additions & 1 deletion sdks/node-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,35 @@ export { Conversation } from "./Conversation";
export { Conversations } from "./Conversations";
export { DecodedMessage } from "./DecodedMessage";
export type { StreamCallback } from "./AsyncStream";
export type * from "@xmtp/node-bindings";
export type {
Consent,
ContentTypeId,
CreateGroupOptions,
EncodedContent,
InboxState,
Installation,
ListConversationsOptions,
ListMessagesOptions,
LogOptions,
Message,
PermissionPolicySet,
} from "@xmtp/node-bindings";
export {
ConsentEntityType,
ConsentState,
ConversationType,
DeliveryStatus,
GroupMember,
GroupMembershipState,
GroupMessageKind,
GroupMetadata,
GroupPermissions,
GroupPermissionsOptions,
Level,
PermissionLevel,
PermissionPolicy,
PermissionUpdateType,
SignatureRequestType,
SortDirection,
} from "@xmtp/node-bindings";
export { generateInboxId, getInboxIdForAddress } from "./helpers/inboxId";
7 changes: 3 additions & 4 deletions sdks/node-sdk/test/Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ describe("Client", () => {
const client2 = await createRegisteredClient(user);
expect(client2.isRegistered).toBe(true);
expect(await client2.createInboxSignatureText()).toBe(null);
expect(await client2.canMessage([user.account.address])).toEqual({
[user.account.address.toLowerCase()]: true,
});
});

it("should be able to message registered identity", async () => {
const user = createUser();
const client = await createRegisteredClient(user);
const canMessage = await client.canMessage([user.account.address]);
expect(canMessage).toEqual({ [user.account.address.toLowerCase()]: true });
expect(Object.fromEntries(canMessage)).toEqual({
[user.account.address.toLowerCase()]: true,
});
});

it("should get an inbox ID from an address", async () => {
Expand Down
4 changes: 3 additions & 1 deletion sdks/node-sdk/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getRandomValues } from "node:crypto";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import {
Expand All @@ -13,6 +14,7 @@ import { sepolia } from "viem/chains";
import { Client, type ClientOptions } from "@/Client";

const __dirname = dirname(fileURLToPath(import.meta.url));
const testEncryptionKey = getRandomValues(new Uint8Array(32));

export const createUser = () => {
const key = generatePrivateKey();
Expand Down Expand Up @@ -47,7 +49,7 @@ export const createClient = async (user: User, options?: ClientOptions) => {
...options,
env: options?.env ?? "local",
};
return Client.create(user.account.address, {
return Client.create(user.account.address, testEncryptionKey, {
...opts,
dbPath: join(__dirname, `./test-${user.uuid}.db3`),
rygine marked this conversation as resolved.
Show resolved Hide resolved
});
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4842,10 +4842,10 @@ __metadata:
languageName: unknown
linkType: soft

"@xmtp/node-bindings@npm:^0.0.17":
version: 0.0.17
resolution: "@xmtp/node-bindings@npm:0.0.17"
checksum: 10/2f461623966ba04dfcf7710f5229f762a9bffceedbc01474d58eb7bbb1f2f138e0e2891eadef416fb03f9fcfa3d32edadf050e007656b64f1cd9da1989c5ee9d
"@xmtp/node-bindings@npm:^0.0.18":
version: 0.0.18
resolution: "@xmtp/node-bindings@npm:0.0.18"
checksum: 10/5cca0748f163728c013ff17cf7fba81a64a7a6029a9782c6b568fd1044940c8ae641110f0a187ee2ac7787f303552712ebe1d5c515c5dad4dc0baa31506d8b83
languageName: node
linkType: hard

Expand All @@ -4860,7 +4860,7 @@ __metadata:
"@xmtp/content-type-group-updated": "npm:^1.0.0"
"@xmtp/content-type-primitives": "npm:^1.0.2"
"@xmtp/content-type-text": "npm:^1.0.0"
"@xmtp/node-bindings": "npm:^0.0.17"
"@xmtp/node-bindings": "npm:^0.0.18"
"@xmtp/proto": "npm:^3.62.1"
"@xmtp/xmtp-js": "workspace:^"
fast-glob: "npm:^3.3.2"
Expand Down
Loading