Skip to content

Commit

Permalink
Merge pull request #711 from xmtp/rygine/bsdk-update
Browse files Browse the repository at this point in the history
Update Browser SDK with latest WASM bindings
  • Loading branch information
rygine authored Nov 7, 2024
2 parents f9ac486 + 5479e31 commit c1bfeae
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 241 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-tables-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xmtp/browser-sdk": patch
---

Update Browser SDK with latest WASM bindings
7 changes: 2 additions & 5 deletions examples/react-vite-browser-sdk/src/createClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, WasmSignatureRequestType } from "@xmtp/browser-sdk";
import { Client, SignatureRequestType } from "@xmtp/browser-sdk";
import { toBytes } from "viem/utils";
import { createWallet } from "./wallets";

Expand All @@ -24,10 +24,7 @@ export const createClient = async (walletKey: string) => {
if (!isRegistered) {
const signature = await getSignature(client, wallet);
if (signature) {
await client.addSignature(
WasmSignatureRequestType.CreateInbox,
signature,
);
await client.addSignature(SignatureRequestType.CreateInbox, signature);
}
await client.registerIdentity();
}
Expand Down
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.2",
"@xmtp/wasm-bindings": "^0.0.3",
"uuid": "^10.0.0"
},
"devDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions sdks/browser-sdk/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
} from "@xmtp/content-type-primitives";
import { TextCodec } from "@xmtp/content-type-text";
import {
WasmGroupMessageKind,
type WasmConsentEntityType,
type WasmSignatureRequestType,
GroupMessageKind,
type ConsentEntityType,
type SignatureRequestType,
} from "@xmtp/wasm-bindings";
import { ClientWorkerClass } from "@/ClientWorkerClass";
import { Conversations } from "@/Conversations";
Expand Down Expand Up @@ -99,7 +99,7 @@ export class Client extends ClientWorkerClass {
return this.sendMessage("getRevokeInstallationsSignatureText", undefined);
}

async addSignature(type: WasmSignatureRequestType, bytes: Uint8Array) {
async addSignature(type: SignatureRequestType, bytes: Uint8Array) {
return this.sendMessage("addSignature", { type, bytes });
}

Expand Down Expand Up @@ -137,7 +137,7 @@ export class Client extends ClientWorkerClass {
return this.sendMessage("setConsentStates", { records });
}

async getConsentState(entityType: WasmConsentEntityType, entity: string) {
async getConsentState(entityType: ConsentEntityType, entity: string) {
return this.sendMessage("getConsentState", { entityType, entity });
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export class Client extends ClientWorkerClass {
// throw an error if there's an invalid group membership change message
if (
contentType.sameAs(ContentTypeGroupUpdated) &&
message.kind !== WasmGroupMessageKind.MembershipChange
message.kind !== GroupMessageKind.MembershipChange
) {
throw new Error("Error decoding group membership change");
}
Expand Down
4 changes: 2 additions & 2 deletions sdks/browser-sdk/src/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ContentTypeId } from "@xmtp/content-type-primitives";
import { ContentTypeText } from "@xmtp/content-type-text";
import type { WasmConsentState } from "@xmtp/wasm-bindings";
import type { ConsentState } from "@xmtp/wasm-bindings";
import type { Client } from "@/Client";
import { DecodedMessage } from "@/DecodedMessage";
import type {
Expand Down Expand Up @@ -276,7 +276,7 @@ export class Conversation {
});
}

async updateConsentState(state: WasmConsentState) {
async updateConsentState(state: ConsentState) {
return this.#client.sendMessage("updateGroupConsentState", {
id: this.#id,
state,
Expand Down
12 changes: 6 additions & 6 deletions sdks/browser-sdk/src/DecodedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ContentTypeId } from "@xmtp/content-type-primitives";
import { WasmDeliveryStatus, WasmGroupMessageKind } from "@xmtp/wasm-bindings";
import { DeliveryStatus, GroupMessageKind } from "@xmtp/wasm-bindings";
import type { Client } from "@/Client";
import { fromSafeContentTypeId, type SafeMessage } from "@/utils/conversions";

Expand Down Expand Up @@ -39,23 +39,23 @@ export class DecodedMessage {
this.senderInboxId = message.senderInboxId;

switch (message.kind) {
case WasmGroupMessageKind.Application:
case GroupMessageKind.Application:
this.kind = "application";
break;
case WasmGroupMessageKind.MembershipChange:
case GroupMessageKind.MembershipChange:
this.kind = "membership_change";
break;
// no default
}

switch (message.deliveryStatus) {
case WasmDeliveryStatus.Unpublished:
case DeliveryStatus.Unpublished:
this.deliveryStatus = "unpublished";
break;
case WasmDeliveryStatus.Published:
case DeliveryStatus.Published:
this.deliveryStatus = "published";
break;
case WasmDeliveryStatus.Failed:
case DeliveryStatus.Failed:
this.deliveryStatus = "failed";
break;
// no default
Expand Down
14 changes: 7 additions & 7 deletions sdks/browser-sdk/src/WorkerClient.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {
type WasmClient,
type WasmConsentEntityType,
type WasmSignatureRequestType,
type Client,
type ConsentEntityType,
type SignatureRequestType,
} from "@xmtp/wasm-bindings";
import type { ClientOptions } from "@/types";
import { fromSafeConsent, type SafeConsent } from "@/utils/conversions";
import { createClient } from "@/utils/createClient";
import { WorkerConversations } from "@/WorkerConversations";

export class WorkerClient {
#client: WasmClient;
#client: Client;

#conversations: WorkerConversations;

#accountAddress: string;

constructor(client: WasmClient) {
constructor(client: Client) {
this.#client = client;
this.#accountAddress = client.accountAddress;
this.#conversations = new WorkerConversations(this, client.conversations());
Expand Down Expand Up @@ -80,7 +80,7 @@ export class WorkerClient {
}
}

async addSignature(type: WasmSignatureRequestType, bytes: Uint8Array) {
async addSignature(type: SignatureRequestType, bytes: Uint8Array) {
return this.#client.addSignature(type, bytes);
}

Expand Down Expand Up @@ -114,7 +114,7 @@ export class WorkerClient {
return this.#client.setConsentStates(records.map(fromSafeConsent));
}

async getConsentState(entityType: WasmConsentEntityType, entity: string) {
async getConsentState(entityType: ConsentEntityType, entity: string) {
return this.#client.getConsentState(entityType, entity);
}

Expand Down
88 changes: 45 additions & 43 deletions sdks/browser-sdk/src/WorkerConversation.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type {
WasmConsentState,
WasmEncodedContent,
WasmGroup,
WasmGroupMember,
ConsentState,
Conversation,
EncodedContent,
} from "@xmtp/wasm-bindings";
import {
fromSafeListMessagesOptions,
toSafeGroupMember,
type SafeListMessagesOptions,
type WasmGroupMember,
} from "@/utils/conversions";
import type { WorkerClient } from "@/WorkerClient";

export class WorkerConversation {
// eslint-disable-next-line no-unused-private-class-members
#client: WorkerClient;

#group: WasmGroup;
#group: Conversation;

constructor(client: WorkerClient, group: WasmGroup) {
constructor(client: WorkerClient, group: Conversation) {
this.#client = client;
this.#group = group;
}
Expand All @@ -26,148 +27,149 @@ export class WorkerConversation {
}

get name() {
return this.#group.group_name();
return this.#group.groupName();
}

async updateName(name: string) {
return this.#group.update_group_name(name);
return this.#group.updateGroupName(name);
}

get imageUrl() {
return this.#group.group_image_url_square();
return this.#group.groupImageUrlSquare();
}

async updateImageUrl(imageUrl: string) {
return this.#group.update_group_image_url_square(imageUrl);
return this.#group.updateGroupImageUrlSquare(imageUrl);
}

get description() {
return this.#group.group_description();
return this.#group.groupDescription();
}

async updateDescription(description: string) {
return this.#group.update_group_description(description);
return this.#group.updateGroupDescription(description);
}

get pinnedFrameUrl() {
return this.#group.group_pinned_frame_url();
return this.#group.groupPinnedFrameUrl();
}

async updatePinnedFrameUrl(pinnedFrameUrl: string) {
return this.#group.update_group_pinned_frame_url(pinnedFrameUrl);
return this.#group.updateGroupPinnedFrameUrl(pinnedFrameUrl);
}

get isActive() {
return this.#group.is_active();
return this.#group.isActive();
}

get addedByInboxId() {
return this.#group.added_by_inbox_id();
return this.#group.addedByInboxId();
}

get createdAtNs() {
return this.#group.created_at_ns();
return this.#group.createdAtNs();
}

get metadata() {
const metadata = this.#group.group_metadata();
const metadata = this.#group.groupMetadata();
return {
creatorInboxId: metadata.creator_inbox_id(),
conversationType: metadata.conversation_type(),
};
}

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

get admins() {
return this.#group.admin_list();
return this.#group.adminList();
}

get superAdmins() {
return this.#group.super_admin_list();
return this.#group.superAdminList();
}

get permissions() {
const permissions = this.#group.group_permissions();
const permissions = this.#group.groupPermissions();
return {
policyType: permissions.policy_type(),
policySet: permissions.policy_set(),
policyType: permissions.policyType(),
policySet: permissions.policySet(),
};
}

isAdmin(inboxId: string) {
return this.#group.is_admin(inboxId);
return this.#group.isAdmin(inboxId);
}

isSuperAdmin(inboxId: string) {
return this.#group.is_super_admin(inboxId);
return this.#group.isSuperAdmin(inboxId);
}

async sync() {
return this.#group.sync();
}

async addMembers(accountAddresses: string[]) {
return this.#group.add_members(accountAddresses);
return this.#group.addMembers(accountAddresses);
}

async addMembersByInboxId(inboxIds: string[]) {
return this.#group.add_members_by_inbox_id(inboxIds);
return this.#group.addMembersByInboxId(inboxIds);
}

async removeMembers(accountAddresses: string[]) {
return this.#group.remove_members(accountAddresses);
return this.#group.removeMembers(accountAddresses);
}

async removeMembersByInboxId(inboxIds: string[]) {
return this.#group.remove_members_by_inbox_id(inboxIds);
return this.#group.removeMembersByInboxId(inboxIds);
}

async addAdmin(inboxId: string) {
return this.#group.add_admin(inboxId);
return this.#group.addAdmin(inboxId);
}

async removeAdmin(inboxId: string) {
return this.#group.remove_admin(inboxId);
return this.#group.removeAdmin(inboxId);
}

async addSuperAdmin(inboxId: string) {
return this.#group.add_super_admin(inboxId);
return this.#group.addSuperAdmin(inboxId);
}

async removeSuperAdmin(inboxId: string) {
return this.#group.remove_super_admin(inboxId);
return this.#group.removeSuperAdmin(inboxId);
}

async publishMessages() {
return this.#group.publish_messages();
return this.#group.publishMessages();
}

sendOptimistic(encodedContent: WasmEncodedContent) {
return this.#group.send_optimistic(encodedContent);
sendOptimistic(encodedContent: EncodedContent) {
return this.#group.sendOptimistic(encodedContent);
}

async send(encodedContent: WasmEncodedContent) {
async send(encodedContent: EncodedContent) {
return this.#group.send(encodedContent);
}

messages(options?: SafeListMessagesOptions) {
return this.#group.find_messages(
return this.#group.findMessages(
options ? fromSafeListMessagesOptions(options) : undefined,
);
}

get consentState() {
return this.#group.consent_state();
return this.#group.consentState();
}

updateConsentState(state: WasmConsentState) {
this.#group.update_consent_state(state);
updateConsentState(state: ConsentState) {
this.#group.updateConsentState(state);
}

dmPeerInboxId() {
return this.#group.dm_peer_inbox_id();
return this.#group.dmPeerInboxId();
}
}
Loading

0 comments on commit c1bfeae

Please sign in to comment.