@xmtp/browser-sdk 0.0.21 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -54
- package/dist/index.d.ts +147 -40
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/workers/client.js +1 -1
- package/dist/workers/client.js.map +1 -1
- package/package.json +2 -2
- package/src/Client.ts +111 -22
- package/src/Conversation.ts +10 -172
- package/src/Conversations.ts +101 -29
- package/src/Dm.ts +21 -0
- package/src/Group.ts +190 -0
- package/src/WorkerConversations.ts +25 -0
- package/src/index.ts +13 -5
- package/src/types/clientEvents.ts +16 -0
- package/src/types/clientStreamEvents.ts +16 -1
- package/src/utils/conversions.ts +4 -11
- package/src/workers/client.ts +290 -710
package/src/Client.ts
CHANGED
|
@@ -112,34 +112,91 @@ export class Client extends ClientWorkerClass {
|
|
|
112
112
|
return this.#installationIdBytes;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
/**
|
|
116
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
117
|
+
* for use in special cases where the provided workflows do not meet the
|
|
118
|
+
* requirements of an application.
|
|
119
|
+
*
|
|
120
|
+
* It is highly recommended to use the `register` function instead.
|
|
121
|
+
*/
|
|
122
|
+
async unsafe_createInboxSignatureText() {
|
|
116
123
|
return this.sendMessage("createInboxSignatureText", undefined);
|
|
117
124
|
}
|
|
118
125
|
|
|
119
|
-
|
|
126
|
+
/**
|
|
127
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
128
|
+
* for use in special cases where the provided workflows do not meet the
|
|
129
|
+
* requirements of an application.
|
|
130
|
+
*
|
|
131
|
+
* It is highly recommended to use the `unsafe_addAccount` function instead.
|
|
132
|
+
*
|
|
133
|
+
* The `allowInboxReassign` parameter must be true or this function will
|
|
134
|
+
* throw an error.
|
|
135
|
+
*/
|
|
136
|
+
async unsafe_addAccountSignatureText(
|
|
137
|
+
newAccountAddress: string,
|
|
138
|
+
allowInboxReassign: boolean = false,
|
|
139
|
+
) {
|
|
140
|
+
if (!allowInboxReassign) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
"Unable to create add account signature text, `allowInboxReassign` must be true",
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
120
146
|
return this.sendMessage("addAccountSignatureText", {
|
|
121
147
|
newAccountAddress,
|
|
122
148
|
});
|
|
123
149
|
}
|
|
124
150
|
|
|
125
|
-
|
|
151
|
+
/**
|
|
152
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
153
|
+
* for use in special cases where the provided workflows do not meet the
|
|
154
|
+
* requirements of an application.
|
|
155
|
+
*
|
|
156
|
+
* It is highly recommended to use the `removeAccount` function instead.
|
|
157
|
+
*/
|
|
158
|
+
async unsafe_removeAccountSignatureText(accountAddress: string) {
|
|
126
159
|
return this.sendMessage("removeAccountSignatureText", { accountAddress });
|
|
127
160
|
}
|
|
128
161
|
|
|
129
|
-
|
|
162
|
+
/**
|
|
163
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
164
|
+
* for use in special cases where the provided workflows do not meet the
|
|
165
|
+
* requirements of an application.
|
|
166
|
+
*
|
|
167
|
+
* It is highly recommended to use the `revokeAllOtherInstallations` function
|
|
168
|
+
* instead.
|
|
169
|
+
*/
|
|
170
|
+
async unsafe_revokeAllOtherInstallationsSignatureText() {
|
|
130
171
|
return this.sendMessage(
|
|
131
172
|
"revokeAllOtherInstallationsSignatureText",
|
|
132
173
|
undefined,
|
|
133
174
|
);
|
|
134
175
|
}
|
|
135
176
|
|
|
136
|
-
|
|
177
|
+
/**
|
|
178
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
179
|
+
* for use in special cases where the provided workflows do not meet the
|
|
180
|
+
* requirements of an application.
|
|
181
|
+
*
|
|
182
|
+
* It is highly recommended to use the `revokeInstallations` function instead.
|
|
183
|
+
*/
|
|
184
|
+
async unsafe_revokeInstallationsSignatureText(installationIds: Uint8Array[]) {
|
|
137
185
|
return this.sendMessage("revokeInstallationsSignatureText", {
|
|
138
186
|
installationIds,
|
|
139
187
|
});
|
|
140
188
|
}
|
|
141
189
|
|
|
142
|
-
|
|
190
|
+
/**
|
|
191
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
192
|
+
* for use in special cases where the provided workflows do not meet the
|
|
193
|
+
* requirements of an application.
|
|
194
|
+
*
|
|
195
|
+
* It is highly recommended to use the `register`, `addAccount`,
|
|
196
|
+
* `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
|
|
197
|
+
* functions instead.
|
|
198
|
+
*/
|
|
199
|
+
async unsafe_addSignature(
|
|
143
200
|
signatureType: SignatureRequestType,
|
|
144
201
|
signatureText: string,
|
|
145
202
|
signer: Signer,
|
|
@@ -161,19 +218,28 @@ export class Client extends ClientWorkerClass {
|
|
|
161
218
|
}
|
|
162
219
|
}
|
|
163
220
|
|
|
164
|
-
|
|
221
|
+
/**
|
|
222
|
+
* WARNING: This function should be used with caution. It is only provided
|
|
223
|
+
* for use in special cases where the provided workflows do not meet the
|
|
224
|
+
* requirements of an application.
|
|
225
|
+
*
|
|
226
|
+
* It is highly recommended to use the `register`, `addAccount`,
|
|
227
|
+
* `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
|
|
228
|
+
* functions instead.
|
|
229
|
+
*/
|
|
230
|
+
async unsafe_applySignatures() {
|
|
165
231
|
return this.sendMessage("applySignatures", undefined);
|
|
166
232
|
}
|
|
167
233
|
|
|
168
234
|
async register() {
|
|
169
|
-
const signatureText = await this
|
|
235
|
+
const signatureText = await this.unsafe_createInboxSignatureText();
|
|
170
236
|
|
|
171
237
|
// if the signature text is not available, the client is already registered
|
|
172
238
|
if (!signatureText) {
|
|
173
239
|
return;
|
|
174
240
|
}
|
|
175
241
|
|
|
176
|
-
await this
|
|
242
|
+
await this.unsafe_addSignature(
|
|
177
243
|
SignatureRequestType.CreateInbox,
|
|
178
244
|
signatureText,
|
|
179
245
|
this.#signer,
|
|
@@ -182,44 +248,67 @@ export class Client extends ClientWorkerClass {
|
|
|
182
248
|
return this.sendMessage("registerIdentity", undefined);
|
|
183
249
|
}
|
|
184
250
|
|
|
185
|
-
|
|
186
|
-
|
|
251
|
+
/**
|
|
252
|
+
* WARNING: This function should be used with caution. Adding a wallet already
|
|
253
|
+
* associated with an inboxId will cause the wallet to lose access to
|
|
254
|
+
* that inbox.
|
|
255
|
+
*
|
|
256
|
+
* The `allowInboxReassign` parameter must be true to reassign an inbox
|
|
257
|
+
* already associated with a different account.
|
|
258
|
+
*/
|
|
259
|
+
async unsafe_addAccount(
|
|
260
|
+
newAccountSigner: Signer,
|
|
261
|
+
allowInboxReassign: boolean = false,
|
|
262
|
+
) {
|
|
263
|
+
// check for existing inbox id
|
|
264
|
+
const existingInboxId = await this.findInboxIdByAddress(
|
|
265
|
+
await newAccountSigner.getAddress(),
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
if (existingInboxId && !allowInboxReassign) {
|
|
269
|
+
throw new Error(
|
|
270
|
+
`Signer address already associated with inbox ${existingInboxId}`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const signatureText = await this.unsafe_addAccountSignatureText(
|
|
187
275
|
await newAccountSigner.getAddress(),
|
|
276
|
+
true,
|
|
188
277
|
);
|
|
189
278
|
|
|
190
279
|
if (!signatureText) {
|
|
191
280
|
throw new Error("Unable to generate add account signature text");
|
|
192
281
|
}
|
|
193
282
|
|
|
194
|
-
await this
|
|
283
|
+
await this.unsafe_addSignature(
|
|
195
284
|
SignatureRequestType.AddWallet,
|
|
196
285
|
signatureText,
|
|
197
286
|
newAccountSigner,
|
|
198
287
|
);
|
|
199
288
|
|
|
200
|
-
await this
|
|
289
|
+
await this.unsafe_applySignatures();
|
|
201
290
|
}
|
|
202
291
|
|
|
203
292
|
async removeAccount(accountAddress: string) {
|
|
204
293
|
const signatureText =
|
|
205
|
-
await this
|
|
294
|
+
await this.unsafe_removeAccountSignatureText(accountAddress);
|
|
206
295
|
|
|
207
296
|
if (!signatureText) {
|
|
208
297
|
throw new Error("Unable to generate remove account signature text");
|
|
209
298
|
}
|
|
210
299
|
|
|
211
|
-
await this
|
|
300
|
+
await this.unsafe_addSignature(
|
|
212
301
|
SignatureRequestType.RevokeWallet,
|
|
213
302
|
signatureText,
|
|
214
303
|
this.#signer,
|
|
215
304
|
);
|
|
216
305
|
|
|
217
|
-
await this
|
|
306
|
+
await this.unsafe_applySignatures();
|
|
218
307
|
}
|
|
219
308
|
|
|
220
309
|
async revokeAllOtherInstallations() {
|
|
221
310
|
const signatureText =
|
|
222
|
-
await this
|
|
311
|
+
await this.unsafe_revokeAllOtherInstallationsSignatureText();
|
|
223
312
|
|
|
224
313
|
if (!signatureText) {
|
|
225
314
|
throw new Error(
|
|
@@ -227,30 +316,30 @@ export class Client extends ClientWorkerClass {
|
|
|
227
316
|
);
|
|
228
317
|
}
|
|
229
318
|
|
|
230
|
-
await this
|
|
319
|
+
await this.unsafe_addSignature(
|
|
231
320
|
SignatureRequestType.RevokeInstallations,
|
|
232
321
|
signatureText,
|
|
233
322
|
this.#signer,
|
|
234
323
|
);
|
|
235
324
|
|
|
236
|
-
await this
|
|
325
|
+
await this.unsafe_applySignatures();
|
|
237
326
|
}
|
|
238
327
|
|
|
239
328
|
async revokeInstallations(installationIds: Uint8Array[]) {
|
|
240
329
|
const signatureText =
|
|
241
|
-
await this
|
|
330
|
+
await this.unsafe_revokeInstallationsSignatureText(installationIds);
|
|
242
331
|
|
|
243
332
|
if (!signatureText) {
|
|
244
333
|
throw new Error("Unable to generate revoke installations signature text");
|
|
245
334
|
}
|
|
246
335
|
|
|
247
|
-
await this
|
|
336
|
+
await this.unsafe_addSignature(
|
|
248
337
|
SignatureRequestType.RevokeInstallations,
|
|
249
338
|
signatureText,
|
|
250
339
|
this.#signer,
|
|
251
340
|
);
|
|
252
341
|
|
|
253
|
-
await this
|
|
342
|
+
await this.unsafe_applySignatures();
|
|
254
343
|
}
|
|
255
344
|
|
|
256
345
|
async isRegistered() {
|
package/src/Conversation.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import type { ContentTypeId } from "@xmtp/content-type-primitives";
|
|
2
2
|
import { ContentTypeText } from "@xmtp/content-type-text";
|
|
3
|
-
import type {
|
|
4
|
-
ConsentState,
|
|
5
|
-
MetadataField,
|
|
6
|
-
PermissionPolicy,
|
|
7
|
-
PermissionUpdateType,
|
|
8
|
-
} from "@xmtp/wasm-bindings";
|
|
3
|
+
import type { ConsentState } from "@xmtp/wasm-bindings";
|
|
9
4
|
import { v4 } from "uuid";
|
|
10
5
|
import { AsyncStream, type StreamCallback } from "@/AsyncStream";
|
|
11
6
|
import type { Client } from "@/Client";
|
|
@@ -22,12 +17,6 @@ export class Conversation {
|
|
|
22
17
|
|
|
23
18
|
#id: string;
|
|
24
19
|
|
|
25
|
-
#name?: SafeConversation["name"];
|
|
26
|
-
|
|
27
|
-
#imageUrl?: SafeConversation["imageUrl"];
|
|
28
|
-
|
|
29
|
-
#description?: SafeConversation["description"];
|
|
30
|
-
|
|
31
20
|
#isActive?: SafeConversation["isActive"];
|
|
32
21
|
|
|
33
22
|
#addedByInboxId?: SafeConversation["addedByInboxId"];
|
|
@@ -36,10 +25,6 @@ export class Conversation {
|
|
|
36
25
|
|
|
37
26
|
#createdAtNs?: SafeConversation["createdAtNs"];
|
|
38
27
|
|
|
39
|
-
#admins: SafeConversation["admins"] = [];
|
|
40
|
-
|
|
41
|
-
#superAdmins: SafeConversation["superAdmins"] = [];
|
|
42
|
-
|
|
43
28
|
constructor(client: Client, id: string, data?: SafeConversation) {
|
|
44
29
|
this.#client = client;
|
|
45
30
|
this.#id = id;
|
|
@@ -47,57 +32,16 @@ export class Conversation {
|
|
|
47
32
|
}
|
|
48
33
|
|
|
49
34
|
#syncData(data?: SafeConversation) {
|
|
50
|
-
this.#name = data?.name ?? "";
|
|
51
|
-
this.#imageUrl = data?.imageUrl ?? "";
|
|
52
|
-
this.#description = data?.description ?? "";
|
|
53
35
|
this.#isActive = data?.isActive ?? undefined;
|
|
54
36
|
this.#addedByInboxId = data?.addedByInboxId ?? "";
|
|
55
37
|
this.#metadata = data?.metadata ?? undefined;
|
|
56
38
|
this.#createdAtNs = data?.createdAtNs ?? undefined;
|
|
57
|
-
this.#admins = data?.admins ?? [];
|
|
58
|
-
this.#superAdmins = data?.superAdmins ?? [];
|
|
59
39
|
}
|
|
60
40
|
|
|
61
41
|
get id() {
|
|
62
42
|
return this.#id;
|
|
63
43
|
}
|
|
64
44
|
|
|
65
|
-
get name() {
|
|
66
|
-
return this.#name;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async updateName(name: string) {
|
|
70
|
-
await this.#client.sendMessage("updateGroupName", {
|
|
71
|
-
id: this.#id,
|
|
72
|
-
name,
|
|
73
|
-
});
|
|
74
|
-
this.#name = name;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
get imageUrl() {
|
|
78
|
-
return this.#imageUrl;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async updateImageUrl(imageUrl: string) {
|
|
82
|
-
await this.#client.sendMessage("updateGroupImageUrlSquare", {
|
|
83
|
-
id: this.#id,
|
|
84
|
-
imageUrl,
|
|
85
|
-
});
|
|
86
|
-
this.#imageUrl = imageUrl;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
get description() {
|
|
90
|
-
return this.#description;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async updateDescription(description: string) {
|
|
94
|
-
await this.#client.sendMessage("updateGroupDescription", {
|
|
95
|
-
id: this.#id,
|
|
96
|
-
description,
|
|
97
|
-
});
|
|
98
|
-
this.#description = description;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
45
|
get isActive() {
|
|
102
46
|
return this.#isActive;
|
|
103
47
|
}
|
|
@@ -124,118 +68,12 @@ export class Conversation {
|
|
|
124
68
|
});
|
|
125
69
|
}
|
|
126
70
|
|
|
127
|
-
get admins() {
|
|
128
|
-
return this.#admins;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
get superAdmins() {
|
|
132
|
-
return this.#superAdmins;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async syncAdmins() {
|
|
136
|
-
const admins = await this.#client.sendMessage("getGroupAdmins", {
|
|
137
|
-
id: this.#id,
|
|
138
|
-
});
|
|
139
|
-
this.#admins = admins;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async syncSuperAdmins() {
|
|
143
|
-
const superAdmins = await this.#client.sendMessage("getGroupSuperAdmins", {
|
|
144
|
-
id: this.#id,
|
|
145
|
-
});
|
|
146
|
-
this.#superAdmins = superAdmins;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async permissions() {
|
|
150
|
-
return this.#client.sendMessage("getGroupPermissions", {
|
|
151
|
-
id: this.#id,
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async updatePermission(
|
|
156
|
-
permissionType: PermissionUpdateType,
|
|
157
|
-
policy: PermissionPolicy,
|
|
158
|
-
metadataField?: MetadataField,
|
|
159
|
-
) {
|
|
160
|
-
return this.#client.sendMessage("updateGroupPermissionPolicy", {
|
|
161
|
-
id: this.#id,
|
|
162
|
-
permissionType,
|
|
163
|
-
policy,
|
|
164
|
-
metadataField,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
async isAdmin(inboxId: string) {
|
|
169
|
-
await this.syncAdmins();
|
|
170
|
-
return this.#admins.includes(inboxId);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async isSuperAdmin(inboxId: string) {
|
|
174
|
-
await this.syncSuperAdmins();
|
|
175
|
-
return this.#superAdmins.includes(inboxId);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
71
|
async sync() {
|
|
179
72
|
const data = await this.#client.sendMessage("syncGroup", {
|
|
180
73
|
id: this.#id,
|
|
181
74
|
});
|
|
182
75
|
this.#syncData(data);
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
async addMembers(accountAddresses: string[]) {
|
|
186
|
-
return this.#client.sendMessage("addGroupMembers", {
|
|
187
|
-
id: this.#id,
|
|
188
|
-
accountAddresses,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async addMembersByInboxId(inboxIds: string[]) {
|
|
193
|
-
return this.#client.sendMessage("addGroupMembersByInboxId", {
|
|
194
|
-
id: this.#id,
|
|
195
|
-
inboxIds,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async removeMembers(accountAddresses: string[]) {
|
|
200
|
-
return this.#client.sendMessage("removeGroupMembers", {
|
|
201
|
-
id: this.#id,
|
|
202
|
-
accountAddresses,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async removeMembersByInboxId(inboxIds: string[]) {
|
|
207
|
-
return this.#client.sendMessage("removeGroupMembersByInboxId", {
|
|
208
|
-
id: this.#id,
|
|
209
|
-
inboxIds,
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async addAdmin(inboxId: string) {
|
|
214
|
-
return this.#client.sendMessage("addGroupAdmin", {
|
|
215
|
-
id: this.#id,
|
|
216
|
-
inboxId,
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async removeAdmin(inboxId: string) {
|
|
221
|
-
return this.#client.sendMessage("removeGroupAdmin", {
|
|
222
|
-
id: this.#id,
|
|
223
|
-
inboxId,
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
async addSuperAdmin(inboxId: string) {
|
|
228
|
-
return this.#client.sendMessage("addGroupSuperAdmin", {
|
|
229
|
-
id: this.#id,
|
|
230
|
-
inboxId,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async removeSuperAdmin(inboxId: string) {
|
|
235
|
-
return this.#client.sendMessage("removeGroupSuperAdmin", {
|
|
236
|
-
id: this.#id,
|
|
237
|
-
inboxId,
|
|
238
|
-
});
|
|
76
|
+
return data;
|
|
239
77
|
}
|
|
240
78
|
|
|
241
79
|
async publishMessages() {
|
|
@@ -304,12 +142,6 @@ export class Conversation {
|
|
|
304
142
|
});
|
|
305
143
|
}
|
|
306
144
|
|
|
307
|
-
async dmPeerInboxId() {
|
|
308
|
-
return this.#client.sendMessage("getDmPeerInboxId", {
|
|
309
|
-
id: this.#id,
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
|
|
313
145
|
async messageDisappearingSettings() {
|
|
314
146
|
return this.#client.sendMessage("getGroupMessageDisappearingSettings", {
|
|
315
147
|
id: this.#id,
|
|
@@ -342,11 +174,17 @@ export class Conversation {
|
|
|
342
174
|
const endStream = this.#client.handleStreamMessage<SafeMessage>(
|
|
343
175
|
streamId,
|
|
344
176
|
(error, value) => {
|
|
177
|
+
if (error) {
|
|
178
|
+
void asyncStream.callback(error, undefined);
|
|
179
|
+
void callback?.(error, undefined);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
345
183
|
const decodedMessage = value
|
|
346
184
|
? new DecodedMessage(this.#client, value)
|
|
347
185
|
: undefined;
|
|
348
|
-
void asyncStream.callback(
|
|
349
|
-
void callback?.(
|
|
186
|
+
void asyncStream.callback(null, decodedMessage);
|
|
187
|
+
void callback?.(null, decodedMessage);
|
|
350
188
|
},
|
|
351
189
|
);
|
|
352
190
|
await this.#client.sendMessage("streamGroupMessages", {
|