Skip to content

Commit

Permalink
Replace legacy keyBackup types (#4486)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros authored Oct 30, 2024
1 parent fd73d50 commit 16153e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 4 additions & 6 deletions spec/unit/rust-crypto/backup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import fetchMock from "fetch-mock-jest";
import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-wasm";

import { HttpApiEvent, HttpApiEventHandlerMap, MatrixHttpApi, TypedEventEmitter } from "../../../src";
import { CryptoEvent } from "../../../src/crypto-api/index.ts";
import { CryptoEvent, KeyBackupSession } from "../../../src/crypto-api/index.ts";
import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor";
import * as testData from "../../test-utils/test-data";
import * as TestData from "../../test-utils/test-data";
import { IKeyBackup } from "../../../src/crypto/backup";
import { IKeyBackupSession } from "../../../src/crypto/keybackup";
import { RustBackupManager } from "../../../src/rust-crypto/backup";
import { RustBackupManager, KeyBackup } from "../../../src/rust-crypto/backup";

describe("Upload keys to backup", () => {
/** The backup manager under test */
Expand All @@ -27,15 +25,15 @@ describe("Upload keys to backup", () => {

let idGenerator = 0;
function mockBackupRequest(keyCount: number): RustSdkCryptoJs.KeysBackupRequest {
const requestBody: IKeyBackup = {
const requestBody: KeyBackup = {
rooms: {
"!room1:server": {
sessions: {},
},
},
};
for (let i = 0; i < keyCount; i++) {
requestBody.rooms["!room1:server"].sessions["session" + i] = {} as IKeyBackupSession;
requestBody.rooms["!room1:server"].sessions["session" + i] = {} as KeyBackupSession;
}
return {
id: "id" + idGenerator++,
Expand Down
15 changes: 11 additions & 4 deletions src/rust-crypto/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
KeyBackupInfo,
KeyBackupSession,
Curve25519SessionData,
KeyBackupRoomSessions,
} from "../crypto-api/keybackup.ts";
import { logger } from "../logger.ts";
import { ClientPrefix, IHttpOpts, MatrixError, MatrixHttpApi, Method } from "../http-api/index.ts";
Expand All @@ -34,8 +35,6 @@ import { OutgoingRequestProcessor } from "./OutgoingRequestProcessor.ts";
import { sleep } from "../utils.ts";
import { BackupDecryptor } from "../common-crypto/CryptoBackend.ts";
import { ImportRoomKeyProgressData, ImportRoomKeysOpts, CryptoEvent } from "../crypto-api/index.ts";
import { IKeyBackupInfo } from "../crypto/keybackup.ts";
import { IKeyBackup } from "../crypto/backup.ts";
import { AESEncryptedSecretStoragePayload } from "../@types/AESEncryptedSecretStoragePayload.ts";

/** Authentification of the backup info, depends on algorithm */
Expand Down Expand Up @@ -488,7 +487,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
* @returns The number of keys in the backup request.
*/
private keysCountInBatch(batch: RustSdkCryptoJs.KeysBackupRequest): number {
const parsedBody: IKeyBackup = JSON.parse(batch.body);
const parsedBody: KeyBackup = JSON.parse(batch.body);
let count = 0;
for (const { sessions } of Object.values(parsedBody.rooms)) {
count += Object.keys(sessions).length;
Expand Down Expand Up @@ -653,7 +652,7 @@ export class RustBackupDecryptor implements BackupDecryptor {

export async function requestKeyBackupVersion(
http: MatrixHttpApi<IHttpOpts & { onlyData: true }>,
): Promise<IKeyBackupInfo | null> {
): Promise<KeyBackupInfo | null> {
try {
return await http.authedRequest<KeyBackupInfo>(Method.Get, "/room_keys/version", undefined, undefined, {
prefix: ClientPrefix.V3,
Expand All @@ -679,3 +678,11 @@ export type RustBackupCryptoEventMap = {
[CryptoEvent.KeyBackupFailed]: (errCode: string) => void;
[CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void;
};

/**
* Response from GET `/room_keys/keys` endpoint.
* See https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3room_keyskeys
*/
export interface KeyBackup {
rooms: Record<string, { sessions: KeyBackupRoomSessions }>;
}

0 comments on commit 16153e5

Please sign in to comment.