From 878bb89c1ef8cde6427bc6cabe3d20d64cdd135e Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 19 Apr 2023 13:02:27 +0100 Subject: [PATCH] DeviceVerificationStatus: take a param object --- src/crypto-api.ts | 56 +++++++++++++++++++++------------- src/crypto/CrossSigning.ts | 9 ++++++ src/rust-crypto/rust-crypto.ts | 11 +++---- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/crypto-api.ts b/src/crypto-api.ts index 0c3f4a151e5..ca849a88392 100644 --- a/src/crypto-api.ts +++ b/src/crypto-api.ts @@ -106,29 +106,41 @@ export interface CryptoApi { } export class DeviceVerificationStatus { + /** + * True if this device has been verified via cross signing. + * + * This does *not* take into account `trustCrossSignedDevices`. + */ + public readonly crossSigningVerified: boolean; + + /** + * TODO: tofu magic wtf does this do? + */ + public readonly tofu: boolean; + + /** + * True if the device has been marked as locally verified. + */ + public readonly localVerified: boolean; + + /** + * True if the client has been configured to trust cross-signed devices via {@link CryptoApi#setTrustCrossSignedDevices}. + */ + private readonly trustCrossSignedDevices: boolean; + public constructor( - /** - * True if this device has been verified via cross signing. - * - * This does *not* take into account `trustCrossSignedDevices`. - */ - public readonly crossSigningVerified: boolean, - - /** - * TODO: tofu magic wtf does this do? - */ - public readonly tofu: boolean, - - /** - * True if the device has been marked as locally verified. - */ - public readonly localVerified: boolean, - - /** - * True if the client has been configured to trust cross-signed devices via {@link CryptoApi#setTrustCrossSignedDevices}. - */ - private readonly trustCrossSignedDevices: boolean, - ) {} + opts: Partial & { + /** + * True if cross-signed devices should be considered verified for {@link DeviceVerificationStatus#isVerified}. + */ + trustCrossSignedDevices?: boolean; + }, + ) { + this.crossSigningVerified = opts.crossSigningVerified ?? false; + this.tofu = opts.tofu ?? false; + this.localVerified = opts.localVerified ?? false; + this.trustCrossSignedDevices = opts.trustCrossSignedDevices ?? false; + } /** * Check if we should consider this device "verified". diff --git a/src/crypto/CrossSigning.ts b/src/crypto/CrossSigning.ts index 8d184dbb36f..597ea54a29c 100644 --- a/src/crypto/CrossSigning.ts +++ b/src/crypto/CrossSigning.ts @@ -634,6 +634,15 @@ export class UserTrustLevel { * @deprecated Use {@link DeviceVerificationStatus}. */ export class DeviceTrustLevel extends DeviceVerificationStatus { + public constructor( + crossSigningVerified: boolean, + tofu: boolean, + localVerified: boolean, + trustCrossSignedDevices: boolean, + ) { + super({ crossSigningVerified, tofu, localVerified, trustCrossSignedDevices }); + } + public static fromUserTrustLevel( userTrustLevel: UserTrustLevel, localVerified: boolean, diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 6754da8f585..2ae3b7b5fce 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -192,12 +192,11 @@ export class RustCrypto implements CryptoBackend { if (!device) return null; - return new DeviceVerificationStatus( - device.isCrossSigningTrusted(), - false, // tofu - device.isLocallyTrusted(), - this._trustCrossSignedDevices, - ); + return new DeviceVerificationStatus({ + crossSigningVerified: device.isCrossSigningTrusted(), + localVerified: device.isLocallyTrusted(), + trustCrossSignedDevices: this._trustCrossSignedDevices, + }); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////