@xmtp/node-sdk 3.1.0-dev.51bc782 → 3.1.0

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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { GroupUpdatedCodec, ContentTypeGroupUpdated } from '@xmtp/content-type-group-updated';
2
2
  import { ContentTypeText, TextCodec } from '@xmtp/content-type-text';
3
- import { generateInboxId as generateInboxId$1, getInboxIdForIdentifier as getInboxIdForIdentifier$1, createClient as createClient$1, verifySignedWithPublicKey, isAddressAuthorized, isInstallationAuthorized } from '@xmtp/node-bindings';
4
- export { ConsentEntityType, ConsentState, ConversationType, DeliveryStatus, GroupMember, GroupMembershipState, GroupMessageKind, GroupMetadata, GroupPermissions, GroupPermissionsOptions, IdentifierKind, LogLevel, MetadataField, PermissionLevel, PermissionPolicy, PermissionUpdateType, SignatureRequestType, SortDirection } from '@xmtp/node-bindings';
3
+ import { generateInboxId as generateInboxId$1, getInboxIdForIdentifier as getInboxIdForIdentifier$1, createClient as createClient$1, revokeInstallationsSignatureRequest, applySignatureRequest, inboxStateFromInboxIds, verifySignedWithPublicKey, isAddressAuthorized, isInstallationAuthorized } from '@xmtp/node-bindings';
4
+ export { ConsentEntityType, ConsentState, ConversationType, DeliveryStatus, GroupMember, GroupMembershipState, GroupMessageKind, GroupMetadata, GroupPermissions, GroupPermissionsOptions, IdentifierKind, LogLevel, MetadataField, PermissionLevel, PermissionPolicy, PermissionUpdateType, SignatureRequestHandle, SortDirection } from '@xmtp/node-bindings';
5
5
  import { ContentTypeId } from '@xmtp/content-type-primitives';
6
6
  import { join } from 'node:path';
7
7
  import process from 'node:process';
@@ -35,52 +35,49 @@ const HistorySyncUrls = {
35
35
  };
36
36
 
37
37
  class AsyncStream {
38
- #done = false;
38
+ #isDone = false;
39
39
  #resolveNext;
40
40
  #rejectNext;
41
41
  #queue;
42
42
  #error;
43
- onReturn = undefined;
44
- onError = undefined;
43
+ onReturn;
44
+ onError;
45
45
  constructor() {
46
46
  this.#queue = [];
47
- this.#resolveNext = null;
48
- this.#rejectNext = null;
49
- this.#error = null;
50
- this.#done = false;
47
+ this.#isDone = false;
51
48
  }
52
- #endStream() {
49
+ #done() {
53
50
  this.#queue = [];
54
- this.#resolveNext = null;
55
- this.#rejectNext = null;
56
- this.#done = true;
51
+ this.#resolveNext = undefined;
52
+ this.#rejectNext = undefined;
53
+ this.#isDone = true;
57
54
  }
58
55
  get error() {
59
56
  return this.#error;
60
57
  }
61
58
  get isDone() {
62
- return this.#done;
59
+ return this.#isDone;
63
60
  }
64
61
  callback = (error, value) => {
62
+ if (this.#isDone) {
63
+ return;
64
+ }
65
65
  if (error) {
66
66
  this.#error = error;
67
67
  if (this.#rejectNext) {
68
68
  this.#rejectNext(error);
69
- this.#endStream();
69
+ this.#done();
70
70
  this.onError?.(error);
71
71
  }
72
72
  return;
73
73
  }
74
- if (this.#done) {
75
- return;
76
- }
77
74
  if (this.#resolveNext) {
78
75
  this.#resolveNext({
79
76
  done: false,
80
77
  value,
81
78
  });
82
- this.#resolveNext = null;
83
- this.#rejectNext = null;
79
+ this.#resolveNext = undefined;
80
+ this.#rejectNext = undefined;
84
81
  }
85
82
  else {
86
83
  this.#queue.push(value);
@@ -88,7 +85,7 @@ class AsyncStream {
88
85
  };
89
86
  next = () => {
90
87
  if (this.#error) {
91
- this.#endStream();
88
+ this.#done();
92
89
  this.onError?.(this.#error);
93
90
  return Promise.reject(this.#error);
94
91
  }
@@ -98,24 +95,22 @@ class AsyncStream {
98
95
  value: this.#queue.shift(),
99
96
  });
100
97
  }
101
- if (this.#done) {
102
- return Promise.resolve({
103
- done: true,
104
- value: undefined,
105
- });
106
- }
107
98
  return new Promise((resolve, reject) => {
108
99
  this.#resolveNext = resolve;
109
100
  this.#rejectNext = reject;
110
101
  });
111
102
  };
112
103
  return = (value) => {
113
- this.#endStream();
114
- this.onReturn?.();
115
- return Promise.resolve({
104
+ this.#resolveNext?.({
116
105
  done: true,
117
106
  value,
118
107
  });
108
+ this.onReturn?.();
109
+ this.#done();
110
+ return {
111
+ done: true,
112
+ value,
113
+ };
119
114
  };
120
115
  end = () => this.return();
121
116
  [Symbol.asyncIterator]() {
@@ -222,29 +217,6 @@ class AccountAlreadyAssociatedError extends Error {
222
217
  super(`Account already associated with inbox ${inboxId}`);
223
218
  }
224
219
  }
225
- class GenerateSignatureError extends Error {
226
- constructor(signatureType) {
227
- let type = "";
228
- switch (signatureType) {
229
- case 0 /* SignatureRequestType.AddWallet */:
230
- type = "add account";
231
- break;
232
- case 1 /* SignatureRequestType.CreateInbox */:
233
- type = "create inbox";
234
- break;
235
- case 2 /* SignatureRequestType.RevokeWallet */:
236
- type = "remove account";
237
- break;
238
- case 3 /* SignatureRequestType.RevokeInstallations */:
239
- type = "revoke installations";
240
- break;
241
- case 4 /* SignatureRequestType.ChangeRecoveryIdentifier */:
242
- type = "change recovery identifier";
243
- break;
244
- }
245
- super(`Failed to generate ${type} signature text`);
246
- }
247
- }
248
220
  class InvalidGroupMembershipChangeError extends Error {
249
221
  constructor(messageId) {
250
222
  super(`Invalid group membership change for message ${messageId}`);
@@ -353,7 +325,7 @@ class Conversation {
353
325
  * @param callback - Optional callback function for handling new stream values
354
326
  * @returns Stream instance for new messages
355
327
  */
356
- stream(callback) {
328
+ stream(callback, onFail) {
357
329
  const asyncStream = new AsyncStream();
358
330
  const stream = this.#conversation.stream((error, value) => {
359
331
  let err = error;
@@ -368,8 +340,10 @@ class Conversation {
368
340
  }
369
341
  asyncStream.callback(err, message);
370
342
  callback?.(err, message);
371
- });
372
- asyncStream.onReturn = stream.end.bind(stream);
343
+ }, onFail ?? (() => { }));
344
+ asyncStream.onReturn = () => {
345
+ stream.end();
346
+ };
373
347
  return asyncStream;
374
348
  }
375
349
  /**
@@ -921,7 +895,7 @@ class Conversations {
921
895
  * @param callback - Optional callback function for handling new stream value
922
896
  * @returns Stream instance for new conversations
923
897
  */
924
- stream(callback) {
898
+ stream(callback, onFail) {
925
899
  const asyncStream = new AsyncStream();
926
900
  const stream = this.#conversations.stream((err, value) => {
927
901
  if (err) {
@@ -951,7 +925,7 @@ class Conversations {
951
925
  asyncStream.callback(error, undefined);
952
926
  callback?.(error, undefined);
953
927
  });
954
- });
928
+ }, onFail ?? (() => { }));
955
929
  asyncStream.onReturn = stream.end.bind(stream);
956
930
  return asyncStream;
957
931
  }
@@ -961,7 +935,7 @@ class Conversations {
961
935
  * @param callback - Optional callback function for handling new stream value
962
936
  * @returns Stream instance for new group conversations
963
937
  */
964
- streamGroups(callback) {
938
+ streamGroups(callback, onFail) {
965
939
  const asyncStream = new AsyncStream();
966
940
  const stream = this.#conversations.stream((error, value) => {
967
941
  let err = error;
@@ -976,7 +950,7 @@ class Conversations {
976
950
  }
977
951
  asyncStream.callback(err, group);
978
952
  callback?.(err, group);
979
- }, 1 /* ConversationType.Group */);
953
+ }, onFail ?? (() => { }), 1 /* ConversationType.Group */);
980
954
  asyncStream.onReturn = stream.end.bind(stream);
981
955
  return asyncStream;
982
956
  }
@@ -986,7 +960,7 @@ class Conversations {
986
960
  * @param callback - Optional callback function for handling new stream value
987
961
  * @returns Stream instance for new DM conversations
988
962
  */
989
- streamDms(callback) {
963
+ streamDms(callback, onFail) {
990
964
  const asyncStream = new AsyncStream();
991
965
  const stream = this.#conversations.stream((error, value) => {
992
966
  let err = error;
@@ -1001,7 +975,7 @@ class Conversations {
1001
975
  }
1002
976
  asyncStream.callback(err, dm);
1003
977
  callback?.(err, dm);
1004
- }, 0 /* ConversationType.Dm */);
978
+ }, onFail ?? (() => { }), 0 /* ConversationType.Dm */);
1005
979
  asyncStream.onReturn = stream.end.bind(stream);
1006
980
  return asyncStream;
1007
981
  }
@@ -1011,7 +985,7 @@ class Conversations {
1011
985
  * @param callback - Optional callback function for handling new stream value
1012
986
  * @returns Stream instance for new messages
1013
987
  */
1014
- async streamAllMessages(callback, conversationType, consentStates) {
988
+ async streamAllMessages(callback, conversationType, consentStates, onFail) {
1015
989
  // sync conversations first
1016
990
  await this.sync();
1017
991
  const asyncStream = new AsyncStream();
@@ -1028,7 +1002,7 @@ class Conversations {
1028
1002
  }
1029
1003
  asyncStream.callback(err, message);
1030
1004
  callback?.(err, message);
1031
- }, conversationType, consentStates);
1005
+ }, onFail ?? (() => { }), conversationType, consentStates);
1032
1006
  asyncStream.onReturn = stream.end.bind(stream);
1033
1007
  return asyncStream;
1034
1008
  }
@@ -1038,8 +1012,8 @@ class Conversations {
1038
1012
  * @param callback - Optional callback function for handling new stream value
1039
1013
  * @returns Stream instance for new group messages
1040
1014
  */
1041
- async streamAllGroupMessages(callback, consentStates) {
1042
- return this.streamAllMessages(callback, 1 /* ConversationType.Group */, consentStates);
1015
+ async streamAllGroupMessages(callback, consentStates, onFail) {
1016
+ return this.streamAllMessages(callback, 1 /* ConversationType.Group */, consentStates, onFail);
1043
1017
  }
1044
1018
  /**
1045
1019
  * Creates a stream for all new DM messages
@@ -1047,8 +1021,8 @@ class Conversations {
1047
1021
  * @param callback - Optional callback function for handling new stream value
1048
1022
  * @returns Stream instance for new DM messages
1049
1023
  */
1050
- async streamAllDmMessages(callback, consentStates) {
1051
- return this.streamAllMessages(callback, 0 /* ConversationType.Dm */, consentStates);
1024
+ async streamAllDmMessages(callback, consentStates, onFail) {
1025
+ return this.streamAllMessages(callback, 0 /* ConversationType.Dm */, consentStates, onFail);
1052
1026
  }
1053
1027
  /**
1054
1028
  * Retrieves HMAC keys for all conversations
@@ -1060,6 +1034,37 @@ class Conversations {
1060
1034
  }
1061
1035
  }
1062
1036
 
1037
+ /**
1038
+ * Debug information helpers for the client
1039
+ *
1040
+ * This class is not intended to be initialized directly.
1041
+ */
1042
+ class DebugInformation {
1043
+ #client;
1044
+ #options;
1045
+ constructor(client, options) {
1046
+ this.#client = client;
1047
+ this.#options = options;
1048
+ }
1049
+ apiStatistics() {
1050
+ return this.#client.apiStatistics();
1051
+ }
1052
+ apiIdentityStatistics() {
1053
+ return this.#client.apiIdentityStatistics();
1054
+ }
1055
+ apiAggregateStatistics() {
1056
+ return this.#client.apiAggregateStatistics();
1057
+ }
1058
+ clearAllStatistics() {
1059
+ this.#client.clearAllStatistics();
1060
+ }
1061
+ uploadDebugArchive(serverUrl) {
1062
+ const env = this.#options?.env || "dev";
1063
+ const historySyncUrl = this.#options?.historySyncUrl || HistorySyncUrls[env];
1064
+ return this.#client.uploadDebugArchive(serverUrl || historySyncUrl);
1065
+ }
1066
+ }
1067
+
1063
1068
  /**
1064
1069
  * Manages user preferences and consent states
1065
1070
  *
@@ -1134,7 +1139,7 @@ class Preferences {
1134
1139
  * @param callback - Optional callback function for handling stream updates
1135
1140
  * @returns Stream instance for consent updates
1136
1141
  */
1137
- streamConsent(callback) {
1142
+ streamConsent(callback, onFail) {
1138
1143
  const asyncStream = new AsyncStream();
1139
1144
  const stream = this.#conversations.streamConsent((err, value) => {
1140
1145
  if (err) {
@@ -1144,7 +1149,7 @@ class Preferences {
1144
1149
  }
1145
1150
  asyncStream.callback(null, value);
1146
1151
  callback?.(null, value);
1147
- });
1152
+ }, onFail ?? (() => { }));
1148
1153
  asyncStream.onReturn = stream.end.bind(stream);
1149
1154
  return asyncStream;
1150
1155
  }
@@ -1154,7 +1159,7 @@ class Preferences {
1154
1159
  * @param callback - Optional callback function for handling stream updates
1155
1160
  * @returns Stream instance for preference updates
1156
1161
  */
1157
- streamPreferences(callback) {
1162
+ streamPreferences(callback, onFail) {
1158
1163
  const asyncStream = new AsyncStream();
1159
1164
  const stream = this.#conversations.streamPreferences((err, value) => {
1160
1165
  if (err) {
@@ -1165,7 +1170,7 @@ class Preferences {
1165
1170
  // TODO: remove this once the node bindings type is updated
1166
1171
  asyncStream.callback(null, value);
1167
1172
  callback?.(null, value);
1168
- });
1173
+ }, onFail ?? (() => { }));
1169
1174
  asyncStream.onReturn = stream.end.bind(stream);
1170
1175
  return asyncStream;
1171
1176
  }
@@ -1210,6 +1215,7 @@ const version = `${bindingsVersion.branch}@${bindingsVersion.version} (${binding
1210
1215
  class Client {
1211
1216
  #client;
1212
1217
  #conversations;
1218
+ #debugInformation;
1213
1219
  #preferences;
1214
1220
  #signer;
1215
1221
  #codecs;
@@ -1248,6 +1254,7 @@ class Client {
1248
1254
  this.#client = await createClient(identifier, this.#options);
1249
1255
  const conversations = this.#client.conversations();
1250
1256
  this.#conversations = new Conversations(this, conversations);
1257
+ this.#debugInformation = new DebugInformation(this.#client, this.#options);
1251
1258
  this.#preferences = new Preferences(this.#client, conversations);
1252
1259
  }
1253
1260
  /**
@@ -1352,6 +1359,17 @@ class Client {
1352
1359
  }
1353
1360
  return this.#conversations;
1354
1361
  }
1362
+ /**
1363
+ * Gets the debug information helpersfor this client
1364
+ *
1365
+ * @throws {ClientNotInitializedError} if the client is not initialized
1366
+ */
1367
+ get debugInformation() {
1368
+ if (!this.#debugInformation) {
1369
+ throw new ClientNotInitializedError();
1370
+ }
1371
+ return this.#debugInformation;
1372
+ }
1355
1373
  /**
1356
1374
  * Gets the preferences manager for this client
1357
1375
  *
@@ -1364,31 +1382,61 @@ class Client {
1364
1382
  return this.#preferences;
1365
1383
  }
1366
1384
  /**
1367
- * Creates signature text for creating a new inbox
1385
+ * Adds a signature to a signature request using the client's signer (or the
1386
+ * provided signer)
1368
1387
  *
1369
1388
  * WARNING: This function should be used with caution. It is only provided
1370
1389
  * for use in special cases where the provided workflows do not meet the
1371
1390
  * requirements of an application.
1372
1391
  *
1373
- * It is highly recommended to use the `register` method instead.
1392
+ * It is highly recommended to use the `register`, `unsafe_addAccount`,
1393
+ * `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
1394
+ * methods instead.
1374
1395
  *
1375
- * @returns The signature text
1396
+ * @param signatureRequest - The signature request to add the signature to
1376
1397
  * @throws {ClientNotInitializedError} if the client is not initialized
1398
+ * @throws {SignerUnavailableError} if no signer is available
1377
1399
  */
1378
- async unsafe_createInboxSignatureText() {
1400
+ async unsafe_addSignature(signatureRequest, signer) {
1379
1401
  if (!this.#client) {
1380
1402
  throw new ClientNotInitializedError();
1381
1403
  }
1382
- try {
1383
- const signatureText = await this.#client.createInboxSignatureText();
1384
- return signatureText;
1404
+ if (!this.#signer) {
1405
+ throw new SignerUnavailableError();
1385
1406
  }
1386
- catch {
1387
- return undefined;
1407
+ const finalSigner = signer ?? this.#signer;
1408
+ const signature = await finalSigner.signMessage(await signatureRequest.signatureText());
1409
+ const identifier = await finalSigner.getIdentifier();
1410
+ switch (finalSigner.type) {
1411
+ case "SCW":
1412
+ await signatureRequest.addScwSignature(identifier, signature, finalSigner.getChainId(), finalSigner.getBlockNumber?.());
1413
+ break;
1414
+ case "EOA":
1415
+ await signatureRequest.addEcdsaSignature(signature);
1416
+ break;
1388
1417
  }
1389
1418
  }
1390
1419
  /**
1391
- * Creates signature text for adding a new account to the client's inbox
1420
+ * Returns a signature request handler for creating a new inbox
1421
+ *
1422
+ * WARNING: This function should be used with caution. It is only provided
1423
+ * for use in special cases where the provided workflows do not meet the
1424
+ * requirements of an application.
1425
+ *
1426
+ * It is highly recommended to use the `register` method instead.
1427
+ *
1428
+ * @returns The signature text
1429
+ * @throws {ClientNotInitializedError} if the client is not initialized
1430
+ */
1431
+ async unsafe_createInboxSignatureRequest() {
1432
+ if (!this.#client) {
1433
+ throw new ClientNotInitializedError();
1434
+ }
1435
+ return this.#client.createInboxSignatureRequest();
1436
+ }
1437
+ /**
1438
+ * Returns a signature request handler for adding a new account to the
1439
+ * client's inbox
1392
1440
  *
1393
1441
  * WARNING: This function should be used with caution. It is only provided
1394
1442
  * for use in special cases where the provided workflows do not meet the
@@ -1404,23 +1452,18 @@ class Client {
1404
1452
  * @returns The signature text
1405
1453
  * @throws {ClientNotInitializedError} if the client is not initialized
1406
1454
  */
1407
- async unsafe_addAccountSignatureText(newAccountIdentifier, allowInboxReassign = false) {
1455
+ async unsafe_addAccountSignatureRequest(newAccountIdentifier, allowInboxReassign = false) {
1408
1456
  if (!this.#client) {
1409
1457
  throw new ClientNotInitializedError();
1410
1458
  }
1411
1459
  if (!allowInboxReassign) {
1412
1460
  throw new InboxReassignError();
1413
1461
  }
1414
- try {
1415
- const signatureText = await this.#client.addIdentifierSignatureText(newAccountIdentifier);
1416
- return signatureText;
1417
- }
1418
- catch {
1419
- return undefined;
1420
- }
1462
+ return this.#client.addIdentifierSignatureRequest(newAccountIdentifier);
1421
1463
  }
1422
1464
  /**
1423
- * Creates signature text for removing an account from the client's inbox
1465
+ * Returns a signature request handler for removing an account from the
1466
+ * client's inbox
1424
1467
  *
1425
1468
  * WARNING: This function should be used with caution. It is only provided
1426
1469
  * for use in special cases where the provided workflows do not meet the
@@ -1432,21 +1475,15 @@ class Client {
1432
1475
  * @returns The signature text
1433
1476
  * @throws {ClientNotInitializedError} if the client is not initialized
1434
1477
  */
1435
- async unsafe_removeAccountSignatureText(identifier) {
1478
+ async unsafe_removeAccountSignatureRequest(identifier) {
1436
1479
  if (!this.#client) {
1437
1480
  throw new ClientNotInitializedError();
1438
1481
  }
1439
- try {
1440
- const signatureText = await this.#client.revokeIdentifierSignatureText(identifier);
1441
- return signatureText;
1442
- }
1443
- catch {
1444
- return undefined;
1445
- }
1482
+ return this.#client.revokeIdentifierSignatureRequest(identifier);
1446
1483
  }
1447
1484
  /**
1448
- * Creates signature text for revoking all other installations of the
1449
- * client's inbox
1485
+ * Returns a signature request handler for revoking all other installations
1486
+ * of the client's inbox
1450
1487
  *
1451
1488
  * WARNING: This function should be used with caution. It is only provided
1452
1489
  * for use in special cases where the provided workflows do not meet the
@@ -1457,21 +1494,15 @@ class Client {
1457
1494
  * @returns The signature text
1458
1495
  * @throws {ClientNotInitializedError} if the client is not initialized
1459
1496
  */
1460
- async unsafe_revokeAllOtherInstallationsSignatureText() {
1497
+ async unsafe_revokeAllOtherInstallationsSignatureRequest() {
1461
1498
  if (!this.#client) {
1462
1499
  throw new ClientNotInitializedError();
1463
1500
  }
1464
- try {
1465
- const signatureText = await this.#client.revokeAllOtherInstallationsSignatureText();
1466
- return signatureText;
1467
- }
1468
- catch {
1469
- return undefined;
1470
- }
1501
+ return this.#client.revokeAllOtherInstallationsSignatureRequest();
1471
1502
  }
1472
1503
  /**
1473
- * Creates signature text for revoking specific installations of the
1474
- * client's inbox
1504
+ * Returns a signature request handler for revoking specific installations
1505
+ * of the client's inbox
1475
1506
  *
1476
1507
  * WARNING: This function should be used with caution. It is only provided
1477
1508
  * for use in special cases where the provided workflows do not meet the
@@ -1483,21 +1514,15 @@ class Client {
1483
1514
  * @returns The signature text
1484
1515
  * @throws {ClientNotInitializedError} if the client is not initialized
1485
1516
  */
1486
- async unsafe_revokeInstallationsSignatureText(installationIds) {
1517
+ async unsafe_revokeInstallationsSignatureRequest(installationIds) {
1487
1518
  if (!this.#client) {
1488
1519
  throw new ClientNotInitializedError();
1489
1520
  }
1490
- try {
1491
- const signatureText = await this.#client.revokeInstallationsSignatureText(installationIds);
1492
- return signatureText;
1493
- }
1494
- catch {
1495
- return undefined;
1496
- }
1521
+ return this.#client.revokeInstallationsSignatureRequest(installationIds);
1497
1522
  }
1498
1523
  /**
1499
- * Creates signature text for changing the recovery identifier for this
1500
- * client's inbox
1524
+ * Returns a signature request handler for changing the recovery identifier
1525
+ * for this client's inbox
1501
1526
  *
1502
1527
  * WARNING: This function should be used with caution. It is only provided
1503
1528
  * for use in special cases where the provided workflows do not meet the
@@ -1509,20 +1534,14 @@ class Client {
1509
1534
  * @returns The signature text
1510
1535
  * @throws {ClientNotInitializedError} if the client is not initialized
1511
1536
  */
1512
- async unsafe_changeRecoveryIdentifierSignatureText(identifier) {
1537
+ async unsafe_changeRecoveryIdentifierSignatureRequest(identifier) {
1513
1538
  if (!this.#client) {
1514
1539
  throw new ClientNotInitializedError();
1515
1540
  }
1516
- try {
1517
- const signatureText = await this.#client.changeRecoveryIdentifierSignatureText(identifier);
1518
- return signatureText;
1519
- }
1520
- catch {
1521
- return undefined;
1522
- }
1541
+ return this.#client.changeRecoveryIdentifierSignatureRequest(identifier);
1523
1542
  }
1524
1543
  /**
1525
- * Adds a signature for a specific request type
1544
+ * Applies a signature request to the client
1526
1545
  *
1527
1546
  * WARNING: This function should be used with caution. It is only provided
1528
1547
  * for use in special cases where the provided workflows do not meet the
@@ -1532,42 +1551,13 @@ class Client {
1532
1551
  * `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
1533
1552
  * methods instead.
1534
1553
  *
1535
- * @param signatureType - The type of signature request
1536
- * @param signatureText - The text to sign
1537
- * @param signer - The signer to use
1538
1554
  * @throws {ClientNotInitializedError} if the client is not initialized
1539
1555
  */
1540
- async unsafe_addSignature(signatureType, signatureText, signer) {
1556
+ async unsafe_applySignatureRequest(signatureRequest) {
1541
1557
  if (!this.#client) {
1542
1558
  throw new ClientNotInitializedError();
1543
1559
  }
1544
- switch (signer.type) {
1545
- case "SCW":
1546
- await this.#client.addScwSignature(signatureType, await signer.signMessage(signatureText), signer.getChainId(), signer.getBlockNumber?.());
1547
- break;
1548
- case "EOA":
1549
- await this.#client.addEcdsaSignature(signatureType, await signer.signMessage(signatureText));
1550
- break;
1551
- }
1552
- }
1553
- /**
1554
- * Applies all pending signatures
1555
- *
1556
- * WARNING: This function should be used with caution. It is only provided
1557
- * for use in special cases where the provided workflows do not meet the
1558
- * requirements of an application.
1559
- *
1560
- * It is highly recommended to use the `register`, `unsafe_addAccount`,
1561
- * `removeAccount`, `revokeAllOtherInstallations`, or `revokeInstallations`
1562
- * methods instead.
1563
- *
1564
- * @throws {ClientNotInitializedError} if the client is not initialized
1565
- */
1566
- async unsafe_applySignatures() {
1567
- if (!this.#client) {
1568
- throw new ClientNotInitializedError();
1569
- }
1570
- return this.#client.applySignatureRequests();
1560
+ return this.#client.applySignatureRequest(signatureRequest);
1571
1561
  }
1572
1562
  /**
1573
1563
  * Registers the client with the XMTP network
@@ -1578,19 +1568,12 @@ class Client {
1578
1568
  * @throws {SignerUnavailableError} if no signer is available
1579
1569
  */
1580
1570
  async register() {
1581
- if (!this.#client) {
1582
- throw new ClientNotInitializedError();
1583
- }
1584
- if (!this.#signer) {
1585
- throw new SignerUnavailableError();
1586
- }
1587
- const signatureText = await this.unsafe_createInboxSignatureText();
1588
- // if the signature text is not available, the client is already registered
1589
- if (!signatureText) {
1571
+ const signatureRequest = await this.unsafe_createInboxSignatureRequest();
1572
+ if (!signatureRequest) {
1590
1573
  return;
1591
1574
  }
1592
- await this.unsafe_addSignature(1 /* SignatureRequestType.CreateInbox */, signatureText, this.#signer);
1593
- return this.#client.registerIdentity();
1575
+ await this.unsafe_addSignature(signatureRequest);
1576
+ await this.#client?.registerIdentity(signatureRequest);
1594
1577
  }
1595
1578
  /**
1596
1579
  * Adds a new account to the client inbox
@@ -1606,27 +1589,20 @@ class Client {
1606
1589
  *
1607
1590
  * @param newAccountSigner - The signer for the new account
1608
1591
  * @param allowInboxReassign - Whether to allow inbox reassignment
1609
- * @throws {ClientNotInitializedError} if the client is not initialized
1610
1592
  * @throws {AccountAlreadyAssociatedError} if the account is already associated with an inbox ID
1611
- * @throws {GenerateSignatureError} if the signature cannot be generated
1593
+ * @throws {ClientNotInitializedError} if the client is not initialized
1612
1594
  * @throws {SignerUnavailableError} if no signer is available
1613
1595
  */
1614
1596
  async unsafe_addAccount(newAccountSigner, allowInboxReassign = false) {
1615
- if (!this.#client) {
1616
- throw new ClientNotInitializedError();
1617
- }
1618
1597
  // check for existing inbox id
1619
1598
  const identifier = await newAccountSigner.getIdentifier();
1620
1599
  const existingInboxId = await this.getInboxIdByIdentifier(identifier);
1621
1600
  if (existingInboxId && !allowInboxReassign) {
1622
1601
  throw new AccountAlreadyAssociatedError(existingInboxId);
1623
1602
  }
1624
- const signatureText = await this.unsafe_addAccountSignatureText(identifier, true);
1625
- if (!signatureText) {
1626
- throw new GenerateSignatureError(0 /* SignatureRequestType.AddWallet */);
1627
- }
1628
- await this.unsafe_addSignature(0 /* SignatureRequestType.AddWallet */, signatureText, newAccountSigner);
1629
- await this.unsafe_applySignatures();
1603
+ const signatureRequest = await this.unsafe_addAccountSignatureRequest(identifier, allowInboxReassign);
1604
+ await this.unsafe_addSignature(signatureRequest, newAccountSigner);
1605
+ await this.unsafe_applySignatureRequest(signatureRequest);
1630
1606
  }
1631
1607
  /**
1632
1608
  * Removes an account from the client's inbox
@@ -1635,22 +1611,12 @@ class Client {
1635
1611
  *
1636
1612
  * @param identifier - The identifier of the account to remove
1637
1613
  * @throws {ClientNotInitializedError} if the client is not initialized
1638
- * @throws {GenerateSignatureError} if the signature cannot be generated
1639
1614
  * @throws {SignerUnavailableError} if no signer is available
1640
1615
  */
1641
1616
  async removeAccount(identifier) {
1642
- if (!this.#client) {
1643
- throw new ClientNotInitializedError();
1644
- }
1645
- if (!this.#signer) {
1646
- throw new SignerUnavailableError();
1647
- }
1648
- const signatureText = await this.unsafe_removeAccountSignatureText(identifier);
1649
- if (!signatureText) {
1650
- throw new GenerateSignatureError(2 /* SignatureRequestType.RevokeWallet */);
1651
- }
1652
- await this.unsafe_addSignature(2 /* SignatureRequestType.RevokeWallet */, signatureText, this.#signer);
1653
- await this.unsafe_applySignatures();
1617
+ const signatureRequest = await this.unsafe_removeAccountSignatureRequest(identifier);
1618
+ await this.unsafe_addSignature(signatureRequest);
1619
+ await this.unsafe_applySignatureRequest(signatureRequest);
1654
1620
  }
1655
1621
  /**
1656
1622
  * Revokes all other installations of the client's inbox
@@ -1658,22 +1624,12 @@ class Client {
1658
1624
  * Requires a signer, use `Client.create` to create a client with a signer.
1659
1625
  *
1660
1626
  * @throws {ClientNotInitializedError} if the client is not initialized
1661
- * @throws {GenerateSignatureError} if the signature cannot be generated
1662
1627
  * @throws {SignerUnavailableError} if no signer is available
1663
1628
  */
1664
1629
  async revokeAllOtherInstallations() {
1665
- if (!this.#client) {
1666
- throw new ClientNotInitializedError();
1667
- }
1668
- if (!this.#signer) {
1669
- throw new SignerUnavailableError();
1670
- }
1671
- const signatureText = await this.unsafe_revokeAllOtherInstallationsSignatureText();
1672
- if (!signatureText) {
1673
- throw new GenerateSignatureError(3 /* SignatureRequestType.RevokeInstallations */);
1674
- }
1675
- await this.unsafe_addSignature(3 /* SignatureRequestType.RevokeInstallations */, signatureText, this.#signer);
1676
- await this.unsafe_applySignatures();
1630
+ const signatureRequest = await this.unsafe_revokeAllOtherInstallationsSignatureRequest();
1631
+ await this.unsafe_addSignature(signatureRequest);
1632
+ await this.unsafe_applySignatureRequest(signatureRequest);
1677
1633
  }
1678
1634
  /**
1679
1635
  * Revokes specific installations of the client's inbox
@@ -1683,21 +1639,46 @@ class Client {
1683
1639
  * @param installationIds - The installation IDs to revoke
1684
1640
  * @throws {ClientNotInitializedError} if the client is not initialized
1685
1641
  * @throws {SignerUnavailableError} if no signer is available
1686
- * @throws {GenerateSignatureError} if the signature cannot be generated
1687
1642
  */
1688
1643
  async revokeInstallations(installationIds) {
1689
- if (!this.#client) {
1690
- throw new ClientNotInitializedError();
1691
- }
1692
- if (!this.#signer) {
1693
- throw new SignerUnavailableError();
1694
- }
1695
- const signatureText = await this.unsafe_revokeInstallationsSignatureText(installationIds);
1696
- if (!signatureText) {
1697
- throw new GenerateSignatureError(3 /* SignatureRequestType.RevokeInstallations */);
1644
+ const signatureRequest = await this.unsafe_revokeInstallationsSignatureRequest(installationIds);
1645
+ await this.unsafe_addSignature(signatureRequest);
1646
+ await this.unsafe_applySignatureRequest(signatureRequest);
1647
+ }
1648
+ /**
1649
+ * Revokes specific installations of the client's inbox without a client
1650
+ *
1651
+ * @param env - The environment to use
1652
+ * @param signer - The signer to use
1653
+ * @param inboxId - The inbox ID to revoke installations for
1654
+ * @param installationIds - The installation IDs to revoke
1655
+ */
1656
+ static async revokeInstallations(signer, inboxId, installationIds, env) {
1657
+ const host = ApiUrls[env ?? "dev"];
1658
+ const identifier = await signer.getIdentifier();
1659
+ const signatureRequest = await revokeInstallationsSignatureRequest(host, identifier, inboxId, installationIds);
1660
+ const signatureText = await signatureRequest.signatureText();
1661
+ const signature = await signer.signMessage(signatureText);
1662
+ switch (signer.type) {
1663
+ case "SCW":
1664
+ await signatureRequest.addScwSignature(identifier, signature, signer.getChainId(), signer.getBlockNumber?.());
1665
+ break;
1666
+ case "EOA":
1667
+ await signatureRequest.addEcdsaSignature(signature);
1668
+ break;
1698
1669
  }
1699
- await this.unsafe_addSignature(3 /* SignatureRequestType.RevokeInstallations */, signatureText, this.#signer);
1700
- await this.unsafe_applySignatures();
1670
+ await applySignatureRequest(host, signatureRequest);
1671
+ }
1672
+ /**
1673
+ * Gets the inbox state for the specified inbox IDs without a client
1674
+ *
1675
+ * @param env - The environment to use
1676
+ * @param inboxIds - The inbox IDs to get the state for
1677
+ * @returns The inbox state for the specified inbox IDs
1678
+ */
1679
+ static async inboxStateFromInboxIds(inboxIds, env) {
1680
+ const host = ApiUrls[env ?? "dev"];
1681
+ return inboxStateFromInboxIds(host, inboxIds);
1701
1682
  }
1702
1683
  /**
1703
1684
  * Changes the recovery identifier for the client's inbox
@@ -1707,21 +1688,11 @@ class Client {
1707
1688
  * @param identifier - The new recovery identifier
1708
1689
  * @throws {ClientNotInitializedError} if the client is not initialized
1709
1690
  * @throws {SignerUnavailableError} if no signer is available
1710
- * @throws {GenerateSignatureError} if the signature cannot be generated
1711
1691
  */
1712
1692
  async changeRecoveryIdentifier(identifier) {
1713
- if (!this.#client) {
1714
- throw new ClientNotInitializedError();
1715
- }
1716
- if (!this.#signer) {
1717
- throw new SignerUnavailableError();
1718
- }
1719
- const signatureText = await this.unsafe_changeRecoveryIdentifierSignatureText(identifier);
1720
- if (!signatureText) {
1721
- throw new GenerateSignatureError(4 /* SignatureRequestType.ChangeRecoveryIdentifier */);
1722
- }
1723
- await this.unsafe_addSignature(4 /* SignatureRequestType.ChangeRecoveryIdentifier */, signatureText, this.#signer);
1724
- await this.unsafe_applySignatures();
1693
+ const signatureRequest = await this.unsafe_changeRecoveryIdentifierSignatureRequest(identifier);
1694
+ await this.unsafe_addSignature(signatureRequest);
1695
+ await this.unsafe_applySignatureRequest(signatureRequest);
1725
1696
  }
1726
1697
  /**
1727
1698
  * Checks if the client can message the specified identifiers
@@ -1886,8 +1857,8 @@ class Client {
1886
1857
  * @param options - Optional network options
1887
1858
  * @returns Whether the address is authorized
1888
1859
  */
1889
- static async isAddressAuthorized(inboxId, address, options) {
1890
- const host = options?.apiUrl || ApiUrls[options?.env || "dev"];
1860
+ static async isAddressAuthorized(inboxId, address, env) {
1861
+ const host = ApiUrls[env ?? "dev"];
1891
1862
  return await isAddressAuthorized(host, inboxId, address);
1892
1863
  }
1893
1864
  /**
@@ -1898,8 +1869,8 @@ class Client {
1898
1869
  * @param options - Optional network options
1899
1870
  * @returns Whether the installation is authorized
1900
1871
  */
1901
- static async isInstallationAuthorized(inboxId, installation, options) {
1902
- const host = options?.apiUrl || ApiUrls[options?.env || "dev"];
1872
+ static async isInstallationAuthorized(inboxId, installation, env) {
1873
+ const host = ApiUrls[env ?? "dev"];
1903
1874
  return await isInstallationAuthorized(host, inboxId, installation);
1904
1875
  }
1905
1876
  /**
@@ -1910,5 +1881,5 @@ class Client {
1910
1881
  }
1911
1882
  }
1912
1883
 
1913
- export { ApiUrls, Client, Conversation, Conversations, DecodedMessage, Dm, Group, HistorySyncUrls, generateInboxId, getInboxIdForIdentifier };
1884
+ export { AccountAlreadyAssociatedError, ApiUrls, Client, ClientNotInitializedError, CodecNotFoundError, Conversation, Conversations, DecodedMessage, Dm, Group, HistorySyncUrls, InboxReassignError, InvalidGroupMembershipChangeError, MissingContentTypeError, SignerUnavailableError, generateInboxId, getInboxIdForIdentifier };
1914
1885
  //# sourceMappingURL=index.js.map