Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine QrCodeEvent, SasEvent and VerificationEvent #3386

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/crypto-api/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixEvent } from "../models/event";

/** Events emitted by `Verifier`. */
export enum VerifierEvent {
/**
* The verification has been cancelled, by us or the other side.
*
* The payload is either an {@link Error}, or an (incoming or outgoing) {@link MatrixEvent}, depending on
* unspecified reasons.
*/
Cancel = "cancel",

/**
* SAS data has been exchanged and should be displayed to the user.
*
* The payload is the {@link ShowQrCodeCallbacks} object.
*/
ShowSas = "show_sas",

/**
* QR code data should be displayed to the user.
*
* The payload is the {@link ShowQrCodeCallbacks} object.
*/
ShowReciprocateQr = "show_reciprocate_qr",
}

/** Listener type map for {@link VerifierEvent}s. */
export type VerifierEventHandlerMap = {
[VerifierEvent.Cancel]: (e: Error | MatrixEvent) => void;
[VerifierEvent.ShowSas]: (sas: ShowSasCallbacks) => void;
[VerifierEvent.ShowReciprocateQr]: (qr: ShowQrCodeCallbacks) => void;
};

/**
* Callbacks for user actions while a QR code is displayed.
*
Expand Down
19 changes: 12 additions & 7 deletions src/crypto/verification/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { KeysDuringVerification, requestKeysDuringVerification } from "../CrossS
import { IVerificationChannel } from "./request/Channel";
import { MatrixClient } from "../../client";
import { VerificationRequest } from "./request/VerificationRequest";
import { ListenerMap, TypedEventEmitter } from "../../models/typed-event-emitter";
import { TypedEventEmitter } from "../../models/typed-event-emitter";
import { VerifierEvent, VerifierEventHandlerMap } from "../../crypto-api/verification";

const timeoutException = new Error("Verification timed out");

Expand All @@ -40,18 +41,22 @@ export class SwitchStartEventError extends Error {

export type KeyVerifier = (keyId: string, device: DeviceInfo, keyInfo: string) => void;

export enum VerificationEvent {
Cancel = "cancel",
}
/** @deprecated use VerifierEvent */
export type VerificationEvent = VerifierEvent;
/** @deprecated use VerifierEvent */
export const VerificationEvent = VerifierEvent;

/** @deprecated use VerifierEventHandlerMap */
export type VerificationEventHandlerMap = {
[VerificationEvent.Cancel]: (e: Error | MatrixEvent) => void;
};

export class VerificationBase<
richvdh marked this conversation as resolved.
Show resolved Hide resolved
Events extends string,
Arguments extends ListenerMap<Events | VerificationEvent>,
> extends TypedEventEmitter<Events | VerificationEvent, Arguments, VerificationEventHandlerMap> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Events extends string = VerifierEvent,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Arguments = VerifierEventHandlerMap,
Comment on lines +57 to +60
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these type parameters are no longer used, but we need something here to maintain backwards compatibility with applications that reference VerificationBase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add that as a comment?

> extends TypedEventEmitter<VerifierEvent, VerifierEventHandlerMap> {
private cancelled = false;
private _done = false;
private promise: Promise<void> | null = null;
Expand Down
17 changes: 7 additions & 10 deletions src/crypto/verification/QRCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@ limitations under the License.
* QR code key verification.
*/

import { VerificationBase as Base, VerificationEventHandlerMap } from "./Base";
import { VerificationBase as Base } from "./Base";
import { newKeyMismatchError, newUserCancelledError } from "./Error";
import { decodeBase64, encodeUnpaddedBase64 } from "../olmlib";
import { logger } from "../../logger";
import { VerificationRequest } from "./request/VerificationRequest";
import { MatrixClient } from "../../client";
import { IVerificationChannel } from "./request/Channel";
import { MatrixEvent } from "../../models/event";
import { ShowQrCodeCallbacks } from "../../crypto-api/verification";
import { ShowQrCodeCallbacks, VerifierEvent } from "../../crypto-api/verification";

export const SHOW_QR_CODE_METHOD = "m.qr_code.show.v1";
export const SCAN_QR_CODE_METHOD = "m.qr_code.scan.v1";

export enum QrCodeEvent {
ShowReciprocateQr = "show_reciprocate_qr",
}

type EventHandlerMap = {
[QrCodeEvent.ShowReciprocateQr]: (qr: ShowQrCodeCallbacks) => void;
} & VerificationEventHandlerMap;
/** @deprecated use VerifierEvent */
export type QrCodeEvent = VerifierEvent;
/** @deprecated use VerifierEvent */
export const QrCodeEvent = VerifierEvent;

export class ReciprocateQRCode extends Base<QrCodeEvent, EventHandlerMap> {
export class ReciprocateQRCode extends Base {
public reciprocateQREvent?: ShowQrCodeCallbacks;

public static factory(
Expand Down
17 changes: 7 additions & 10 deletions src/crypto/verification/SAS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ limitations under the License.
import anotherjson from "another-json";
import { Utility, SAS as OlmSAS } from "@matrix-org/olm";

import { VerificationBase as Base, SwitchStartEventError, VerificationEventHandlerMap } from "./Base";
import { VerificationBase as Base, SwitchStartEventError } from "./Base";
import {
errorFactory,
newInvalidMessageError,
Expand All @@ -33,7 +33,7 @@ import { logger } from "../../logger";
import { IContent, MatrixEvent } from "../../models/event";
import { generateDecimalSas } from "./SASDecimal";
import { EventType } from "../../@types/event";
import { EmojiMapping, GeneratedSas, ShowSasCallbacks } from "../../crypto-api/verification";
import { EmojiMapping, GeneratedSas, ShowSasCallbacks, VerifierEvent } from "../../crypto-api/verification";

// backwards-compatibility exports
export {
Expand Down Expand Up @@ -214,15 +214,12 @@ function intersection<T>(anArray: T[], aSet: Set<T>): T[] {
return Array.isArray(anArray) ? anArray.filter((x) => aSet.has(x)) : [];
}

export enum SasEvent {
ShowSas = "show_sas",
}

type EventHandlerMap = {
[SasEvent.ShowSas]: (sas: ShowSasCallbacks) => void;
} & VerificationEventHandlerMap;
/** @deprecated use VerifierEvent */
export type SasEvent = VerifierEvent;
/** @deprecated use VerifierEvent */
export const SasEvent = VerifierEvent;

export class SAS extends Base<SasEvent, EventHandlerMap> {
export class SAS extends Base {
private waitingForAccept?: boolean;
public ourSASPubKey?: string;
public theirSASPubKey?: string;
Expand Down