@xmtp/browser-sdk 5.3.0 → 6.0.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.
Files changed (53) hide show
  1. package/dist/index.d.ts +541 -671
  2. package/dist/index.js +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/workers/client.js +1 -1
  5. package/dist/workers/client.js.map +1 -1
  6. package/dist/workers/opfs.js +2 -0
  7. package/dist/workers/opfs.js.map +1 -0
  8. package/package.json +12 -16
  9. package/src/Client.ts +92 -219
  10. package/src/CodecRegistry.ts +27 -0
  11. package/src/Conversation.ts +275 -104
  12. package/src/Conversations.ts +188 -99
  13. package/src/DebugInformation.ts +10 -27
  14. package/src/DecodedMessage.ts +155 -58
  15. package/src/Dm.ts +25 -9
  16. package/src/Group.ts +30 -29
  17. package/src/Opfs.ts +63 -0
  18. package/src/Preferences.ts +68 -52
  19. package/src/WorkerClient.ts +5 -5
  20. package/src/WorkerConversation.ts +98 -45
  21. package/src/WorkerConversations.ts +35 -74
  22. package/src/WorkerDebugInformation.ts +1 -12
  23. package/src/WorkerPreferences.ts +6 -14
  24. package/src/index.ts +53 -24
  25. package/src/types/actions/client.ts +6 -17
  26. package/src/types/actions/conversation.ts +160 -31
  27. package/src/types/actions/conversations.ts +21 -24
  28. package/src/types/actions/debugInformation.ts +3 -11
  29. package/src/types/actions/dm.ts +1 -1
  30. package/src/types/actions/opfs.ts +66 -0
  31. package/src/types/actions/preferences.ts +6 -13
  32. package/src/types/actions/streams.ts +8 -8
  33. package/src/types/actions.ts +11 -9
  34. package/src/types/options.ts +47 -6
  35. package/src/{ClientWorkerClass.ts → utils/WorkerBridge.ts} +35 -45
  36. package/src/utils/contentTypes.ts +77 -0
  37. package/src/utils/conversions.ts +17 -590
  38. package/src/utils/createClient.ts +16 -11
  39. package/src/utils/errors.ts +13 -19
  40. package/src/utils/inboxId.ts +46 -0
  41. package/src/utils/inboxState.ts +23 -0
  42. package/src/utils/installations.ts +95 -0
  43. package/src/utils/metadata.ts +15 -0
  44. package/src/utils/signer.ts +4 -4
  45. package/src/utils/uuid.ts +8 -0
  46. package/src/workers/client.ts +176 -135
  47. package/src/workers/opfs.ts +127 -0
  48. package/dist/workers/utils.js +0 -2
  49. package/dist/workers/utils.js.map +0 -1
  50. package/src/Utils.ts +0 -143
  51. package/src/UtilsWorkerClass.ts +0 -121
  52. package/src/types/actions/utils.ts +0 -69
  53. package/src/workers/utils.ts +0 -155
@@ -1,5 +1,3 @@
1
- import type { ContentTypeId } from "@xmtp/content-type-primitives";
2
-
3
1
  export class ClientNotInitializedError extends Error {
4
2
  constructor() {
5
3
  super(
@@ -16,12 +14,6 @@ export class SignerUnavailableError extends Error {
16
14
  }
17
15
  }
18
16
 
19
- export class CodecNotFoundError extends Error {
20
- constructor(contentType: ContentTypeId) {
21
- super(`Codec not found for "${contentType.toString()}" content type`);
22
- }
23
- }
24
-
25
17
  export class InboxReassignError extends Error {
26
18
  constructor() {
27
19
  super(
@@ -48,27 +40,29 @@ export class StreamNotFoundError extends Error {
48
40
  }
49
41
  }
50
42
 
51
- export class InvalidGroupMembershipChangeError extends Error {
52
- constructor(messageId: string) {
53
- super(`Invalid group membership change for message ${messageId}`);
43
+ export class StreamFailedError extends Error {
44
+ constructor(retryAttempts: number) {
45
+ const times = `time${retryAttempts !== 1 ? "s" : ""}`;
46
+ super(`Stream failed, retried ${retryAttempts} ${times}`);
54
47
  }
55
48
  }
56
49
 
57
- export class MissingContentTypeError extends Error {
50
+ export class StreamInvalidRetryAttemptsError extends Error {
58
51
  constructor() {
59
- super("Content type is required when sending content other than text");
52
+ super("Stream retry attempts must be greater than 0");
60
53
  }
61
54
  }
62
55
 
63
- export class StreamFailedError extends Error {
64
- constructor(retryAttempts: number) {
65
- const times = `time${retryAttempts !== 1 ? "s" : ""}`;
66
- super(`Stream failed, retried ${retryAttempts} ${times}`);
56
+ export class OpfsNotInitializedError extends Error {
57
+ constructor() {
58
+ super("OPFS must be initialized before accessing its methods");
67
59
  }
68
60
  }
69
61
 
70
- export class StreamInvalidRetryAttemptsError extends Error {
62
+ export class OpfsInitializationError extends Error {
71
63
  constructor() {
72
- super("Stream retry attempts must be greater than 0");
64
+ super(
65
+ "Failed to initialize OPFS, ensure that there are no other active XMTP clients or Opfs instances",
66
+ );
73
67
  }
74
68
  }
@@ -0,0 +1,46 @@
1
+ import init, {
2
+ generateInboxId as wasmGenerateInboxId,
3
+ getInboxIdForIdentifier as wasmGetInboxIdForIdentifier,
4
+ type Identifier,
5
+ } from "@xmtp/wasm-bindings";
6
+ import { ApiUrls } from "@/constants";
7
+ import type { XmtpEnv } from "@/types/options";
8
+
9
+ /**
10
+ * Generates an inbox ID for a given identifier
11
+ *
12
+ * @param identifier - The identifier to generate an inbox ID for
13
+ * @param nonce - Optional nonce to use for generating the inbox ID
14
+ * @returns Promise that resolves with the generated inbox ID
15
+ */
16
+ export const generateInboxId = async (
17
+ identifier: Identifier,
18
+ nonce?: bigint,
19
+ ): Promise<string> => {
20
+ await init();
21
+ return wasmGenerateInboxId(identifier, nonce);
22
+ };
23
+
24
+ /**
25
+ * Gets the inbox ID for a specific identifier and optional environment
26
+ *
27
+ * @param identifier - The identifier to get the inbox ID for
28
+ * @param env - Optional XMTP environment configuration (default: "dev")
29
+ * @param gatewayHost - Optional gateway host override
30
+ * @returns Promise that resolves with the inbox ID for the identifier
31
+ */
32
+ export const getInboxIdForIdentifier = async (
33
+ identifier: Identifier,
34
+ env?: XmtpEnv,
35
+ gatewayHost?: string,
36
+ ): Promise<string | undefined> => {
37
+ await init();
38
+ const host = env ? ApiUrls[env] : ApiUrls.dev;
39
+ const isSecure = host.startsWith("https");
40
+ return wasmGetInboxIdForIdentifier(
41
+ host,
42
+ gatewayHost ?? null,
43
+ isSecure,
44
+ identifier,
45
+ );
46
+ };
@@ -0,0 +1,23 @@
1
+ import init, {
2
+ inboxStateFromInboxIds as wasmInboxStateFromInboxIds,
3
+ } from "@xmtp/wasm-bindings";
4
+ import { ApiUrls } from "@/constants";
5
+ import type { XmtpEnv } from "@/types/options";
6
+
7
+ /**
8
+ * Gets the inbox state for the specified inbox IDs without a client
9
+ *
10
+ * @param inboxIds - The inbox IDs to get the state for
11
+ * @param env - Optional XMTP environment configuration (default: "dev")
12
+ * @param gatewayHost - Optional gateway host override
13
+ * @returns The inbox state for the specified inbox IDs
14
+ */
15
+ export const inboxStateFromInboxIds = async (
16
+ inboxIds: string[],
17
+ env?: XmtpEnv,
18
+ gatewayHost?: string,
19
+ ) => {
20
+ await init();
21
+ const host = ApiUrls[env ?? "dev"];
22
+ return wasmInboxStateFromInboxIds(host, gatewayHost ?? null, inboxIds);
23
+ };
@@ -0,0 +1,95 @@
1
+ import init, {
2
+ applySignatureRequest,
3
+ revokeInstallationsSignatureRequest,
4
+ type Identifier,
5
+ type SignatureRequestHandle,
6
+ } from "@xmtp/wasm-bindings";
7
+ import { ApiUrls } from "@/constants";
8
+ import type { XmtpEnv } from "@/types/options";
9
+ import type { Signer } from "@/utils/signer";
10
+
11
+ /**
12
+ * Creates signature text for revoking installations
13
+ *
14
+ * WARNING: This function should be used with caution. It is only provided
15
+ * for use in special cases where the provided workflows do not meet the
16
+ * requirements of an application.
17
+ *
18
+ * It is highly recommended to use the `revokeInstallations` function instead.
19
+ *
20
+ * @param identifier - The identifier to revoke installations for
21
+ * @param inboxId - The inbox ID to revoke installations for
22
+ * @param installationIds - The installation IDs to revoke
23
+ * @param env - Optional XMTP environment configuration (default: "dev")
24
+ * @param gatewayHost - Optional gateway host override
25
+ * @returns The signature text and signature request ID
26
+ */
27
+ export const revokeInstallationsSignatureText = async (
28
+ identifier: Identifier,
29
+ inboxId: string,
30
+ installationIds: Uint8Array[],
31
+ env?: XmtpEnv,
32
+ gatewayHost?: string,
33
+ ): Promise<{
34
+ signatureText: string;
35
+ signatureRequest: SignatureRequestHandle;
36
+ }> => {
37
+ await init();
38
+ const host = ApiUrls[env ?? "dev"];
39
+ const signatureRequest = revokeInstallationsSignatureRequest(
40
+ host,
41
+ gatewayHost ?? null,
42
+ identifier,
43
+ inboxId,
44
+ installationIds,
45
+ );
46
+ const signatureText = await signatureRequest.signatureText();
47
+ return { signatureText, signatureRequest };
48
+ };
49
+
50
+ /**
51
+ * Revokes installations for a given inbox ID
52
+ *
53
+ * @param signer - The signer to use
54
+ * @param inboxId - The inbox ID to revoke installations for
55
+ * @param installationIds - The installation IDs to revoke
56
+ * @param env - Optional XMTP environment configuration (default: "dev")
57
+ * @param gatewayHost - Optional gateway host override
58
+ * @returns Promise that resolves when the revoke installations operation is complete
59
+ */
60
+ export const revokeInstallations = async (
61
+ signer: Signer,
62
+ inboxId: string,
63
+ installationIds: Uint8Array[],
64
+ env?: XmtpEnv,
65
+ gatewayHost?: string,
66
+ ): Promise<void> => {
67
+ await init();
68
+ const identifier = await signer.getIdentifier();
69
+ const { signatureText, signatureRequest } =
70
+ await revokeInstallationsSignatureText(
71
+ identifier,
72
+ inboxId,
73
+ installationIds,
74
+ env,
75
+ gatewayHost,
76
+ );
77
+ const signature = await signer.signMessage(signatureText);
78
+ const host = ApiUrls[env ?? "dev"];
79
+
80
+ switch (signer.type) {
81
+ case "EOA":
82
+ await signatureRequest.addEcdsaSignature(signature);
83
+ break;
84
+ case "SCW":
85
+ await signatureRequest.addScwSignature(
86
+ identifier,
87
+ signature,
88
+ signer.getChainId(),
89
+ signer.getBlockNumber?.(),
90
+ );
91
+ break;
92
+ }
93
+
94
+ await applySignatureRequest(host, gatewayHost ?? null, signatureRequest);
95
+ };
@@ -0,0 +1,15 @@
1
+ import init, {
2
+ metadataFieldName as wasmMetadataFieldName,
3
+ type MetadataField,
4
+ } from "@xmtp/wasm-bindings";
5
+
6
+ /**
7
+ * Gets the name of a metadata field
8
+ *
9
+ * @param field - The metadata field to get the name for
10
+ * @returns The name of the metadata field
11
+ */
12
+ export const metadataFieldName = async (field: MetadataField) => {
13
+ await init();
14
+ return wasmMetadataFieldName(field);
15
+ };
@@ -1,9 +1,9 @@
1
1
  import type { Identifier } from "@xmtp/wasm-bindings";
2
2
 
3
- export type SignMessage = (message: string) => Promise<Uint8Array> | Uint8Array;
4
- export type GetIdentifier = () => Promise<Identifier> | Identifier;
5
- export type GetChainId = () => bigint;
6
- export type GetBlockNumber = () => bigint;
3
+ type SignMessage = (message: string) => Promise<Uint8Array> | Uint8Array;
4
+ type GetIdentifier = () => Promise<Identifier> | Identifier;
5
+ type GetChainId = () => bigint;
6
+ type GetBlockNumber = () => bigint;
7
7
 
8
8
  export type Signer =
9
9
  | {
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a unique identifier using crypto.getRandomValues()
3
+ */
4
+ export const uuid = (): string => {
5
+ const bytes = new Uint8Array(16);
6
+ crypto.getRandomValues(bytes);
7
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
8
+ };