Skip to content

Commit 4e45fb5

Browse files
committed
Define VerificationRequest interface
1 parent b24a7d0 commit 4e45fb5

File tree

2 files changed

+149
-5
lines changed

2 files changed

+149
-5
lines changed

src/crypto-api/verification.ts

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,148 @@ limitations under the License.
1717
import { MatrixEvent } from "../models/event";
1818
import { TypedEventEmitter } from "../models/typed-event-emitter";
1919

20-
/** Events emitted by `VerificationRequest` */
20+
/**
21+
* An incoming, or outgoing, request to verify a user or a device via cross-signing.
22+
*/
23+
export interface VerificationRequest
24+
extends TypedEventEmitter<VerificationRequestEvent, VerificationRequestEventHandlerMap> {
25+
/**
26+
* Unique ID for this verification request.
27+
*
28+
* An ID isn't assigned until the first message is sent, so this may be `undefined` in the early phases.
29+
*/
30+
get transactionId(): string | undefined;
31+
32+
/**
33+
* For an in-room verification, the ID of the room.
34+
*
35+
* For to-device verifictions, `undefined`.
36+
*/
37+
get roomId(): string | undefined;
38+
39+
/**
40+
* True if this request was initiated by the local client.
41+
*
42+
* For in-room verifications, the initiator is who sent the `m.key.verification.request` event.
43+
* For to-device verifications, the initiator is who sent the `m.key.verification.start` event.
44+
*/
45+
get initiatedByMe(): boolean;
46+
47+
/** The user id of the other party in this request */
48+
get otherUserId(): string;
49+
50+
/** For verifications via to-device messages: the ID of the other device. Otherwise, undefined. */
51+
get otherDeviceId(): string | undefined;
52+
53+
/** True if the other party in this request is one of this user's own devices. */
54+
get isSelfVerification(): boolean;
55+
56+
/** current phase of the request. */
57+
get phase(): VerificationPhase;
58+
59+
/** True if the request has sent its initial event and needs more events to complete
60+
* (ie it is in phase `Requested`, `Ready` or `Started`).
61+
*/
62+
get pending(): boolean;
63+
64+
/**
65+
* True if we have started the process of sending an `m.key.verification.ready` (but have not necessarily received
66+
* the remote echo which causes a transition to {@link VerificationPhase.Ready}.
67+
*/
68+
get accepting(): boolean;
69+
70+
/**
71+
* True if we have started the process of sending an `m.key.verification.cancel` (but have not necessarily received
72+
* the remote echo which causes a transition to {@link VerificationPhase.Cancelled}).
73+
*/
74+
get declining(): boolean;
75+
76+
/**
77+
* The remaining number of ms before the request will be automatically cancelled.
78+
*
79+
* `null` indicates that there is no timeout
80+
*/
81+
get timeout(): number | null;
82+
83+
/** once the phase is Started (and !initiatedByMe) or Ready: common methods supported by both sides */
84+
get methods(): string[];
85+
86+
/** the method picked in the .start event */
87+
get chosenMethod(): string | null;
88+
89+
/**
90+
* Checks whether the other party supports a given verification method.
91+
* This is useful when setting up the QR code UI, as it is somewhat asymmetrical:
92+
* if the other party supports SCAN_QR, we should show a QR code in the UI, and vice versa.
93+
* For methods that need to be supported by both ends, use the `methods` property.
94+
*
95+
* @param method - the method to check
96+
* @returns true if the other party said they supported the method
97+
*/
98+
otherPartySupportsMethod(method: string): boolean;
99+
100+
/**
101+
* Accepts the request, sending a .ready event to the other party
102+
*
103+
* @returns Promise which resolves when the event has been sent.
104+
*/
105+
accept(): Promise<void>;
106+
107+
/**
108+
* Cancels the request, sending a cancellation to the other party
109+
*
110+
* @param params - Details for the cancellation, including `reason` (defaults to "User declined"), and `code`
111+
* (defaults to `m.user`).
112+
*
113+
* @returns Promise which resolves when the event has been sent.
114+
*/
115+
cancel(params?: { reason?: string; code?: string }): Promise<void>;
116+
117+
/**
118+
* Create a {@link Verifier} to do this verification via a particular method.
119+
*
120+
* If a verifier has already been created for this request, returns that verifier.
121+
*
122+
* This does *not* send the `m.key.verification.start` event - to do so, call {@link Verifier#verifier} on the
123+
* returned verifier.
124+
*
125+
* If no previous events have been sent, pass in `targetDevice` to set who to direct this request to.
126+
*
127+
* @param method - the name of the verification method to use.
128+
* @param targetDevice - details of where to send the request to.
129+
*
130+
* @returns The verifier which will do the actual verification.
131+
*/
132+
beginKeyVerification(method: string, targetDevice?: { userId?: string; deviceId?: string }): Verifier;
133+
134+
/**
135+
* The verifier which is doing the actual verification, once the method has been established.
136+
* Only defined when the `phase` is Started.
137+
*/
138+
get verifier(): Verifier | undefined;
139+
140+
/**
141+
* Get the data for a QR code allowing the other device to verify this one, if it supports it.
142+
*
143+
* Only set after a .ready if the other party can scan a QR code, otherwise undefined.
144+
*/
145+
getQRCodeBytes(): Buffer | undefined;
146+
147+
/**
148+
* If this request has been cancelled, the cancellation code (e.g `m.user`) which is responsible for cancelling
149+
* this verification.
150+
*/
151+
get cancellationCode(): string | null;
152+
153+
/**
154+
* The id of the user that cancelled the request.
155+
*
156+
* Only defined when phase is Cancelled
157+
*/
158+
get cancellingUserId(): string | undefined;
159+
}
160+
161+
/** Events emitted by {@link VerificationRequest}. */
21162
export enum VerificationRequestEvent {
22163
/**
23164
* Fires whenever the state of the request object has changed.

src/crypto/verification/request/VerificationRequest.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { VerificationMethod } from "../../index";
2626
import { TypedEventEmitter } from "../../../models/typed-event-emitter";
2727
import {
2828
VerificationPhase as Phase,
29+
VerificationRequest as IVerificationRequest,
2930
VerificationRequestEvent,
3031
VerificationRequestEventHandlerMap,
3132
} from "../../../crypto-api/verification";
@@ -74,11 +75,13 @@ interface ITransition {
7475
* State machine for verification requests.
7576
* Things that differ based on what channel is used to
7677
* send and receive verification events are put in `InRoomChannel` or `ToDeviceChannel`.
78+
*
79+
* @deprecated Avoid direct references: instead prefer {@link Crypto.VerificationRequest}.
7780
*/
78-
export class VerificationRequest<C extends IVerificationChannel = IVerificationChannel> extends TypedEventEmitter<
79-
VerificationRequestEvent,
80-
VerificationRequestEventHandlerMap
81-
> {
81+
export class VerificationRequest<C extends IVerificationChannel = IVerificationChannel>
82+
extends TypedEventEmitter<VerificationRequestEvent, VerificationRequestEventHandlerMap>
83+
implements IVerificationRequest
84+
{
8285
private eventsByUs = new Map<string, MatrixEvent>();
8386
private eventsByThem = new Map<string, MatrixEvent>();
8487
private _observeOnly = false;

0 commit comments

Comments
 (0)