diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index c1b8f0519..61fc30cd9 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -309,6 +309,40 @@ export class RustCrypto extends TypedEventEmitter): Promise { + if (backupInfo.algorithm != "m.megolm_backup.v1.curve25519-aes-sha2") { + throw new Error(`getBackupDecryptor Unsupported algorithm ${backupInfo.algorithm}`); + } + + const authData = backupInfo.auth_data; + + if (!(privKey instanceof Uint8Array)) { + throw new Error(`getBackupDecryptor expects Uint8Array`); + } + + const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(privKey)); + + if (authData.public_key != backupDecryptionKey.megolmV1PublicKey.publicKeyBase64) { + throw new Error(`getBackupDecryptor key mismatch error`); + } + + return this.backupManager.createBackupDecryptor(backupDecryptionKey); + } + + /** + * Implementation of {@link CryptoBackend#importBackedUpRoomKeys}. + */ + public async importBackedUpRoomKeys( + keys: IMegolmSessionData[], + backupVersion: string, + opts?: ImportRoomKeysOpts, + ): Promise { + return await this.backupManager.importBackedUpRoomKeys(keys, backupVersion, opts); + } + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // CryptoApi implementation @@ -1166,30 +1200,12 @@ export class RustCrypto extends TypedEventEmitter>[0], - ): Promise { - const secretsBundle = RustSdkCryptoJs.SecretsBundle.from_json(secrets); - await this.getOlmMachineOrThrow().importSecretsBundle(secretsBundle); // this method frees the SecretsBundle - } - - /** - * Implementation of {@link CryptoApi#exportSecretsBundle}. - */ - public async exportsSecretsBundle(): ReturnType> { - const secretsBundle = await this.getOlmMachineOrThrow().exportSecretsBundle(); - const secrets = secretsBundle.to_json(); - secretsBundle.free(); - return secrets; - } - /** * Signs the given object with the current device and current identity (if available). * As defined in {@link https://spec.matrix.org/v1.8/appendices/#signing-json | Signing JSON}. * + * Helper for {@link RustCrypto#resetKeyBackup}. + * * @param obj - The object to sign */ private async signObject(obj: T): Promise { @@ -1213,48 +1229,14 @@ export class RustCrypto extends TypedEventEmitter): Promise { - if (backupInfo.algorithm != "m.megolm_backup.v1.curve25519-aes-sha2") { - throw new Error(`getBackupDecryptor Unsupported algorithm ${backupInfo.algorithm}`); - } - - const authData = backupInfo.auth_data; - - if (!(privKey instanceof Uint8Array)) { - throw new Error(`getBackupDecryptor expects Uint8Array`); - } - - const backupDecryptionKey = RustSdkCryptoJs.BackupDecryptionKey.fromBase64(encodeBase64(privKey)); - - if (authData.public_key != backupDecryptionKey.megolmV1PublicKey.publicKeyBase64) { - throw new Error(`getBackupDecryptor key mismatch error`); - } - - return this.backupManager.createBackupDecryptor(backupDecryptionKey); - } - - /** - * Implementation of {@link CryptoBackend#importBackedUpRoomKeys}. - */ - public async importBackedUpRoomKeys( - keys: IMegolmSessionData[], - backupVersion: string, - opts?: ImportRoomKeysOpts, - ): Promise { - return await this.backupManager.importBackedUpRoomKeys(keys, backupVersion, opts); - } - - /** - * Implementation of {@link CryptoBackend#isDehydrationSupported}. + * Implementation of {@link CryptoApi#isDehydrationSupported}. */ public async isDehydrationSupported(): Promise { return await this.dehydratedDeviceManager.isSupported(); } /** - * Implementation of {@link CryptoBackend#startDehydration}. + * Implementation of {@link CryptoApi#startDehydration}. */ public async startDehydration(createNewKey?: boolean): Promise { if (!(await this.isCrossSigningReady()) || !(await this.isSecretStorageReady())) { @@ -1263,6 +1245,26 @@ export class RustCrypto extends TypedEventEmitter>[0], + ): Promise { + const secretsBundle = RustSdkCryptoJs.SecretsBundle.from_json(secrets); + await this.getOlmMachineOrThrow().importSecretsBundle(secretsBundle); // this method frees the SecretsBundle + } + + /** + * Implementation of {@link CryptoApi#exportSecretsBundle}. + */ + public async exportsSecretsBundle(): ReturnType> { + const secretsBundle = await this.getOlmMachineOrThrow().exportSecretsBundle(); + const secrets = secretsBundle.to_json(); + secretsBundle.free(); + return secrets; + } + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // SyncCryptoCallbacks implementation