Skip to content

Commit

Permalink
- Rename RustCryptoEvent as CryptoEvent
Browse files Browse the repository at this point in the history
- Declare `CryptoEventHandlerMap` into the crypto api
  • Loading branch information
florianduros committed Oct 8, 2024
1 parent d4aea54 commit 9c9d9de
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 63 deletions.
26 changes: 13 additions & 13 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand Down Expand Up @@ -1194,7 +1194,7 @@ export type ClientEventHandlerMap = {
} & RoomEventHandlerMap &
RoomStateEventHandlerMap &
LegacyCryptoEventHandlerMap &
RustCryptoEventMap &
CryptoEventHandlerMap &
MatrixEventHandlerMap &
RoomMemberEventHandlerMap &
UserEventHandlerMap &
Expand Down Expand Up @@ -2279,7 +2279,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
legacyCryptoStore: this.cryptoStore,
legacyPickleKey: this.pickleKey ?? "DEFAULT_KEY",
legacyMigrationProgressListener: (progress: number, total: number): void => {
this.emit(LegacyCryptoEvent.LegacyCryptoStoreMigrationProgress, progress, total);
this.emit(CryptoEvent.LegacyCryptoStoreMigrationProgress, progress, total);
},
});

Expand All @@ -2295,14 +2295,14 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

// re-emit the events emitted by the crypto impl
this.reEmitter.reEmit(rustCrypto, [
RustCryptoEvent.VerificationRequestReceived,
RustCryptoEvent.UserTrustStatusChanged,
RustCryptoEvent.KeyBackupStatus,
RustCryptoEvent.KeyBackupSessionsRemaining,
RustCryptoEvent.KeyBackupFailed,
RustCryptoEvent.KeyBackupDecryptionKeyCached,
RustCryptoEvent.KeysChanged,
RustCryptoEvent.DevicesUpdated,
CryptoEvent.VerificationRequestReceived,
CryptoEvent.UserTrustStatusChanged,
CryptoEvent.KeyBackupStatus,
CryptoEvent.KeyBackupSessionsRemaining,
CryptoEvent.KeyBackupFailed,
CryptoEvent.KeyBackupDecryptionKeyCached,
CryptoEvent.KeysChanged,
CryptoEvent.DevicesUpdated,
LegacyCryptoEvent.WillUpdateDevices,
]);
}
Expand Down Expand Up @@ -2425,7 +2425,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns
*
* @remarks
* Fires {@link LegacyCryptoEvent#DeviceVerificationChanged}
* Fires {@link CryptoEvent#DeviceVerificationChanged}
*/
public setDeviceVerified(userId: string, deviceId: string, verified = true): Promise<void> {
const prom = this.setDeviceVerification(userId, deviceId, verified, null, null);
Expand Down
62 changes: 62 additions & 0 deletions src/crypto-api/CryptoEventHandlerMap.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/crypto-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
57 changes: 7 additions & 50 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -103,7 +105,7 @@ interface ISignableObject {
*
* @internal
*/
export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEventMap> implements CryptoBackend {
export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventHandlerMap> implements CryptoBackend {
/**
* The number of iterations to use when deriving a recovery key from a passphrase.
*/
Expand All @@ -126,7 +128,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
private outgoingRequestsManager: OutgoingRequestsManager;
private readonly perSessionBackupDownloader: PerSessionKeyBackupDownloader;
private readonly dehydratedDeviceManager: DehydratedDeviceManager;
private readonly reemitter = new TypedReEmitter<RustCryptoEvents, RustCryptoEventMap>(this);
private readonly reemitter = new TypedReEmitter<RustCryptoEvents, CryptoEventHandlerMap>(this);

public constructor(
private readonly logger: Logger,
Expand Down Expand Up @@ -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<CryptoEvents, CryptoEvent.LegacyCryptoStoreMigrationProgress>
| LegacyCryptoEvent.WillUpdateDevices;

0 comments on commit 9c9d9de

Please sign in to comment.