1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-23 17:02:25 +03:00

move get device key API from client to crypto (#3899)

MatrixClient API was exposing two methods that only worked for legacy crypto:
- getDeviceEd25519Key
- getDeviceCurve25519Key

=> These are used in the react-sdk for some functionality (rageshake, sentry, rendez-vous).

I have deprecated those calls from MatrixClient and created a new API in CryptoApi (where it belongs):

getOwnDeviceKeys(): Promise<OwnDeviceKeys>
This commit is contained in:
Valere
2023-11-29 18:54:06 +01:00
committed by GitHub
parent d90ae11e2b
commit 48d9d9b4c9
5 changed files with 72 additions and 0 deletions

View File

@@ -397,6 +397,19 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
expect(aliceClient.getCrypto()).toHaveProperty("globalBlacklistUnverifiedDevices"); expect(aliceClient.getCrypto()).toHaveProperty("globalBlacklistUnverifiedDevices");
}); });
it("CryptoAPI.getOwnedDeviceKeys returns the correct values", async () => {
const homeserverUrl = aliceClient.getHomeserverUrl();
keyResponder = new E2EKeyResponder(homeserverUrl);
await startClientAndAwaitFirstSync();
keyResponder.addKeyReceiver("@alice:localhost", keyReceiver);
const deviceKeys = await aliceClient.getCrypto()!.getOwnDeviceKeys();
expect(deviceKeys.curve25519).toEqual(keyReceiver.getDeviceKey());
expect(deviceKeys.ed25519).toEqual(keyReceiver.getSigningKey());
});
it("Alice receives a megolm message", async () => { it("Alice receives a megolm message", async () => {
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} }); expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
await startClientAndAwaitFirstSync(); await startClientAndAwaitFirstSync();

View File

@@ -2393,6 +2393,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* *
* @returns base64-encoded ed25519 key. Null if crypto is * @returns base64-encoded ed25519 key. Null if crypto is
* disabled. * disabled.
*
* @deprecated Prefer {@link CryptoApi.getOwnDeviceKeys}
*/ */
public getDeviceEd25519Key(): string | null { public getDeviceEd25519Key(): string | null {
return this.crypto?.getDeviceEd25519Key() ?? null; return this.crypto?.getDeviceEd25519Key() ?? null;
@@ -2403,6 +2405,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* *
* @returns base64-encoded curve25519 key. Null if crypto is * @returns base64-encoded curve25519 key. Null if crypto is
* disabled. * disabled.
*
* @deprecated Use {@link CryptoApi.getOwnDeviceKeys}
*/ */
public getDeviceCurve25519Key(): string | null { public getDeviceCurve25519Key(): string | null {
return this.crypto?.getDeviceCurve25519Key() ?? null; return this.crypto?.getDeviceCurve25519Key() ?? null;

View File

@@ -46,6 +46,13 @@ export interface CryptoApi {
*/ */
getVersion(): string; getVersion(): string;
/**
* Get the public part of the device keys for the current device.
*
* @returns The public device keys.
*/
getOwnDeviceKeys(): Promise<OwnDeviceKeys>;
/** /**
* Perform any background tasks that can be done before a message is ready to * Perform any background tasks that can be done before a message is ready to
* send, in order to speed up sending of the message. * send, in order to speed up sending of the message.
@@ -764,5 +771,13 @@ export enum EventShieldReason {
MISMATCHED_SENDER_KEY, MISMATCHED_SENDER_KEY,
} }
/** The result of a call to {@link CryptoApi.getOwnDeviceKeys} */
export interface OwnDeviceKeys {
/** Public part of the Ed25519 fingerprint key for the current device, base64 encoded. */
ed25519: string;
/** Public part of the Curve25519 identity key for the current device, base64 encoded. */
curve25519: string;
}
export * from "./crypto-api/verification"; export * from "./crypto-api/verification";
export * from "./crypto-api/keybackup"; export * from "./crypto-api/keybackup";

View File

@@ -98,6 +98,7 @@ import {
KeyBackupCheck, KeyBackupCheck,
KeyBackupInfo, KeyBackupInfo,
VerificationRequest as CryptoApiVerificationRequest, VerificationRequest as CryptoApiVerificationRequest,
OwnDeviceKeys,
} from "../crypto-api"; } from "../crypto-api";
import { Device, DeviceMap } from "../models/device"; import { Device, DeviceMap } from "../models/device";
import { deviceInfoToDevice } from "./device-converter"; import { deviceInfoToDevice } from "./device-converter";
@@ -1968,6 +1969,8 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* Get the Ed25519 key for this device * Get the Ed25519 key for this device
* *
* @returns base64-encoded ed25519 key. * @returns base64-encoded ed25519 key.
*
* @deprecated Use {@link CryptoApi#getOwnDeviceKeys}.
*/ */
public getDeviceEd25519Key(): string | null { public getDeviceEd25519Key(): string | null {
return this.olmDevice.deviceEd25519Key; return this.olmDevice.deviceEd25519Key;
@@ -1977,11 +1980,29 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* Get the Curve25519 key for this device * Get the Curve25519 key for this device
* *
* @returns base64-encoded curve25519 key. * @returns base64-encoded curve25519 key.
*
* @deprecated Use {@link CryptoApi#getOwnDeviceKeys}
*/ */
public getDeviceCurve25519Key(): string | null { public getDeviceCurve25519Key(): string | null {
return this.olmDevice.deviceCurve25519Key; return this.olmDevice.deviceCurve25519Key;
} }
/**
* Implementation of {@link CryptoApi#getOwnDeviceKeys}.
*/
public async getOwnDeviceKeys(): Promise<OwnDeviceKeys> {
if (!this.olmDevice.deviceCurve25519Key) {
throw new Error("Curve25519 key not yet created");
}
if (!this.olmDevice.deviceEd25519Key) {
throw new Error("Ed25519 key not yet created");
}
return {
ed25519: this.olmDevice.deviceEd25519Key,
curve25519: this.olmDevice.deviceCurve25519Key,
};
}
/** /**
* Set the global override for whether the client should ever send encrypted * Set the global override for whether the client should ever send encrypted
* messages to unverified devices. This provides the default for rooms which * messages to unverified devices. This provides the default for rooms which

View File

@@ -49,6 +49,7 @@ import {
KeyBackupCheck, KeyBackupCheck,
KeyBackupInfo, KeyBackupInfo,
KeyBackupSession, KeyBackupSession,
OwnDeviceKeys,
UserVerificationStatus, UserVerificationStatus,
VerificationRequest, VerificationRequest,
} from "../crypto-api"; } from "../crypto-api";
@@ -371,6 +372,24 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
return `Rust SDK ${versions.matrix_sdk_crypto} (${versions.git_sha}), Vodozemac ${versions.vodozemac}`; return `Rust SDK ${versions.matrix_sdk_crypto} (${versions.git_sha}), Vodozemac ${versions.vodozemac}`;
} }
/**
* Implementation of {@link CryptoApi#getOwnDeviceKeys}.
*/
public async getOwnDeviceKeys(): Promise<OwnDeviceKeys> {
const device: RustSdkCryptoJs.Device = await this.olmMachine.getDevice(
this.olmMachine.userId,
this.olmMachine.deviceId,
);
// could be undefined if there is no such algorithm for that device.
if (device.curve25519Key && device.ed25519Key) {
return {
ed25519: device.ed25519Key.toBase64(),
curve25519: device.curve25519Key.toBase64(),
};
}
throw new Error("Device keys not found");
}
public prepareToEncrypt(room: Room): void { public prepareToEncrypt(room: Room): void {
const encryptor = this.roomEncryptors[room.roomId]; const encryptor = this.roomEncryptors[room.roomId];