@archipelagolab/lobi 1.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.
- package/CHANGELOG.md +164 -0
- package/ENDOFFILE +0 -0
- package/EOF +0 -0
- package/LICENSE +21 -0
- package/SPEC-SUPPORT.md +116 -0
- package/YAMLEND +0 -0
- package/api.ts +18 -0
- package/archipelagolab-lobi-1.0.0.tgz +0 -0
- package/auth-presence.ts +56 -0
- package/channel-plugin-api.ts +3 -0
- package/cli-metadata.ts +11 -0
- package/contract-api.ts +17 -0
- package/docs/CHECKLIST.md +83 -0
- package/docs/FORK_SDK_GUIDE.md +279 -0
- package/helper-api.ts +3 -0
- package/index.test.ts +61 -0
- package/index.ts +65 -0
- package/openclaw.plugin.json +23 -0
- package/package.json +52 -0
- package/plugin-entry.handlers.runtime.ts +1 -0
- package/runtime-api.ts +54 -0
- package/runtime-heavy-api.ts +1 -0
- package/scripts/migrate-to-lobi.sh +72 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +13 -0
- package/src/account-selection.test.ts +124 -0
- package/src/account-selection.ts +226 -0
- package/src/actions.account-propagation.test.ts +251 -0
- package/src/actions.test.ts +251 -0
- package/src/actions.ts +336 -0
- package/src/approval-auth.test.ts +23 -0
- package/src/approval-auth.ts +25 -0
- package/src/approval-handler.runtime.test.ts +46 -0
- package/src/approval-handler.runtime.ts +400 -0
- package/src/approval-ids.ts +6 -0
- package/src/approval-native.test.ts +329 -0
- package/src/approval-native.ts +336 -0
- package/src/approval-reactions.test.ts +107 -0
- package/src/approval-reactions.ts +158 -0
- package/src/auth-precedence.ts +61 -0
- package/src/channel-account-paths.ts +92 -0
- package/src/channel.account-paths.test.ts +102 -0
- package/src/channel.directory.test.ts +601 -0
- package/src/channel.resolve.test.ts +38 -0
- package/src/channel.runtime.ts +16 -0
- package/src/channel.setup.test.ts +269 -0
- package/src/channel.ts +570 -0
- package/src/cli-metadata.ts +19 -0
- package/src/cli.test.ts +1015 -0
- package/src/cli.ts +1198 -0
- package/src/config-adapter.ts +41 -0
- package/src/config-schema.test.ts +90 -0
- package/src/config-schema.ts +114 -0
- package/src/directory-live.test.ts +200 -0
- package/src/directory-live.ts +238 -0
- package/src/doctor-contract.ts +287 -0
- package/src/doctor.test.ts +440 -0
- package/src/doctor.ts +262 -0
- package/src/env-vars.ts +92 -0
- package/src/exec-approval-resolver.test.ts +68 -0
- package/src/exec-approval-resolver.ts +23 -0
- package/src/exec-approvals.test.ts +483 -0
- package/src/exec-approvals.ts +290 -0
- package/src/group-mentions.ts +41 -0
- package/src/legacy-crypto-inspector-availability.test.ts +81 -0
- package/src/legacy-crypto-inspector-availability.ts +60 -0
- package/src/legacy-crypto.test.ts +234 -0
- package/src/legacy-crypto.ts +549 -0
- package/src/legacy-state.test.ts +86 -0
- package/src/legacy-state.ts +156 -0
- package/src/matrix/account-config.ts +150 -0
- package/src/matrix/accounts.readiness.test.ts +27 -0
- package/src/matrix/accounts.test.ts +757 -0
- package/src/matrix/accounts.ts +194 -0
- package/src/matrix/actions/client.test.ts +215 -0
- package/src/matrix/actions/client.ts +31 -0
- package/src/matrix/actions/devices.test.ts +114 -0
- package/src/matrix/actions/devices.ts +34 -0
- package/src/matrix/actions/limits.test.ts +15 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.test.ts +289 -0
- package/src/matrix/actions/messages.ts +123 -0
- package/src/matrix/actions/pins.test.ts +74 -0
- package/src/matrix/actions/pins.ts +64 -0
- package/src/matrix/actions/polls.test.ts +71 -0
- package/src/matrix/actions/polls.ts +109 -0
- package/src/matrix/actions/profile.test.ts +109 -0
- package/src/matrix/actions/profile.ts +37 -0
- package/src/matrix/actions/reactions.test.ts +135 -0
- package/src/matrix/actions/reactions.ts +59 -0
- package/src/matrix/actions/room.test.ts +79 -0
- package/src/matrix/actions/room.ts +71 -0
- package/src/matrix/actions/summary.test.ts +87 -0
- package/src/matrix/actions/summary.ts +88 -0
- package/src/matrix/actions/types.ts +82 -0
- package/src/matrix/actions/verification.test.ts +105 -0
- package/src/matrix/actions/verification.ts +237 -0
- package/src/matrix/actions.ts +37 -0
- package/src/matrix/active-client.ts +26 -0
- package/src/matrix/async-lock.ts +18 -0
- package/src/matrix/backup-health.ts +115 -0
- package/src/matrix/client/config-runtime-api.ts +14 -0
- package/src/matrix/client/config-secret-input.runtime.ts +1 -0
- package/src/matrix/client/config.ts +982 -0
- package/src/matrix/client/create-client.test.ts +115 -0
- package/src/matrix/client/create-client.ts +101 -0
- package/src/matrix/client/env-auth.ts +6 -0
- package/src/matrix/client/file-sync-store.test.ts +265 -0
- package/src/matrix/client/file-sync-store.ts +289 -0
- package/src/matrix/client/logging.ts +123 -0
- package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
- package/src/matrix/client/private-network-host.ts +56 -0
- package/src/matrix/client/runtime.ts +4 -0
- package/src/matrix/client/shared.test.ts +344 -0
- package/src/matrix/client/shared.ts +306 -0
- package/src/matrix/client/storage.test.ts +634 -0
- package/src/matrix/client/storage.ts +544 -0
- package/src/matrix/client/types.ts +50 -0
- package/src/matrix/client-bootstrap.test.ts +84 -0
- package/src/matrix/client-bootstrap.ts +164 -0
- package/src/matrix/client-resolver.test-helpers.ts +147 -0
- package/src/matrix/client.test.ts +1521 -0
- package/src/matrix/client.ts +23 -0
- package/src/matrix/config-paths.ts +31 -0
- package/src/matrix/config-update.test.ts +237 -0
- package/src/matrix/config-update.ts +291 -0
- package/src/matrix/credentials-read.ts +206 -0
- package/src/matrix/credentials-write.runtime.ts +26 -0
- package/src/matrix/credentials.test.ts +501 -0
- package/src/matrix/credentials.ts +95 -0
- package/src/matrix/deps.test.ts +74 -0
- package/src/matrix/deps.ts +225 -0
- package/src/matrix/device-health.test.ts +45 -0
- package/src/matrix/device-health.ts +31 -0
- package/src/matrix/direct-management.test.ts +350 -0
- package/src/matrix/direct-management.ts +347 -0
- package/src/matrix/direct-room.test.ts +61 -0
- package/src/matrix/direct-room.ts +128 -0
- package/src/matrix/draft-stream.test.ts +406 -0
- package/src/matrix/draft-stream.ts +216 -0
- package/src/matrix/encryption-guidance.ts +27 -0
- package/src/matrix/errors.ts +21 -0
- package/src/matrix/format.test.ts +340 -0
- package/src/matrix/format.ts +428 -0
- package/src/matrix/legacy-crypto-inspector.ts +95 -0
- package/src/matrix/media-errors.ts +20 -0
- package/src/matrix/media-text.ts +169 -0
- package/src/matrix/monitor/access-state.test.ts +45 -0
- package/src/matrix/monitor/access-state.ts +77 -0
- package/src/matrix/monitor/ack-config.test.ts +57 -0
- package/src/matrix/monitor/ack-config.ts +26 -0
- package/src/matrix/monitor/allowlist.test.ts +45 -0
- package/src/matrix/monitor/allowlist.ts +94 -0
- package/src/matrix/monitor/auto-join.test.ts +203 -0
- package/src/matrix/monitor/auto-join.ts +86 -0
- package/src/matrix/monitor/config.test.ts +197 -0
- package/src/matrix/monitor/config.ts +303 -0
- package/src/matrix/monitor/context-summary.ts +43 -0
- package/src/matrix/monitor/direct.test.ts +529 -0
- package/src/matrix/monitor/direct.ts +270 -0
- package/src/matrix/monitor/events.test.ts +1524 -0
- package/src/matrix/monitor/events.ts +213 -0
- package/src/matrix/monitor/handler.body-for-agent.test.ts +396 -0
- package/src/matrix/monitor/handler.group-history.test.ts +648 -0
- package/src/matrix/monitor/handler.media-failure.test.ts +267 -0
- package/src/matrix/monitor/handler.test-helpers.ts +308 -0
- package/src/matrix/monitor/handler.test.ts +2952 -0
- package/src/matrix/monitor/handler.thread-root-media.test.ts +82 -0
- package/src/matrix/monitor/handler.ts +1679 -0
- package/src/matrix/monitor/inbound-dedupe.test.ts +146 -0
- package/src/matrix/monitor/inbound-dedupe.ts +267 -0
- package/src/matrix/monitor/index.test.ts +920 -0
- package/src/matrix/monitor/index.ts +434 -0
- package/src/matrix/monitor/legacy-crypto-restore.test.ts +206 -0
- package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
- package/src/matrix/monitor/location.ts +100 -0
- package/src/matrix/monitor/media.test.ts +159 -0
- package/src/matrix/monitor/media.ts +119 -0
- package/src/matrix/monitor/mentions.test.ts +289 -0
- package/src/matrix/monitor/mentions.ts +177 -0
- package/src/matrix/monitor/reaction-events.test.ts +326 -0
- package/src/matrix/monitor/reaction-events.ts +187 -0
- package/src/matrix/monitor/recent-invite.test.ts +92 -0
- package/src/matrix/monitor/recent-invite.ts +30 -0
- package/src/matrix/monitor/replies.test.ts +265 -0
- package/src/matrix/monitor/replies.ts +136 -0
- package/src/matrix/monitor/reply-context.test.ts +276 -0
- package/src/matrix/monitor/reply-context.ts +92 -0
- package/src/matrix/monitor/room-history.test.ts +258 -0
- package/src/matrix/monitor/room-history.ts +301 -0
- package/src/matrix/monitor/room-info.test.ts +201 -0
- package/src/matrix/monitor/room-info.ts +126 -0
- package/src/matrix/monitor/rooms.test.ts +121 -0
- package/src/matrix/monitor/rooms.ts +52 -0
- package/src/matrix/monitor/route.test.ts +255 -0
- package/src/matrix/monitor/route.ts +178 -0
- package/src/matrix/monitor/runtime-api.ts +31 -0
- package/src/matrix/monitor/startup-verification.test.ts +294 -0
- package/src/matrix/monitor/startup-verification.ts +237 -0
- package/src/matrix/monitor/startup.test.ts +257 -0
- package/src/matrix/monitor/startup.ts +218 -0
- package/src/matrix/monitor/status.ts +111 -0
- package/src/matrix/monitor/sync-lifecycle.test.ts +224 -0
- package/src/matrix/monitor/sync-lifecycle.ts +91 -0
- package/src/matrix/monitor/task-runner.ts +38 -0
- package/src/matrix/monitor/thread-context.test.ts +149 -0
- package/src/matrix/monitor/thread-context.ts +108 -0
- package/src/matrix/monitor/threads.test.ts +68 -0
- package/src/matrix/monitor/threads.ts +85 -0
- package/src/matrix/monitor/types.ts +30 -0
- package/src/matrix/monitor/verification-events.ts +627 -0
- package/src/matrix/monitor/verification-utils.test.ts +47 -0
- package/src/matrix/monitor/verification-utils.ts +46 -0
- package/src/matrix/outbound-media-runtime.ts +1 -0
- package/src/matrix/poll-summary.ts +110 -0
- package/src/matrix/poll-types.test.ts +205 -0
- package/src/matrix/poll-types.ts +433 -0
- package/src/matrix/probe.runtime.ts +4 -0
- package/src/matrix/probe.test.ts +154 -0
- package/src/matrix/probe.ts +96 -0
- package/src/matrix/profile.test.ts +154 -0
- package/src/matrix/profile.ts +184 -0
- package/src/matrix/reaction-common.test.ts +96 -0
- package/src/matrix/reaction-common.ts +147 -0
- package/src/matrix/sdk/crypto-bootstrap.test.ts +505 -0
- package/src/matrix/sdk/crypto-bootstrap.ts +341 -0
- package/src/matrix/sdk/crypto-facade.test.ts +197 -0
- package/src/matrix/sdk/crypto-facade.ts +207 -0
- package/src/matrix/sdk/crypto-node.runtime.test.ts +27 -0
- package/src/matrix/sdk/crypto-node.runtime.ts +9 -0
- package/src/matrix/sdk/crypto-runtime.ts +11 -0
- package/src/matrix/sdk/decrypt-bridge.ts +356 -0
- package/src/matrix/sdk/event-helpers.test.ts +60 -0
- package/src/matrix/sdk/event-helpers.ts +71 -0
- package/src/matrix/sdk/http-client.test.ts +134 -0
- package/src/matrix/sdk/http-client.ts +87 -0
- package/src/matrix/sdk/idb-persistence-lock.ts +51 -0
- package/src/matrix/sdk/idb-persistence.lock-order.test.ts +108 -0
- package/src/matrix/sdk/idb-persistence.test-helpers.ts +88 -0
- package/src/matrix/sdk/idb-persistence.test.ts +149 -0
- package/src/matrix/sdk/idb-persistence.ts +283 -0
- package/src/matrix/sdk/logger.test.ts +25 -0
- package/src/matrix/sdk/logger.ts +108 -0
- package/src/matrix/sdk/read-response-with-limit.ts +19 -0
- package/src/matrix/sdk/recovery-key-store.test.ts +385 -0
- package/src/matrix/sdk/recovery-key-store.ts +430 -0
- package/src/matrix/sdk/transport.test.ts +161 -0
- package/src/matrix/sdk/transport.ts +344 -0
- package/src/matrix/sdk/types.ts +236 -0
- package/src/matrix/sdk/verification-manager.test.ts +509 -0
- package/src/matrix/sdk/verification-manager.ts +694 -0
- package/src/matrix/sdk/verification-status.ts +23 -0
- package/src/matrix/sdk.test.ts +2568 -0
- package/src/matrix/sdk.ts +1789 -0
- package/src/matrix/send/client.test.ts +174 -0
- package/src/matrix/send/client.ts +90 -0
- package/src/matrix/send/formatting.ts +189 -0
- package/src/matrix/send/media.ts +244 -0
- package/src/matrix/send/targets.test.ts +254 -0
- package/src/matrix/send/targets.ts +104 -0
- package/src/matrix/send/types.ts +134 -0
- package/src/matrix/send.test.ts +958 -0
- package/src/matrix/send.ts +609 -0
- package/src/matrix/session-store-metadata.ts +108 -0
- package/src/matrix/startup-abort.ts +44 -0
- package/src/matrix/sync-state.ts +27 -0
- package/src/matrix/target-ids.ts +102 -0
- package/src/matrix/thread-bindings-shared.ts +201 -0
- package/src/matrix/thread-bindings.test.ts +673 -0
- package/src/matrix/thread-bindings.ts +577 -0
- package/src/matrix-migration.runtime.ts +9 -0
- package/src/migration-config.test.ts +228 -0
- package/src/migration-config.ts +243 -0
- package/src/migration-snapshot-backup.ts +117 -0
- package/src/migration-snapshot.test.ts +184 -0
- package/src/migration-snapshot.ts +55 -0
- package/src/onboarding.resolve.test.ts +55 -0
- package/src/onboarding.test-harness.ts +158 -0
- package/src/onboarding.test.ts +665 -0
- package/src/onboarding.ts +773 -0
- package/src/outbound.test.ts +173 -0
- package/src/outbound.ts +78 -0
- package/src/plugin-entry.runtime.js +159 -0
- package/src/plugin-entry.runtime.test.ts +108 -0
- package/src/plugin-entry.runtime.ts +68 -0
- package/src/profile-update.ts +68 -0
- package/src/record-shared.ts +3 -0
- package/src/resolve-targets.test.ts +178 -0
- package/src/resolve-targets.ts +175 -0
- package/src/resolver.ts +21 -0
- package/src/runtime-api.ts +144 -0
- package/src/runtime.ts +7 -0
- package/src/secret-contract.ts +174 -0
- package/src/session-route.test.ts +315 -0
- package/src/session-route.ts +113 -0
- package/src/setup-bootstrap.ts +94 -0
- package/src/setup-config.ts +222 -0
- package/src/setup-contract.ts +89 -0
- package/src/setup-core.test.ts +326 -0
- package/src/setup-core.ts +50 -0
- package/src/setup-surface.ts +4 -0
- package/src/startup-maintenance.test.ts +227 -0
- package/src/startup-maintenance.ts +114 -0
- package/src/storage-paths.ts +92 -0
- package/src/test-helpers.ts +42 -0
- package/src/test-mocks.ts +55 -0
- package/src/test-runtime.ts +72 -0
- package/src/test-support/monitor-route-test-support.ts +8 -0
- package/src/tool-actions.runtime.ts +1 -0
- package/src/tool-actions.test.ts +422 -0
- package/src/tool-actions.ts +498 -0
- package/src/types.ts +230 -0
- package/test-api.ts +2 -0
- package/thread-bindings-runtime.ts +4 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VerificationPhase,
|
|
3
|
+
VerificationRequestEvent,
|
|
4
|
+
VerifierEvent,
|
|
5
|
+
} from "@archipelagolab/lobi-js-sdk/lib/crypto-api/verification.js";
|
|
6
|
+
import { VerificationMethod } from "@archipelagolab/lobi-js-sdk/lib/types.js";
|
|
7
|
+
import { formatMatrixErrorMessage } from "../errors.js";
|
|
8
|
+
|
|
9
|
+
export type MatrixVerificationMethod = "sas" | "show-qr" | "scan-qr";
|
|
10
|
+
type MatrixVerificationPhase = VerificationPhase | -1;
|
|
11
|
+
|
|
12
|
+
const MATRIX_VERIFICATION_PHASES = new Set<MatrixVerificationPhase>([
|
|
13
|
+
-1,
|
|
14
|
+
VerificationPhase.Unsent,
|
|
15
|
+
VerificationPhase.Requested,
|
|
16
|
+
VerificationPhase.Ready,
|
|
17
|
+
VerificationPhase.Started,
|
|
18
|
+
VerificationPhase.Cancelled,
|
|
19
|
+
VerificationPhase.Done,
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
function isMatrixVerificationPhase(value: unknown): value is MatrixVerificationPhase {
|
|
23
|
+
return (
|
|
24
|
+
typeof value === "number" && MATRIX_VERIFICATION_PHASES.has(value as MatrixVerificationPhase)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type MatrixVerificationSummary = {
|
|
29
|
+
id: string;
|
|
30
|
+
transactionId?: string;
|
|
31
|
+
roomId?: string;
|
|
32
|
+
otherUserId: string;
|
|
33
|
+
otherDeviceId?: string;
|
|
34
|
+
isSelfVerification: boolean;
|
|
35
|
+
initiatedByMe: boolean;
|
|
36
|
+
phase: number;
|
|
37
|
+
phaseName: string;
|
|
38
|
+
pending: boolean;
|
|
39
|
+
methods: string[];
|
|
40
|
+
chosenMethod?: string | null;
|
|
41
|
+
canAccept: boolean;
|
|
42
|
+
hasSas: boolean;
|
|
43
|
+
sas?: {
|
|
44
|
+
decimal?: [number, number, number];
|
|
45
|
+
emoji?: Array<[string, string]>;
|
|
46
|
+
};
|
|
47
|
+
hasReciprocateQr: boolean;
|
|
48
|
+
completed: boolean;
|
|
49
|
+
error?: string;
|
|
50
|
+
createdAt: string;
|
|
51
|
+
updatedAt: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type MatrixVerificationSummaryListener = (summary: MatrixVerificationSummary) => void;
|
|
55
|
+
|
|
56
|
+
export type MatrixShowSasCallbacks = {
|
|
57
|
+
sas: {
|
|
58
|
+
decimal?: [number, number, number];
|
|
59
|
+
emoji?: Array<[string, string]>;
|
|
60
|
+
};
|
|
61
|
+
confirm: () => Promise<void>;
|
|
62
|
+
mismatch: () => void;
|
|
63
|
+
cancel: () => void;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type MatrixShowQrCodeCallbacks = {
|
|
67
|
+
confirm: () => void;
|
|
68
|
+
cancel: () => void;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type MatrixVerifierLike = {
|
|
72
|
+
verify: () => Promise<void>;
|
|
73
|
+
cancel: (e: Error) => void;
|
|
74
|
+
getShowSasCallbacks: () => MatrixShowSasCallbacks | null;
|
|
75
|
+
getReciprocateQrCodeCallbacks: () => MatrixShowQrCodeCallbacks | null;
|
|
76
|
+
on: (eventName: string, listener: (...args: unknown[]) => void) => void;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export type MatrixVerificationRequestLike = {
|
|
80
|
+
transactionId?: string;
|
|
81
|
+
roomId?: string;
|
|
82
|
+
initiatedByMe: boolean;
|
|
83
|
+
otherUserId: string;
|
|
84
|
+
otherDeviceId?: string;
|
|
85
|
+
isSelfVerification: boolean;
|
|
86
|
+
phase: number;
|
|
87
|
+
pending: boolean;
|
|
88
|
+
accepting: boolean;
|
|
89
|
+
declining: boolean;
|
|
90
|
+
methods: string[];
|
|
91
|
+
chosenMethod?: string | null;
|
|
92
|
+
cancellationCode?: string | null;
|
|
93
|
+
accept: () => Promise<void>;
|
|
94
|
+
cancel: (params?: { reason?: string; code?: string }) => Promise<void>;
|
|
95
|
+
startVerification: (method: string) => Promise<MatrixVerifierLike>;
|
|
96
|
+
scanQRCode: (qrCodeData: Uint8ClampedArray) => Promise<MatrixVerifierLike>;
|
|
97
|
+
generateQRCode: () => Promise<Uint8ClampedArray | undefined>;
|
|
98
|
+
verifier?: MatrixVerifierLike;
|
|
99
|
+
on: (eventName: string, listener: (...args: unknown[]) => void) => void;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export type MatrixVerificationCryptoApi = {
|
|
103
|
+
requestOwnUserVerification: () => Promise<MatrixVerificationRequestLike | null>;
|
|
104
|
+
findVerificationRequestDMInProgress?: (
|
|
105
|
+
roomId: string,
|
|
106
|
+
userId: string,
|
|
107
|
+
) => MatrixVerificationRequestLike | undefined;
|
|
108
|
+
requestDeviceVerification?: (
|
|
109
|
+
userId: string,
|
|
110
|
+
deviceId: string,
|
|
111
|
+
) => Promise<MatrixVerificationRequestLike>;
|
|
112
|
+
requestVerificationDM?: (
|
|
113
|
+
userId: string,
|
|
114
|
+
roomId: string,
|
|
115
|
+
) => Promise<MatrixVerificationRequestLike>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type MatrixVerificationSession = {
|
|
119
|
+
id: string;
|
|
120
|
+
request: MatrixVerificationRequestLike;
|
|
121
|
+
createdAtMs: number;
|
|
122
|
+
updatedAtMs: number;
|
|
123
|
+
error?: string;
|
|
124
|
+
activeVerifier?: MatrixVerifierLike;
|
|
125
|
+
verifyPromise?: Promise<void>;
|
|
126
|
+
verifyStarted: boolean;
|
|
127
|
+
startRequested: boolean;
|
|
128
|
+
acceptRequested: boolean;
|
|
129
|
+
sasAutoConfirmStarted: boolean;
|
|
130
|
+
sasAutoConfirmTimer?: ReturnType<typeof setTimeout>;
|
|
131
|
+
sasCallbacks?: MatrixShowSasCallbacks;
|
|
132
|
+
reciprocateQrCallbacks?: MatrixShowQrCodeCallbacks;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const MAX_TRACKED_VERIFICATION_SESSIONS = 256;
|
|
136
|
+
const TERMINAL_SESSION_RETENTION_MS = 24 * 60 * 60 * 1000;
|
|
137
|
+
const SAS_AUTO_CONFIRM_DELAY_MS = 30_000;
|
|
138
|
+
|
|
139
|
+
export class MatrixVerificationManager {
|
|
140
|
+
private readonly verificationSessions = new Map<string, MatrixVerificationSession>();
|
|
141
|
+
private verificationSessionCounter = 0;
|
|
142
|
+
private readonly trackedVerificationRequests = new WeakSet<object>();
|
|
143
|
+
private readonly trackedVerificationVerifiers = new WeakSet<object>();
|
|
144
|
+
private readonly summaryListeners = new Set<MatrixVerificationSummaryListener>();
|
|
145
|
+
|
|
146
|
+
private readRequestValue<T>(
|
|
147
|
+
request: MatrixVerificationRequestLike,
|
|
148
|
+
reader: () => T,
|
|
149
|
+
fallback: T,
|
|
150
|
+
): T {
|
|
151
|
+
try {
|
|
152
|
+
return reader();
|
|
153
|
+
} catch {
|
|
154
|
+
return fallback;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private readVerificationPhase(
|
|
159
|
+
request: MatrixVerificationRequestLike,
|
|
160
|
+
fallback: MatrixVerificationPhase,
|
|
161
|
+
): MatrixVerificationPhase {
|
|
162
|
+
const phase = this.readRequestValue<unknown>(request, () => request.phase, fallback);
|
|
163
|
+
return isMatrixVerificationPhase(phase) ? phase : fallback;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private pruneVerificationSessions(nowMs: number): void {
|
|
167
|
+
for (const [id, session] of this.verificationSessions) {
|
|
168
|
+
const phase = this.readVerificationPhase(session.request, -1);
|
|
169
|
+
const isTerminal = phase === VerificationPhase.Done || phase === VerificationPhase.Cancelled;
|
|
170
|
+
if (isTerminal && nowMs - session.updatedAtMs > TERMINAL_SESSION_RETENTION_MS) {
|
|
171
|
+
this.verificationSessions.delete(id);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (this.verificationSessions.size <= MAX_TRACKED_VERIFICATION_SESSIONS) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const sortedByAge = Array.from(this.verificationSessions.entries()).toSorted(
|
|
180
|
+
(a, b) => a[1].updatedAtMs - b[1].updatedAtMs,
|
|
181
|
+
);
|
|
182
|
+
const overflow = this.verificationSessions.size - MAX_TRACKED_VERIFICATION_SESSIONS;
|
|
183
|
+
for (let i = 0; i < overflow; i += 1) {
|
|
184
|
+
const entry = sortedByAge[i];
|
|
185
|
+
if (entry) {
|
|
186
|
+
this.verificationSessions.delete(entry[0]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private getVerificationPhaseName(phase: MatrixVerificationPhase): string {
|
|
192
|
+
switch (phase) {
|
|
193
|
+
case VerificationPhase.Unsent:
|
|
194
|
+
return "unsent";
|
|
195
|
+
case VerificationPhase.Requested:
|
|
196
|
+
return "requested";
|
|
197
|
+
case VerificationPhase.Ready:
|
|
198
|
+
return "ready";
|
|
199
|
+
case VerificationPhase.Started:
|
|
200
|
+
return "started";
|
|
201
|
+
case VerificationPhase.Cancelled:
|
|
202
|
+
return "cancelled";
|
|
203
|
+
case VerificationPhase.Done:
|
|
204
|
+
return "done";
|
|
205
|
+
default:
|
|
206
|
+
return `unknown(${phase})`;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private emitVerificationSummary(session: MatrixVerificationSession): void {
|
|
211
|
+
const summary = this.buildVerificationSummary(session);
|
|
212
|
+
for (const listener of this.summaryListeners) {
|
|
213
|
+
listener(summary);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private touchVerificationSession(session: MatrixVerificationSession): void {
|
|
218
|
+
session.updatedAtMs = Date.now();
|
|
219
|
+
this.emitVerificationSummary(session);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private clearSasAutoConfirmTimer(session: MatrixVerificationSession): void {
|
|
223
|
+
if (!session.sasAutoConfirmTimer) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
clearTimeout(session.sasAutoConfirmTimer);
|
|
227
|
+
session.sasAutoConfirmTimer = undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private buildVerificationSummary(session: MatrixVerificationSession): MatrixVerificationSummary {
|
|
231
|
+
const request = session.request;
|
|
232
|
+
const phase = this.readVerificationPhase(request, VerificationPhase.Requested);
|
|
233
|
+
const accepting = this.readRequestValue(request, () => request.accepting, false);
|
|
234
|
+
const declining = this.readRequestValue(request, () => request.declining, false);
|
|
235
|
+
const pending = this.readRequestValue(request, () => request.pending, false);
|
|
236
|
+
const methodsRaw = this.readRequestValue<unknown>(request, () => request.methods, []);
|
|
237
|
+
const methods = Array.isArray(methodsRaw)
|
|
238
|
+
? methodsRaw.filter((entry): entry is string => typeof entry === "string")
|
|
239
|
+
: [];
|
|
240
|
+
const sasCallbacks = session.sasCallbacks ?? session.activeVerifier?.getShowSasCallbacks();
|
|
241
|
+
if (sasCallbacks) {
|
|
242
|
+
session.sasCallbacks = sasCallbacks;
|
|
243
|
+
}
|
|
244
|
+
const canAccept = phase < VerificationPhase.Ready && !accepting && !declining;
|
|
245
|
+
return {
|
|
246
|
+
id: session.id,
|
|
247
|
+
transactionId: this.readRequestValue(request, () => request.transactionId, undefined),
|
|
248
|
+
roomId: this.readRequestValue(request, () => request.roomId, undefined),
|
|
249
|
+
otherUserId: this.readRequestValue(request, () => request.otherUserId, "unknown"),
|
|
250
|
+
otherDeviceId: this.readRequestValue(request, () => request.otherDeviceId, undefined),
|
|
251
|
+
isSelfVerification: this.readRequestValue(request, () => request.isSelfVerification, false),
|
|
252
|
+
initiatedByMe: this.readRequestValue(request, () => request.initiatedByMe, false),
|
|
253
|
+
phase,
|
|
254
|
+
phaseName: this.getVerificationPhaseName(phase),
|
|
255
|
+
pending,
|
|
256
|
+
methods,
|
|
257
|
+
chosenMethod: this.readRequestValue(request, () => request.chosenMethod ?? null, null),
|
|
258
|
+
canAccept,
|
|
259
|
+
hasSas: Boolean(sasCallbacks),
|
|
260
|
+
sas: sasCallbacks
|
|
261
|
+
? {
|
|
262
|
+
decimal: sasCallbacks.sas.decimal,
|
|
263
|
+
emoji: sasCallbacks.sas.emoji,
|
|
264
|
+
}
|
|
265
|
+
: undefined,
|
|
266
|
+
hasReciprocateQr: Boolean(session.reciprocateQrCallbacks),
|
|
267
|
+
completed: phase === VerificationPhase.Done,
|
|
268
|
+
error: session.error,
|
|
269
|
+
createdAt: new Date(session.createdAtMs).toISOString(),
|
|
270
|
+
updatedAt: new Date(session.updatedAtMs).toISOString(),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private findVerificationSession(id: string): MatrixVerificationSession {
|
|
275
|
+
const direct = this.verificationSessions.get(id);
|
|
276
|
+
if (direct) {
|
|
277
|
+
return direct;
|
|
278
|
+
}
|
|
279
|
+
for (const session of this.verificationSessions.values()) {
|
|
280
|
+
const txId = this.readRequestValue(session.request, () => session.request.transactionId, "");
|
|
281
|
+
if (txId === id) {
|
|
282
|
+
return session;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
throw new Error(`Matrix verification request not found: ${id}`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private ensureVerificationRequestTracked(session: MatrixVerificationSession): void {
|
|
289
|
+
const requestObj = session.request as unknown as object;
|
|
290
|
+
if (this.trackedVerificationRequests.has(requestObj)) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
this.trackedVerificationRequests.add(requestObj);
|
|
294
|
+
session.request.on(VerificationRequestEvent.Change, () => {
|
|
295
|
+
this.touchVerificationSession(session);
|
|
296
|
+
this.maybeAutoAcceptInboundRequest(session);
|
|
297
|
+
const verifier = this.readRequestValue(session.request, () => session.request.verifier, null);
|
|
298
|
+
if (verifier) {
|
|
299
|
+
this.attachVerifierToVerificationSession(session, verifier);
|
|
300
|
+
}
|
|
301
|
+
this.maybeAutoStartInboundSas(session);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private maybeAutoAcceptInboundRequest(session: MatrixVerificationSession): void {
|
|
306
|
+
if (session.acceptRequested) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const request = session.request;
|
|
310
|
+
const isSelfVerification = this.readRequestValue(
|
|
311
|
+
request,
|
|
312
|
+
() => request.isSelfVerification,
|
|
313
|
+
false,
|
|
314
|
+
);
|
|
315
|
+
const initiatedByMe = this.readRequestValue(request, () => request.initiatedByMe, false);
|
|
316
|
+
const phase = this.readVerificationPhase(request, VerificationPhase.Requested);
|
|
317
|
+
const accepting = this.readRequestValue(request, () => request.accepting, false);
|
|
318
|
+
const declining = this.readRequestValue(request, () => request.declining, false);
|
|
319
|
+
if (isSelfVerification || initiatedByMe) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (phase !== VerificationPhase.Requested || accepting || declining) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
session.acceptRequested = true;
|
|
327
|
+
void request
|
|
328
|
+
.accept()
|
|
329
|
+
.then(() => {
|
|
330
|
+
this.touchVerificationSession(session);
|
|
331
|
+
})
|
|
332
|
+
.catch((err) => {
|
|
333
|
+
session.acceptRequested = false;
|
|
334
|
+
session.error = formatMatrixErrorMessage(err);
|
|
335
|
+
this.touchVerificationSession(session);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private maybeAutoStartInboundSas(session: MatrixVerificationSession): void {
|
|
340
|
+
if (session.activeVerifier || session.verifyStarted || session.startRequested) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if (this.readRequestValue(session.request, () => session.request.initiatedByMe, true)) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (!this.readRequestValue(session.request, () => session.request.isSelfVerification, false)) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const phase = this.readVerificationPhase(session.request, VerificationPhase.Requested);
|
|
350
|
+
if (phase < VerificationPhase.Ready || phase >= VerificationPhase.Cancelled) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const methodsRaw = this.readRequestValue<unknown>(
|
|
354
|
+
session.request,
|
|
355
|
+
() => session.request.methods,
|
|
356
|
+
[],
|
|
357
|
+
);
|
|
358
|
+
const methods = Array.isArray(methodsRaw)
|
|
359
|
+
? methodsRaw.filter((entry): entry is string => typeof entry === "string")
|
|
360
|
+
: [];
|
|
361
|
+
const chosenMethod = this.readRequestValue(
|
|
362
|
+
session.request,
|
|
363
|
+
() => session.request.chosenMethod,
|
|
364
|
+
null,
|
|
365
|
+
);
|
|
366
|
+
const supportsSas =
|
|
367
|
+
methods.includes(VerificationMethod.Sas) || chosenMethod === VerificationMethod.Sas;
|
|
368
|
+
if (!supportsSas) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
session.startRequested = true;
|
|
373
|
+
void session.request
|
|
374
|
+
.startVerification(VerificationMethod.Sas)
|
|
375
|
+
.then((verifier) => {
|
|
376
|
+
this.attachVerifierToVerificationSession(session, verifier);
|
|
377
|
+
this.touchVerificationSession(session);
|
|
378
|
+
})
|
|
379
|
+
.catch(() => {
|
|
380
|
+
session.startRequested = false;
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private attachVerifierToVerificationSession(
|
|
385
|
+
session: MatrixVerificationSession,
|
|
386
|
+
verifier: MatrixVerifierLike,
|
|
387
|
+
): void {
|
|
388
|
+
session.activeVerifier = verifier;
|
|
389
|
+
this.touchVerificationSession(session);
|
|
390
|
+
|
|
391
|
+
const maybeSas = verifier.getShowSasCallbacks();
|
|
392
|
+
if (maybeSas) {
|
|
393
|
+
session.sasCallbacks = maybeSas;
|
|
394
|
+
this.maybeAutoConfirmSas(session);
|
|
395
|
+
}
|
|
396
|
+
const maybeReciprocateQr = verifier.getReciprocateQrCodeCallbacks();
|
|
397
|
+
if (maybeReciprocateQr) {
|
|
398
|
+
session.reciprocateQrCallbacks = maybeReciprocateQr;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const verifierObj = verifier as unknown as object;
|
|
402
|
+
if (this.trackedVerificationVerifiers.has(verifierObj)) {
|
|
403
|
+
this.ensureVerificationStarted(session);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
this.trackedVerificationVerifiers.add(verifierObj);
|
|
407
|
+
|
|
408
|
+
verifier.on(VerifierEvent.ShowSas, (sas) => {
|
|
409
|
+
session.sasCallbacks = sas as MatrixShowSasCallbacks;
|
|
410
|
+
this.touchVerificationSession(session);
|
|
411
|
+
this.maybeAutoConfirmSas(session);
|
|
412
|
+
});
|
|
413
|
+
verifier.on(VerifierEvent.ShowReciprocateQr, (qr) => {
|
|
414
|
+
session.reciprocateQrCallbacks = qr as MatrixShowQrCodeCallbacks;
|
|
415
|
+
this.touchVerificationSession(session);
|
|
416
|
+
});
|
|
417
|
+
verifier.on(VerifierEvent.Cancel, (err) => {
|
|
418
|
+
this.clearSasAutoConfirmTimer(session);
|
|
419
|
+
session.error = formatMatrixErrorMessage(err);
|
|
420
|
+
this.touchVerificationSession(session);
|
|
421
|
+
});
|
|
422
|
+
this.ensureVerificationStarted(session);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
private maybeAutoConfirmSas(session: MatrixVerificationSession): void {
|
|
426
|
+
if (session.sasAutoConfirmStarted || session.sasAutoConfirmTimer) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (this.readRequestValue(session.request, () => session.request.initiatedByMe, true)) {
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const callbacks = session.sasCallbacks ?? session.activeVerifier?.getShowSasCallbacks();
|
|
433
|
+
if (!callbacks) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
session.sasCallbacks = callbacks;
|
|
437
|
+
// Give the remote client a moment to surface the compare-emoji UI before
|
|
438
|
+
// we send our MAC and finish our side of the SAS flow.
|
|
439
|
+
session.sasAutoConfirmTimer = setTimeout(() => {
|
|
440
|
+
session.sasAutoConfirmTimer = undefined;
|
|
441
|
+
const phase = this.readVerificationPhase(session.request, VerificationPhase.Requested);
|
|
442
|
+
if (phase >= VerificationPhase.Cancelled) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
session.sasAutoConfirmStarted = true;
|
|
446
|
+
void callbacks
|
|
447
|
+
.confirm()
|
|
448
|
+
.then(() => {
|
|
449
|
+
this.touchVerificationSession(session);
|
|
450
|
+
})
|
|
451
|
+
.catch((err) => {
|
|
452
|
+
session.error = formatMatrixErrorMessage(err);
|
|
453
|
+
this.touchVerificationSession(session);
|
|
454
|
+
});
|
|
455
|
+
}, SAS_AUTO_CONFIRM_DELAY_MS);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
private ensureVerificationStarted(session: MatrixVerificationSession): void {
|
|
459
|
+
if (!session.activeVerifier || session.verifyStarted) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
session.verifyStarted = true;
|
|
463
|
+
const verifier = session.activeVerifier;
|
|
464
|
+
session.verifyPromise = verifier
|
|
465
|
+
.verify()
|
|
466
|
+
.then(() => {
|
|
467
|
+
this.touchVerificationSession(session);
|
|
468
|
+
})
|
|
469
|
+
.catch((err) => {
|
|
470
|
+
session.error = formatMatrixErrorMessage(err);
|
|
471
|
+
this.touchVerificationSession(session);
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
onSummaryChanged(listener: MatrixVerificationSummaryListener): () => void {
|
|
476
|
+
this.summaryListeners.add(listener);
|
|
477
|
+
return () => {
|
|
478
|
+
this.summaryListeners.delete(listener);
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
trackVerificationRequest(request: MatrixVerificationRequestLike): MatrixVerificationSummary {
|
|
483
|
+
this.pruneVerificationSessions(Date.now());
|
|
484
|
+
const txId = this.readRequestValue(request, () => request.transactionId?.trim(), "");
|
|
485
|
+
if (txId) {
|
|
486
|
+
for (const existing of this.verificationSessions.values()) {
|
|
487
|
+
const existingTxId = this.readRequestValue(
|
|
488
|
+
existing.request,
|
|
489
|
+
() => existing.request.transactionId,
|
|
490
|
+
"",
|
|
491
|
+
);
|
|
492
|
+
if (existingTxId === txId) {
|
|
493
|
+
existing.request = request;
|
|
494
|
+
this.ensureVerificationRequestTracked(existing);
|
|
495
|
+
const verifier = this.readRequestValue(request, () => request.verifier, null);
|
|
496
|
+
if (verifier) {
|
|
497
|
+
this.attachVerifierToVerificationSession(existing, verifier);
|
|
498
|
+
}
|
|
499
|
+
this.touchVerificationSession(existing);
|
|
500
|
+
return this.buildVerificationSummary(existing);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const now = Date.now();
|
|
506
|
+
const id = `verification-${++this.verificationSessionCounter}`;
|
|
507
|
+
const session: MatrixVerificationSession = {
|
|
508
|
+
id,
|
|
509
|
+
request,
|
|
510
|
+
createdAtMs: now,
|
|
511
|
+
updatedAtMs: now,
|
|
512
|
+
verifyStarted: false,
|
|
513
|
+
startRequested: false,
|
|
514
|
+
acceptRequested: false,
|
|
515
|
+
sasAutoConfirmStarted: false,
|
|
516
|
+
};
|
|
517
|
+
this.verificationSessions.set(session.id, session);
|
|
518
|
+
this.ensureVerificationRequestTracked(session);
|
|
519
|
+
this.maybeAutoAcceptInboundRequest(session);
|
|
520
|
+
const verifier = this.readRequestValue(request, () => request.verifier, null);
|
|
521
|
+
if (verifier) {
|
|
522
|
+
this.attachVerifierToVerificationSession(session, verifier);
|
|
523
|
+
}
|
|
524
|
+
this.maybeAutoStartInboundSas(session);
|
|
525
|
+
this.emitVerificationSummary(session);
|
|
526
|
+
return this.buildVerificationSummary(session);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
async requestOwnUserVerification(
|
|
530
|
+
crypto: MatrixVerificationCryptoApi | undefined,
|
|
531
|
+
): Promise<MatrixVerificationSummary | null> {
|
|
532
|
+
if (!crypto) {
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
const request = await crypto.requestOwnUserVerification();
|
|
536
|
+
if (!request) {
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
return this.trackVerificationRequest(request);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
listVerifications(): MatrixVerificationSummary[] {
|
|
543
|
+
this.pruneVerificationSessions(Date.now());
|
|
544
|
+
const summaries = Array.from(this.verificationSessions.values()).map((session) =>
|
|
545
|
+
this.buildVerificationSummary(session),
|
|
546
|
+
);
|
|
547
|
+
return summaries.toSorted((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async requestVerification(
|
|
551
|
+
crypto: MatrixVerificationCryptoApi | undefined,
|
|
552
|
+
params: {
|
|
553
|
+
ownUser?: boolean;
|
|
554
|
+
userId?: string;
|
|
555
|
+
deviceId?: string;
|
|
556
|
+
roomId?: string;
|
|
557
|
+
},
|
|
558
|
+
): Promise<MatrixVerificationSummary> {
|
|
559
|
+
if (!crypto) {
|
|
560
|
+
throw new Error("Matrix crypto is not available");
|
|
561
|
+
}
|
|
562
|
+
let request: MatrixVerificationRequestLike | null = null;
|
|
563
|
+
if (params.ownUser) {
|
|
564
|
+
request = await crypto.requestOwnUserVerification();
|
|
565
|
+
} else if (params.userId && params.deviceId && crypto.requestDeviceVerification) {
|
|
566
|
+
request = await crypto.requestDeviceVerification(params.userId, params.deviceId);
|
|
567
|
+
} else if (params.userId && params.roomId && crypto.requestVerificationDM) {
|
|
568
|
+
request = await crypto.requestVerificationDM(params.userId, params.roomId);
|
|
569
|
+
} else {
|
|
570
|
+
throw new Error(
|
|
571
|
+
"Matrix verification request requires one of: ownUser, userId+deviceId, or userId+roomId",
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (!request) {
|
|
576
|
+
throw new Error("Matrix verification request could not be created");
|
|
577
|
+
}
|
|
578
|
+
return this.trackVerificationRequest(request);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
async acceptVerification(id: string): Promise<MatrixVerificationSummary> {
|
|
582
|
+
const session = this.findVerificationSession(id);
|
|
583
|
+
await session.request.accept();
|
|
584
|
+
this.touchVerificationSession(session);
|
|
585
|
+
return this.buildVerificationSummary(session);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
async cancelVerification(
|
|
589
|
+
id: string,
|
|
590
|
+
params?: { reason?: string; code?: string },
|
|
591
|
+
): Promise<MatrixVerificationSummary> {
|
|
592
|
+
const session = this.findVerificationSession(id);
|
|
593
|
+
await session.request.cancel(params);
|
|
594
|
+
this.touchVerificationSession(session);
|
|
595
|
+
return this.buildVerificationSummary(session);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
async startVerification(
|
|
599
|
+
id: string,
|
|
600
|
+
method: MatrixVerificationMethod = "sas",
|
|
601
|
+
): Promise<MatrixVerificationSummary> {
|
|
602
|
+
const session = this.findVerificationSession(id);
|
|
603
|
+
if (method !== "sas") {
|
|
604
|
+
throw new Error("Matrix startVerification currently supports only SAS directly");
|
|
605
|
+
}
|
|
606
|
+
const verifier = await session.request.startVerification(VerificationMethod.Sas);
|
|
607
|
+
this.attachVerifierToVerificationSession(session, verifier);
|
|
608
|
+
this.ensureVerificationStarted(session);
|
|
609
|
+
return this.buildVerificationSummary(session);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
async generateVerificationQr(id: string): Promise<{ qrDataBase64: string }> {
|
|
613
|
+
const session = this.findVerificationSession(id);
|
|
614
|
+
const qr = await session.request.generateQRCode();
|
|
615
|
+
if (!qr) {
|
|
616
|
+
throw new Error("Matrix verification QR data is not available yet");
|
|
617
|
+
}
|
|
618
|
+
return { qrDataBase64: Buffer.from(qr).toString("base64") };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
async scanVerificationQr(id: string, qrDataBase64: string): Promise<MatrixVerificationSummary> {
|
|
622
|
+
const session = this.findVerificationSession(id);
|
|
623
|
+
const trimmed = qrDataBase64.trim();
|
|
624
|
+
if (!trimmed) {
|
|
625
|
+
throw new Error("Matrix verification QR payload is required");
|
|
626
|
+
}
|
|
627
|
+
const qrBytes = Buffer.from(trimmed, "base64");
|
|
628
|
+
if (qrBytes.length === 0) {
|
|
629
|
+
throw new Error("Matrix verification QR payload is invalid base64");
|
|
630
|
+
}
|
|
631
|
+
const verifier = await session.request.scanQRCode(new Uint8ClampedArray(qrBytes));
|
|
632
|
+
this.attachVerifierToVerificationSession(session, verifier);
|
|
633
|
+
this.ensureVerificationStarted(session);
|
|
634
|
+
return this.buildVerificationSummary(session);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
async confirmVerificationSas(id: string): Promise<MatrixVerificationSummary> {
|
|
638
|
+
const session = this.findVerificationSession(id);
|
|
639
|
+
const callbacks = session.sasCallbacks ?? session.activeVerifier?.getShowSasCallbacks();
|
|
640
|
+
if (!callbacks) {
|
|
641
|
+
throw new Error("Matrix SAS confirmation is not available for this verification request");
|
|
642
|
+
}
|
|
643
|
+
this.clearSasAutoConfirmTimer(session);
|
|
644
|
+
session.sasCallbacks = callbacks;
|
|
645
|
+
session.sasAutoConfirmStarted = true;
|
|
646
|
+
await callbacks.confirm();
|
|
647
|
+
this.touchVerificationSession(session);
|
|
648
|
+
return this.buildVerificationSummary(session);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
mismatchVerificationSas(id: string): MatrixVerificationSummary {
|
|
652
|
+
const session = this.findVerificationSession(id);
|
|
653
|
+
const callbacks = session.sasCallbacks ?? session.activeVerifier?.getShowSasCallbacks();
|
|
654
|
+
if (!callbacks) {
|
|
655
|
+
throw new Error("Matrix SAS mismatch is not available for this verification request");
|
|
656
|
+
}
|
|
657
|
+
this.clearSasAutoConfirmTimer(session);
|
|
658
|
+
session.sasCallbacks = callbacks;
|
|
659
|
+
callbacks.mismatch();
|
|
660
|
+
this.touchVerificationSession(session);
|
|
661
|
+
return this.buildVerificationSummary(session);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
confirmVerificationReciprocateQr(id: string): MatrixVerificationSummary {
|
|
665
|
+
const session = this.findVerificationSession(id);
|
|
666
|
+
const callbacks =
|
|
667
|
+
session.reciprocateQrCallbacks ?? session.activeVerifier?.getReciprocateQrCodeCallbacks();
|
|
668
|
+
if (!callbacks) {
|
|
669
|
+
throw new Error(
|
|
670
|
+
"Matrix reciprocate-QR confirmation is not available for this verification request",
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
session.reciprocateQrCallbacks = callbacks;
|
|
674
|
+
callbacks.confirm();
|
|
675
|
+
this.touchVerificationSession(session);
|
|
676
|
+
return this.buildVerificationSummary(session);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
getVerificationSas(id: string): {
|
|
680
|
+
decimal?: [number, number, number];
|
|
681
|
+
emoji?: Array<[string, string]>;
|
|
682
|
+
} {
|
|
683
|
+
const session = this.findVerificationSession(id);
|
|
684
|
+
const callbacks = session.sasCallbacks ?? session.activeVerifier?.getShowSasCallbacks();
|
|
685
|
+
if (!callbacks) {
|
|
686
|
+
throw new Error("Matrix SAS data is not available for this verification request");
|
|
687
|
+
}
|
|
688
|
+
session.sasCallbacks = callbacks;
|
|
689
|
+
return {
|
|
690
|
+
decimal: callbacks.sas.decimal,
|
|
691
|
+
emoji: callbacks.sas.emoji,
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
}
|