From f963ca55627e340bc8016c2ab5f47e9457214827 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Fri, 8 Sep 2023 05:40:02 +0100 Subject: [PATCH] Element-R: Emit `CryptoEvent.UserTrustStatusChanged` when user identity is updated (#3716) * Emit a `UserTrustStatusChanged` when user identity is updated * Remove redundant `onCrossSigningKeysImport` callback This now happens as a side-effect of importing the keys. * bump to alpha release of matrix-rust-sdk-crypto-wasm * fixup! Remove redundant `onCrossSigningKeysImport` callback --- package.json | 2 +- .../rust-crypto/CrossSigningIdentity.spec.ts | 2 +- src/rust-crypto/CrossSigningIdentity.ts | 4 --- src/rust-crypto/index.ts | 3 +++ src/rust-crypto/rust-crypto.ts | 27 ++++++++++--------- yarn.lock | 8 +++--- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 97c27f77f..1d60216b7 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ ], "dependencies": { "@babel/runtime": "^7.12.5", - "@matrix-org/matrix-sdk-crypto-wasm": "^1.2.1", + "@matrix-org/matrix-sdk-crypto-wasm": "^1.2.3-alpha.0", "another-json": "^0.2.0", "bs58": "^5.0.0", "content-type": "^1.0.4", diff --git a/spec/unit/rust-crypto/CrossSigningIdentity.spec.ts b/spec/unit/rust-crypto/CrossSigningIdentity.spec.ts index 83d747162..e01763543 100644 --- a/spec/unit/rust-crypto/CrossSigningIdentity.spec.ts +++ b/spec/unit/rust-crypto/CrossSigningIdentity.spec.ts @@ -52,7 +52,7 @@ describe("CrossSigningIdentity", () => { get: jest.fn(), } as unknown as Mocked; - crossSigning = new CrossSigningIdentity(olmMachine, outgoingRequestProcessor, secretStorage, jest.fn()); + crossSigning = new CrossSigningIdentity(olmMachine, outgoingRequestProcessor, secretStorage); }); it("should do nothing if keys are present on-device and in secret storage", async () => { diff --git a/src/rust-crypto/CrossSigningIdentity.ts b/src/rust-crypto/CrossSigningIdentity.ts index 21d3f9abc..b98ec1fd7 100644 --- a/src/rust-crypto/CrossSigningIdentity.ts +++ b/src/rust-crypto/CrossSigningIdentity.ts @@ -32,8 +32,6 @@ export class CrossSigningIdentity { private readonly olmMachine: OlmMachine, private readonly outgoingRequestProcessor: OutgoingRequestProcessor, private readonly secretStorage: ServerSideSecretStorage, - /** Called if the cross signing keys are imported from the secret storage */ - private readonly onCrossSigningKeysImport: () => Promise, ) {} /** @@ -95,8 +93,6 @@ export class CrossSigningIdentity { // Sign the device with our cross-signing key and upload the signature const request: RustSdkCryptoJs.SignatureUploadRequest = await device.verify(); await this.outgoingRequestProcessor.makeOutgoingRequest(request); - - await this.onCrossSigningKeysImport(); } // TODO: we might previously have bootstrapped cross-signing but not completed uploading the keys to the diff --git a/src/rust-crypto/index.ts b/src/rust-crypto/index.ts index 9d8fe8d53..74249bbb9 100644 --- a/src/rust-crypto/index.ts +++ b/src/rust-crypto/index.ts @@ -65,6 +65,9 @@ export async function initRustCrypto( await olmMachine.registerRoomKeyUpdatedCallback((sessions: RustSdkCryptoJs.RoomKeyInfo[]) => rustCrypto.onRoomKeysUpdated(sessions), ); + await olmMachine.registerUserIdentityUpdatedCallback((userId: RustSdkCryptoJs.UserId) => + rustCrypto.onUserIdentityUpdated(userId), + ); // Tell the OlmMachine to think about its outgoing requests before we hand control back to the application. // diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index fcb373d49..94def22a9 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -136,17 +136,7 @@ export class RustCrypto extends TypedEventEmitter => { - const newVerification = await this.getUserVerificationStatus(this.userId); - this.emit(CryptoEvent.UserTrustStatusChanged, this.userId, newVerification); - }; - this.crossSigningIdentity = new CrossSigningIdentity( - olmMachine, - this.outgoingRequestProcessor, - secretStorage, - onCrossSigningKeysImport, - ); + this.crossSigningIdentity = new CrossSigningIdentity(olmMachine, this.outgoingRequestProcessor, secretStorage); } /** @@ -1249,6 +1239,19 @@ export class RustCrypto extends TypedEventEmitter { + const newVerification = await this.getUserVerificationStatus(userId.toString()); + this.emit(CryptoEvent.UserTrustStatusChanged, userId.toString(), newVerification); + } + /** * Handle a live event received via /sync. * See {@link ClientEventHandlerMap#event} @@ -1457,7 +1460,7 @@ type RustCryptoEventMap = { [CryptoEvent.VerificationRequestReceived]: (request: VerificationRequest) => void; /** - * Fires when the cross signing keys are imported during {@link CryptoApi#bootstrapCrossSigning} + * Fires when the trust status of a user changes. */ [CryptoEvent.UserTrustStatusChanged]: (userId: string, userTrustLevel: UserVerificationStatus) => void; } & RustBackupCryptoEventMap; diff --git a/yarn.lock b/yarn.lock index e953689e3..48f286079 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1537,10 +1537,10 @@ "@jridgewell/resolve-uri" "^3.1.0" "@jridgewell/sourcemap-codec" "^1.4.14" -"@matrix-org/matrix-sdk-crypto-wasm@^1.2.1": - version "1.2.1" - resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-wasm/-/matrix-sdk-crypto-wasm-1.2.1.tgz#5b546c8a0e53b614f10b77b3b649818aed9d0db1" - integrity sha512-DCb7Q83PCQK0uav5vB3KNV/hJPlxAhT/ddar+VHz2kC39hMLKGzWYVhprpLYVcavaE/6OX+Q/xFkAoV/3QtUHQ== +"@matrix-org/matrix-sdk-crypto-wasm@^1.2.3-alpha.0": + version "1.2.3-alpha.0" + resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-wasm/-/matrix-sdk-crypto-wasm-1.2.3-alpha.0.tgz#f6f93e3ee44c5f1e0e255badd26f4a7d3fb1dab8" + integrity sha512-BFLqfq/WbYZ+83r4UWLhwtBYvTp5DKTHNeWUSDBVvudFtqBvkntNAAUz+xmhmO1XkyNm+sBaElxF8IS9S8zdww== "@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.14.tgz": version "3.2.14"