@xmtp/node-sdk 2.0.0 → 2.0.1
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 +29 -6
- package/dist/index.d.ts +730 -23
- package/dist/index.js +727 -19
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -126,6 +126,26 @@ function nsToDate(ns) {
|
|
|
126
126
|
return new Date(ns / 1_000_000);
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Represents a decoded XMTP message
|
|
131
|
+
*
|
|
132
|
+
* This class transforms network messages into a structured format with
|
|
133
|
+
* content decoding.
|
|
134
|
+
*
|
|
135
|
+
* @class
|
|
136
|
+
* @property {any} content - The decoded content of the message
|
|
137
|
+
* @property {ContentTypeId} contentType - The content type of the message content
|
|
138
|
+
* @property {string} conversationId - Unique identifier for the conversation
|
|
139
|
+
* @property {MessageDeliveryStatus} deliveryStatus - Current delivery status of the message ("unpublished" | "published" | "failed")
|
|
140
|
+
* @property {string} [fallback] - Optional fallback text for the message
|
|
141
|
+
* @property {number} [compression] - Optional compression level applied to the message
|
|
142
|
+
* @property {string} id - Unique identifier for the message
|
|
143
|
+
* @property {MessageKind} kind - Type of message ("application" | "membership_change")
|
|
144
|
+
* @property {Record<string, string>} parameters - Additional parameters associated with the message
|
|
145
|
+
* @property {string} senderInboxId - Identifier for the sender's inbox
|
|
146
|
+
* @property {Date} sentAt - Timestamp when the message was sent
|
|
147
|
+
* @property {number} sentAtNs - Timestamp when the message was sent (in nanoseconds)
|
|
148
|
+
*/
|
|
129
149
|
class DecodedMessage {
|
|
130
150
|
#client;
|
|
131
151
|
content;
|
|
@@ -174,10 +194,15 @@ class DecodedMessage {
|
|
|
174
194
|
this.parameters = message.content.parameters;
|
|
175
195
|
this.fallback = message.content.fallback;
|
|
176
196
|
this.compression = message.content.compression;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
197
|
+
this.content = undefined;
|
|
198
|
+
if (this.contentType) {
|
|
199
|
+
try {
|
|
200
|
+
this.content = this.#client.decodeContent(message, this.contentType);
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
this.content = undefined;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
181
206
|
}
|
|
182
207
|
}
|
|
183
208
|
|
|
@@ -240,10 +265,22 @@ class ClientNotInitializedError extends Error {
|
|
|
240
265
|
}
|
|
241
266
|
}
|
|
242
267
|
|
|
268
|
+
/**
|
|
269
|
+
* Represents a conversation
|
|
270
|
+
*
|
|
271
|
+
* This class is not intended to be initialized directly.
|
|
272
|
+
*/
|
|
243
273
|
class Conversation {
|
|
244
274
|
#client;
|
|
245
275
|
#conversation;
|
|
246
276
|
#lastMessage;
|
|
277
|
+
/**
|
|
278
|
+
* Creates a new conversation instance
|
|
279
|
+
*
|
|
280
|
+
* @param client - The client instance managing the conversation
|
|
281
|
+
* @param conversation - The underlying conversation instance
|
|
282
|
+
* @param lastMessage - Optional last message in the conversation
|
|
283
|
+
*/
|
|
247
284
|
constructor(client, conversation, lastMessage) {
|
|
248
285
|
this.#client = client;
|
|
249
286
|
this.#conversation = conversation;
|
|
@@ -251,21 +288,41 @@ class Conversation {
|
|
|
251
288
|
? new DecodedMessage(client, lastMessage)
|
|
252
289
|
: undefined;
|
|
253
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* Gets the unique identifier for this conversation
|
|
293
|
+
*/
|
|
254
294
|
get id() {
|
|
255
295
|
return this.#conversation.id();
|
|
256
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* Gets whether this conversation is currently active
|
|
299
|
+
*/
|
|
257
300
|
get isActive() {
|
|
258
301
|
return this.#conversation.isActive();
|
|
259
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Gets the inbox ID that added this client's inbox to the conversation
|
|
305
|
+
*/
|
|
260
306
|
get addedByInboxId() {
|
|
261
307
|
return this.#conversation.addedByInboxId();
|
|
262
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* Gets the timestamp when the conversation was created in nanoseconds
|
|
311
|
+
*/
|
|
263
312
|
get createdAtNs() {
|
|
264
313
|
return this.#conversation.createdAtNs();
|
|
265
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Gets the date when the conversation was created
|
|
317
|
+
*/
|
|
266
318
|
get createdAt() {
|
|
267
319
|
return nsToDate(this.createdAtNs);
|
|
268
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* Gets the metadata for this conversation
|
|
323
|
+
*
|
|
324
|
+
* @returns Promise that resolves with the conversation metadata
|
|
325
|
+
*/
|
|
269
326
|
async metadata() {
|
|
270
327
|
const metadata = await this.#conversation.groupMetadata();
|
|
271
328
|
return {
|
|
@@ -273,12 +330,28 @@ class Conversation {
|
|
|
273
330
|
conversationType: metadata.conversationType(),
|
|
274
331
|
};
|
|
275
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Gets the members of this conversation
|
|
335
|
+
*
|
|
336
|
+
* @returns Promise that resolves with the conversation members
|
|
337
|
+
*/
|
|
276
338
|
async members() {
|
|
277
339
|
return this.#conversation.listMembers();
|
|
278
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Synchronizes conversation data from the network
|
|
343
|
+
*
|
|
344
|
+
* @returns Promise that resolves when synchronization is complete
|
|
345
|
+
*/
|
|
279
346
|
async sync() {
|
|
280
347
|
return this.#conversation.sync();
|
|
281
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* Creates a stream for new messages in this conversation
|
|
351
|
+
*
|
|
352
|
+
* @param callback - Optional callback function for handling new stream values
|
|
353
|
+
* @returns Stream instance for new messages
|
|
354
|
+
*/
|
|
282
355
|
stream(callback) {
|
|
283
356
|
const asyncStream = new AsyncStream();
|
|
284
357
|
const stream = this.#conversation.stream((error, value) => {
|
|
@@ -298,9 +371,22 @@ class Conversation {
|
|
|
298
371
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
299
372
|
return asyncStream;
|
|
300
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Publishes pending messages that were sent optimistically
|
|
376
|
+
*
|
|
377
|
+
* @returns Promise that resolves when publishing is complete
|
|
378
|
+
*/
|
|
301
379
|
async publishMessages() {
|
|
302
380
|
return this.#conversation.publishMessages();
|
|
303
381
|
}
|
|
382
|
+
/**
|
|
383
|
+
* Prepares a message to be published
|
|
384
|
+
*
|
|
385
|
+
* @param content - The content to send
|
|
386
|
+
* @param contentType - Optional content type of the message content
|
|
387
|
+
* @returns Promise that resolves with the message ID
|
|
388
|
+
* @throws {MissingContentTypeError} if content type is required but not provided
|
|
389
|
+
*/
|
|
304
390
|
sendOptimistic(content, contentType) {
|
|
305
391
|
if (typeof content !== "string" && !contentType) {
|
|
306
392
|
throw new MissingContentTypeError();
|
|
@@ -311,6 +397,14 @@ class Conversation {
|
|
|
311
397
|
this.#client.encodeContent(content, contentType);
|
|
312
398
|
return this.#conversation.sendOptimistic(encodedContent);
|
|
313
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Publishes a new message
|
|
402
|
+
*
|
|
403
|
+
* @param content - The content to send
|
|
404
|
+
* @param contentType - Optional content type of the message content
|
|
405
|
+
* @returns Promise that resolves with the message ID after it has been sent
|
|
406
|
+
* @throws {MissingContentTypeError} if content type is required but not provided
|
|
407
|
+
*/
|
|
314
408
|
async send(content, contentType) {
|
|
315
409
|
if (typeof content !== "string" && !contentType) {
|
|
316
410
|
throw new MissingContentTypeError();
|
|
@@ -321,80 +415,179 @@ class Conversation {
|
|
|
321
415
|
this.#client.encodeContent(content, contentType);
|
|
322
416
|
return this.#conversation.send(encodedContent);
|
|
323
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* Lists messages in this conversation
|
|
420
|
+
*
|
|
421
|
+
* @param options - Optional filtering and pagination options
|
|
422
|
+
* @returns Promise that resolves with an array of decoded messages
|
|
423
|
+
*/
|
|
324
424
|
async messages(options) {
|
|
325
425
|
const messages = await this.#conversation.findMessages(options);
|
|
326
|
-
return (
|
|
327
|
-
.map((message) => new DecodedMessage(this.#client, message))
|
|
328
|
-
// filter out messages without content
|
|
329
|
-
.filter((message) => message.content !== undefined));
|
|
426
|
+
return messages.map((message) => new DecodedMessage(this.#client, message));
|
|
330
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* Gets the last message in this conversation
|
|
430
|
+
*
|
|
431
|
+
* @returns Promise that resolves with the last message or undefined if none exists
|
|
432
|
+
*/
|
|
331
433
|
async lastMessage() {
|
|
332
434
|
return this.#lastMessage ?? (await this.messages({ limit: 1 }))[0];
|
|
333
435
|
}
|
|
436
|
+
/**
|
|
437
|
+
* Gets the consent state for this conversation
|
|
438
|
+
*/
|
|
334
439
|
get consentState() {
|
|
335
440
|
return this.#conversation.consentState();
|
|
336
441
|
}
|
|
442
|
+
/**
|
|
443
|
+
* Updates the consent state for this conversation
|
|
444
|
+
*
|
|
445
|
+
* @param consentState - The new consent state to set
|
|
446
|
+
*/
|
|
337
447
|
updateConsentState(consentState) {
|
|
338
448
|
this.#conversation.updateConsentState(consentState);
|
|
339
449
|
}
|
|
450
|
+
/**
|
|
451
|
+
* Gets the message disappearing settings for this conversation
|
|
452
|
+
*
|
|
453
|
+
* @returns The current message disappearing settings or undefined if not set
|
|
454
|
+
*/
|
|
340
455
|
messageDisappearingSettings() {
|
|
341
456
|
return this.#conversation.messageDisappearingSettings() ?? undefined;
|
|
342
457
|
}
|
|
458
|
+
/**
|
|
459
|
+
* Updates message disappearing settings for this conversation
|
|
460
|
+
*
|
|
461
|
+
* @param fromNs - The timestamp from which messages should start disappearing
|
|
462
|
+
* @param inNs - The duration after which messages should disappear
|
|
463
|
+
* @returns Promise that resolves when the update is complete
|
|
464
|
+
*/
|
|
343
465
|
async updateMessageDisappearingSettings(fromNs, inNs) {
|
|
344
466
|
return this.#conversation.updateMessageDisappearingSettings({
|
|
345
467
|
fromNs,
|
|
346
468
|
inNs,
|
|
347
469
|
});
|
|
348
470
|
}
|
|
471
|
+
/**
|
|
472
|
+
* Removes message disappearing settings from this conversation
|
|
473
|
+
*
|
|
474
|
+
* @returns Promise that resolves when the settings are removed
|
|
475
|
+
*/
|
|
349
476
|
async removeMessageDisappearingSettings() {
|
|
350
477
|
return this.#conversation.removeMessageDisappearingSettings();
|
|
351
478
|
}
|
|
479
|
+
/**
|
|
480
|
+
* Checks if message disappearing is enabled for this conversation
|
|
481
|
+
*
|
|
482
|
+
* @returns Whether message disappearing is enabled
|
|
483
|
+
*/
|
|
352
484
|
isMessageDisappearingEnabled() {
|
|
353
485
|
return this.#conversation.isMessageDisappearingEnabled();
|
|
354
486
|
}
|
|
355
487
|
pausedForVersion() {
|
|
356
488
|
return this.#conversation.pausedForVersion() ?? undefined;
|
|
357
489
|
}
|
|
490
|
+
/**
|
|
491
|
+
* Retrieves HMAC keys for this conversation
|
|
492
|
+
*
|
|
493
|
+
* @returns The HMAC keys for this conversation
|
|
494
|
+
*/
|
|
358
495
|
getHmacKeys() {
|
|
359
496
|
return this.#conversation.getHmacKeys();
|
|
360
497
|
}
|
|
361
498
|
}
|
|
362
499
|
|
|
500
|
+
/**
|
|
501
|
+
* Represents a direct message conversation between two inboxes
|
|
502
|
+
*
|
|
503
|
+
* This class is not intended to be initialized directly.
|
|
504
|
+
*/
|
|
363
505
|
class Dm extends Conversation {
|
|
364
506
|
#conversation;
|
|
507
|
+
/**
|
|
508
|
+
* Creates a new direct message conversation instance
|
|
509
|
+
*
|
|
510
|
+
* @param client - The client instance managing this direct message conversation
|
|
511
|
+
* @param conversation - The underlying conversation instance
|
|
512
|
+
* @param lastMessage - Optional last message in the conversation
|
|
513
|
+
*/
|
|
365
514
|
constructor(client, conversation, lastMessage) {
|
|
366
515
|
super(client, conversation, lastMessage);
|
|
367
516
|
this.#conversation = conversation;
|
|
368
517
|
}
|
|
518
|
+
/**
|
|
519
|
+
* Retrieves the inbox ID of the other participant in the DM
|
|
520
|
+
*
|
|
521
|
+
* @returns Promise that resolves with the peer's inbox ID
|
|
522
|
+
*/
|
|
369
523
|
get peerInboxId() {
|
|
370
524
|
return this.#conversation.dmPeerInboxId();
|
|
371
525
|
}
|
|
372
526
|
}
|
|
373
527
|
|
|
528
|
+
/**
|
|
529
|
+
* Represents a group conversation between multiple inboxes
|
|
530
|
+
*
|
|
531
|
+
* This class is not intended to be initialized directly.
|
|
532
|
+
*/
|
|
374
533
|
class Group extends Conversation {
|
|
375
534
|
#conversation;
|
|
535
|
+
/**
|
|
536
|
+
* Creates a new group conversation instance
|
|
537
|
+
*
|
|
538
|
+
* @param client - The client instance managing this group conversation
|
|
539
|
+
* @param conversation - The underlying conversation object
|
|
540
|
+
* @param lastMessage - Optional last message in the conversation
|
|
541
|
+
*/
|
|
376
542
|
constructor(client, conversation, lastMessage) {
|
|
377
543
|
super(client, conversation, lastMessage);
|
|
378
544
|
this.#conversation = conversation;
|
|
379
545
|
}
|
|
546
|
+
/**
|
|
547
|
+
* The name of the group
|
|
548
|
+
*/
|
|
380
549
|
get name() {
|
|
381
550
|
return this.#conversation.groupName();
|
|
382
551
|
}
|
|
552
|
+
/**
|
|
553
|
+
* Updates the group's name
|
|
554
|
+
*
|
|
555
|
+
* @param name The new name for the group
|
|
556
|
+
*/
|
|
383
557
|
async updateName(name) {
|
|
384
558
|
return this.#conversation.updateGroupName(name);
|
|
385
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* The image URL of the group
|
|
562
|
+
*/
|
|
386
563
|
get imageUrl() {
|
|
387
564
|
return this.#conversation.groupImageUrlSquare();
|
|
388
565
|
}
|
|
566
|
+
/**
|
|
567
|
+
* Updates the group's image URL
|
|
568
|
+
*
|
|
569
|
+
* @param imageUrl The new image URL for the group
|
|
570
|
+
*/
|
|
389
571
|
async updateImageUrl(imageUrl) {
|
|
390
572
|
return this.#conversation.updateGroupImageUrlSquare(imageUrl);
|
|
391
573
|
}
|
|
574
|
+
/**
|
|
575
|
+
* The description of the group
|
|
576
|
+
*/
|
|
392
577
|
get description() {
|
|
393
578
|
return this.#conversation.groupDescription();
|
|
394
579
|
}
|
|
580
|
+
/**
|
|
581
|
+
* Updates the group's description
|
|
582
|
+
*
|
|
583
|
+
* @param description The new description for the group
|
|
584
|
+
*/
|
|
395
585
|
async updateDescription(description) {
|
|
396
586
|
return this.#conversation.updateGroupDescription(description);
|
|
397
587
|
}
|
|
588
|
+
/**
|
|
589
|
+
* The permissions of the group
|
|
590
|
+
*/
|
|
398
591
|
get permissions() {
|
|
399
592
|
const permissions = this.#conversation.groupPermissions();
|
|
400
593
|
return {
|
|
@@ -402,54 +595,136 @@ class Group extends Conversation {
|
|
|
402
595
|
policySet: permissions.policySet(),
|
|
403
596
|
};
|
|
404
597
|
}
|
|
598
|
+
/**
|
|
599
|
+
* Updates a specific permission policy for the group
|
|
600
|
+
*
|
|
601
|
+
* @param permissionType The type of permission to update
|
|
602
|
+
* @param policy The new permission policy
|
|
603
|
+
* @param metadataField Optional metadata field for the permission
|
|
604
|
+
*/
|
|
405
605
|
async updatePermission(permissionType, policy, metadataField) {
|
|
406
606
|
return this.#conversation.updatePermissionPolicy(permissionType, policy, metadataField);
|
|
407
607
|
}
|
|
608
|
+
/**
|
|
609
|
+
* The list of admins of the group
|
|
610
|
+
*/
|
|
408
611
|
get admins() {
|
|
409
612
|
return this.#conversation.adminList();
|
|
410
613
|
}
|
|
614
|
+
/**
|
|
615
|
+
* The list of super admins of the group
|
|
616
|
+
*/
|
|
411
617
|
get superAdmins() {
|
|
412
618
|
return this.#conversation.superAdminList();
|
|
413
619
|
}
|
|
620
|
+
/**
|
|
621
|
+
* Checks if an inbox is an admin of the group
|
|
622
|
+
*
|
|
623
|
+
* @param inboxId The inbox ID to check
|
|
624
|
+
* @returns Boolean indicating if the inbox is an admin
|
|
625
|
+
*/
|
|
414
626
|
isAdmin(inboxId) {
|
|
415
627
|
return this.#conversation.isAdmin(inboxId);
|
|
416
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Checks if an inbox is a super admin of the group
|
|
631
|
+
*
|
|
632
|
+
* @param inboxId The inbox ID to check
|
|
633
|
+
* @returns Boolean indicating if the inbox is a super admin
|
|
634
|
+
*/
|
|
417
635
|
isSuperAdmin(inboxId) {
|
|
418
636
|
return this.#conversation.isSuperAdmin(inboxId);
|
|
419
637
|
}
|
|
638
|
+
/**
|
|
639
|
+
* Adds members to the group using identifiers
|
|
640
|
+
*
|
|
641
|
+
* @param identifiers Array of member identifiers to add
|
|
642
|
+
*/
|
|
420
643
|
async addMembersByIdentifiers(identifiers) {
|
|
421
644
|
return this.#conversation.addMembers(identifiers);
|
|
422
645
|
}
|
|
646
|
+
/**
|
|
647
|
+
* Adds members to the group using inbox IDs
|
|
648
|
+
*
|
|
649
|
+
* @param inboxIds Array of inbox IDs to add
|
|
650
|
+
*/
|
|
423
651
|
async addMembers(inboxIds) {
|
|
424
652
|
return this.#conversation.addMembersByInboxId(inboxIds);
|
|
425
653
|
}
|
|
654
|
+
/**
|
|
655
|
+
* Removes members from the group using identifiers
|
|
656
|
+
*
|
|
657
|
+
* @param identifiers Array of member identifiers to remove
|
|
658
|
+
*/
|
|
426
659
|
async removeMembersByIdentifiers(identifiers) {
|
|
427
660
|
return this.#conversation.removeMembers(identifiers);
|
|
428
661
|
}
|
|
662
|
+
/**
|
|
663
|
+
* Removes members from the group using inbox IDs
|
|
664
|
+
*
|
|
665
|
+
* @param inboxIds Array of inbox IDs to remove
|
|
666
|
+
*/
|
|
429
667
|
async removeMembers(inboxIds) {
|
|
430
668
|
return this.#conversation.removeMembersByInboxId(inboxIds);
|
|
431
669
|
}
|
|
670
|
+
/**
|
|
671
|
+
* Promotes a group member to admin status
|
|
672
|
+
*
|
|
673
|
+
* @param inboxId The inbox ID of the member to promote
|
|
674
|
+
*/
|
|
432
675
|
async addAdmin(inboxId) {
|
|
433
676
|
return this.#conversation.addAdmin(inboxId);
|
|
434
677
|
}
|
|
678
|
+
/**
|
|
679
|
+
* Removes admin status from a group member
|
|
680
|
+
*
|
|
681
|
+
* @param inboxId The inbox ID of the admin to demote
|
|
682
|
+
*/
|
|
435
683
|
async removeAdmin(inboxId) {
|
|
436
684
|
return this.#conversation.removeAdmin(inboxId);
|
|
437
685
|
}
|
|
686
|
+
/**
|
|
687
|
+
* Promotes a group member to super admin status
|
|
688
|
+
*
|
|
689
|
+
* @param inboxId The inbox ID of the member to promote
|
|
690
|
+
*/
|
|
438
691
|
async addSuperAdmin(inboxId) {
|
|
439
692
|
return this.#conversation.addSuperAdmin(inboxId);
|
|
440
693
|
}
|
|
694
|
+
/**
|
|
695
|
+
* Removes super admin status from a group member
|
|
696
|
+
*
|
|
697
|
+
* @param inboxId The inbox ID of the super admin to demote
|
|
698
|
+
*/
|
|
441
699
|
async removeSuperAdmin(inboxId) {
|
|
442
700
|
return this.#conversation.removeSuperAdmin(inboxId);
|
|
443
701
|
}
|
|
444
702
|
}
|
|
445
703
|
|
|
704
|
+
/**
|
|
705
|
+
* Manages conversations
|
|
706
|
+
*
|
|
707
|
+
* This class is not intended to be initialized directly.
|
|
708
|
+
*/
|
|
446
709
|
class Conversations {
|
|
447
710
|
#client;
|
|
448
711
|
#conversations;
|
|
712
|
+
/**
|
|
713
|
+
* Creates a new conversations instance
|
|
714
|
+
*
|
|
715
|
+
* @param client - The client instance managing the conversations
|
|
716
|
+
* @param conversations - The underlying conversations instance
|
|
717
|
+
*/
|
|
449
718
|
constructor(client, conversations) {
|
|
450
719
|
this.#client = client;
|
|
451
720
|
this.#conversations = conversations;
|
|
452
721
|
}
|
|
722
|
+
/**
|
|
723
|
+
* Retrieves a conversation by its ID
|
|
724
|
+
*
|
|
725
|
+
* @param id - The conversation ID to look up
|
|
726
|
+
* @returns The conversation if found, undefined otherwise
|
|
727
|
+
*/
|
|
453
728
|
async getConversationById(id) {
|
|
454
729
|
try {
|
|
455
730
|
// findGroupById will throw if group is not found
|
|
@@ -463,6 +738,12 @@ class Conversations {
|
|
|
463
738
|
return undefined;
|
|
464
739
|
}
|
|
465
740
|
}
|
|
741
|
+
/**
|
|
742
|
+
* Retrieves a DM by inbox ID
|
|
743
|
+
*
|
|
744
|
+
* @param inboxId - The inbox ID to look up
|
|
745
|
+
* @returns The DM if found, undefined otherwise
|
|
746
|
+
*/
|
|
466
747
|
getDmByInboxId(inboxId) {
|
|
467
748
|
try {
|
|
468
749
|
// findDmByTargetInboxId will throw if group is not found
|
|
@@ -473,6 +754,12 @@ class Conversations {
|
|
|
473
754
|
return undefined;
|
|
474
755
|
}
|
|
475
756
|
}
|
|
757
|
+
/**
|
|
758
|
+
* Retrieves a message by its ID
|
|
759
|
+
*
|
|
760
|
+
* @param id - The message ID to look up
|
|
761
|
+
* @returns The decoded message if found, undefined otherwise
|
|
762
|
+
*/
|
|
476
763
|
getMessageById(id) {
|
|
477
764
|
try {
|
|
478
765
|
// findMessageById will throw if message is not found
|
|
@@ -483,26 +770,60 @@ class Conversations {
|
|
|
483
770
|
return undefined;
|
|
484
771
|
}
|
|
485
772
|
}
|
|
773
|
+
/**
|
|
774
|
+
* Creates a new group conversation with the specified identifiers
|
|
775
|
+
*
|
|
776
|
+
* @param identifiers - Array of identifiers for group members
|
|
777
|
+
* @param options - Optional group creation options
|
|
778
|
+
* @returns The new group
|
|
779
|
+
*/
|
|
486
780
|
async newGroupWithIdentifiers(identifiers, options) {
|
|
487
781
|
const group = await this.#conversations.createGroup(identifiers, options);
|
|
488
782
|
const conversation = new Group(this.#client, group);
|
|
489
783
|
return conversation;
|
|
490
784
|
}
|
|
785
|
+
/**
|
|
786
|
+
* Creates a new group conversation with the specified inbox IDs
|
|
787
|
+
*
|
|
788
|
+
* @param inboxIds - Array of inbox IDs for group members
|
|
789
|
+
* @param options - Optional group creation options
|
|
790
|
+
* @returns The new group
|
|
791
|
+
*/
|
|
491
792
|
async newGroup(inboxIds, options) {
|
|
492
793
|
const group = await this.#conversations.createGroupByInboxId(inboxIds, options);
|
|
493
794
|
const conversation = new Group(this.#client, group);
|
|
494
795
|
return conversation;
|
|
495
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* Creates a new DM conversation with the specified identifier
|
|
799
|
+
*
|
|
800
|
+
* @param identifier - Identifier for the DM recipient
|
|
801
|
+
* @param options - Optional DM creation options
|
|
802
|
+
* @returns The new DM
|
|
803
|
+
*/
|
|
496
804
|
async newDmWithIdentifier(identifier, options) {
|
|
497
805
|
const group = await this.#conversations.createDm(identifier, options);
|
|
498
806
|
const conversation = new Dm(this.#client, group);
|
|
499
807
|
return conversation;
|
|
500
808
|
}
|
|
809
|
+
/**
|
|
810
|
+
* Creates a new DM conversation with the specified inbox ID
|
|
811
|
+
*
|
|
812
|
+
* @param inboxId - Inbox ID for the DM recipient
|
|
813
|
+
* @param options - Optional DM creation options
|
|
814
|
+
* @returns The new DM
|
|
815
|
+
*/
|
|
501
816
|
async newDm(inboxId, options) {
|
|
502
817
|
const group = await this.#conversations.createDmByInboxId(inboxId, options);
|
|
503
818
|
const conversation = new Dm(this.#client, group);
|
|
504
819
|
return conversation;
|
|
505
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* Lists all conversations with optional filtering
|
|
823
|
+
*
|
|
824
|
+
* @param options - Optional filtering and pagination options
|
|
825
|
+
* @returns Array of conversations
|
|
826
|
+
*/
|
|
506
827
|
async list(options) {
|
|
507
828
|
const groups = this.#conversations.list(options);
|
|
508
829
|
return Promise.all(groups.map(async (item) => {
|
|
@@ -512,6 +833,12 @@ class Conversations {
|
|
|
512
833
|
: new Group(this.#client, item.conversation, item.lastMessage);
|
|
513
834
|
}));
|
|
514
835
|
}
|
|
836
|
+
/**
|
|
837
|
+
* Lists all groups with optional filtering
|
|
838
|
+
*
|
|
839
|
+
* @param options - Optional filtering and pagination options
|
|
840
|
+
* @returns Array of groups
|
|
841
|
+
*/
|
|
515
842
|
listGroups(options) {
|
|
516
843
|
const groups = this.#conversations.listGroups(options);
|
|
517
844
|
return groups.map((item) => {
|
|
@@ -519,6 +846,12 @@ class Conversations {
|
|
|
519
846
|
return conversation;
|
|
520
847
|
});
|
|
521
848
|
}
|
|
849
|
+
/**
|
|
850
|
+
* Lists all DMs with optional filtering
|
|
851
|
+
*
|
|
852
|
+
* @param options - Optional filtering and pagination options
|
|
853
|
+
* @returns Array of DMs
|
|
854
|
+
*/
|
|
522
855
|
listDms(options) {
|
|
523
856
|
const groups = this.#conversations.listDms(options);
|
|
524
857
|
return groups.map((item) => {
|
|
@@ -526,12 +859,30 @@ class Conversations {
|
|
|
526
859
|
return conversation;
|
|
527
860
|
});
|
|
528
861
|
}
|
|
862
|
+
/**
|
|
863
|
+
* Synchronizes conversations for the current client from the network
|
|
864
|
+
*
|
|
865
|
+
* @returns Promise that resolves when sync is complete
|
|
866
|
+
*/
|
|
529
867
|
async sync() {
|
|
530
868
|
return this.#conversations.sync();
|
|
531
869
|
}
|
|
870
|
+
/**
|
|
871
|
+
* Synchronizes all conversations and messages from the network with optional
|
|
872
|
+
* consent state filtering
|
|
873
|
+
*
|
|
874
|
+
* @param consentStates - Optional array of consent states to filter by
|
|
875
|
+
* @returns Promise that resolves when sync is complete
|
|
876
|
+
*/
|
|
532
877
|
async syncAll(consentStates) {
|
|
533
878
|
return this.#conversations.syncAllConversations(consentStates);
|
|
534
879
|
}
|
|
880
|
+
/**
|
|
881
|
+
* Creates a stream for new conversations
|
|
882
|
+
*
|
|
883
|
+
* @param callback - Optional callback function for handling new stream value
|
|
884
|
+
* @returns Stream instance for new conversations
|
|
885
|
+
*/
|
|
535
886
|
stream(callback) {
|
|
536
887
|
const asyncStream = new AsyncStream();
|
|
537
888
|
const stream = this.#conversations.stream((err, value) => {
|
|
@@ -557,6 +908,12 @@ class Conversations {
|
|
|
557
908
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
558
909
|
return asyncStream;
|
|
559
910
|
}
|
|
911
|
+
/**
|
|
912
|
+
* Creates a stream for new group conversations
|
|
913
|
+
*
|
|
914
|
+
* @param callback - Optional callback function for handling new stream value
|
|
915
|
+
* @returns Stream instance for new group conversations
|
|
916
|
+
*/
|
|
560
917
|
streamGroups(callback) {
|
|
561
918
|
const asyncStream = new AsyncStream();
|
|
562
919
|
const stream = this.#conversations.streamGroups((error, value) => {
|
|
@@ -576,6 +933,12 @@ class Conversations {
|
|
|
576
933
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
577
934
|
return asyncStream;
|
|
578
935
|
}
|
|
936
|
+
/**
|
|
937
|
+
* Creates a stream for new DM conversations
|
|
938
|
+
*
|
|
939
|
+
* @param callback - Optional callback function for handling new stream value
|
|
940
|
+
* @returns Stream instance for new DM conversations
|
|
941
|
+
*/
|
|
579
942
|
streamDms(callback) {
|
|
580
943
|
const asyncStream = new AsyncStream();
|
|
581
944
|
const stream = this.#conversations.streamDms((error, value) => {
|
|
@@ -595,6 +958,12 @@ class Conversations {
|
|
|
595
958
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
596
959
|
return asyncStream;
|
|
597
960
|
}
|
|
961
|
+
/**
|
|
962
|
+
* Creates a stream for all new messages
|
|
963
|
+
*
|
|
964
|
+
* @param callback - Optional callback function for handling new stream value
|
|
965
|
+
* @returns Stream instance for new messages
|
|
966
|
+
*/
|
|
598
967
|
async streamAllMessages(callback) {
|
|
599
968
|
// sync conversations first
|
|
600
969
|
await this.sync();
|
|
@@ -616,6 +985,12 @@ class Conversations {
|
|
|
616
985
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
617
986
|
return asyncStream;
|
|
618
987
|
}
|
|
988
|
+
/**
|
|
989
|
+
* Creates a stream for all new group messages
|
|
990
|
+
*
|
|
991
|
+
* @param callback - Optional callback function for handling new stream value
|
|
992
|
+
* @returns Stream instance for new group messages
|
|
993
|
+
*/
|
|
619
994
|
async streamAllGroupMessages(callback) {
|
|
620
995
|
// sync conversations first
|
|
621
996
|
await this.sync();
|
|
@@ -637,6 +1012,12 @@ class Conversations {
|
|
|
637
1012
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
638
1013
|
return asyncStream;
|
|
639
1014
|
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Creates a stream for all new DM messages
|
|
1017
|
+
*
|
|
1018
|
+
* @param callback - Optional callback function for handling new stream value
|
|
1019
|
+
* @returns Stream instance for new DM messages
|
|
1020
|
+
*/
|
|
640
1021
|
async streamAllDmMessages(callback) {
|
|
641
1022
|
// sync conversations first
|
|
642
1023
|
await this.sync();
|
|
@@ -658,33 +1039,87 @@ class Conversations {
|
|
|
658
1039
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
659
1040
|
return asyncStream;
|
|
660
1041
|
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Retrieves HMAC keys for all conversations
|
|
1044
|
+
*
|
|
1045
|
+
* @returns The HMAC keys for all conversations
|
|
1046
|
+
*/
|
|
661
1047
|
hmacKeys() {
|
|
662
1048
|
return this.#conversations.getHmacKeys();
|
|
663
1049
|
}
|
|
664
1050
|
}
|
|
665
1051
|
|
|
1052
|
+
/**
|
|
1053
|
+
* Manages user preferences and consent states
|
|
1054
|
+
*
|
|
1055
|
+
* This class is not intended to be initialized directly.
|
|
1056
|
+
*/
|
|
666
1057
|
class Preferences {
|
|
667
1058
|
#client;
|
|
668
1059
|
#conversations;
|
|
1060
|
+
/**
|
|
1061
|
+
* Creates a new preferences instance
|
|
1062
|
+
*
|
|
1063
|
+
* @param client - The client instance managing preferences
|
|
1064
|
+
* @param conversations - The underlying conversations instance
|
|
1065
|
+
*/
|
|
669
1066
|
constructor(client, conversations) {
|
|
670
1067
|
this.#client = client;
|
|
671
1068
|
this.#conversations = conversations;
|
|
672
1069
|
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Retrieves the current inbox state
|
|
1072
|
+
*
|
|
1073
|
+
* @param refreshFromNetwork - Optional flag to force refresh from network
|
|
1074
|
+
* @returns Promise that resolves with the inbox state
|
|
1075
|
+
*/
|
|
673
1076
|
async inboxState(refreshFromNetwork = false) {
|
|
674
1077
|
return this.#client.inboxState(refreshFromNetwork);
|
|
675
1078
|
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Gets the latest inbox state for a specific inbox
|
|
1081
|
+
*
|
|
1082
|
+
* @param inboxId - The inbox ID to get state for
|
|
1083
|
+
* @returns Promise that resolves with the latest inbox state
|
|
1084
|
+
*/
|
|
676
1085
|
async getLatestInboxState(inboxId) {
|
|
677
1086
|
return this.#client.getLatestInboxState(inboxId);
|
|
678
1087
|
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Retrieves inbox state for specific inbox IDs
|
|
1090
|
+
*
|
|
1091
|
+
* @param inboxIds - Array of inbox IDs to get state for
|
|
1092
|
+
* @param refreshFromNetwork - Optional flag to force refresh from network
|
|
1093
|
+
* @returns Promise that resolves with the inbox state for the inbox IDs
|
|
1094
|
+
*/
|
|
679
1095
|
async inboxStateFromInboxIds(inboxIds, refreshFromNetwork) {
|
|
680
1096
|
return this.#client.addressesFromInboxId(refreshFromNetwork ?? false, inboxIds);
|
|
681
1097
|
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Updates consent states for multiple records
|
|
1100
|
+
*
|
|
1101
|
+
* @param consentStates - Array of consent records to update
|
|
1102
|
+
* @returns Promise that resolves when consent states are updated
|
|
1103
|
+
*/
|
|
682
1104
|
async setConsentStates(consentStates) {
|
|
683
1105
|
return this.#client.setConsentStates(consentStates);
|
|
684
1106
|
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Retrieves consent state for a specific entity
|
|
1109
|
+
*
|
|
1110
|
+
* @param entityType - Type of entity to get consent for
|
|
1111
|
+
* @param entity - Entity identifier
|
|
1112
|
+
* @returns Promise that resolves with the consent state
|
|
1113
|
+
*/
|
|
685
1114
|
async getConsentState(entityType, entity) {
|
|
686
1115
|
return this.#client.getConsentState(entityType, entity);
|
|
687
1116
|
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Creates a stream of consent state updates
|
|
1119
|
+
*
|
|
1120
|
+
* @param callback - Optional callback function for handling stream updates
|
|
1121
|
+
* @returns Stream instance for consent updates
|
|
1122
|
+
*/
|
|
688
1123
|
streamConsent(callback) {
|
|
689
1124
|
const asyncStream = new AsyncStream();
|
|
690
1125
|
const stream = this.#conversations.streamConsent((err, value) => {
|
|
@@ -699,6 +1134,12 @@ class Preferences {
|
|
|
699
1134
|
asyncStream.onReturn = stream.end.bind(stream);
|
|
700
1135
|
return asyncStream;
|
|
701
1136
|
}
|
|
1137
|
+
/**
|
|
1138
|
+
* Creates a stream of user preference updates
|
|
1139
|
+
*
|
|
1140
|
+
* @param callback - Optional callback function for handling stream updates
|
|
1141
|
+
* @returns Stream instance for preference updates
|
|
1142
|
+
*/
|
|
702
1143
|
streamPreferences(callback) {
|
|
703
1144
|
const asyncStream = new AsyncStream();
|
|
704
1145
|
const stream = this.#conversations.streamPreferences((err, value) => {
|
|
@@ -744,6 +1185,9 @@ const createClient = async (identifier, options) => {
|
|
|
744
1185
|
|
|
745
1186
|
const version = `${bindingsVersion.branch}@${bindingsVersion.version} (${bindingsVersion.date})`;
|
|
746
1187
|
|
|
1188
|
+
/**
|
|
1189
|
+
* Client for interacting with the XMTP network
|
|
1190
|
+
*/
|
|
747
1191
|
class Client {
|
|
748
1192
|
#client;
|
|
749
1193
|
#conversations;
|
|
@@ -752,6 +1196,14 @@ class Client {
|
|
|
752
1196
|
#codecs;
|
|
753
1197
|
#identifier;
|
|
754
1198
|
#options;
|
|
1199
|
+
/**
|
|
1200
|
+
* Creates a new XMTP client instance
|
|
1201
|
+
*
|
|
1202
|
+
* This class is not intended to be initialized directly.
|
|
1203
|
+
* Use `Client.create` or `Client.build` instead.
|
|
1204
|
+
*
|
|
1205
|
+
* @param options - Optional configuration for the client
|
|
1206
|
+
*/
|
|
755
1207
|
constructor(options) {
|
|
756
1208
|
this.#options = options;
|
|
757
1209
|
const codecs = [
|
|
@@ -761,6 +1213,14 @@ class Client {
|
|
|
761
1213
|
];
|
|
762
1214
|
this.#codecs = new Map(codecs.map((codec) => [codec.contentType.toString(), codec]));
|
|
763
1215
|
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Initializes the client with the provided identifier
|
|
1218
|
+
*
|
|
1219
|
+
* This is not meant to be called directly.
|
|
1220
|
+
* Use `Client.create` or `Client.build` instead.
|
|
1221
|
+
*
|
|
1222
|
+
* @param identifier - The identifier to initialize the client with
|
|
1223
|
+
*/
|
|
764
1224
|
async init(identifier) {
|
|
765
1225
|
if (this.#client) {
|
|
766
1226
|
return;
|
|
@@ -771,6 +1231,13 @@ class Client {
|
|
|
771
1231
|
this.#conversations = new Conversations(this, conversations);
|
|
772
1232
|
this.#preferences = new Preferences(this.#client, conversations);
|
|
773
1233
|
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Creates a new client instance with a signer
|
|
1236
|
+
*
|
|
1237
|
+
* @param signer - The signer to use for authentication
|
|
1238
|
+
* @param options - Optional configuration for the client
|
|
1239
|
+
* @returns A new client instance
|
|
1240
|
+
*/
|
|
774
1241
|
static async create(signer, options) {
|
|
775
1242
|
const identifier = await signer.getIdentifier();
|
|
776
1243
|
const client = new Client(options);
|
|
@@ -781,6 +1248,16 @@ class Client {
|
|
|
781
1248
|
}
|
|
782
1249
|
return client;
|
|
783
1250
|
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Creates a new client instance with an identifier
|
|
1253
|
+
*
|
|
1254
|
+
* Clients created with this method must already be registered.
|
|
1255
|
+
* Any methods called that require a signer will throw an error.
|
|
1256
|
+
*
|
|
1257
|
+
* @param identifier - The identifier to use
|
|
1258
|
+
* @param options - Optional configuration for the client
|
|
1259
|
+
* @returns A new client instance
|
|
1260
|
+
*/
|
|
784
1261
|
static async build(identifier, options) {
|
|
785
1262
|
const client = new Client({
|
|
786
1263
|
...options,
|
|
@@ -789,45 +1266,78 @@ class Client {
|
|
|
789
1266
|
await client.init(identifier);
|
|
790
1267
|
return client;
|
|
791
1268
|
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Gets the client options
|
|
1271
|
+
*/
|
|
792
1272
|
get options() {
|
|
793
1273
|
return this.#options;
|
|
794
1274
|
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Gets the signer associated with this client
|
|
1277
|
+
*/
|
|
795
1278
|
get signer() {
|
|
796
1279
|
return this.#signer;
|
|
797
1280
|
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Gets the account identifier for this client
|
|
1283
|
+
*/
|
|
798
1284
|
get accountIdentifier() {
|
|
799
1285
|
return this.#identifier;
|
|
800
1286
|
}
|
|
1287
|
+
/**
|
|
1288
|
+
* Gets the inbox ID associated with this client
|
|
1289
|
+
*/
|
|
801
1290
|
get inboxId() {
|
|
802
1291
|
if (!this.#client) {
|
|
803
1292
|
throw new ClientNotInitializedError();
|
|
804
1293
|
}
|
|
805
1294
|
return this.#client.inboxId();
|
|
806
1295
|
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Gets the installation ID for this client
|
|
1298
|
+
*/
|
|
807
1299
|
get installationId() {
|
|
808
1300
|
if (!this.#client) {
|
|
809
1301
|
throw new ClientNotInitializedError();
|
|
810
1302
|
}
|
|
811
1303
|
return this.#client.installationId();
|
|
812
1304
|
}
|
|
1305
|
+
/**
|
|
1306
|
+
* Gets the installation ID bytes for this client
|
|
1307
|
+
*/
|
|
813
1308
|
get installationIdBytes() {
|
|
814
1309
|
if (!this.#client) {
|
|
815
1310
|
throw new ClientNotInitializedError();
|
|
816
1311
|
}
|
|
817
1312
|
return this.#client.installationIdBytes();
|
|
818
1313
|
}
|
|
1314
|
+
/**
|
|
1315
|
+
* Gets whether the client is registered with the XMTP network
|
|
1316
|
+
*
|
|
1317
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1318
|
+
*/
|
|
819
1319
|
get isRegistered() {
|
|
820
1320
|
if (!this.#client) {
|
|
821
1321
|
throw new ClientNotInitializedError();
|
|
822
1322
|
}
|
|
823
1323
|
return this.#client.isRegistered();
|
|
824
1324
|
}
|
|
1325
|
+
/**
|
|
1326
|
+
* Gets the conversations manager for this client
|
|
1327
|
+
*
|
|
1328
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1329
|
+
*/
|
|
825
1330
|
get conversations() {
|
|
826
1331
|
if (!this.#conversations) {
|
|
827
1332
|
throw new ClientNotInitializedError();
|
|
828
1333
|
}
|
|
829
1334
|
return this.#conversations;
|
|
830
1335
|
}
|
|
1336
|
+
/**
|
|
1337
|
+
* Gets the preferences manager for this client
|
|
1338
|
+
*
|
|
1339
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1340
|
+
*/
|
|
831
1341
|
get preferences() {
|
|
832
1342
|
if (!this.#preferences) {
|
|
833
1343
|
throw new ClientNotInitializedError();
|
|
@@ -835,11 +1345,16 @@ class Client {
|
|
|
835
1345
|
return this.#preferences;
|
|
836
1346
|
}
|
|
837
1347
|
/**
|
|
1348
|
+
* Creates signature text for creating a new inbox
|
|
1349
|
+
*
|
|
838
1350
|
* WARNING: This function should be used with caution. It is only provided
|
|
839
1351
|
* for use in special cases where the provided workflows do not meet the
|
|
840
1352
|
* requirements of an application.
|
|
841
1353
|
*
|
|
842
|
-
* It is highly recommended to use the `register`
|
|
1354
|
+
* It is highly recommended to use the `register` method instead.
|
|
1355
|
+
*
|
|
1356
|
+
* @returns The signature text
|
|
1357
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
843
1358
|
*/
|
|
844
1359
|
async unsafe_createInboxSignatureText() {
|
|
845
1360
|
if (!this.#client) {
|
|
@@ -854,14 +1369,21 @@ class Client {
|
|
|
854
1369
|
}
|
|
855
1370
|
}
|
|
856
1371
|
/**
|
|
1372
|
+
* Creates signature text for adding a new account to the client's inbox
|
|
1373
|
+
*
|
|
857
1374
|
* WARNING: This function should be used with caution. It is only provided
|
|
858
1375
|
* for use in special cases where the provided workflows do not meet the
|
|
859
1376
|
* requirements of an application.
|
|
860
1377
|
*
|
|
861
|
-
* It is highly recommended to use the `unsafe_addAccount`
|
|
1378
|
+
* It is highly recommended to use the `unsafe_addAccount` method instead.
|
|
862
1379
|
*
|
|
863
1380
|
* The `allowInboxReassign` parameter must be true or this function will
|
|
864
1381
|
* throw an error.
|
|
1382
|
+
*
|
|
1383
|
+
* @param newAccountIdentifier - The identifier of the new account
|
|
1384
|
+
* @param allowInboxReassign - Whether to allow inbox reassignment
|
|
1385
|
+
* @returns The signature text
|
|
1386
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
865
1387
|
*/
|
|
866
1388
|
async unsafe_addAccountSignatureText(newAccountIdentifier, allowInboxReassign = false) {
|
|
867
1389
|
if (!this.#client) {
|
|
@@ -879,11 +1401,17 @@ class Client {
|
|
|
879
1401
|
}
|
|
880
1402
|
}
|
|
881
1403
|
/**
|
|
1404
|
+
* Creates signature text for removing an account from the client's inbox
|
|
1405
|
+
*
|
|
882
1406
|
* WARNING: This function should be used with caution. It is only provided
|
|
883
1407
|
* for use in special cases where the provided workflows do not meet the
|
|
884
1408
|
* requirements of an application.
|
|
885
1409
|
*
|
|
886
|
-
* It is highly recommended to use the `removeAccount`
|
|
1410
|
+
* It is highly recommended to use the `removeAccount` method instead.
|
|
1411
|
+
*
|
|
1412
|
+
* @param identifier - The identifier of the account to remove
|
|
1413
|
+
* @returns The signature text
|
|
1414
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
887
1415
|
*/
|
|
888
1416
|
async unsafe_removeAccountSignatureText(identifier) {
|
|
889
1417
|
if (!this.#client) {
|
|
@@ -898,12 +1426,17 @@ class Client {
|
|
|
898
1426
|
}
|
|
899
1427
|
}
|
|
900
1428
|
/**
|
|
1429
|
+
* Creates signature text for revoking all other installations of the
|
|
1430
|
+
* client's inbox
|
|
1431
|
+
*
|
|
901
1432
|
* WARNING: This function should be used with caution. It is only provided
|
|
902
1433
|
* for use in special cases where the provided workflows do not meet the
|
|
903
1434
|
* requirements of an application.
|
|
904
1435
|
*
|
|
905
|
-
* It is highly recommended to use the `revokeAllOtherInstallations`
|
|
906
|
-
*
|
|
1436
|
+
* It is highly recommended to use the `revokeAllOtherInstallations` method instead.
|
|
1437
|
+
*
|
|
1438
|
+
* @returns The signature text
|
|
1439
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
907
1440
|
*/
|
|
908
1441
|
async unsafe_revokeAllOtherInstallationsSignatureText() {
|
|
909
1442
|
if (!this.#client) {
|
|
@@ -918,11 +1451,18 @@ class Client {
|
|
|
918
1451
|
}
|
|
919
1452
|
}
|
|
920
1453
|
/**
|
|
1454
|
+
* Creates signature text for revoking specific installations of the
|
|
1455
|
+
* client's inbox
|
|
1456
|
+
*
|
|
921
1457
|
* WARNING: This function should be used with caution. It is only provided
|
|
922
1458
|
* for use in special cases where the provided workflows do not meet the
|
|
923
1459
|
* requirements of an application.
|
|
924
1460
|
*
|
|
925
|
-
* It is highly recommended to use the `revokeInstallations`
|
|
1461
|
+
* It is highly recommended to use the `revokeInstallations` method instead.
|
|
1462
|
+
*
|
|
1463
|
+
* @param installationIds - The installation IDs to revoke
|
|
1464
|
+
* @returns The signature text
|
|
1465
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
926
1466
|
*/
|
|
927
1467
|
async unsafe_revokeInstallationsSignatureText(installationIds) {
|
|
928
1468
|
if (!this.#client) {
|
|
@@ -937,11 +1477,18 @@ class Client {
|
|
|
937
1477
|
}
|
|
938
1478
|
}
|
|
939
1479
|
/**
|
|
1480
|
+
* Creates signature text for changing the recovery identifier for this
|
|
1481
|
+
* client's inbox
|
|
1482
|
+
*
|
|
940
1483
|
* WARNING: This function should be used with caution. It is only provided
|
|
941
1484
|
* for use in special cases where the provided workflows do not meet the
|
|
942
1485
|
* requirements of an application.
|
|
943
1486
|
*
|
|
944
|
-
* It is highly recommended to use the `
|
|
1487
|
+
* It is highly recommended to use the `changeRecoveryIdentifier` method instead.
|
|
1488
|
+
*
|
|
1489
|
+
* @param identifier - The new recovery identifier
|
|
1490
|
+
* @returns The signature text
|
|
1491
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
945
1492
|
*/
|
|
946
1493
|
async unsafe_changeRecoveryIdentifierSignatureText(identifier) {
|
|
947
1494
|
if (!this.#client) {
|
|
@@ -956,13 +1503,20 @@ class Client {
|
|
|
956
1503
|
}
|
|
957
1504
|
}
|
|
958
1505
|
/**
|
|
1506
|
+
* Adds a signature for a specific request type
|
|
1507
|
+
*
|
|
959
1508
|
* WARNING: This function should be used with caution. It is only provided
|
|
960
1509
|
* for use in special cases where the provided workflows do not meet the
|
|
961
1510
|
* requirements of an application.
|
|
962
1511
|
*
|
|
963
1512
|
* It is highly recommended to use the `register`, `unsafe_addAccount`,
|
|
964
1513
|
* `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
|
|
965
|
-
*
|
|
1514
|
+
* methods instead.
|
|
1515
|
+
*
|
|
1516
|
+
* @param signatureType - The type of signature request
|
|
1517
|
+
* @param signatureText - The text to sign
|
|
1518
|
+
* @param signer - The signer to use
|
|
1519
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
966
1520
|
*/
|
|
967
1521
|
async unsafe_addSignature(signatureType, signatureText, signer) {
|
|
968
1522
|
if (!this.#client) {
|
|
@@ -978,13 +1532,17 @@ class Client {
|
|
|
978
1532
|
}
|
|
979
1533
|
}
|
|
980
1534
|
/**
|
|
1535
|
+
* Applies all pending signatures
|
|
1536
|
+
*
|
|
981
1537
|
* WARNING: This function should be used with caution. It is only provided
|
|
982
1538
|
* for use in special cases where the provided workflows do not meet the
|
|
983
1539
|
* requirements of an application.
|
|
984
1540
|
*
|
|
985
1541
|
* It is highly recommended to use the `register`, `unsafe_addAccount`,
|
|
986
1542
|
* `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
|
|
987
|
-
*
|
|
1543
|
+
* methods instead.
|
|
1544
|
+
*
|
|
1545
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
988
1546
|
*/
|
|
989
1547
|
async unsafe_applySignatures() {
|
|
990
1548
|
if (!this.#client) {
|
|
@@ -992,6 +1550,14 @@ class Client {
|
|
|
992
1550
|
}
|
|
993
1551
|
return this.#client.applySignatureRequests();
|
|
994
1552
|
}
|
|
1553
|
+
/**
|
|
1554
|
+
* Registers the client with the XMTP network
|
|
1555
|
+
*
|
|
1556
|
+
* Requires a signer, use `Client.create` to create a client with a signer.
|
|
1557
|
+
*
|
|
1558
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1559
|
+
* @throws {SignerUnavailableError} if no signer is available
|
|
1560
|
+
*/
|
|
995
1561
|
async register() {
|
|
996
1562
|
if (!this.#client) {
|
|
997
1563
|
throw new ClientNotInitializedError();
|
|
@@ -1008,12 +1574,23 @@ class Client {
|
|
|
1008
1574
|
return this.#client.registerIdentity();
|
|
1009
1575
|
}
|
|
1010
1576
|
/**
|
|
1577
|
+
* Adds a new account to the client inbox
|
|
1578
|
+
*
|
|
1011
1579
|
* WARNING: This function should be used with caution. Adding a wallet already
|
|
1012
|
-
* associated with an
|
|
1580
|
+
* associated with an inbox ID will cause the wallet to lose access to
|
|
1013
1581
|
* that inbox.
|
|
1014
1582
|
*
|
|
1015
1583
|
* The `allowInboxReassign` parameter must be true to reassign an inbox
|
|
1016
1584
|
* already associated with a different account.
|
|
1585
|
+
*
|
|
1586
|
+
* Requires a signer, use `Client.create` to create a client with a signer.
|
|
1587
|
+
*
|
|
1588
|
+
* @param newAccountSigner - The signer for the new account
|
|
1589
|
+
* @param allowInboxReassign - Whether to allow inbox reassignment
|
|
1590
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1591
|
+
* @throws {AccountAlreadyAssociatedError} if the account is already associated with an inbox ID
|
|
1592
|
+
* @throws {GenerateSignatureError} if the signature cannot be generated
|
|
1593
|
+
* @throws {SignerUnavailableError} if no signer is available
|
|
1017
1594
|
*/
|
|
1018
1595
|
async unsafe_addAccount(newAccountSigner, allowInboxReassign = false) {
|
|
1019
1596
|
if (!this.#client) {
|
|
@@ -1032,6 +1609,16 @@ class Client {
|
|
|
1032
1609
|
await this.unsafe_addSignature(0 /* SignatureRequestType.AddWallet */, signatureText, newAccountSigner);
|
|
1033
1610
|
await this.unsafe_applySignatures();
|
|
1034
1611
|
}
|
|
1612
|
+
/**
|
|
1613
|
+
* Removes an account from the client's inbox
|
|
1614
|
+
*
|
|
1615
|
+
* Requires a signer, use `Client.create` to create a client with a signer.
|
|
1616
|
+
*
|
|
1617
|
+
* @param identifier - The identifier of the account to remove
|
|
1618
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1619
|
+
* @throws {GenerateSignatureError} if the signature cannot be generated
|
|
1620
|
+
* @throws {SignerUnavailableError} if no signer is available
|
|
1621
|
+
*/
|
|
1035
1622
|
async removeAccount(identifier) {
|
|
1036
1623
|
if (!this.#client) {
|
|
1037
1624
|
throw new ClientNotInitializedError();
|
|
@@ -1046,6 +1633,15 @@ class Client {
|
|
|
1046
1633
|
await this.unsafe_addSignature(2 /* SignatureRequestType.RevokeWallet */, signatureText, this.#signer);
|
|
1047
1634
|
await this.unsafe_applySignatures();
|
|
1048
1635
|
}
|
|
1636
|
+
/**
|
|
1637
|
+
* Revokes all other installations of the client's inbox
|
|
1638
|
+
*
|
|
1639
|
+
* Requires a signer, use `Client.create` to create a client with a signer.
|
|
1640
|
+
*
|
|
1641
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1642
|
+
* @throws {GenerateSignatureError} if the signature cannot be generated
|
|
1643
|
+
* @throws {SignerUnavailableError} if no signer is available
|
|
1644
|
+
*/
|
|
1049
1645
|
async revokeAllOtherInstallations() {
|
|
1050
1646
|
if (!this.#client) {
|
|
1051
1647
|
throw new ClientNotInitializedError();
|
|
@@ -1060,6 +1656,16 @@ class Client {
|
|
|
1060
1656
|
await this.unsafe_addSignature(3 /* SignatureRequestType.RevokeInstallations */, signatureText, this.#signer);
|
|
1061
1657
|
await this.unsafe_applySignatures();
|
|
1062
1658
|
}
|
|
1659
|
+
/**
|
|
1660
|
+
* Revokes specific installations of the client's inbox
|
|
1661
|
+
*
|
|
1662
|
+
* Requires a signer, use `Client.create` to create a client with a signer.
|
|
1663
|
+
*
|
|
1664
|
+
* @param installationIds - The installation IDs to revoke
|
|
1665
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1666
|
+
* @throws {SignerUnavailableError} if no signer is available
|
|
1667
|
+
* @throws {GenerateSignatureError} if the signature cannot be generated
|
|
1668
|
+
*/
|
|
1063
1669
|
async revokeInstallations(installationIds) {
|
|
1064
1670
|
if (!this.#client) {
|
|
1065
1671
|
throw new ClientNotInitializedError();
|
|
@@ -1074,6 +1680,16 @@ class Client {
|
|
|
1074
1680
|
await this.unsafe_addSignature(3 /* SignatureRequestType.RevokeInstallations */, signatureText, this.#signer);
|
|
1075
1681
|
await this.unsafe_applySignatures();
|
|
1076
1682
|
}
|
|
1683
|
+
/**
|
|
1684
|
+
* Changes the recovery identifier for the client's inbox
|
|
1685
|
+
*
|
|
1686
|
+
* Requires a signer, use `Client.create` to create a client with a signer.
|
|
1687
|
+
*
|
|
1688
|
+
* @param identifier - The new recovery identifier
|
|
1689
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1690
|
+
* @throws {SignerUnavailableError} if no signer is available
|
|
1691
|
+
* @throws {GenerateSignatureError} if the signature cannot be generated
|
|
1692
|
+
*/
|
|
1077
1693
|
async changeRecoveryIdentifier(identifier) {
|
|
1078
1694
|
if (!this.#client) {
|
|
1079
1695
|
throw new ClientNotInitializedError();
|
|
@@ -1088,6 +1704,13 @@ class Client {
|
|
|
1088
1704
|
await this.unsafe_addSignature(4 /* SignatureRequestType.ChangeRecoveryIdentifier */, signatureText, this.#signer);
|
|
1089
1705
|
await this.unsafe_applySignatures();
|
|
1090
1706
|
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Checks if the client can message the specified identifiers
|
|
1709
|
+
*
|
|
1710
|
+
* @param identifiers - The identifiers to check
|
|
1711
|
+
* @returns Whether the client can message the identifiers
|
|
1712
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1713
|
+
*/
|
|
1091
1714
|
async canMessage(identifiers) {
|
|
1092
1715
|
if (!this.#client) {
|
|
1093
1716
|
throw new ClientNotInitializedError();
|
|
@@ -1095,6 +1718,13 @@ class Client {
|
|
|
1095
1718
|
const canMessage = await this.#client.canMessage(identifiers);
|
|
1096
1719
|
return new Map(Object.entries(canMessage));
|
|
1097
1720
|
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Checks if the specified identifiers can be messaged
|
|
1723
|
+
*
|
|
1724
|
+
* @param identifiers - The identifiers to check
|
|
1725
|
+
* @param env - Optional XMTP environment
|
|
1726
|
+
* @returns Map of identifiers to whether they can be messaged
|
|
1727
|
+
*/
|
|
1098
1728
|
static async canMessage(identifiers, env) {
|
|
1099
1729
|
const canMessageMap = new Map();
|
|
1100
1730
|
for (const identifier of identifiers) {
|
|
@@ -1103,15 +1733,36 @@ class Client {
|
|
|
1103
1733
|
}
|
|
1104
1734
|
return canMessageMap;
|
|
1105
1735
|
}
|
|
1736
|
+
/**
|
|
1737
|
+
* Gets the key package statuses for the specified installation IDs
|
|
1738
|
+
*
|
|
1739
|
+
* @param installationIds - The installation IDs to check
|
|
1740
|
+
* @returns The key package statuses
|
|
1741
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1742
|
+
*/
|
|
1106
1743
|
async getKeyPackageStatusesForInstallationIds(installationIds) {
|
|
1107
1744
|
if (!this.#client) {
|
|
1108
1745
|
throw new ClientNotInitializedError();
|
|
1109
1746
|
}
|
|
1110
1747
|
return this.#client.getKeyPackageStatusesForInstallationIds(installationIds);
|
|
1111
1748
|
}
|
|
1749
|
+
/**
|
|
1750
|
+
* Gets the codec for a given content type
|
|
1751
|
+
*
|
|
1752
|
+
* @param contentType - The content type to get the codec for
|
|
1753
|
+
* @returns The codec, if found
|
|
1754
|
+
*/
|
|
1112
1755
|
codecFor(contentType) {
|
|
1113
1756
|
return this.#codecs.get(contentType.toString());
|
|
1114
1757
|
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Encodes content for a given content type
|
|
1760
|
+
*
|
|
1761
|
+
* @param content - The content to encode
|
|
1762
|
+
* @param contentType - The content type to encode for
|
|
1763
|
+
* @returns The encoded content
|
|
1764
|
+
* @throws {CodecNotFoundError} if no codec is found for the content type
|
|
1765
|
+
*/
|
|
1115
1766
|
encodeContent(content, contentType) {
|
|
1116
1767
|
const codec = this.codecFor(contentType);
|
|
1117
1768
|
if (!codec) {
|
|
@@ -1124,6 +1775,15 @@ class Client {
|
|
|
1124
1775
|
}
|
|
1125
1776
|
return encoded;
|
|
1126
1777
|
}
|
|
1778
|
+
/**
|
|
1779
|
+
* Decodes a message for a given content type
|
|
1780
|
+
*
|
|
1781
|
+
* @param message - The message to decode
|
|
1782
|
+
* @param contentType - The content type to decode for
|
|
1783
|
+
* @returns The decoded content
|
|
1784
|
+
* @throws {CodecNotFoundError} if no codec is found for the content type
|
|
1785
|
+
* @throws {InvalidGroupMembershipChangeError} if the message is an invalid group membership change
|
|
1786
|
+
*/
|
|
1127
1787
|
decodeContent(message, contentType) {
|
|
1128
1788
|
const codec = this.codecFor(contentType);
|
|
1129
1789
|
if (!codec) {
|
|
@@ -1134,21 +1794,42 @@ class Client {
|
|
|
1134
1794
|
message.kind !== 1 /* GroupMessageKind.MembershipChange */) {
|
|
1135
1795
|
throw new InvalidGroupMembershipChangeError(message.id);
|
|
1136
1796
|
}
|
|
1137
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
1138
1797
|
return codec.decode(message.content, this);
|
|
1139
1798
|
}
|
|
1799
|
+
/**
|
|
1800
|
+
* Finds the inbox ID for a given identifier
|
|
1801
|
+
*
|
|
1802
|
+
* @param identifier - The identifier to look up
|
|
1803
|
+
* @returns The inbox ID, if found
|
|
1804
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1805
|
+
*/
|
|
1140
1806
|
async getInboxIdByIdentifier(identifier) {
|
|
1141
1807
|
if (!this.#client) {
|
|
1142
1808
|
throw new ClientNotInitializedError();
|
|
1143
1809
|
}
|
|
1144
1810
|
return this.#client.findInboxIdByIdentifier(identifier);
|
|
1145
1811
|
}
|
|
1812
|
+
/**
|
|
1813
|
+
* Signs a message with the installation key
|
|
1814
|
+
*
|
|
1815
|
+
* @param signatureText - The text to sign
|
|
1816
|
+
* @returns The signature
|
|
1817
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1818
|
+
*/
|
|
1146
1819
|
signWithInstallationKey(signatureText) {
|
|
1147
1820
|
if (!this.#client) {
|
|
1148
1821
|
throw new ClientNotInitializedError();
|
|
1149
1822
|
}
|
|
1150
1823
|
return this.#client.signWithInstallationKey(signatureText);
|
|
1151
1824
|
}
|
|
1825
|
+
/**
|
|
1826
|
+
* Verifies a signature was made with the installation key
|
|
1827
|
+
*
|
|
1828
|
+
* @param signatureText - The text that was signed
|
|
1829
|
+
* @param signatureBytes - The signature bytes to verify
|
|
1830
|
+
* @returns Whether the signature is valid
|
|
1831
|
+
* @throws {ClientNotInitializedError} if the client is not initialized
|
|
1832
|
+
*/
|
|
1152
1833
|
verifySignedWithInstallationKey(signatureText, signatureBytes) {
|
|
1153
1834
|
if (!this.#client) {
|
|
1154
1835
|
throw new ClientNotInitializedError();
|
|
@@ -1161,6 +1842,14 @@ class Client {
|
|
|
1161
1842
|
return false;
|
|
1162
1843
|
}
|
|
1163
1844
|
}
|
|
1845
|
+
/**
|
|
1846
|
+
* Verifies a signature was made with a public key
|
|
1847
|
+
*
|
|
1848
|
+
* @param signatureText - The text that was signed
|
|
1849
|
+
* @param signatureBytes - The signature bytes to verify
|
|
1850
|
+
* @param publicKey - The public key to verify against
|
|
1851
|
+
* @returns Whether the signature is valid
|
|
1852
|
+
*/
|
|
1164
1853
|
static verifySignedWithPublicKey(signatureText, signatureBytes, publicKey) {
|
|
1165
1854
|
try {
|
|
1166
1855
|
verifySignedWithPublicKey(signatureText, signatureBytes, publicKey);
|
|
@@ -1170,14 +1859,33 @@ class Client {
|
|
|
1170
1859
|
return false;
|
|
1171
1860
|
}
|
|
1172
1861
|
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Checks if an address is authorized for an inbox
|
|
1864
|
+
*
|
|
1865
|
+
* @param inboxId - The inbox ID to check
|
|
1866
|
+
* @param address - The address to check
|
|
1867
|
+
* @param options - Optional network options
|
|
1868
|
+
* @returns Whether the address is authorized
|
|
1869
|
+
*/
|
|
1173
1870
|
static async isAddressAuthorized(inboxId, address, options) {
|
|
1174
1871
|
const host = options?.apiUrl || ApiUrls[options?.env || "dev"];
|
|
1175
1872
|
return await isAddressAuthorized(host, inboxId, address);
|
|
1176
1873
|
}
|
|
1874
|
+
/**
|
|
1875
|
+
* Checks if an installation is authorized for an inbox
|
|
1876
|
+
*
|
|
1877
|
+
* @param inboxId - The inbox ID to check
|
|
1878
|
+
* @param installation - The installation to check
|
|
1879
|
+
* @param options - Optional network options
|
|
1880
|
+
* @returns Whether the installation is authorized
|
|
1881
|
+
*/
|
|
1177
1882
|
static async isInstallationAuthorized(inboxId, installation, options) {
|
|
1178
1883
|
const host = options?.apiUrl || ApiUrls[options?.env || "dev"];
|
|
1179
1884
|
return await isInstallationAuthorized(host, inboxId, installation);
|
|
1180
1885
|
}
|
|
1886
|
+
/**
|
|
1887
|
+
* Gets the version of the Node bindings
|
|
1888
|
+
*/
|
|
1181
1889
|
static get version() {
|
|
1182
1890
|
return version;
|
|
1183
1891
|
}
|