@xmtp/browser-sdk 5.2.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 (54) hide show
  1. package/dist/index.d.ts +587 -670
  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 +69 -26
  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 +119 -44
  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/group.ts +25 -0
  31. package/src/types/actions/opfs.ts +66 -0
  32. package/src/types/actions/preferences.ts +6 -13
  33. package/src/types/actions/streams.ts +8 -8
  34. package/src/types/actions.ts +11 -9
  35. package/src/types/options.ts +47 -6
  36. package/src/{ClientWorkerClass.ts → utils/WorkerBridge.ts} +35 -45
  37. package/src/utils/contentTypes.ts +77 -0
  38. package/src/utils/conversions.ts +18 -588
  39. package/src/utils/createClient.ts +16 -11
  40. package/src/utils/errors.ts +13 -19
  41. package/src/utils/inboxId.ts +46 -0
  42. package/src/utils/inboxState.ts +23 -0
  43. package/src/utils/installations.ts +95 -0
  44. package/src/utils/metadata.ts +15 -0
  45. package/src/utils/signer.ts +4 -4
  46. package/src/utils/uuid.ts +8 -0
  47. package/src/workers/client.ts +191 -132
  48. package/src/workers/opfs.ts +127 -0
  49. package/dist/workers/utils.js +0 -2
  50. package/dist/workers/utils.js.map +0 -1
  51. package/src/Utils.ts +0 -143
  52. package/src/UtilsWorkerClass.ts +0 -121
  53. package/src/types/actions/utils.ts +0 -69
  54. package/src/workers/utils.ts +0 -155
package/src/Utils.ts DELETED
@@ -1,143 +0,0 @@
1
- import type { Identifier } from "@xmtp/wasm-bindings";
2
- import { v4 } from "uuid";
3
- import type { XmtpEnv } from "@/types/options";
4
- import { toSafeSigner, type Signer } from "@/utils/signer";
5
- import { UtilsWorkerClass } from "@/UtilsWorkerClass";
6
-
7
- /**
8
- * Utility class that provides helper functions for XMTP inbox IDs
9
- */
10
- export class Utils extends UtilsWorkerClass {
11
- /**
12
- * Creates a new Utils instance
13
- *
14
- * @param enableLogging - Optional flag to enable logging
15
- */
16
- constructor(enableLogging?: boolean) {
17
- const worker = new Worker(new URL("./workers/utils", import.meta.url), {
18
- type: "module",
19
- });
20
- super(worker, enableLogging ?? false);
21
- }
22
-
23
- /**
24
- * Generates an inbox ID for a given identifier
25
- *
26
- * @param identifier - The identifier to generate an inbox ID for
27
- * @returns Promise that resolves with the generated inbox ID
28
- */
29
- async generateInboxId(identifier: Identifier) {
30
- return this.sendMessage("utils.generateInboxId", {
31
- identifier,
32
- });
33
- }
34
-
35
- /**
36
- * Gets the inbox ID for a specific identifier and optional environment
37
- *
38
- * @param identifier - The identifier to get the inbox ID for
39
- * @param env - Optional XMTP environment configuration (default: "dev")
40
- * @param gatewayHost - Optional gateway host override
41
- * @returns Promise that resolves with the inbox ID for the identifier
42
- */
43
- async getInboxIdForIdentifier(
44
- identifier: Identifier,
45
- env?: XmtpEnv,
46
- gatewayHost?: string,
47
- ) {
48
- return this.sendMessage("utils.getInboxIdForIdentifier", {
49
- identifier,
50
- env,
51
- gatewayHost,
52
- });
53
- }
54
-
55
- /**
56
- * Creates signature text for revoking installations
57
- *
58
- * WARNING: This function should be used with caution. It is only provided
59
- * for use in special cases where the provided workflows do not meet the
60
- * requirements of an application.
61
- *
62
- * It is highly recommended to use the `revokeInstallations` method instead.
63
- *
64
- * @param env - The environment to use
65
- * @param identifier - The identifier to revoke installations for
66
- * @param inboxId - The inbox ID to revoke installations for
67
- * @param installationIds - The installation IDs to revoke
68
- * @param gatewayHost - Optional gateway host override
69
- * @returns The signature text and signature request ID
70
- */
71
- async revokeInstallationsSignatureText(
72
- identifier: Identifier,
73
- inboxId: string,
74
- installationIds: Uint8Array[],
75
- env?: XmtpEnv,
76
- gatewayHost?: string,
77
- ) {
78
- return this.sendMessage("utils.revokeInstallationsSignatureText", {
79
- env,
80
- identifier,
81
- inboxId,
82
- installationIds,
83
- signatureRequestId: v4(),
84
- gatewayHost,
85
- });
86
- }
87
-
88
- /**
89
- * Revokes installations for a given inbox ID
90
- *
91
- * @param env - The environment to use
92
- * @param signer - The signer to use
93
- * @param inboxId - The inbox ID to revoke installations for
94
- * @param installationIds - The installation IDs to revoke
95
- * @param gatewayHost - Optional gateway host override
96
- * @returns Promise that resolves with the result of the revoke installations operation
97
- */
98
- async revokeInstallations(
99
- signer: Signer,
100
- inboxId: string,
101
- installationIds: Uint8Array[],
102
- env?: XmtpEnv,
103
- gatewayHost?: string,
104
- ) {
105
- const identifier = await signer.getIdentifier();
106
- const { signatureText, signatureRequestId } =
107
- await this.revokeInstallationsSignatureText(
108
- identifier,
109
- inboxId,
110
- installationIds,
111
- env,
112
- gatewayHost,
113
- );
114
- const signature = await signer.signMessage(signatureText);
115
- const safeSigner = await toSafeSigner(signer, signature);
116
-
117
- return this.sendMessage("utils.revokeInstallations", {
118
- signer: safeSigner,
119
- signatureRequestId,
120
- env,
121
- gatewayHost,
122
- });
123
- }
124
-
125
- /**
126
- * Gets the inbox state for the specified inbox IDs without a client
127
- *
128
- * @param inboxIds - The inbox IDs to get the state for
129
- * @param env - The environment to use
130
- * @returns The inbox state for the specified inbox IDs
131
- */
132
- async inboxStateFromInboxIds(
133
- inboxIds: string[],
134
- env?: XmtpEnv,
135
- gatewayHost?: string,
136
- ) {
137
- return this.sendMessage("utils.inboxStateFromInboxIds", {
138
- inboxIds,
139
- env,
140
- gatewayHost,
141
- });
142
- }
143
- }
@@ -1,121 +0,0 @@
1
- import { v4 } from "uuid";
2
- import type {
3
- ActionErrorData,
4
- ActionName,
5
- ActionWithoutData,
6
- ExtractActionData,
7
- ExtractActionResult,
8
- } from "@/types/actions";
9
- import type { UtilsWorkerAction } from "@/types/actions/utils";
10
-
11
- const handleError = (event: ErrorEvent) => {
12
- console.error(event.message);
13
- };
14
-
15
- /**
16
- * Class that sets up a worker and provides communications for utility functions
17
- *
18
- * This class is not meant to be used directly, it is extended by the Utils class
19
- * to provide an interface to the worker.
20
- *
21
- * @param worker - The worker to use for the utils class
22
- * @param enableLogging - Whether to enable logging in the worker
23
- * @returns A new UtilsWorkerClass instance
24
- */
25
- export class UtilsWorkerClass {
26
- #worker: Worker;
27
-
28
- #enableLogging: boolean;
29
-
30
- #promises = new Map<
31
- string,
32
- { resolve: (value: unknown) => void; reject: (reason?: unknown) => void }
33
- >();
34
-
35
- constructor(worker: Worker, enableLogging: boolean) {
36
- this.#worker = worker;
37
- this.#worker.addEventListener("message", this.handleMessage);
38
- if (enableLogging) {
39
- this.#worker.addEventListener("error", handleError);
40
- }
41
- this.#enableLogging = enableLogging;
42
- }
43
-
44
- /**
45
- * Initializes the utils worker
46
- *
47
- * @param enableLogging - Whether to enable logging in the worker
48
- * @returns A promise that resolves when the worker is initialized
49
- */
50
- async init() {
51
- return this.sendMessage("utils.init", {
52
- enableLogging: this.#enableLogging,
53
- });
54
- }
55
-
56
- /**
57
- * Sends an action message to the utils worker
58
- *
59
- * @param action - The action to send to the worker
60
- * @param data - The data to send to the worker
61
- * @returns A promise that resolves when the action is completed
62
- */
63
- sendMessage<A extends ActionName<UtilsWorkerAction>>(
64
- action: A,
65
- data: ExtractActionData<UtilsWorkerAction, A>,
66
- ) {
67
- const promiseId = v4();
68
- this.#worker.postMessage({
69
- action,
70
- id: promiseId,
71
- data,
72
- });
73
- const promise = new Promise((resolve, reject) => {
74
- this.#promises.set(promiseId, {
75
- resolve: resolve as (value: unknown) => void,
76
- reject,
77
- });
78
- });
79
- return promise as [ExtractActionResult<UtilsWorkerAction, A>] extends [
80
- undefined,
81
- ]
82
- ? Promise<void>
83
- : Promise<ExtractActionResult<UtilsWorkerAction, A>>;
84
- }
85
-
86
- /**
87
- * Handles a message from the utils worker
88
- *
89
- * @param event - The event to handle
90
- */
91
- handleMessage = (
92
- event: MessageEvent<
93
- ActionWithoutData<UtilsWorkerAction> | ActionErrorData<UtilsWorkerAction>
94
- >,
95
- ) => {
96
- const eventData = event.data;
97
- if (this.#enableLogging) {
98
- console.log("utils received event data", eventData);
99
- }
100
- const promise = this.#promises.get(eventData.id);
101
- if (promise) {
102
- this.#promises.delete(eventData.id);
103
- if ("error" in eventData) {
104
- promise.reject(eventData.error);
105
- } else {
106
- promise.resolve(eventData.result);
107
- }
108
- }
109
- };
110
-
111
- /**
112
- * Removes all event listeners and terminates the worker
113
- */
114
- close() {
115
- this.#worker.removeEventListener("message", this.handleMessage);
116
- if (this.#enableLogging) {
117
- this.#worker.removeEventListener("error", handleError);
118
- }
119
- this.#worker.terminate();
120
- }
121
- }
@@ -1,69 +0,0 @@
1
- import type { Identifier } from "@xmtp/wasm-bindings";
2
- import type { XmtpEnv } from "@/types/options";
3
- import type { SafeInboxState } from "@/utils/conversions";
4
- import type { SafeSigner } from "@/utils/signer";
5
-
6
- export type UtilsWorkerAction =
7
- | {
8
- action: "utils.init";
9
- id: string;
10
- result: undefined;
11
- data: {
12
- enableLogging: boolean;
13
- };
14
- }
15
- | {
16
- action: "utils.generateInboxId";
17
- id: string;
18
- result: string;
19
- data: {
20
- identifier: Identifier;
21
- };
22
- }
23
- | {
24
- action: "utils.getInboxIdForIdentifier";
25
- id: string;
26
- result: string | undefined;
27
- data: {
28
- identifier: Identifier;
29
- env?: XmtpEnv;
30
- gatewayHost?: string;
31
- };
32
- }
33
- | {
34
- action: "utils.revokeInstallationsSignatureText";
35
- id: string;
36
- result: {
37
- signatureText: string;
38
- signatureRequestId: string;
39
- };
40
- data: {
41
- env?: XmtpEnv;
42
- identifier: Identifier;
43
- inboxId: string;
44
- gatewayHost?: string;
45
- installationIds: Uint8Array[];
46
- signatureRequestId: string;
47
- };
48
- }
49
- | {
50
- action: "utils.revokeInstallations";
51
- id: string;
52
- result: undefined;
53
- data: {
54
- env?: XmtpEnv;
55
- signer: SafeSigner;
56
- signatureRequestId: string;
57
- gatewayHost?: string;
58
- };
59
- }
60
- | {
61
- action: "utils.inboxStateFromInboxIds";
62
- id: string;
63
- result: SafeInboxState[];
64
- data: {
65
- inboxIds: string[];
66
- env?: XmtpEnv;
67
- gatewayHost?: string;
68
- };
69
- };
@@ -1,155 +0,0 @@
1
- import init, {
2
- applySignatureRequest,
3
- generateInboxId,
4
- getInboxIdForIdentifier as get_inbox_id_for_identifier,
5
- inboxStateFromInboxIds,
6
- revokeInstallationsSignatureRequest,
7
- type Identifier,
8
- type SignatureRequestHandle,
9
- } from "@xmtp/wasm-bindings";
10
- import { ApiUrls } from "@/constants";
11
- import type {
12
- ActionErrorData,
13
- ActionName,
14
- ActionWithoutResult,
15
- ExtractActionWithoutData,
16
- } from "@/types/actions";
17
- import type { UtilsWorkerAction } from "@/types/actions/utils";
18
- import type { XmtpEnv } from "@/types/options";
19
- import { toSafeInboxState } from "@/utils/conversions";
20
-
21
- const signatureRequests = new Map<string, SignatureRequestHandle>();
22
-
23
- /**
24
- * Type-safe postMessage
25
- */
26
- const postMessage = <A extends ActionName<UtilsWorkerAction>>(
27
- data: ExtractActionWithoutData<UtilsWorkerAction, A>,
28
- ) => {
29
- self.postMessage(data);
30
- };
31
-
32
- /**
33
- * Type-safe postMessage for errors
34
- */
35
- const postMessageError = (data: ActionErrorData<UtilsWorkerAction>) => {
36
- self.postMessage(data);
37
- };
38
-
39
- const getInboxIdForIdentifier = async (
40
- identifier: Identifier,
41
- env?: XmtpEnv,
42
- gatewayHost?: string,
43
- ) => {
44
- const host = env ? ApiUrls[env] : ApiUrls.dev;
45
- return get_inbox_id_for_identifier(host, gatewayHost ?? null, identifier);
46
- };
47
-
48
- let enableLogging = false;
49
-
50
- self.onmessage = async (
51
- event: MessageEvent<ActionWithoutResult<UtilsWorkerAction>>,
52
- ) => {
53
- const { action, id, data } = event.data;
54
-
55
- if (enableLogging) {
56
- console.log("utils worker received event data", event.data);
57
- }
58
-
59
- // initialize WASM module
60
- await init();
61
-
62
- try {
63
- switch (action) {
64
- case "utils.init": {
65
- enableLogging = data.enableLogging;
66
- postMessage({ id, action, result: undefined });
67
- break;
68
- }
69
- case "utils.generateInboxId": {
70
- const result = generateInboxId(data.identifier);
71
- postMessage({
72
- id,
73
- action,
74
- result,
75
- });
76
- break;
77
- }
78
- case "utils.getInboxIdForIdentifier": {
79
- const result = await getInboxIdForIdentifier(
80
- data.identifier,
81
- data.env,
82
- data.gatewayHost,
83
- );
84
- postMessage({ id, action, result });
85
- break;
86
- }
87
- case "utils.revokeInstallationsSignatureText": {
88
- const host = ApiUrls[data.env ?? "dev"];
89
- const signatureRequest = revokeInstallationsSignatureRequest(
90
- host,
91
- data.gatewayHost,
92
- data.identifier,
93
- data.inboxId,
94
- data.installationIds,
95
- );
96
- const signatureText = await signatureRequest.signatureText();
97
- signatureRequests.set(data.signatureRequestId, signatureRequest);
98
- const result = {
99
- signatureText,
100
- signatureRequestId: data.signatureRequestId,
101
- };
102
- postMessage({ id, action, result });
103
- break;
104
- }
105
- case "utils.revokeInstallations": {
106
- const host = ApiUrls[data.env ?? "dev"];
107
- const signatureRequest = signatureRequests.get(data.signatureRequestId);
108
- if (!signatureRequest) {
109
- throw new Error("Signature request not found");
110
- }
111
- switch (data.signer.type) {
112
- case "EOA":
113
- await signatureRequest.addEcdsaSignature(data.signer.signature);
114
- break;
115
- case "SCW":
116
- await signatureRequest.addScwSignature(
117
- data.signer.identifier,
118
- data.signer.signature,
119
- data.signer.chainId,
120
- data.signer.blockNumber,
121
- );
122
- break;
123
- }
124
- await applySignatureRequest(
125
- host,
126
- data.gatewayHost ?? null,
127
- signatureRequest,
128
- );
129
- signatureRequests.delete(data.signatureRequestId);
130
- postMessage({ id, action, result: [] });
131
- break;
132
- }
133
-
134
- case "utils.inboxStateFromInboxIds": {
135
- const host = ApiUrls[data.env ?? "dev"];
136
- try {
137
- const inboxStates = await inboxStateFromInboxIds(
138
- host,
139
- data.gatewayHost ?? null,
140
- data.inboxIds,
141
- );
142
- const result = inboxStates.map((inboxState) =>
143
- toSafeInboxState(inboxState),
144
- );
145
- postMessage({ id, action, result });
146
- } catch (e) {
147
- console.error("utils received error", e);
148
- }
149
- break;
150
- }
151
- }
152
- } catch (e) {
153
- postMessageError({ id, action, error: e as Error });
154
- }
155
- };