Skip to content

Commit 5981fee

Browse files
authored
Element-R: Pull out an interface from VerificationBase (#3414)
* pull out `Verifier` interface * Mark old classes as deprecated * Update integration tests to use new interface
1 parent 51218dd commit 5981fee

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

spec/integ/crypto/verification.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import fetchMock from "fetch-mock-jest";
1818
import { MockResponse } from "fetch-mock";
1919

2020
import { createClient, MatrixClient } from "../../../src";
21-
import { ShowQrCodeCallbacks, ShowSasCallbacks, VerifierEvent } from "../../../src/crypto-api/verification";
21+
import { ShowQrCodeCallbacks, ShowSasCallbacks, Verifier, VerifierEvent } from "../../../src/crypto-api/verification";
2222
import { escapeRegExp } from "../../../src/utils";
23-
import { VerificationBase } from "../../../src/crypto/verification/Base";
2423
import { CRYPTO_BACKENDS, InitCrypto } from "../../test-utils/test-utils";
2524
import { SyncResponder } from "../../test-utils/SyncResponder";
2625
import {
@@ -170,7 +169,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
170169
expect(request.chosenMethod).toEqual("m.sas.v1");
171170

172171
// there should now be a verifier
173-
const verifier: VerificationBase = request.verifier!;
172+
const verifier: Verifier = request.verifier!;
174173
expect(verifier).toBeDefined();
175174
expect(verifier.getShowSasCallbacks()).toBeNull();
176175

@@ -325,7 +324,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
325324
expect(request.chosenMethod).toEqual("m.reciprocate.v1");
326325

327326
// there should now be a verifier
328-
const verifier: VerificationBase = request.verifier!;
327+
const verifier: Verifier = request.verifier!;
329328
expect(verifier).toBeDefined();
330329
expect(verifier.getReciprocateQrCodeCallbacks()).toBeNull();
331330

src/crypto-api/verification.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,67 @@ limitations under the License.
1515
*/
1616

1717
import { MatrixEvent } from "../models/event";
18+
import { TypedEventEmitter } from "../models/typed-event-emitter";
1819

19-
/** Events emitted by `Verifier`. */
20+
/**
21+
* A `Verifier` is responsible for performing the verification using a particular method, such as via QR code or SAS
22+
* (emojis).
23+
*
24+
* A verifier object can be created by calling `VerificationRequest.beginVerification`; one is also created
25+
* automatically when a `m.key.verification.start` event is received for an existing VerificationRequest.
26+
*
27+
* Once a verifier object is created, the verification can be started by calling the {@link Verifier#verify} method.
28+
*/
29+
export interface Verifier extends TypedEventEmitter<VerifierEvent, VerifierEventHandlerMap> {
30+
/**
31+
* Returns true if the verification has been cancelled, either by us or the other side.
32+
*/
33+
get hasBeenCancelled(): boolean;
34+
35+
/**
36+
* The ID of the other user in the verification process.
37+
*/
38+
get userId(): string;
39+
40+
/**
41+
* Start the key verification, if it has not already been started.
42+
*
43+
* This means sending a `m.key.verification.start` if we are the first responder, or a `m.key.verification.accept`
44+
* if the other side has already sent a start event.
45+
*
46+
* @returns Promise which resolves when the verification has completed, or rejects if the verification is cancelled
47+
* or times out.
48+
*/
49+
verify(): Promise<void>;
50+
51+
/**
52+
* Cancel a verification.
53+
*
54+
* We will send an `m.key.verification.cancel` if the verification is still in flight. The verification promise
55+
* will reject, and a {@link Crypto.VerifierEvent#Cancel} will be emitted.
56+
*
57+
* @param e - the reason for the cancellation.
58+
*/
59+
cancel(e: Error): void;
60+
61+
/**
62+
* Get the details for an SAS verification, if one is in progress
63+
*
64+
* Returns `null`, unless this verifier is for a SAS-based verification and we are waiting for the user to confirm
65+
* the SAS matches.
66+
*/
67+
getShowSasCallbacks(): ShowSasCallbacks | null;
68+
69+
/**
70+
* Get the details for reciprocating QR code verification, if one is in progress
71+
*
72+
* Returns `null`, unless this verifier is for reciprocating a QR-code-based verification (ie, the other user has
73+
* already scanned our QR code), and we are waiting for the user to confirm.
74+
*/
75+
getReciprocateQrCodeCallbacks(): ShowQrCodeCallbacks | null;
76+
}
77+
78+
/** Events emitted by {@link Verifier} */
2079
export enum VerifierEvent {
2180
/**
2281
* The verification has been cancelled, by us or the other side.
@@ -29,7 +88,7 @@ export enum VerifierEvent {
2988
/**
3089
* SAS data has been exchanged and should be displayed to the user.
3190
*
32-
* The payload is the {@link ShowQrCodeCallbacks} object.
91+
* The payload is the {@link ShowSasCallbacks} object.
3392
*/
3493
ShowSas = "show_sas",
3594

src/crypto/verification/Base.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { TypedEventEmitter } from "../../models/typed-event-emitter";
3232
import {
3333
ShowQrCodeCallbacks,
3434
ShowSasCallbacks,
35+
Verifier,
3536
VerifierEvent,
3637
VerifierEventHandlerMap,
3738
} from "../../crypto-api/verification";
@@ -56,14 +57,18 @@ export type VerificationEventHandlerMap = {
5657
[VerificationEvent.Cancel]: (e: Error | MatrixEvent) => void;
5758
};
5859

60+
/** @deprecated Avoid referencing this class directly; instead use {@link Crypto.Verifier}. */
5961
// The type parameters of VerificationBase are no longer used, but we need some placeholders to maintain
6062
// backwards compatibility with applications that reference the class.
6163
export class VerificationBase<
62-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
63-
Events extends string = VerifierEvent,
64-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
65-
Arguments = VerifierEventHandlerMap,
66-
> extends TypedEventEmitter<VerifierEvent, VerifierEventHandlerMap> {
64+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
65+
Events extends string = VerifierEvent,
66+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
67+
Arguments = VerifierEventHandlerMap,
68+
>
69+
extends TypedEventEmitter<VerifierEvent, VerifierEventHandlerMap>
70+
implements Verifier
71+
{
6772
private cancelled = false;
6873
private _done = false;
6974
private promise: Promise<void> | null = null;

src/crypto/verification/QRCode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type QrCodeEvent = VerifierEvent;
3636
/** @deprecated use VerifierEvent */
3737
export const QrCodeEvent = VerifierEvent;
3838

39+
/** @deprecated Avoid referencing this class directly; instead use {@link Crypto.Verifier}. */
3940
export class ReciprocateQRCode extends Base {
4041
public reciprocateQREvent?: ShowQrCodeCallbacks;
4142

src/crypto/verification/SAS.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export type SasEvent = VerifierEvent;
219219
/** @deprecated use VerifierEvent */
220220
export const SasEvent = VerifierEvent;
221221

222+
/** @deprecated Avoid referencing this class directly; instead use {@link Crypto.Verifier}. */
222223
export class SAS extends Base {
223224
private waitingForAccept?: boolean;
224225
public ourSASPubKey?: string;

0 commit comments

Comments
 (0)