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 Browser SDK #713

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ yarn-error.log*

# local env files
.env*
!.env.example

# turbo
.turbo
Expand Down
2 changes: 2 additions & 0 deletions examples/react-vite-browser-sdk/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VITE_PROJECT_ID= # Your Reown (https://reown.com/) project ID
VITE_ENCRYPTION_KEY= # 32 byte hex string
7 changes: 6 additions & 1 deletion examples/react-vite-browser-sdk/src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ export const getSignature = async (client: Client, wallet: Wallet) => {
};

export const createClient = async (walletKey: string) => {
const encryptionKeyHex = import.meta.env.VITE_ENCRYPTION_KEY;
if (!encryptionKeyHex) {
throw new Error("VITE_ENCRYPTION_KEY must be set in the environment");
}
rygine marked this conversation as resolved.
Show resolved Hide resolved
const encryptionBytes = toBytes(encryptionKeyHex);
const wallet = createWallet(walletKey);
const client = await Client.create(wallet.account.address, {
const client = await Client.create(wallet.account.address, encryptionBytes, {
rygine marked this conversation as resolved.
Show resolved Hide resolved
env: "local",
});
const isRegistered = await client.isRegistered();
Expand Down
3 changes: 2 additions & 1 deletion examples/react-vite-browser-sdk/src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface ImportMeta {
env: {
VITE_PROJECT_ID: string;
VITE_PROJECT_ID?: string;
VITE_ENCRYPTION_KEY?: string;
rygine marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down
7 changes: 6 additions & 1 deletion examples/react-vite-browser-sdk/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import { WagmiProvider } from "wagmi";
import { App } from "./App";
import "./index.css";

const projectId = import.meta.env.VITE_PROJECT_ID;
if (!projectId) {
throw new Error("VITE_PROJECT_ID must be set in the environment");
}

export const config = getDefaultConfig({
appName: "XMTP V3 Browser SDK Example",
projectId: import.meta.env.VITE_PROJECT_ID,
projectId,
chains: [mainnet],
transports: {
[mainnet.id]: http(),
Expand Down
1 change: 1 addition & 0 deletions packages/frames-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"devDependencies": {
"@open-frames/types": "^0.1.1",
"@rollup/plugin-typescript": "^12.1.1",
"@types/bl": "^5.1.4",
"@xmtp/frames-client": "^0.5.4",
"@xmtp/xmtp-js": "^12.1.0",
"ethers": "^6.10.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/frames-validator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "tsconfig/base.json",
"includes": ["src", "rollup.config.js", "vitest.config.ts", "vitest.setup.ts"]
"include": ["src", "rollup.config.js", "vitest.config.ts"],
"exclude": ["dist", "node_modules"]
}
2 changes: 1 addition & 1 deletion sdks/browser-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@xmtp/content-type-primitives": "^1.0.1",
"@xmtp/content-type-text": "^1.0.0",
"@xmtp/proto": "^3.70.0",
"@xmtp/wasm-bindings": "^0.0.3",
"@xmtp/wasm-bindings": "^0.0.4",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions sdks/browser-sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const external = [
"@xmtp/content-type-text",
"@xmtp/wasm-bindings",
"@xmtp/content-type-primitives",
"@xmtp/content-type-group-updated",
"@xmtp/proto",
"uuid",
];
Expand Down
37 changes: 33 additions & 4 deletions sdks/browser-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ export class Client extends ClientWorkerClass {

#codecs: Map<string, ContentCodec>;

constructor(address: string, options?: ClientOptions) {
#encryptionKey: Uint8Array;

constructor(
address: string,
encryptionKey: Uint8Array,
options?: ClientOptions,
) {
rygine marked this conversation as resolved.
Show resolved Hide resolved
const worker = new Worker(new URL("./workers/client", import.meta.url), {
type: "module",
});
super(worker, options?.enableLogging ?? false);
super(
worker,
options?.loggingLevel !== undefined && options.loggingLevel !== "off",
);
this.address = address;
this.options = options;
this.#encryptionKey = encryptionKey;
this.#conversations = new Conversations(this);
const codecs = [
new GroupUpdatedCodec(),
Expand All @@ -58,15 +68,20 @@ export class Client extends ClientWorkerClass {
async init() {
const result = await this.sendMessage("init", {
address: this.address,
encryptionKey: this.#encryptionKey,
options: this.options,
});
rygine marked this conversation as resolved.
Show resolved Hide resolved
this.#inboxId = result.inboxId;
this.#installationId = result.installationId;
this.#isReady = true;
}

static async create(address: string, options?: ClientOptions) {
const client = new Client(address, options);
static async create(
address: string,
encryptionKey: Uint8Array,
options?: ClientOptions,
) {
const client = new Client(address, encryptionKey, options);
await client.init();
return client;
}
Expand Down Expand Up @@ -103,6 +118,20 @@ export class Client extends ClientWorkerClass {
return this.sendMessage("addSignature", { type, bytes });
}

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

async applySignatures() {
return this.sendMessage("applySignatures", undefined);
}
Expand Down
12 changes: 11 additions & 1 deletion sdks/browser-sdk/src/WorkerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export class WorkerClient {

static async create(
accountAddress: string,
encryptionKey: Uint8Array,
options?: Omit<ClientOptions, "codecs">,
) {
const client = await createClient(accountAddress, options);
const client = await createClient(accountAddress, encryptionKey, options);
return new WorkerClient(client);
}

Expand Down Expand Up @@ -84,6 +85,15 @@ export class WorkerClient {
return this.#client.addSignature(type, bytes);
}

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

async applySignatures() {
return this.#client.applySignatureRequests();
}
Expand Down
8 changes: 4 additions & 4 deletions sdks/browser-sdk/src/WorkerConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type {
ConsentState,
Conversation,
EncodedContent,
GroupMember,
} from "@xmtp/wasm-bindings";
import {
fromSafeListMessagesOptions,
toSafeGroupMember,
type SafeListMessagesOptions,
type WasmGroupMember,
} from "@/utils/conversions";
import type { WorkerClient } from "@/WorkerClient";

Expand Down Expand Up @@ -73,13 +73,13 @@ export class WorkerConversation {
get metadata() {
const metadata = this.#group.groupMetadata();
return {
creatorInboxId: metadata.creator_inbox_id(),
conversationType: metadata.conversation_type(),
creatorInboxId: metadata.creatorInboxId(),
conversationType: metadata.conversationType(),
};
}

async members() {
const members = (await this.#group.listMembers()) as WasmGroupMember[];
const members = (await this.#group.listMembers()) as GroupMember[];
return members.map((member) => toSafeGroupMember(member));
}

Expand Down
31 changes: 29 additions & 2 deletions sdks/browser-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
export { Client } from "./Client";
export { Conversations } from "./Conversations";
export { Conversation } from "./Conversation";
export * from "./DecodedMessage";
export type { MessageDeliveryStatus, MessageKind } from "./DecodedMessage";
export { DecodedMessage } from "./DecodedMessage";
export { Utils } from "./Utils";
export { ApiUrls } from "./constants";
export type * from "./types";
export * from "./utils/conversions";
export type * from "@xmtp/wasm-bindings";
export {
ConsentEntityType,
ConsentState,
ConversationType,
CreateGroupOptions,
DeliveryStatus,
GroupMembershipState,
EncodedContent,
GroupMember,
GroupMetadata,
GroupPermissions,
GroupMessageKind,
GroupPermissionsOptions,
InboxState,
Installation,
ListConversationsOptions,
ListMessagesOptions,
Message,
PermissionLevel,
PermissionPolicy,
PermissionPolicySet,
PermissionUpdateType,
SignatureRequestType,
SortDirection,
Consent,
ContentTypeId,
} from "@xmtp/wasm-bindings";
12 changes: 12 additions & 0 deletions sdks/browser-sdk/src/types/clientEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type ClientEvents =
};
data: {
address: string;
encryptionKey: Uint8Array;
rygine marked this conversation as resolved.
Show resolved Hide resolved
options?: ClientOptions;
};
}
Expand Down Expand Up @@ -78,6 +79,17 @@ export type ClientEvents =
bytes: Uint8Array;
};
}
| {
action: "addScwSignature";
id: string;
result: undefined;
data: {
type: SignatureRequestType;
bytes: Uint8Array;
chainId: bigint;
blockNumber?: bigint;
};
}
| {
action: "applySignatures";
id: string;
Expand Down
23 changes: 10 additions & 13 deletions sdks/browser-sdk/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ export type NetworkOptions = {
apiUrl?: string;
};

/**
* Encryption options
*/
export type EncryptionOptions = {
/**
* Encryption key to use for the local DB
*/
encryptionKey?: Uint8Array;
};

/**
* Storage options
*/
Expand All @@ -47,13 +37,20 @@ export type ContentOptions = {

export type OtherOptions = {
/**
* Enable logging of events between the client and worker
* Enable structured JSON logging
*/
structuredLogging?: boolean;
/**
* Enable performance metrics
*/
performanceLogging?: boolean;
/**
* Logging level
*/
enableLogging?: boolean;
loggingLevel?: "off" | "error" | "warn" | "info" | "debug" | "trace";
};

export type ClientOptions = NetworkOptions &
EncryptionOptions &
StorageOptions &
ContentOptions &
OtherOptions;
36 changes: 6 additions & 30 deletions sdks/browser-sdk/src/utils/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,36 +341,12 @@ export type SafeGroupMember = {
permissionLevel: PermissionLevel;
};

export class WasmGroupMember {
account_addresses: string[];
consent_state: ConsentState;
inbox_id: string;
installation_ids: string[];
permission_level: PermissionLevel;

constructor(
inbox_id: string,
account_addresses: string[],
installation_ids: string[],
permission_level: PermissionLevel,
consent_state: ConsentState,
) {
this.inbox_id = inbox_id;
this.account_addresses = account_addresses;
this.installation_ids = installation_ids;
this.permission_level = permission_level;
this.consent_state = consent_state;
}
}

export const toSafeGroupMember = (
member: WasmGroupMember,
): SafeGroupMember => ({
accountAddresses: member.account_addresses,
consentState: member.consent_state,
inboxId: member.inbox_id,
installationIds: member.installation_ids,
permissionLevel: member.permission_level,
export const toSafeGroupMember = (member: GroupMember): SafeGroupMember => ({
accountAddresses: member.accountAddresses,
consentState: member.consentState,
inboxId: member.inboxId,
installationIds: member.installationIds,
permissionLevel: member.permissionLevel,
});

export const fromSafeGroupMember = (member: SafeGroupMember): GroupMember =>
Expand Down
Loading
Loading