From 9c9d9deffeb54cc5eefd1da7142eb3663300ee5c Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Tue, 8 Oct 2024 16:14:58 +0200 Subject: [PATCH] - Rename `RustCryptoEvent` as `CryptoEvent` - Declare `CryptoEventHandlerMap` into the crypto api --- src/client.ts | 26 +++++------ src/crypto-api/CryptoEventHandlerMap.ts | 62 +++++++++++++++++++++++++ src/crypto-api/index.ts | 1 + src/rust-crypto/rust-crypto.ts | 57 +++-------------------- 4 files changed, 83 insertions(+), 63 deletions(-) create mode 100644 src/crypto-api/CryptoEventHandlerMap.ts diff --git a/src/client.ts b/src/client.ts index 8d3e686a49..ade8ff5a7d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -227,8 +227,9 @@ import { CryptoApi, decodeRecoveryKey, ImportRoomKeysOpts, - CryptoEvent as RustCryptoEvent, + CryptoEvent, CryptoEvents as CryptoApiEvents, + CryptoEventHandlerMap, } from "./crypto-api/index.ts"; import { DeviceInfoMap } from "./crypto/DeviceList.ts"; import { @@ -246,7 +247,6 @@ import { ImageInfo } from "./@types/media.ts"; import { Capabilities, ServerCapabilities } from "./serverCapabilities.ts"; import { sha256 } from "./digest.ts"; import { keyFromAuthData } from "./common-crypto/key-passphrase.ts"; -import { RustCryptoEventMap } from "./rust-crypto/rust-crypto.ts"; export type Store = IStore; @@ -1194,7 +1194,7 @@ export type ClientEventHandlerMap = { } & RoomEventHandlerMap & RoomStateEventHandlerMap & LegacyCryptoEventHandlerMap & - RustCryptoEventMap & + CryptoEventHandlerMap & MatrixEventHandlerMap & RoomMemberEventHandlerMap & UserEventHandlerMap & @@ -2279,7 +2279,7 @@ export class MatrixClient extends TypedEventEmitter { - this.emit(LegacyCryptoEvent.LegacyCryptoStoreMigrationProgress, progress, total); + this.emit(CryptoEvent.LegacyCryptoStoreMigrationProgress, progress, total); }, }); @@ -2295,14 +2295,14 @@ export class MatrixClient extends TypedEventEmitter { const prom = this.setDeviceVerification(userId, deviceId, verified, null, null); diff --git a/src/crypto-api/CryptoEventHandlerMap.ts b/src/crypto-api/CryptoEventHandlerMap.ts new file mode 100644 index 0000000000..698e4babf7 --- /dev/null +++ b/src/crypto-api/CryptoEventHandlerMap.ts @@ -0,0 +1,62 @@ +/* + * Copyright 2024 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { CryptoEvent } from "./CryptoEvent.ts"; +import { CryptoEvent as LegacyCryptoEvent } from "../crypto/index.ts"; +import { VerificationRequest } from "./verification.ts"; +import { UserVerificationStatus } from "./index.ts"; +import { RustBackupCryptoEventMap } from "../rust-crypto/backup.ts"; + +export type CryptoEventHandlerMap = { + /** + * Fires when a key verification request is received. + */ + [CryptoEvent.VerificationRequestReceived]: (request: VerificationRequest) => void; + + /** + * Fires when the trust status of a user changes. + */ + [CryptoEvent.UserTrustStatusChanged]: (userId: string, userTrustLevel: UserVerificationStatus) => void; + + [CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void; + /** + * Fires when the user's cross-signing keys have changed or cross-signing + * has been enabled/disabled. The client can use getStoredCrossSigningForUser + * with the user ID of the logged in user to check if cross-signing is + * enabled on the account. If enabled, it can test whether the current key + * is trusted using with checkUserTrust with the user ID of the logged + * in user. The checkOwnCrossSigningTrust function may be used to reconcile + * the trust in the account key. + * + * The cross-signing API is currently UNSTABLE and may change without notice. + * @experimental + */ + [CryptoEvent.KeysChanged]: (data: {}) => void; + /** + * Fires whenever the stored devices for a user will be updated + * @param users - A list of user IDs that will be updated + * @param initialFetch - If true, the store is empty (apart + * from our own device) and is being seeded. + */ + [LegacyCryptoEvent.WillUpdateDevices]: (users: string[], initialFetch: boolean) => void; + /** + * Fires whenever the stored devices for a user have changed + * @param users - A list of user IDs that were updated + * @param initialFetch - If true, the store was empty (apart + * from our own device) and has been seeded. + */ + [CryptoEvent.DevicesUpdated]: (users: string[], initialFetch: boolean) => void; +} & RustBackupCryptoEventMap; diff --git a/src/crypto-api/index.ts b/src/crypto-api/index.ts index 052c490c1a..8a59b9c050 100644 --- a/src/crypto-api/index.ts +++ b/src/crypto-api/index.ts @@ -1084,3 +1084,4 @@ export * from "./keybackup.ts"; export * from "./recovery-key.ts"; export * from "./key-passphrase.ts"; export * from "./CryptoEvent.ts"; +export * from "./CryptoEventHandlerMap.ts"; diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 14ed77bb41..8292aa3c4c 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -64,6 +64,8 @@ import { AllDevicesIsolationMode, DeviceIsolationModeKind, CryptoEvent, + CryptoEvents, + CryptoEventHandlerMap, } from "../crypto-api/index.ts"; import { deviceKeysToDeviceMap, rustDeviceToJsDevice } from "./device-converter.ts"; import { IDownloadKeyResult, IQueryKeysRequest } from "../client.ts"; @@ -74,7 +76,7 @@ import { secretStorageCanAccessSecrets, secretStorageContainsCrossSigningKeys } import { isVerificationEvent, RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification.ts"; import { EventType, MsgType } from "../@types/event.ts"; import { TypedEventEmitter } from "../models/typed-event-emitter.ts"; -import { RustBackupCryptoEventMap, RustBackupCryptoEvents, RustBackupManager } from "./backup.ts"; +import { RustBackupManager } from "./backup.ts"; import { TypedReEmitter } from "../ReEmitter.ts"; import { randomString } from "../randomstring.ts"; import { ClientStoppedError } from "../errors.ts"; @@ -103,7 +105,7 @@ interface ISignableObject { * * @internal */ -export class RustCrypto extends TypedEventEmitter implements CryptoBackend { +export class RustCrypto extends TypedEventEmitter implements CryptoBackend { /** * The number of iterations to use when deriving a recovery key from a passphrase. */ @@ -126,7 +128,7 @@ export class RustCrypto extends TypedEventEmitter(this); + private readonly reemitter = new TypedReEmitter(this); public constructor( private readonly logger: Logger, @@ -2079,50 +2081,5 @@ function rustEncryptionInfoToJsEncryptionInfo( } type RustCryptoEvents = - | CryptoEvent.VerificationRequestReceived - | CryptoEvent.UserTrustStatusChanged - | CryptoEvent.KeysChanged - | LegacyCryptoEvent.WillUpdateDevices - | CryptoEvent.DevicesUpdated - | RustBackupCryptoEvents; - -export type RustCryptoEventMap = { - /** - * Fires when a key verification request is received. - */ - [CryptoEvent.VerificationRequestReceived]: (request: VerificationRequest) => void; - - /** - * Fires when the trust status of a user changes. - */ - [CryptoEvent.UserTrustStatusChanged]: (userId: string, userTrustLevel: UserVerificationStatus) => void; - - [CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void; - /** - * Fires when the user's cross-signing keys have changed or cross-signing - * has been enabled/disabled. The client can use getStoredCrossSigningForUser - * with the user ID of the logged in user to check if cross-signing is - * enabled on the account. If enabled, it can test whether the current key - * is trusted using with checkUserTrust with the user ID of the logged - * in user. The checkOwnCrossSigningTrust function may be used to reconcile - * the trust in the account key. - * - * The cross-signing API is currently UNSTABLE and may change without notice. - * @experimental - */ - [CryptoEvent.KeysChanged]: (data: {}) => void; - /** - * Fires whenever the stored devices for a user will be updated - * @param users - A list of user IDs that will be updated - * @param initialFetch - If true, the store is empty (apart - * from our own device) and is being seeded. - */ - [LegacyCryptoEvent.WillUpdateDevices]: (users: string[], initialFetch: boolean) => void; - /** - * Fires whenever the stored devices for a user have changed - * @param users - A list of user IDs that were updated - * @param initialFetch - If true, the store was empty (apart - * from our own device) and has been seeded. - */ - [CryptoEvent.DevicesUpdated]: (users: string[], initialFetch: boolean) => void; -} & RustBackupCryptoEventMap; + | Exclude + | LegacyCryptoEvent.WillUpdateDevices;