@agora-sdk/secure-chat-core 0.2.0 → 0.4.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 (35) hide show
  1. package/dist/cjs/context/secure-chat-context.d.ts +10 -0
  2. package/dist/cjs/context/secure-chat-context.js +37 -1
  3. package/dist/cjs/context/secure-chat-context.js.map +1 -1
  4. package/dist/cjs/contract/index.d.ts +2 -150
  5. package/dist/cjs/contract/index.js +11 -10
  6. package/dist/cjs/contract/index.js.map +1 -1
  7. package/dist/cjs/hooks/useSecureHandshakes.d.ts +64 -0
  8. package/dist/cjs/hooks/useSecureHandshakes.js +215 -0
  9. package/dist/cjs/hooks/useSecureHandshakes.js.map +1 -0
  10. package/dist/cjs/hooks/useSecureMessages.js +12 -2
  11. package/dist/cjs/hooks/useSecureMessages.js.map +1 -1
  12. package/dist/cjs/index.d.ts +2 -0
  13. package/dist/cjs/index.js +3 -1
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/transport/socket.d.ts +15 -3
  16. package/dist/cjs/transport/socket.js +4 -2
  17. package/dist/cjs/transport/socket.js.map +1 -1
  18. package/dist/esm/context/secure-chat-context.d.ts +10 -0
  19. package/dist/esm/context/secure-chat-context.js +37 -1
  20. package/dist/esm/context/secure-chat-context.js.map +1 -1
  21. package/dist/esm/contract/index.d.ts +2 -150
  22. package/dist/esm/contract/index.js +11 -10
  23. package/dist/esm/contract/index.js.map +1 -1
  24. package/dist/esm/hooks/useSecureHandshakes.d.ts +64 -0
  25. package/dist/esm/hooks/useSecureHandshakes.js +212 -0
  26. package/dist/esm/hooks/useSecureHandshakes.js.map +1 -0
  27. package/dist/esm/hooks/useSecureMessages.js +12 -2
  28. package/dist/esm/hooks/useSecureMessages.js.map +1 -1
  29. package/dist/esm/index.d.ts +2 -0
  30. package/dist/esm/index.js +1 -0
  31. package/dist/esm/index.js.map +1 -1
  32. package/dist/esm/transport/socket.d.ts +15 -3
  33. package/dist/esm/transport/socket.js +4 -2
  34. package/dist/esm/transport/socket.js.map +1 -1
  35. package/package.json +3 -2
@@ -6,6 +6,8 @@ export { useSecureConversations } from "./hooks/useSecureConversations.js";
6
6
  export type { UseSecureConversationsValues } from "./hooks/useSecureConversations.js";
7
7
  export { useSecureMessages } from "./hooks/useSecureMessages.js";
8
8
  export type { UseSecureMessagesOptions, UseSecureMessagesValues, DecryptedSecureMessage, } from "./hooks/useSecureMessages.js";
9
+ export { useSecureHandshakes } from "./hooks/useSecureHandshakes.js";
10
+ export type { UseSecureHandshakesOptions, UseSecureHandshakesValues, } from "./hooks/useSecureHandshakes.js";
9
11
  export { SecureChatRestClient } from "./transport/rest.js";
10
12
  export type { SecureChatRestConfig } from "./transport/rest.js";
11
13
  export { SecureChatSocketClient } from "./transport/socket.js";
package/dist/cjs/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  // injection of a `SecureChatCrypto`. Platform packages (@agora-sdk/secure-chat-react-js, etc.)
6
6
  // re-export this and add the concrete crypto + persistence.
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.bytesToUtf8 = exports.utf8ToBytes = exports.fromBase64 = exports.toBase64 = exports.SecureChatRepository = exports.MemoryStore = exports.SecureChatSocketClient = exports.SecureChatRestClient = exports.useSecureMessages = exports.useSecureConversations = exports.useSecureDevice = exports.useSecureChat = exports.SecureChatProvider = void 0;
8
+ exports.bytesToUtf8 = exports.utf8ToBytes = exports.fromBase64 = exports.toBase64 = exports.SecureChatRepository = exports.MemoryStore = exports.SecureChatSocketClient = exports.SecureChatRestClient = exports.useSecureHandshakes = exports.useSecureMessages = exports.useSecureConversations = exports.useSecureDevice = exports.useSecureChat = exports.SecureChatProvider = void 0;
9
9
  // ── context / provider ──────────────────────────────────────────────────────
10
10
  var secure_chat_context_js_1 = require("./context/secure-chat-context.js");
11
11
  Object.defineProperty(exports, "SecureChatProvider", { enumerable: true, get: function () { return secure_chat_context_js_1.SecureChatProvider; } });
@@ -17,6 +17,8 @@ var useSecureConversations_js_1 = require("./hooks/useSecureConversations.js");
17
17
  Object.defineProperty(exports, "useSecureConversations", { enumerable: true, get: function () { return useSecureConversations_js_1.useSecureConversations; } });
18
18
  var useSecureMessages_js_1 = require("./hooks/useSecureMessages.js");
19
19
  Object.defineProperty(exports, "useSecureMessages", { enumerable: true, get: function () { return useSecureMessages_js_1.useSecureMessages; } });
20
+ var useSecureHandshakes_js_1 = require("./hooks/useSecureHandshakes.js");
21
+ Object.defineProperty(exports, "useSecureHandshakes", { enumerable: true, get: function () { return useSecureHandshakes_js_1.useSecureHandshakes; } });
20
22
  // ── transport (for advanced / non-React use) ─────────────────────────────────
21
23
  var rest_js_1 = require("./transport/rest.js");
22
24
  Object.defineProperty(exports, "SecureChatRestClient", { enumerable: true, get: function () { return rest_js_1.SecureChatRestClient; } });
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,8FAA8F;AAC9F,EAAE;AACF,6FAA6F;AAC7F,+FAA+F;AAC/F,4DAA4D;;;AAE5D,+EAA+E;AAC/E,2EAAqF;AAA5E,4HAAA,kBAAkB,OAAA;AAAE,uHAAA,aAAa,OAAA;AAM1C,gFAAgF;AAChF,iEAA6D;AAApD,qHAAA,eAAe,OAAA;AAExB,+EAA2E;AAAlE,mIAAA,sBAAsB,OAAA;AAE/B,qEAAiE;AAAxD,yHAAA,iBAAiB,OAAA;AAO1B,gFAAgF;AAChF,+CAA2D;AAAlD,+GAAA,oBAAoB,OAAA;AAE7B,mDAA+D;AAAtD,mHAAA,sBAAsB,OAAA;AAwB/B,iEAA4D;AAAnD,8GAAA,WAAW,OAAA;AACpB,6DAAmE;AAA1D,qHAAA,oBAAoB,OAAA;AAG7B,gFAAgF;AAChF,8CAAkF;AAAzE,qGAAA,QAAQ,OAAA;AAAE,uGAAA,UAAU,OAAA;AAAE,wGAAA,WAAW,OAAA;AAAE,wGAAA,WAAW,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,8FAA8F;AAC9F,EAAE;AACF,6FAA6F;AAC7F,+FAA+F;AAC/F,4DAA4D;;;AAE5D,+EAA+E;AAC/E,2EAAqF;AAA5E,4HAAA,kBAAkB,OAAA;AAAE,uHAAA,aAAa,OAAA;AAM1C,gFAAgF;AAChF,iEAA6D;AAApD,qHAAA,eAAe,OAAA;AAExB,+EAA2E;AAAlE,mIAAA,sBAAsB,OAAA;AAE/B,qEAAiE;AAAxD,yHAAA,iBAAiB,OAAA;AAM1B,yEAAqE;AAA5D,6HAAA,mBAAmB,OAAA;AAM5B,gFAAgF;AAChF,+CAA2D;AAAlD,+GAAA,oBAAoB,OAAA;AAE7B,mDAA+D;AAAtD,mHAAA,sBAAsB,OAAA;AAwB/B,iEAA4D;AAAnD,8GAAA,WAAW,OAAA;AACpB,6DAAmE;AAA1D,qHAAA,oBAAoB,OAAA;AAG7B,gFAAgF;AAChF,8CAAkF;AAAzE,qGAAA,QAAQ,OAAA;AAAE,uGAAA,UAAU,OAAA;AAAE,wGAAA,WAAW,OAAA;AAAE,wGAAA,WAAW,OAAA"}
@@ -26,10 +26,22 @@ export interface SecureServerEvents {
26
26
  userId: string;
27
27
  }) => void;
28
28
  }
29
- /** Client → server events. */
29
+ /**
30
+ * Client → server events.
31
+ *
32
+ * @remarks
33
+ * Payloads are **objects**, not bare ids — the server destructures `{ conversationId }` /
34
+ * `{ deviceId }` off the first argument (agora-server `realtime/secure-socket.ts`). Emitting a bare
35
+ * string lands as `undefined` after destructuring (and, on a `null` payload, throws server-side), so
36
+ * the join silently fails. Keep these shapes in lockstep with the server's `SecureClientToServerEvents`.
37
+ */
30
38
  export interface SecureClientEvents {
31
- "join:secure-conversation": (conversationId: string) => void;
32
- "join:secure-device": (deviceId: string) => void;
39
+ "join:secure-conversation": (payload: {
40
+ conversationId: string;
41
+ }) => void;
42
+ "join:secure-device": (payload: {
43
+ deviceId: string;
44
+ }) => void;
33
45
  }
34
46
  /** A socket.io `Socket` typed with the secure-chat event maps in both directions. */
35
47
  export type SecureSocket = Socket<SecureServerEvents, SecureClientEvents>;
@@ -46,11 +46,13 @@ class SecureChatSocketClient {
46
46
  }
47
47
  /** Join a conversation room (membership-gated server-side) to receive its broadcasts. */
48
48
  joinConversation(conversationId) {
49
- this.connect().emit("join:secure-conversation", conversationId);
49
+ // Object payload — the server destructures `{ conversationId }`; a bare string would arrive as
50
+ // `undefined` and the join would silently no-op (see SecureClientEvents).
51
+ this.connect().emit("join:secure-conversation", { conversationId });
50
52
  }
51
53
  /** Explicitly join a device room (ownership-verified). Owned devices auto-join on connect. */
52
54
  joinDevice(deviceId) {
53
- this.connect().emit("join:secure-device", deviceId);
55
+ this.connect().emit("join:secure-device", { deviceId });
54
56
  }
55
57
  /**
56
58
  * Subscribe to a server → client event, auto-connecting if needed.
@@ -1 +1 @@
1
- {"version":3,"file":"socket.js","sourceRoot":"","sources":["../../../src/transport/socket.ts"],"names":[],"mappings":";AAAA,8FAA8F;AAC9F,EAAE;AACF,iGAAiG;AACjG,kGAAkG;AAClG,8FAA8F;AAC9F,kCAAkC;;;AAElC,uDAA8C;AAqC9C;;;;GAIG;AACH,MAAa,sBAAsB;IAGjC,YAA6B,MAA8B;QAA9B,WAAM,GAAN,MAAM,CAAwB;QAFnD,WAAM,GAAwB,IAAI,CAAC;IAEmB,CAAC;IAE/D;;;;OAIG;IACH,OAAO;QACL,IAAI,IAAI,CAAC,MAAM,EAAE,SAAS;YAAE,OAAO,IAAI,CAAC,MAAM,CAAC;QAC/C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,MAAM,GAAG,IAAA,qBAAE,EAAC,GAAG,MAAM,SAAS,EAAE;YACnC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,EAAE;YAC7C,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,CAAC,OAAO,EAAE,CAAC;QACzB,4FAA4F;QAC5F,CAAC,CAAC,EAAE,CAAC,KAAc,EAAE,OAAgB,CAAC,CAAC;QACvC,OAAO,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAc,EAAE,OAAgB,CAAC,CAAC;IACvD,CAAC;CACF;AA1DD,wDA0DC"}
@@ -21,6 +21,16 @@ export interface SecureChatContextValue {
21
21
  resolveGroup: (conversationId: string) => Promise<GroupHandle | null>;
22
22
  /** Cache + persist a conversation's group handle (after createGroup / processWelcome). */
23
23
  rememberGroup: (conversationId: string, handle: GroupHandle) => Promise<void>;
24
+ /**
25
+ * Current change-version for a conversation's group handle. Bumps every time the handle advances
26
+ * (a join or a processed Commit), so consumers can detect "the group moved" without diffing handles.
27
+ */
28
+ getGroupVersion: (conversationId: string) => number;
29
+ /**
30
+ * Subscribe to group-handle changes across all conversations (fired by {@link rememberGroup}).
31
+ * @returns An unsubscribe function.
32
+ */
33
+ subscribeGroupChange: (listener: () => void) => () => void;
24
34
  /** The Agora project id these clients are scoped to. */
25
35
  projectId: string;
26
36
  }
@@ -47,6 +47,12 @@ export function SecureChatProvider({ crypto, projectId, store, accessToken, getA
47
47
  // NOTE: not cleared on a `store` prop swap — a store change in practice means a new provider
48
48
  // instance (fresh cache), not a live prop change on the same mounted provider.
49
49
  const groupCache = useRef(new Map());
50
+ // Per-conversation change counter, bumped whenever a group handle advances (a join or a processed
51
+ // Commit). Lets useSecureMessages re-resolve and flush buffered (undecrypted) rows without a
52
+ // re-fetch. Held in a ref + listener set so a bump notifies consumers WITHOUT re-rendering the
53
+ // provider (and thus rebuilding rest/socket/repo).
54
+ const groupVersion = useRef(new Map());
55
+ const groupListeners = useRef(new Set());
50
56
  const resolveGroup = useCallback(async (conversationId) => {
51
57
  const cached = groupCache.current.get(conversationId);
52
58
  if (cached)
@@ -62,11 +68,41 @@ export function SecureChatProvider({ crypto, projectId, store, accessToken, getA
62
68
  groupCache.current.set(conversationId, handle);
63
69
  const bytes = await crypto.exportGroupState(handle);
64
70
  await repo.saveGroupState(conversationId, bytes);
71
+ // Signal that this conversation's group advanced, so message hooks re-resolve + flush.
72
+ groupVersion.current.set(conversationId, (groupVersion.current.get(conversationId) ?? 0) + 1);
73
+ groupListeners.current.forEach((l) => l());
65
74
  }, [repo, crypto]);
75
+ const getGroupVersion = useCallback((conversationId) => groupVersion.current.get(conversationId) ?? 0, []);
76
+ const subscribeGroupChange = useCallback((listener) => {
77
+ groupListeners.current.add(listener);
78
+ return () => {
79
+ groupListeners.current.delete(listener);
80
+ };
81
+ }, []);
66
82
  useEffect(() => {
67
83
  return () => socket.disconnect();
68
84
  }, [socket]);
69
- const value = useMemo(() => ({ rest, socket, crypto, repo, resolveGroup, rememberGroup, projectId }), [rest, socket, crypto, repo, resolveGroup, rememberGroup, projectId]);
85
+ const value = useMemo(() => ({
86
+ rest,
87
+ socket,
88
+ crypto,
89
+ repo,
90
+ resolveGroup,
91
+ rememberGroup,
92
+ getGroupVersion,
93
+ subscribeGroupChange,
94
+ projectId,
95
+ }), [
96
+ rest,
97
+ socket,
98
+ crypto,
99
+ repo,
100
+ resolveGroup,
101
+ rememberGroup,
102
+ getGroupVersion,
103
+ subscribeGroupChange,
104
+ projectId,
105
+ ]);
70
106
  return _jsx(SecureChatContext.Provider, { value: value, children: children });
71
107
  }
72
108
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"secure-chat-context.js","sourceRoot":"","sources":["../../../src/context/secure-chat-context.tsx"],"names":[],"mappings":";AAAA,yFAAyF;AACzF,EAAE;AACF,gGAAgG;AAChG,kGAAkG;AAClG,+FAA+F;AAC/F,mGAAmG;AACnG,gEAAgE;AAEhE,OAAc,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAClG,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG9D,OAAO,EAAE,oB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AG,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC1C,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;IAC/E,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -1,150 +1,2 @@
1
- export type SecureConversationType = "dm" | "group" | "channel";
2
- export type SecureMemberRole = "admin" | "member";
3
- export type SecureHandshakeKind = "welcome" | "commit" | "proposal";
4
- /** A Welcome targeted at the ONE device whose claimed KeyPackage was consumed. */
5
- export interface WelcomeEnvelope {
6
- targetDeviceId: string;
7
- payload: string;
8
- epoch: string;
9
- }
10
- /** A Commit/Proposal broadcast to the whole group (no target device). */
11
- export interface HandshakeBlob {
12
- payload: string;
13
- epoch: string;
14
- }
15
- export interface RegisterDeviceBody {
16
- deviceId: string;
17
- displayName?: string | null;
18
- signaturePublicKey: string;
19
- credential: string;
20
- ciphersuite: number;
21
- }
22
- export interface PublishKeyPackagesBody {
23
- keyPackages: {
24
- keyPackageRef: string;
25
- keyPackage: string;
26
- ciphersuite: number;
27
- expiresAt?: string | null;
28
- }[];
29
- }
30
- export interface CreateSecureConversationBody {
31
- type: SecureConversationType;
32
- mlsGroupId: string;
33
- spaceId?: string | null;
34
- name?: string | null;
35
- memberUserIds?: string[];
36
- welcomes?: WelcomeEnvelope[];
37
- }
38
- export interface AddSecureMemberBody {
39
- userId: string;
40
- commit: HandshakeBlob;
41
- welcomes: WelcomeEnvelope[];
42
- }
43
- export interface RemoveSecureMemberBody {
44
- commit: HandshakeBlob;
45
- }
46
- export interface SendSecureMessageBody {
47
- ciphertext: string;
48
- epoch: string;
49
- senderDeviceId: string;
50
- contentType?: string | null;
51
- }
52
- export interface UploadKeyBackupBody {
53
- deviceId?: string | null;
54
- blob: string;
55
- nonce: string;
56
- kdf: "argon2id" | "pbkdf2";
57
- kdfParams: Record<string, unknown>;
58
- cipher: "xchacha20poly1305" | "aes-256-gcm";
59
- version: number;
60
- }
61
- export interface SecureDeviceModel {
62
- id: string;
63
- projectId: string;
64
- userId: string;
65
- deviceId: string;
66
- displayName: string | null;
67
- signaturePublicKey: string;
68
- credential: string;
69
- ciphersuite: number;
70
- revokedAt: string | null;
71
- lastSeenAt: string | null;
72
- createdAt: string;
73
- updatedAt: string;
74
- }
75
- export interface SecureKeyPackageClaim {
76
- deviceId: string;
77
- keyPackageRef: string;
78
- keyPackage: string;
79
- ciphersuite: number;
80
- }
81
- export interface SecureConversationMemberModel {
82
- id: string;
83
- projectId: string;
84
- conversationId: string;
85
- userId: string;
86
- role: SecureMemberRole;
87
- isActive: boolean;
88
- joinedAtEpoch: string | null;
89
- lastReadAt: string | null;
90
- leftAt: string | null;
91
- createdAt: string;
92
- updatedAt: string;
93
- }
94
- export interface SecureConversationModel {
95
- id: string;
96
- projectId: string;
97
- type: SecureConversationType;
98
- mlsGroupId: string;
99
- spaceId: string | null;
100
- currentEpoch: string;
101
- name: string | null;
102
- createdById: string | null;
103
- lastMessageAt: string | null;
104
- memberCount?: number;
105
- unreadCount?: number;
106
- currentMember?: SecureConversationMemberModel;
107
- createdAt: string;
108
- updatedAt: string;
109
- }
110
- export interface SecureMessageModel {
111
- id: string;
112
- projectId: string;
113
- conversationId: string;
114
- senderUserId: string | null;
115
- senderDeviceId: string | null;
116
- epoch: string;
117
- ciphertext: string;
118
- contentType: string;
119
- createdAt: string;
120
- }
121
- export interface SecureHandshakeModel {
122
- id: string;
123
- seq: string;
124
- kind: SecureHandshakeKind;
125
- conversationId: string;
126
- epoch: string;
127
- payload: string;
128
- senderDeviceId: string | null;
129
- targetDeviceId: string | null;
130
- }
131
- export interface SecureKeyBackupModel {
132
- id: string;
133
- projectId: string;
134
- userId: string;
135
- deviceId: string | null;
136
- blob: string;
137
- nonce: string;
138
- kdf: string;
139
- kdfParams: Record<string, unknown>;
140
- cipher: string;
141
- version: number;
142
- createdAt: string;
143
- updatedAt: string;
144
- }
145
- /** Standard error envelope: `{ error, code, field? }` with `secure-chat/*` codes. */
146
- export interface SecureChatErrorBody {
147
- error: string;
148
- code: string;
149
- field?: string;
150
- }
1
+ export type { SecureDeviceModel, SecureKeyPackageClaim, SecureConversationMemberModel, SecureConversationModel, SecureMessageModel, SecureHandshakeModel, SecureKeyBackupModel, } from "@agora-server/contract";
2
+ export type { RegisterDeviceBody, PublishKeyPackagesBody, CreateSecureConversationBody, AddSecureMemberBody, RemoveSecureMemberBody, SendSecureMessageBody, UploadKeyBackupBody, WelcomeEnvelope, HandshakeBlob, } from "@agora-server/contract";
@@ -1,15 +1,16 @@
1
- // Secure-chat wire contractstand-in for @agora-server/contract (the Apache-2.0 server contract).
1
+ // Secure-chat wire typesre-exported from the published `@agora-server/contract` (Apache-2.0).
2
2
  //
3
- // Mirrors the response models + request bodies of agora-server's
4
- // `packages/contract/src/secure-chat.ts`. That package is the source of truth (zod + TS); the
5
- // client only needs the TypeScript shapes, so this copy is types-only.
3
+ // The dependency arrow is **SDK contract**: agora-server owns the wire contract, this SDK depends
4
+ // on it. This module used to hold a byte-faithful *copy* of the types (a stand-in until the contract
5
+ // published); now that `@agora-server/contract` is published, it is a thin **type-only re-export** so
6
+ // there is exactly one source of truth and zero drift. The internal import path
7
+ // (`../contract/index.js`) is kept stable so call sites don't churn, and the re-export is scoped to
8
+ // the secure-chat surface (not the contract's reactions/pagination/etc.).
6
9
  //
7
- // ⚠️ Keep this byte-faithful to the server contract. The arrow is SDK contract: once
8
- // `@agora-server/contract` is published, DELETE this file and `import type { ... } from "@agora-server/contract"`.
9
- // Do NOT publish a separate `@agora-sdk/secure-chat-contract` (that would invert the dependency —
10
- // see STATUS.md). Do not let this copy drift in the meantime.
10
+ // Type-only on purpose: `@agora-server/contract` is ESM-only, but these `export type` re-exports are
11
+ // erased from the emitted JS, so core's dual ESM/CJS build never `require()`s it at runtime.
11
12
  //
12
- // Wire conventions: every binary value is **base64** (the server stores/relays opaque blobs and
13
- // never parses them); MLS epochs are **decimal strings** (u64 exceeds JS safe-int range).
13
+ // Wire conventions (owned by the contract): every binary value is **base64**; MLS epochs are
14
+ // **decimal strings** (u64 exceeds JS safe-int range).
14
15
  export {};
15
16
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/contract/index.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,EAAE;AACF,iEAAiE;AACjE,8FAA8F;AAC9F,uEAAuE;AACvE,EAAE;AACF,uFAAuF;AACvF,mHAAmH;AACnH,kGAAkG;AAClG,8DAA8D;AAC9D,EAAE;AACF,gGAAgG;AAChG,0FAA0F"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/contract/index.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,EAAE;AACF,oGAAoG;AACpG,qGAAqG;AACrG,sGAAsG;AACtG,gFAAgF;AAChF,oGAAoG;AACpG,0EAA0E;AAC1E,EAAE;AACF,qGAAqG;AACrG,6FAA6F;AAC7F,EAAE;AACF,6FAA6F;AAC7F,uDAAuD"}
@@ -0,0 +1,64 @@
1
+ import { SecureHandshakeModel } from "../contract/index.js";
2
+ /** Options for {@link useSecureHandshakes}. */
3
+ export interface UseSecureHandshakesOptions {
4
+ /**
5
+ * The device row id whose inbox to drain. Defaults to the persisted device's `.id`. Pass the id
6
+ * from `useSecureDevice` so processing starts the moment the device registers (a persisted device
7
+ * is picked up automatically on reload even without this).
8
+ */
9
+ deviceId?: string;
10
+ /** Disable processing (e.g. before sign-in). Default true. */
11
+ enabled?: boolean;
12
+ /** Page size for the catch-up fetch loop. Default 100. */
13
+ pageSize?: number;
14
+ /** Called per handshake-processing error; the inbox skips the bad row and continues. */
15
+ onError?: (err: unknown, handshake?: SecureHandshakeModel) => void;
16
+ }
17
+ /** The state and actions returned by {@link useSecureHandshakes}. */
18
+ export interface UseSecureHandshakesValues {
19
+ /** True until the initial catch-up loop settles. */
20
+ catchingUp: boolean;
21
+ /** True once catch-up completed and live subscriptions are active. */
22
+ ready: boolean;
23
+ /** The last processed delivery cursor (`seq`), or `null`. */
24
+ cursor: string | null;
25
+ /** Count of handshakes applied this session. */
26
+ processedCount: number;
27
+ /** The last error surfaced (also delivered via `onError`), or `null`. */
28
+ error: unknown;
29
+ /**
30
+ * Re-run the catch-up loop from the persisted cursor. The reusable primitive a future 409
31
+ * epoch-conflict rebase (on a membership Commit) calls before rebuilding + retrying its Commit.
32
+ */
33
+ resync: () => Promise<void>;
34
+ }
35
+ /**
36
+ * Process this device's MLS handshake inbox so the recipient side of secure chat works.
37
+ *
38
+ * On mount (once a device id is available) it catches up via `fetchHandshakes(since=cursor)`, re-joins
39
+ * the socket rooms for groups it already holds, then processes live `secure:welcome` /
40
+ * `secure:handshake` events — all serialized in `seq` order, deduped, and cursor-persisted. Mount it
41
+ * exactly once near `useSecureDevice`.
42
+ *
43
+ * @param options - {@link UseSecureHandshakesOptions}.
44
+ * @returns {@link UseSecureHandshakesValues}.
45
+ *
46
+ * @remarks
47
+ * Single-device only (Phase 3 adds own-device fan-out + MLS generation-counter gap detection). A
48
+ * Commit for a group whose Welcome hasn't been seen is skipped — `seq` ordering puts the Welcome
49
+ * first, so an unknown group means this device is genuinely not a member.
50
+ *
51
+ * Mount it exactly once. Processing is serialized *within* a mount, and the cursor is re-read from
52
+ * storage at the start of each run, so a serialized re-mount (or `deviceId` change) resumes without
53
+ * reprocessing. It does NOT serialize across a *concurrent* re-mount whose prior catch-up is still
54
+ * in flight; that case relies on `processWelcome` / `processCommit` being idempotent for a replayed
55
+ * blob. The mock is idempotent (so dev StrictMode double-invoke is harmless); harden this when the
56
+ * real MLS core lands (Task 1) if its handshake processing is not replay-safe.
57
+ *
58
+ * @example
59
+ * ```tsx
60
+ * const { device } = useSecureDevice();
61
+ * useSecureHandshakes({ deviceId: device?.id });
62
+ * ```
63
+ */
64
+ export declare function useSecureHandshakes(options?: UseSecureHandshakesOptions): UseSecureHandshakesValues;
@@ -0,0 +1,212 @@
1
+ // useSecureHandshakes — drain and process this device's MLS handshake inbox (the recipient side).
2
+ //
3
+ // The blind DS delivers each device a stream of handshakes (Welcomes targeted at it, plus broadcast
4
+ // Commits/Proposals for its groups), ordered by a monotonic `seq`. This hook is what makes a
5
+ // RECIPIENT actually join and stay current: on connect it pulls `GET .../handshakes?since=<cursor>`
6
+ // to the end, then processes live `secure:welcome` / `secure:handshake` events — all funneled through
7
+ // ONE serialized, seq-ordered, idempotent path, persisting the cursor as it goes. A processed Welcome
8
+ // joins a group (rememberGroup); a processed Commit advances its epoch. Mount it once, near
9
+ // useSecureDevice.
10
+ //
11
+ // Ordering invariant: live events that arrive WHILE catching up are buffered and replayed in `seq`
12
+ // order AFTER catch-up — otherwise a high-seq live event would advance the cursor past not-yet-fetched
13
+ // rows and the dedupe check would drop them (data loss).
14
+ import { useCallback, useEffect, useRef, useState } from "react";
15
+ import { fromBase64 } from "../util/base64.js";
16
+ import { useSecureChat } from "../context/secure-chat-context.js";
17
+ /** Compare two decimal-string `seq` cursors numerically (string compare is wrong across digit widths). */
18
+ function compareSeq(a, b) {
19
+ const d = BigInt(a) - BigInt(b);
20
+ return d > 0n ? 1 : d < 0n ? -1 : 0;
21
+ }
22
+ /**
23
+ * Process this device's MLS handshake inbox so the recipient side of secure chat works.
24
+ *
25
+ * On mount (once a device id is available) it catches up via `fetchHandshakes(since=cursor)`, re-joins
26
+ * the socket rooms for groups it already holds, then processes live `secure:welcome` /
27
+ * `secure:handshake` events — all serialized in `seq` order, deduped, and cursor-persisted. Mount it
28
+ * exactly once near `useSecureDevice`.
29
+ *
30
+ * @param options - {@link UseSecureHandshakesOptions}.
31
+ * @returns {@link UseSecureHandshakesValues}.
32
+ *
33
+ * @remarks
34
+ * Single-device only (Phase 3 adds own-device fan-out + MLS generation-counter gap detection). A
35
+ * Commit for a group whose Welcome hasn't been seen is skipped — `seq` ordering puts the Welcome
36
+ * first, so an unknown group means this device is genuinely not a member.
37
+ *
38
+ * Mount it exactly once. Processing is serialized *within* a mount, and the cursor is re-read from
39
+ * storage at the start of each run, so a serialized re-mount (or `deviceId` change) resumes without
40
+ * reprocessing. It does NOT serialize across a *concurrent* re-mount whose prior catch-up is still
41
+ * in flight; that case relies on `processWelcome` / `processCommit` being idempotent for a replayed
42
+ * blob. The mock is idempotent (so dev StrictMode double-invoke is harmless); harden this when the
43
+ * real MLS core lands (Task 1) if its handshake processing is not replay-safe.
44
+ *
45
+ * @example
46
+ * ```tsx
47
+ * const { device } = useSecureDevice();
48
+ * useSecureHandshakes({ deviceId: device?.id });
49
+ * ```
50
+ */
51
+ export function useSecureHandshakes(options = {}) {
52
+ const { rest, crypto, socket, repo, resolveGroup, rememberGroup } = useSecureChat();
53
+ const { deviceId: deviceIdOption, pageSize = 100 } = options;
54
+ const enabled = options.enabled ?? true;
55
+ const [catchingUp, setCatchingUp] = useState(true);
56
+ const [ready, setReady] = useState(false);
57
+ const [cursor, setCursor] = useState(null);
58
+ const [processedCount, setProcessedCount] = useState(0);
59
+ const [error, setError] = useState(null);
60
+ // onError read through a ref so passing an inline callback doesn't re-run the effect.
61
+ const onErrorRef = useRef(options.onError);
62
+ onErrorRef.current = options.onError;
63
+ // The catch-up routine, published from the effect so `resync()` (stable) can invoke it.
64
+ const runCatchUpRef = useRef(null);
65
+ const resync = useCallback(async () => {
66
+ await runCatchUpRef.current?.();
67
+ }, []);
68
+ useEffect(() => {
69
+ if (!enabled)
70
+ return;
71
+ let alive = true;
72
+ const offFns = [];
73
+ // Async state shared across the catch-up loop, live handlers, and the serial apply queue.
74
+ const cursorRef = { current: null };
75
+ const catchingUpRef = { current: true };
76
+ const liveBuffer = [];
77
+ let queue = Promise.resolve();
78
+ let deviceId;
79
+ const report = (err, h) => {
80
+ if (alive)
81
+ setError(err);
82
+ onErrorRef.current?.(err, h);
83
+ };
84
+ const dispatchByKind = async (h) => {
85
+ const payload = fromBase64(h.payload);
86
+ if (h.kind === "welcome") {
87
+ // Targeted at us → join the group, then join its room for future broadcast Commits.
88
+ if (h.targetDeviceId && h.targetDeviceId !== deviceId)
89
+ return;
90
+ const handle = await crypto.processWelcome(payload);
91
+ await rememberGroup(h.conversationId, handle);
92
+ socket.joinConversation(h.conversationId);
93
+ }
94
+ else if (h.kind === "commit") {
95
+ const group = await resolveGroup(h.conversationId);
96
+ if (!group) {
97
+ // Unknown group at a Commit: with seq ordering the Welcome precedes it, so we're not a
98
+ // member of this conversation. Skip (the cursor still advances so we don't re-fetch it).
99
+ report(new Error(`secure-chat: commit for unknown group ${h.conversationId}`), h);
100
+ return;
101
+ }
102
+ const advanced = await crypto.processCommit(group, payload);
103
+ await rememberGroup(h.conversationId, advanced);
104
+ }
105
+ else if (h.kind === "proposal") {
106
+ const group = await resolveGroup(h.conversationId);
107
+ if (group)
108
+ await crypto.processProposal(group, payload);
109
+ }
110
+ };
111
+ // The ONE place that mutates the cursor + crypto state. Dedupes by seq; advances the cursor even
112
+ // when a row is skipped or its dispatch throws, so a poison blob can never wedge the inbox. A hard
113
+ // crash mid-dispatch leaves the cursor unsaved, so the row replays on restart (no loss).
114
+ const applyOrdered = async (h) => {
115
+ if (cursorRef.current !== null && compareSeq(h.seq, cursorRef.current) <= 0)
116
+ return;
117
+ try {
118
+ await dispatchByKind(h);
119
+ }
120
+ catch (err) {
121
+ report(err, h);
122
+ }
123
+ cursorRef.current = h.seq;
124
+ await repo.saveHandshakeCursor(h.seq);
125
+ if (alive) {
126
+ setCursor(h.seq);
127
+ setProcessedCount((n) => n + 1);
128
+ }
129
+ };
130
+ const schedule = (h) => {
131
+ queue = queue.then(() => applyOrdered(h));
132
+ return queue;
133
+ };
134
+ const enqueueLive = (h) => {
135
+ // Buffer while catching up so a high-seq live event can't advance the cursor past rows the
136
+ // catch-up loop hasn't fetched yet (which the dedupe check would then drop).
137
+ if (catchingUpRef.current)
138
+ liveBuffer.push(h);
139
+ else
140
+ schedule(h);
141
+ };
142
+ // Coalesce overlapping invocations: a `resync()` fired while a catch-up is still draining returns
143
+ // the in-flight promise instead of starting a second drain (two drains would race the
144
+ // `catchingUpRef` gate + `liveBuffer` and could break seq ordering).
145
+ let catchUpInFlight = null;
146
+ const runCatchUp = () => {
147
+ if (catchUpInFlight)
148
+ return catchUpInFlight;
149
+ catchUpInFlight = (async () => {
150
+ catchingUpRef.current = true;
151
+ try {
152
+ for (;;) {
153
+ const page = await rest.fetchHandshakes(deviceId, {
154
+ since: cursorRef.current ?? undefined,
155
+ limit: pageSize,
156
+ });
157
+ for (const h of page.handshakes)
158
+ await schedule(h);
159
+ if (!page.hasMore)
160
+ break;
161
+ }
162
+ }
163
+ finally {
164
+ catchingUpRef.current = false;
165
+ // Replay anything that landed live during catch-up, in seq order, through the same queue.
166
+ const buffered = liveBuffer.splice(0).sort((a, b) => compareSeq(a.seq, b.seq));
167
+ for (const h of buffered)
168
+ schedule(h);
169
+ }
170
+ })().finally(() => {
171
+ catchUpInFlight = null;
172
+ });
173
+ return catchUpInFlight;
174
+ };
175
+ (async () => {
176
+ // Resolve the device row id (option wins; else the persisted device). Without one, there is no
177
+ // inbox to drain yet — the effect re-runs when `deviceId` is later supplied.
178
+ deviceId = deviceIdOption ?? (await repo.loadDevice())?.device?.id;
179
+ if (!alive || !deviceId)
180
+ return;
181
+ cursorRef.current = await repo.loadHandshakeCursor();
182
+ if (alive)
183
+ setCursor(cursorRef.current);
184
+ // Subscribe to live events BEFORE catch-up (buffered until catch-up completes).
185
+ offFns.push(socket.on("secure:welcome", enqueueLive));
186
+ offFns.push(socket.on("secure:handshake", enqueueLive));
187
+ runCatchUpRef.current = runCatchUp;
188
+ await runCatchUp();
189
+ // After a reload we hold group state but haven't joined the socket rooms, so broadcast Commits
190
+ // wouldn't arrive — re-join every known conversation.
191
+ try {
192
+ const convIds = await repo.listGroupConversationIds();
193
+ for (const c of convIds)
194
+ socket.joinConversation(c);
195
+ }
196
+ catch (err) {
197
+ report(err);
198
+ }
199
+ if (alive) {
200
+ setCatchingUp(false);
201
+ setReady(true);
202
+ }
203
+ })().catch((err) => report(err));
204
+ return () => {
205
+ alive = false;
206
+ offFns.forEach((off) => off());
207
+ runCatchUpRef.current = null;
208
+ };
209
+ }, [enabled, deviceIdOption, pageSize, rest, socket, repo, crypto, resolveGroup, rememberGroup]);
210
+ return { catchingUp, ready, cursor, processedCount, error, resync };
211
+ }
212
+ //# sourceMappingURL=useSecureHandshakes.js.map