Skip to content

Commit 43022d5

Browse files
authored
RustCrypto: fix ordering of methods (#4230)
* RustCrypto: Move CryptoBackend impl to CryptoBackend impl section Given there is a `CryptoBackend implementation` section, the methods implementing CryptoBackend should be there. * RustCrypto: Fix documentation on dehydration methods * RustCrypto: reunite `resetKeyBackup` with its helper A couple of new methods had snuck into the middle.
1 parent a0fadeb commit 43022d5

File tree

1 file changed

+57
-55
lines changed

1 file changed

+57
-55
lines changed

src/rust-crypto/rust-crypto.ts

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,40 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
309309
return;
310310
}
311311

312+
/**
313+
* Implementation of {@link CryptoBackend#getBackupDecryptor}.
314+
*/
315+
public async getBackupDecryptor(backupInfo: KeyBackupInfo, privKey: ArrayLike<number>): Promise<BackupDecryptor> {
316+
if (backupInfo.algorithm != "m.megolm_backup.v1.curve25519-aes-sha2") {
317+
throw new Error(`getBackupDecryptor Unsupported algorithm ${backupInfo.algorithm}`);
318+
}
319+
320+
const authData = <Curve25519AuthData>backupInfo.auth_data;
321+
322+
if (!(privKey instanceof Uint8Array)) {
323+
throw new Error(`getBackupDecryptor expects Uint8Array`);
324+
}
325+
326+
const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(privKey));
327+
328+
if (authData.public_key != backupDecryptionKey.megolmV1PublicKey.publicKeyBase64) {
329+
throw new Error(`getBackupDecryptor key mismatch error`);
330+
}
331+
332+
return this.backupManager.createBackupDecryptor(backupDecryptionKey);
333+
}
334+
335+
/**
336+
* Implementation of {@link CryptoBackend#importBackedUpRoomKeys}.
337+
*/
338+
public async importBackedUpRoomKeys(
339+
keys: IMegolmSessionData[],
340+
backupVersion: string,
341+
opts?: ImportRoomKeysOpts,
342+
): Promise<void> {
343+
return await this.backupManager.importBackedUpRoomKeys(keys, backupVersion, opts);
344+
}
345+
312346
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
313347
//
314348
// CryptoApi implementation
@@ -1166,30 +1200,12 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
11661200
this.checkKeyBackupAndEnable();
11671201
}
11681202

1169-
/**
1170-
* Implementation of {@link CryptoApi#importSecretsBundle}.
1171-
*/
1172-
public async importSecretsBundle(
1173-
secrets: Parameters<NonNullable<CryptoApi["importSecretsBundle"]>>[0],
1174-
): Promise<void> {
1175-
const secretsBundle = RustSdkCryptoJs.SecretsBundle.from_json(secrets);
1176-
await this.getOlmMachineOrThrow().importSecretsBundle(secretsBundle); // this method frees the SecretsBundle
1177-
}
1178-
1179-
/**
1180-
* Implementation of {@link CryptoApi#exportSecretsBundle}.
1181-
*/
1182-
public async exportsSecretsBundle(): ReturnType<NonNullable<CryptoApi["exportSecretsBundle"]>> {
1183-
const secretsBundle = await this.getOlmMachineOrThrow().exportSecretsBundle();
1184-
const secrets = secretsBundle.to_json();
1185-
secretsBundle.free();
1186-
return secrets;
1187-
}
1188-
11891203
/**
11901204
* Signs the given object with the current device and current identity (if available).
11911205
* As defined in {@link https://spec.matrix.org/v1.8/appendices/#signing-json | Signing JSON}.
11921206
*
1207+
* Helper for {@link RustCrypto#resetKeyBackup}.
1208+
*
11931209
* @param obj - The object to sign
11941210
*/
11951211
private async signObject<T extends ISignableObject & object>(obj: T): Promise<void> {
@@ -1213,54 +1229,40 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
12131229
}
12141230

12151231
/**
1216-
* Implementation of {@link CryptoBackend#getBackupDecryptor}.
1232+
* Implementation of {@link CryptoApi#isDehydrationSupported}.
12171233
*/
1218-
public async getBackupDecryptor(backupInfo: KeyBackupInfo, privKey: ArrayLike<number>): Promise<BackupDecryptor> {
1219-
if (backupInfo.algorithm != "m.megolm_backup.v1.curve25519-aes-sha2") {
1220-
throw new Error(`getBackupDecryptor Unsupported algorithm ${backupInfo.algorithm}`);
1221-
}
1222-
1223-
const authData = <Curve25519AuthData>backupInfo.auth_data;
1224-
1225-
if (!(privKey instanceof Uint8Array)) {
1226-
throw new Error(`getBackupDecryptor expects Uint8Array`);
1227-
}
1228-
1229-
const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(privKey));
1230-
1231-
if (authData.public_key != backupDecryptionKey.megolmV1PublicKey.publicKeyBase64) {
1232-
throw new Error(`getBackupDecryptor key mismatch error`);
1233-
}
1234-
1235-
return this.backupManager.createBackupDecryptor(backupDecryptionKey);
1234+
public async isDehydrationSupported(): Promise<boolean> {
1235+
return await this.dehydratedDeviceManager.isSupported();
12361236
}
12371237

12381238
/**
1239-
* Implementation of {@link CryptoBackend#importBackedUpRoomKeys}.
1239+
* Implementation of {@link CryptoApi#startDehydration}.
12401240
*/
1241-
public async importBackedUpRoomKeys(
1242-
keys: IMegolmSessionData[],
1243-
backupVersion: string,
1244-
opts?: ImportRoomKeysOpts,
1245-
): Promise<void> {
1246-
return await this.backupManager.importBackedUpRoomKeys(keys, backupVersion, opts);
1241+
public async startDehydration(createNewKey?: boolean): Promise<void> {
1242+
if (!(await this.isCrossSigningReady()) || !(await this.isSecretStorageReady())) {
1243+
throw new Error("Device dehydration requires cross-signing and secret storage to be set up");
1244+
}
1245+
return await this.dehydratedDeviceManager.start(createNewKey);
12471246
}
12481247

12491248
/**
1250-
* Implementation of {@link CryptoBackend#isDehydrationSupported}.
1249+
* Implementation of {@link CryptoApi#importSecretsBundle}.
12511250
*/
1252-
public async isDehydrationSupported(): Promise<boolean> {
1253-
return await this.dehydratedDeviceManager.isSupported();
1251+
public async importSecretsBundle(
1252+
secrets: Parameters<NonNullable<CryptoApi["importSecretsBundle"]>>[0],
1253+
): Promise<void> {
1254+
const secretsBundle = RustSdkCryptoJs.SecretsBundle.from_json(secrets);
1255+
await this.getOlmMachineOrThrow().importSecretsBundle(secretsBundle); // this method frees the SecretsBundle
12541256
}
12551257

12561258
/**
1257-
* Implementation of {@link CryptoBackend#startDehydration}.
1259+
* Implementation of {@link CryptoApi#exportSecretsBundle}.
12581260
*/
1259-
public async startDehydration(createNewKey?: boolean): Promise<void> {
1260-
if (!(await this.isCrossSigningReady()) || !(await this.isSecretStorageReady())) {
1261-
throw new Error("Device dehydration requires cross-signing and secret storage to be set up");
1262-
}
1263-
return await this.dehydratedDeviceManager.start(createNewKey);
1261+
public async exportsSecretsBundle(): ReturnType<NonNullable<CryptoApi["exportSecretsBundle"]>> {
1262+
const secretsBundle = await this.getOlmMachineOrThrow().exportSecretsBundle();
1263+
const secrets = secretsBundle.to_json();
1264+
secretsBundle.free();
1265+
return secrets;
12641266
}
12651267

12661268
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)