Skip to content

Commit 554304e

Browse files
committed
Refactor SDK to work with new bindings
1 parent 529a933 commit 554304e

21 files changed

+605
-450
lines changed

sdks/browser-sdk/src/Client.ts

+44-40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
GroupMessageKind,
1212
SignatureRequestType,
1313
type ConsentEntityType,
14+
type Identifier,
1415
} from "@xmtp/wasm-bindings";
1516
import { ClientWorkerClass } from "@/ClientWorkerClass";
1617
import { Conversations } from "@/Conversations";
@@ -24,7 +25,6 @@ import {
2425
import { type Signer } from "@/utils/signer";
2526

2627
export class Client extends ClientWorkerClass {
27-
#accountAddress: string;
2828
#codecs: Map<string, ContentCodec>;
2929
#conversations: Conversations;
3030
#encryptionKey: Uint8Array;
@@ -37,7 +37,6 @@ export class Client extends ClientWorkerClass {
3737

3838
constructor(
3939
signer: Signer,
40-
accountAddress: string,
4140
encryptionKey: Uint8Array,
4241
options?: ClientOptions,
4342
) {
@@ -48,7 +47,6 @@ export class Client extends ClientWorkerClass {
4847
worker,
4948
options?.loggingLevel !== undefined && options.loggingLevel !== "off",
5049
);
51-
this.#accountAddress = accountAddress;
5250
this.options = options;
5351
this.#encryptionKey = encryptionKey;
5452
this.#signer = signer;
@@ -63,13 +61,9 @@ export class Client extends ClientWorkerClass {
6361
);
6462
}
6563

66-
get accountAddress() {
67-
return this.#accountAddress;
68-
}
69-
7064
async init() {
7165
const result = await this.sendMessage("init", {
72-
address: this.accountAddress,
66+
identifier: await this.#signer.getIdentifier(),
7367
encryptionKey: this.#encryptionKey,
7468
options: this.options,
7569
});
@@ -84,8 +78,7 @@ export class Client extends ClientWorkerClass {
8478
encryptionKey: Uint8Array,
8579
options?: ClientOptions,
8680
) {
87-
const address = await signer.getAddress();
88-
const client = new Client(signer, address, encryptionKey, options);
81+
const client = new Client(signer, encryptionKey, options);
8982

9083
await client.init();
9184

@@ -104,6 +97,10 @@ export class Client extends ClientWorkerClass {
10497
return this.#inboxId;
10598
}
10699

100+
async accountIdentifier() {
101+
return this.#signer.getIdentifier();
102+
}
103+
107104
get installationId() {
108105
return this.#installationId;
109106
}
@@ -134,7 +131,7 @@ export class Client extends ClientWorkerClass {
134131
* throw an error.
135132
*/
136133
async unsafe_addAccountSignatureText(
137-
newAccountAddress: string,
134+
newIdentifier: Identifier,
138135
allowInboxReassign: boolean = false,
139136
) {
140137
if (!allowInboxReassign) {
@@ -144,7 +141,7 @@ export class Client extends ClientWorkerClass {
144141
}
145142

146143
return this.sendMessage("addAccountSignatureText", {
147-
newAccountAddress,
144+
newIdentifier,
148145
});
149146
}
150147

@@ -155,8 +152,10 @@ export class Client extends ClientWorkerClass {
155152
*
156153
* It is highly recommended to use the `removeAccount` function instead.
157154
*/
158-
async unsafe_removeAccountSignatureText(accountAddress: string) {
159-
return this.sendMessage("removeAccountSignatureText", { accountAddress });
155+
async unsafe_removeAccountSignatureText(identifier: Identifier) {
156+
return this.sendMessage("removeAccountSignatureText", {
157+
identifier,
158+
});
160159
}
161160

162161
/**
@@ -203,18 +202,21 @@ export class Client extends ClientWorkerClass {
203202
) {
204203
const signature = await signer.signMessage(signatureText);
205204

206-
if (signer.walletType === "SCW") {
207-
await this.sendMessage("addScwSignature", {
208-
type: signatureType,
209-
bytes: signature,
210-
chainId: signer.getChainId(),
211-
blockNumber: signer.getBlockNumber?.(),
212-
});
213-
} else {
214-
await this.sendMessage("addSignature", {
215-
type: signatureType,
216-
bytes: signature,
217-
});
205+
switch (signer.type) {
206+
case "SCW":
207+
await this.sendMessage("addScwSignature", {
208+
type: signatureType,
209+
bytes: signature,
210+
chainId: signer.getChainId(),
211+
blockNumber: signer.getBlockNumber?.(),
212+
});
213+
break;
214+
case "EOA":
215+
await this.sendMessage("addEcdsaSignature", {
216+
type: signatureType,
217+
bytes: signature,
218+
});
219+
break;
218220
}
219221
}
220222

@@ -261,8 +263,8 @@ export class Client extends ClientWorkerClass {
261263
allowInboxReassign: boolean = false,
262264
) {
263265
// check for existing inbox id
264-
const existingInboxId = await this.findInboxIdByAddress(
265-
await newAccountSigner.getAddress(),
266+
const existingInboxId = await this.findInboxIdByIdentifier(
267+
await newAccountSigner.getIdentifier(),
266268
);
267269

268270
if (existingInboxId && !allowInboxReassign) {
@@ -272,7 +274,7 @@ export class Client extends ClientWorkerClass {
272274
}
273275

274276
const signatureText = await this.unsafe_addAccountSignatureText(
275-
await newAccountSigner.getAddress(),
277+
await newAccountSigner.getIdentifier(),
276278
true,
277279
);
278280

@@ -289,9 +291,9 @@ export class Client extends ClientWorkerClass {
289291
await this.unsafe_applySignatures();
290292
}
291293

292-
async removeAccount(accountAddress: string) {
294+
async removeAccount(accountIdentifier: Identifier) {
293295
const signatureText =
294-
await this.unsafe_removeAccountSignatureText(accountAddress);
296+
await this.unsafe_removeAccountSignatureText(accountIdentifier);
295297

296298
if (!signatureText) {
297299
throw new Error("Unable to generate remove account signature text");
@@ -346,15 +348,17 @@ export class Client extends ClientWorkerClass {
346348
return this.sendMessage("isRegistered", undefined);
347349
}
348350

349-
async canMessage(accountAddresses: string[]) {
350-
return this.sendMessage("canMessage", { accountAddresses });
351+
async canMessage(identifiers: Identifier[]) {
352+
return this.sendMessage("canMessage", { identifiers });
351353
}
352354

353-
static async canMessage(accountAddresses: string[], env?: XmtpEnv) {
354-
const accountAddress = "0x0000000000000000000000000000000000000000";
355+
static async canMessage(identifiers: Identifier[], env?: XmtpEnv) {
355356
const signer: Signer = {
356-
walletType: "EOA",
357-
getAddress: () => accountAddress,
357+
type: "EOA",
358+
getIdentifier: () => ({
359+
identifier: "0x0000000000000000000000000000000000000000",
360+
identifierKind: "Ethereum",
361+
}),
358362
signMessage: () => new Uint8Array(),
359363
};
360364
const client = await Client.create(
@@ -365,11 +369,11 @@ export class Client extends ClientWorkerClass {
365369
env,
366370
},
367371
);
368-
return client.canMessage(accountAddresses);
372+
return client.canMessage(identifiers);
369373
}
370374

371-
async findInboxIdByAddress(address: string) {
372-
return this.sendMessage("findInboxIdByAddress", { address });
375+
async findInboxIdByIdentifier(identifier: Identifier) {
376+
return this.sendMessage("findInboxIdByIdentifier", { identifier });
373377
}
374378

375379
async inboxState(refreshFromNetwork?: boolean) {

sdks/browser-sdk/src/Conversation.ts

+6
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,10 @@ export class Conversation {
199199
};
200200
return asyncStream;
201201
}
202+
203+
async pausedForVersion() {
204+
return this.#client.sendMessage("getGroupPausedForVersion", {
205+
id: this.#id,
206+
});
207+
}
202208
}

sdks/browser-sdk/src/Conversations.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ConversationType,
33
type ConsentState,
4+
type Identifier,
45
type UserPreference,
56
} from "@xmtp/wasm-bindings";
67
import { v4 } from "uuid";
@@ -97,38 +98,47 @@ export class Conversations {
9798
);
9899
}
99100

100-
async newGroup(accountAddresses: string[], options?: SafeCreateGroupOptions) {
101-
const conversation = await this.#client.sendMessage("newGroup", {
102-
accountAddresses,
103-
options,
104-
});
101+
async newGroupWithIdentifiers(
102+
identifiers: Identifier[],
103+
options?: SafeCreateGroupOptions,
104+
) {
105+
const conversation = await this.#client.sendMessage(
106+
"newGroupWithIdentifiers",
107+
{
108+
identifiers,
109+
options,
110+
},
111+
);
105112

106113
return new Group(this.#client, conversation.id, conversation);
107114
}
108115

109-
async newGroupByInboxIds(
110-
inboxIds: string[],
111-
options?: SafeCreateGroupOptions,
112-
) {
113-
const conversation = await this.#client.sendMessage("newGroupByInboxIds", {
114-
inboxIds,
115-
options,
116-
});
116+
async newGroup(inboxIds: string[], options?: SafeCreateGroupOptions) {
117+
const conversation = await this.#client.sendMessage(
118+
"newGroupWithInboxIds",
119+
{
120+
inboxIds,
121+
options,
122+
},
123+
);
117124

118125
return new Group(this.#client, conversation.id, conversation);
119126
}
120127

121-
async newDm(accountAddress: string, options?: SafeCreateDmOptions) {
122-
const conversation = await this.#client.sendMessage("newDm", {
123-
accountAddress,
128+
async newDmWithIdentifier(
129+
identifier: Identifier,
130+
options?: SafeCreateDmOptions,
131+
) {
132+
const conversation = await this.#client.sendMessage("newDmWithIdentifier", {
133+
identifier,
124134
options,
125135
});
126136

127137
return new Dm(this.#client, conversation.id, conversation);
128138
}
129139

130-
async newDmByInboxId(inboxId: string, options?: SafeCreateDmOptions) {
131-
const conversation = await this.#client.sendMessage("newDmByInboxId", {
140+
async newDm(inboxId: string, options?: SafeCreateDmOptions) {
141+
const conversation = await this.#client.sendMessage("newDmWithInboxId", {
132142
inboxId,
133143
options,
134144
});

sdks/browser-sdk/src/Group.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
Identifier,
23
MetadataField,
34
PermissionPolicy,
45
PermissionUpdateType,
@@ -132,28 +133,28 @@ export class Group extends Conversation {
132133
return superAdmins.includes(inboxId);
133134
}
134135

135-
async addMembers(accountAddresses: string[]) {
136+
async addMembersByIdentifiers(identifiers: Identifier[]) {
136137
return this.#client.sendMessage("addGroupMembers", {
137138
id: this.#id,
138-
accountAddresses,
139+
identifiers,
139140
});
140141
}
141142

142-
async addMembersByInboxId(inboxIds: string[]) {
143+
async addMembers(inboxIds: string[]) {
143144
return this.#client.sendMessage("addGroupMembersByInboxId", {
144145
id: this.#id,
145146
inboxIds,
146147
});
147148
}
148149

149-
async removeMembers(accountAddresses: string[]) {
150+
async removeMembersByIdentifiers(identifiers: Identifier[]) {
150151
return this.#client.sendMessage("removeGroupMembers", {
151152
id: this.#id,
152-
accountAddresses,
153+
identifiers,
153154
});
154155
}
155156

156-
async removeMembersByInboxId(inboxIds: string[]) {
157+
async removeMembers(inboxIds: string[]) {
157158
return this.#client.sendMessage("removeGroupMembersByInboxId", {
158159
id: this.#id,
159160
inboxIds,

sdks/browser-sdk/src/Utils.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Identifier } from "@xmtp/wasm-bindings";
12
import type { XmtpEnv } from "@/types/options";
23
import { UtilsWorkerClass } from "@/UtilsWorkerClass";
34

@@ -11,16 +12,16 @@ export class Utils extends UtilsWorkerClass {
1112
this.#enableLogging = enableLogging ?? false;
1213
}
1314

14-
async generateInboxId(address: string) {
15+
async generateInboxId(identifier: Identifier) {
1516
return this.sendMessage("generateInboxId", {
16-
address,
17+
identifier,
1718
enableLogging: this.#enableLogging,
1819
});
1920
}
2021

21-
async getInboxIdForAddress(address: string, env?: XmtpEnv) {
22-
return this.sendMessage("getInboxIdForAddress", {
23-
address,
22+
async getInboxIdForIdentifier(identifier: Identifier, env?: XmtpEnv) {
23+
return this.sendMessage("getInboxIdForIdentifier", {
24+
identifier,
2425
env,
2526
enableLogging: this.#enableLogging,
2627
});

0 commit comments

Comments
 (0)