@agora-sdk/secure-chat-core 0.1.0 → 0.3.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/cjs/context/secure-chat-context.d.ts +34 -18
- package/dist/cjs/context/secure-chat-context.js +78 -23
- package/dist/cjs/context/secure-chat-context.js.map +1 -1
- package/dist/cjs/contract/index.js +2 -2
- package/dist/cjs/contract/index.js.map +1 -1
- package/dist/cjs/hooks/useSecureConversations.d.ts +1 -1
- package/dist/cjs/hooks/useSecureConversations.js +9 -7
- package/dist/cjs/hooks/useSecureConversations.js.map +1 -1
- package/dist/cjs/hooks/useSecureDevice.d.ts +14 -13
- package/dist/cjs/hooks/useSecureDevice.js +58 -25
- package/dist/cjs/hooks/useSecureDevice.js.map +1 -1
- package/dist/cjs/hooks/useSecureHandshakes.d.ts +64 -0
- package/dist/cjs/hooks/useSecureHandshakes.js +215 -0
- package/dist/cjs/hooks/useSecureHandshakes.js.map +1 -0
- package/dist/cjs/hooks/useSecureMessages.d.ts +10 -11
- package/dist/cjs/hooks/useSecureMessages.js +99 -21
- package/dist/cjs/hooks/useSecureMessages.js.map +1 -1
- package/dist/cjs/index.d.ts +20 -14
- package/dist/cjs/index.js +25 -19
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/persistence/memory-store.d.ts +9 -0
- package/dist/cjs/persistence/memory-store.js +27 -0
- package/dist/cjs/persistence/memory-store.js.map +1 -0
- package/dist/cjs/persistence/repository.d.ts +36 -0
- package/dist/cjs/persistence/repository.js +72 -0
- package/dist/cjs/persistence/repository.js.map +1 -0
- package/dist/cjs/persistence/store.d.ts +11 -0
- package/dist/cjs/persistence/store.js +8 -0
- package/dist/cjs/persistence/store.js.map +1 -0
- package/dist/cjs/transport/rest.d.ts +1 -1
- package/dist/cjs/transport/socket.d.ts +1 -1
- package/dist/esm/context/secure-chat-context.d.ts +34 -18
- package/dist/esm/context/secure-chat-context.js +77 -22
- package/dist/esm/context/secure-chat-context.js.map +1 -1
- package/dist/esm/contract/index.js +2 -2
- package/dist/esm/contract/index.js.map +1 -1
- package/dist/esm/hooks/useSecureConversations.d.ts +1 -1
- package/dist/esm/hooks/useSecureConversations.js +6 -4
- package/dist/esm/hooks/useSecureConversations.js.map +1 -1
- package/dist/esm/hooks/useSecureDevice.d.ts +14 -13
- package/dist/esm/hooks/useSecureDevice.js +55 -22
- package/dist/esm/hooks/useSecureDevice.js.map +1 -1
- package/dist/esm/hooks/useSecureHandshakes.d.ts +64 -0
- package/dist/esm/hooks/useSecureHandshakes.js +212 -0
- package/dist/esm/hooks/useSecureHandshakes.js.map +1 -0
- package/dist/esm/hooks/useSecureMessages.d.ts +10 -11
- package/dist/esm/hooks/useSecureMessages.js +96 -18
- package/dist/esm/hooks/useSecureMessages.js.map +1 -1
- package/dist/esm/index.d.ts +20 -14
- package/dist/esm/index.js +10 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/persistence/memory-store.d.ts +9 -0
- package/dist/esm/persistence/memory-store.js +23 -0
- package/dist/esm/persistence/memory-store.js.map +1 -0
- package/dist/esm/persistence/repository.d.ts +36 -0
- package/dist/esm/persistence/repository.js +68 -0
- package/dist/esm/persistence/repository.js.map +1 -0
- package/dist/esm/persistence/store.d.ts +11 -0
- package/dist/esm/persistence/store.js +7 -0
- package/dist/esm/persistence/store.js.map +1 -0
- package/dist/esm/transport/rest.d.ts +1 -1
- package/dist/esm/transport/socket.d.ts +1 -1
- package/package.json +3 -3
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { SecureDeviceModel } from "../contract";
|
|
1
|
+
import { SecureDeviceModel } from "../contract/index.js";
|
|
2
2
|
/** Options for {@link useSecureDevice}. */
|
|
3
3
|
export interface UseSecureDeviceOptions {
|
|
4
|
-
/** Stable, persisted client device id. Generated
|
|
4
|
+
/** Stable, persisted client device id. Generated (and persisted) if omitted. A previously
|
|
5
|
+
* persisted device's id takes precedence over this value when one exists on mount. */
|
|
5
6
|
deviceId?: string;
|
|
6
7
|
/** MLS ciphersuite to register under. Defaults to the crypto implementation's preferred suite. */
|
|
7
8
|
ciphersuite?: number;
|
|
@@ -14,13 +15,15 @@ export interface UseSecureDeviceOptions {
|
|
|
14
15
|
export interface UseSecureDeviceValues {
|
|
15
16
|
/** The registered device row (its `.id` is the uuid used as targetDeviceId everywhere). */
|
|
16
17
|
device: SecureDeviceModel | null;
|
|
18
|
+
/** True until the initial persisted-device load settles. */
|
|
19
|
+
loading: boolean;
|
|
17
20
|
/** True while {@link UseSecureDeviceValues.register} is in flight. */
|
|
18
21
|
registering: boolean;
|
|
19
|
-
/** The last error thrown by registration or replenishment, or `null`. */
|
|
22
|
+
/** The last error thrown by load, registration, or replenishment, or `null`. */
|
|
20
23
|
error: unknown;
|
|
21
24
|
/** Last known count of unconsumed KeyPackages, or `null` until refreshed. */
|
|
22
25
|
keyPackagesAvailable: number | null;
|
|
23
|
-
/** Generate identity + register (idempotent server-side on (userId, deviceId)). */
|
|
26
|
+
/** Generate identity + register (idempotent server-side on (userId, deviceId)) and persist it. */
|
|
24
27
|
register: () => Promise<SecureDeviceModel>;
|
|
25
28
|
/** Generate + publish `count` fresh KeyPackages (default = keyPackageTarget). */
|
|
26
29
|
publishKeyPackages: (count?: number) => Promise<number>;
|
|
@@ -28,20 +31,18 @@ export interface UseSecureDeviceValues {
|
|
|
28
31
|
refreshKeyPackageCount: () => Promise<number>;
|
|
29
32
|
}
|
|
30
33
|
/**
|
|
31
|
-
* Register this client as an MLS device
|
|
34
|
+
* Register this client as an MLS device, persist its identity, and keep KeyPackages stocked.
|
|
32
35
|
*
|
|
33
|
-
* On
|
|
34
|
-
*
|
|
35
|
-
* `secure:key-packages-low` for this device. The device's private key material must be persisted by
|
|
36
|
-
* the platform layer — this hook only handles registration and relay.
|
|
36
|
+
* On mount it loads any persisted device and re-hydrates the crypto identity (stable id, no
|
|
37
|
+
* re-register). When none exists, await {@link UseSecureDeviceValues.register}.
|
|
37
38
|
*
|
|
38
|
-
* @param options - {@link UseSecureDeviceOptions}
|
|
39
|
-
* @returns {@link UseSecureDeviceValues}
|
|
39
|
+
* @param options - {@link UseSecureDeviceOptions}.
|
|
40
|
+
* @returns {@link UseSecureDeviceValues}.
|
|
40
41
|
*
|
|
41
42
|
* @example
|
|
42
43
|
* ```tsx
|
|
43
|
-
* const { device, register } = useSecureDevice(
|
|
44
|
-
* useEffect(() => { register(); }, []);
|
|
44
|
+
* const { device, loading, register } = useSecureDevice();
|
|
45
|
+
* useEffect(() => { if (!loading && !device) register(); }, [loading, device, register]);
|
|
45
46
|
* ```
|
|
46
47
|
*/
|
|
47
48
|
export declare function useSecureDevice(options?: UseSecureDeviceOptions): UseSecureDeviceValues;
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
// useSecureDevice — register this client as an MLS device (leaf) and keep its
|
|
1
|
+
// useSecureDevice — register this client as an MLS device (leaf), persist its identity, and keep its
|
|
2
|
+
// KeyPackages topped up.
|
|
2
3
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// NOTE: the device's PRIVATE state (`privateState` from generateDeviceIdentity, and MLS group
|
|
7
|
-
// state) must be persisted by the platform layer (IndexedDB on web, keystore on native — Phase 2/3).
|
|
8
|
-
// Core only performs registration + relay; it does not persist secrets.
|
|
4
|
+
// On mount it re-hydrates a persisted device (stable deviceId + private state via
|
|
5
|
+
// crypto.importDeviceState) so a reload does NOT mint a new identity. register() generates, registers
|
|
6
|
+
// on the server, and persists. Replenishes on the `secure:key-packages-low` realtime signal.
|
|
9
7
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
10
|
-
import { toBase64 } from "../util/base64";
|
|
11
|
-
import { useSecureChat } from "../context/secure-chat-context";
|
|
8
|
+
import { toBase64 } from "../util/base64.js";
|
|
9
|
+
import { useSecureChat } from "../context/secure-chat-context.js";
|
|
12
10
|
/**
|
|
13
11
|
* Mint a device id when the caller doesn't supply one — `crypto.randomUUID()` when available, else a
|
|
14
|
-
* non-cryptographic timestamp+random fallback.
|
|
12
|
+
* non-cryptographic timestamp+random fallback.
|
|
15
13
|
*
|
|
16
14
|
* @returns A fresh device id string.
|
|
17
15
|
*/
|
|
@@ -19,34 +17,64 @@ function newDeviceId() {
|
|
|
19
17
|
const g = globalThis.crypto;
|
|
20
18
|
if (g?.randomUUID)
|
|
21
19
|
return g.randomUUID();
|
|
22
|
-
// Platform layer should supply a persisted, stable device id; this is a non-crypto fallback.
|
|
23
20
|
return `dev-${Date.now().toString(36)}-${Math.floor(Math.random() * 1e9).toString(36)}`;
|
|
24
21
|
}
|
|
25
22
|
/**
|
|
26
|
-
* Register this client as an MLS device
|
|
23
|
+
* Register this client as an MLS device, persist its identity, and keep KeyPackages stocked.
|
|
27
24
|
*
|
|
28
|
-
* On
|
|
29
|
-
*
|
|
30
|
-
* `secure:key-packages-low` for this device. The device's private key material must be persisted by
|
|
31
|
-
* the platform layer — this hook only handles registration and relay.
|
|
25
|
+
* On mount it loads any persisted device and re-hydrates the crypto identity (stable id, no
|
|
26
|
+
* re-register). When none exists, await {@link UseSecureDeviceValues.register}.
|
|
32
27
|
*
|
|
33
|
-
* @param options - {@link UseSecureDeviceOptions}
|
|
34
|
-
* @returns {@link UseSecureDeviceValues}
|
|
28
|
+
* @param options - {@link UseSecureDeviceOptions}.
|
|
29
|
+
* @returns {@link UseSecureDeviceValues}.
|
|
35
30
|
*
|
|
36
31
|
* @example
|
|
37
32
|
* ```tsx
|
|
38
|
-
* const { device, register } = useSecureDevice(
|
|
39
|
-
* useEffect(() => { register(); }, []);
|
|
33
|
+
* const { device, loading, register } = useSecureDevice();
|
|
34
|
+
* useEffect(() => { if (!loading && !device) register(); }, [loading, device, register]);
|
|
40
35
|
* ```
|
|
41
36
|
*/
|
|
42
37
|
export function useSecureDevice(options = {}) {
|
|
43
|
-
const { crypto, rest, socket } = useSecureChat();
|
|
38
|
+
const { crypto, rest, socket, repo } = useSecureChat();
|
|
44
39
|
const { ciphersuite, keyPackageTarget = 20, autoReplenish = true } = options;
|
|
45
40
|
const [device, setDevice] = useState(null);
|
|
41
|
+
const [loading, setLoading] = useState(true);
|
|
46
42
|
const [registering, setRegistering] = useState(false);
|
|
47
43
|
const [error, setError] = useState(null);
|
|
48
44
|
const [keyPackagesAvailable, setKeyPackagesAvailable] = useState(null);
|
|
49
45
|
const deviceIdRef = useRef(options.deviceId ?? newDeviceId());
|
|
46
|
+
const registerStartedRef = useRef(false);
|
|
47
|
+
// On mount: re-hydrate a persisted device (stable id + private state). No persisted device ⇒
|
|
48
|
+
// first-run; the app calls register().
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
let alive = true;
|
|
51
|
+
(async () => {
|
|
52
|
+
const persisted = await repo.loadDevice();
|
|
53
|
+
if (!alive || registerStartedRef.current) {
|
|
54
|
+
setLoading(false);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (persisted) {
|
|
58
|
+
await crypto.importDeviceState(persisted.deviceState);
|
|
59
|
+
// register() may have started during the await — don't overwrite its identity.
|
|
60
|
+
if (!alive || registerStartedRef.current) {
|
|
61
|
+
setLoading(false);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
deviceIdRef.current = persisted.deviceId;
|
|
65
|
+
setDevice(persisted.device);
|
|
66
|
+
}
|
|
67
|
+
setLoading(false);
|
|
68
|
+
})().catch((err) => {
|
|
69
|
+
if (!alive)
|
|
70
|
+
return;
|
|
71
|
+
setError(err);
|
|
72
|
+
setLoading(false);
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
alive = false;
|
|
76
|
+
};
|
|
77
|
+
}, [repo, crypto]);
|
|
50
78
|
const publishKeyPackages = useCallback(async (count = keyPackageTarget) => {
|
|
51
79
|
if (!device)
|
|
52
80
|
throw new Error("Register the device before publishing KeyPackages.");
|
|
@@ -69,6 +97,7 @@ export function useSecureDevice(options = {}) {
|
|
|
69
97
|
return available;
|
|
70
98
|
}, [rest, device]);
|
|
71
99
|
const register = useCallback(async () => {
|
|
100
|
+
registerStartedRef.current = true;
|
|
72
101
|
setRegistering(true);
|
|
73
102
|
setError(null);
|
|
74
103
|
try {
|
|
@@ -82,6 +111,9 @@ export function useSecureDevice(options = {}) {
|
|
|
82
111
|
credential: toBase64(identity.credential),
|
|
83
112
|
ciphersuite: identity.ciphersuite,
|
|
84
113
|
});
|
|
114
|
+
const deviceState = await crypto.exportDeviceState();
|
|
115
|
+
await repo.saveDevice({ deviceId: identity.deviceId, deviceState, device: registered });
|
|
116
|
+
deviceIdRef.current = identity.deviceId;
|
|
85
117
|
setDevice(registered);
|
|
86
118
|
return registered;
|
|
87
119
|
}
|
|
@@ -92,7 +124,7 @@ export function useSecureDevice(options = {}) {
|
|
|
92
124
|
finally {
|
|
93
125
|
setRegistering(false);
|
|
94
126
|
}
|
|
95
|
-
}, [crypto, rest, ciphersuite]);
|
|
127
|
+
}, [crypto, rest, repo, ciphersuite]);
|
|
96
128
|
// Auto-replenish on the server's low-water signal for this device.
|
|
97
129
|
useEffect(() => {
|
|
98
130
|
if (!autoReplenish || !device)
|
|
@@ -106,6 +138,7 @@ export function useSecureDevice(options = {}) {
|
|
|
106
138
|
}, [autoReplenish, device, socket, publishKeyPackages]);
|
|
107
139
|
return {
|
|
108
140
|
device,
|
|
141
|
+
loading,
|
|
109
142
|
registering,
|
|
110
143
|
error,
|
|
111
144
|
keyPackagesAvailable,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSecureDevice.js","sourceRoot":"","sources":["../../../src/hooks/useSecureDevice.tsx"],"names":[],"mappings":"AAAA,qGAAqG;AACrG,
|
|
1
|
+
{"version":3,"file":"useSecureDevice.js","sourceRoot":"","sources":["../../../src/hooks/useSecureDevice.tsx"],"names":[],"mappings":"AAAA,qGAAqG;AACrG,yBAAyB;AACzB,EAAE;AACF,kFAAkF;AAClF,sGAAsG;AACtG,6FAA6F;AAE7F,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EA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,EAAE,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAExD,OAAO;QACL,MAAM;QACN,OAAO;QACP,WAAW;QACX,KAAK;QACL,oBAAoB;QACpB,QAAQ;QACR,kBAAkB;QAClB,sBAAsB;KACvB,CAAC;AACJ,CAAC"}
|
|
@@ -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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSecureHandshakes.js","sourceRoot":"","sources":["../../../src/hooks/useSecureHandshakes.tsx"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,EAAE;AACF,oGAAoG;AACpG,6FAA6F;AAC7F,oGAAoG;AACpG,sGAAsG;AACtG,sGAAsG;AACtG,4FAA4F;AAC5F,mBAAmB;AACnB,EAAE;AACF,mGAAmG;AACnG,uGAAuG;AACvG,yDAAyD;AAEzD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEjE,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAElE,0GAA0G;AAC1G,SAAS,UAAU,CAAC,CAAS,EAAE,CAAS;IACtC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAChC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,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ACb,MAAM,CAAC,GAAG,CAAC,CAAC;YACd,CAAC;YAED,IAAI,KAAK,EAAE,CAAC;gBACV,aAAa,CAAC,KAAK,CAAC,CAAC;gBACrB,QAAQ,CAAC,IAAI,CAAC,CAAC;YACjB,CAAC;QACH,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QAEjC,OAAO,GAAG,EAAE;YACV,KAAK,GAAG,KAAK,CAAC;YACd,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;YAC/B,aAAa,CAAC,OAAO,GAAG,IAAI,CAAC;QAC/B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC,CAAC;IAEjG,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;AACtE,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SecureMessageModel } from "../contract";
|
|
1
|
+
import { SecureMessageModel } from "../contract/index.js";
|
|
2
2
|
import { GroupHandle } from "@agora-sdk/secure-chat-crypto";
|
|
3
3
|
/** A stored message paired with its decrypted text (when a group handle is available). */
|
|
4
4
|
export interface DecryptedSecureMessage {
|
|
@@ -9,9 +9,9 @@ export interface DecryptedSecureMessage {
|
|
|
9
9
|
}
|
|
10
10
|
/** Options for {@link useSecureMessages}. */
|
|
11
11
|
export interface UseSecureMessagesOptions {
|
|
12
|
-
/**
|
|
12
|
+
/** Override the MLS group handle. Defaults to the persisted handle via `resolveGroup`. */
|
|
13
13
|
group?: GroupHandle;
|
|
14
|
-
/**
|
|
14
|
+
/** Override the sender device row id. Defaults to the persisted device's `.id`. */
|
|
15
15
|
senderDeviceId?: string;
|
|
16
16
|
}
|
|
17
17
|
/** The state and actions returned by {@link useSecureMessages}. */
|
|
@@ -28,24 +28,23 @@ export interface UseSecureMessagesValues {
|
|
|
28
28
|
loadMore: () => Promise<void>;
|
|
29
29
|
/** Reload from the newest message, replacing the current list. */
|
|
30
30
|
refresh: () => Promise<void>;
|
|
31
|
-
/** Encrypt + send a text message. Requires
|
|
31
|
+
/** Encrypt + send a text message. Requires a resolvable group + sender device. */
|
|
32
32
|
sendMessage: (text: string) => Promise<void>;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Load, decrypt, send, and live-receive messages in one secure conversation.
|
|
36
36
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* `
|
|
40
|
-
* wiring. Joins the conversation's socket room to receive `secure:message` events live.
|
|
37
|
+
* Auto-resolves the MLS group handle (via `resolveGroup`) and the sender device id (from the
|
|
38
|
+
* persisted device) unless overridden in `options`. Joins the conversation socket room for live
|
|
39
|
+
* `secure:message` events.
|
|
41
40
|
*
|
|
42
41
|
* @param conversationId - The conversation to read and send within.
|
|
43
|
-
* @param options - {@link UseSecureMessagesOptions}
|
|
44
|
-
* @returns {@link UseSecureMessagesValues}
|
|
42
|
+
* @param options - {@link UseSecureMessagesOptions}.
|
|
43
|
+
* @returns {@link UseSecureMessagesValues}.
|
|
45
44
|
*
|
|
46
45
|
* @example
|
|
47
46
|
* ```tsx
|
|
48
|
-
* const { messages, sendMessage } = useSecureMessages(conversationId
|
|
47
|
+
* const { messages, sendMessage } = useSecureMessages(conversationId);
|
|
49
48
|
* await sendMessage("hello 💜");
|
|
50
49
|
* ```
|
|
51
50
|
*/
|