Skip to content

Commit 9c5e1d2

Browse files
committed
pull out Verifier interface
1 parent b0902cc commit 9c5e1d2

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

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 a QR code verification, if one is in progress
71+
*
72+
* Returns `null`, unless this verifier is for a QR-code-based verification and we are waiting for the user to
73+
* confirm a match.
74+
*/
75+
getShowQrCodeCallbacks(): 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: 9 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";
@@ -59,11 +60,14 @@ export type VerificationEventHandlerMap = {
5960
// The type parameters of VerificationBase are no longer used, but we need some placeholders to maintain
6061
// backwards compatibility with applications that reference the class.
6162
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> {
63+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
64+
Events extends string = VerifierEvent,
65+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66+
Arguments = VerifierEventHandlerMap,
67+
>
68+
extends TypedEventEmitter<VerifierEvent, VerifierEventHandlerMap>
69+
implements Verifier
70+
{
6771
private cancelled = false;
6872
private _done = false;
6973
private promise: Promise<void> | null = null;

0 commit comments

Comments
 (0)