Skip to content

Commit a6fd28b

Browse files
authored
feat(sliding sync): Use SyncCryptoCallback api instead of legacy crypto (#4624)
1 parent 78e7e2a commit a6fd28b

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

spec/integ/sliding-sync-sdk.spec.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { logger } from "../../src/logger";
4444
import { emitPromise } from "../test-utils/test-utils";
4545
import { defer } from "../../src/utils";
4646
import { KnownMembership } from "../../src/@types/membership";
47+
import { SyncCryptoCallbacks } from "../../src/common-crypto/CryptoBackend";
4748

4849
declare module "../../src/@types/event" {
4950
interface AccountDataEvents {
@@ -57,6 +58,7 @@ describe("SlidingSyncSdk", () => {
5758
let httpBackend: MockHttpBackend | undefined;
5859
let sdk: SlidingSyncSdk | undefined;
5960
let mockSlidingSync: SlidingSync | undefined;
61+
let syncCryptoCallback: SyncCryptoCallbacks | undefined;
6062
const selfUserId = "@alice:localhost";
6163
const selfAccessToken = "aseukfgwef";
6264

@@ -126,8 +128,9 @@ describe("SlidingSyncSdk", () => {
126128
mockSlidingSync = mockifySlidingSync(new SlidingSync("", new Map(), {}, client, 0));
127129
if (testOpts.withCrypto) {
128130
httpBackend!.when("GET", "/room_keys/version").respond(404, {});
129-
await client!.initLegacyCrypto();
130-
syncOpts.cryptoCallbacks = syncOpts.crypto = client!.crypto;
131+
await client!.initRustCrypto({ useIndexedDB: false });
132+
syncCryptoCallback = client!.getCrypto() as unknown as SyncCryptoCallbacks;
133+
syncOpts.cryptoCallbacks = syncCryptoCallback;
131134
}
132135
httpBackend!.when("GET", "/_matrix/client/v3/pushrules").respond(200, {});
133136
sdk = new SlidingSyncSdk(mockSlidingSync, client, testOpts, syncOpts);
@@ -640,13 +643,6 @@ describe("SlidingSyncSdk", () => {
640643
ext = findExtension("e2ee");
641644
});
642645

643-
afterAll(async () => {
644-
// needed else we do some async operations in the background which can cause Jest to whine:
645-
// "Cannot log after tests are done. Did you forget to wait for something async in your test?"
646-
// Attempted to log "Saving device tracking data null"."
647-
client!.crypto!.stop();
648-
});
649-
650646
it("gets enabled on the initial request only", () => {
651647
expect(ext.onRequest(true)).toEqual({
652648
enabled: true,
@@ -655,28 +651,28 @@ describe("SlidingSyncSdk", () => {
655651
});
656652

657653
it("can update device lists", () => {
658-
client!.crypto!.processDeviceLists = jest.fn();
654+
syncCryptoCallback!.processDeviceLists = jest.fn();
659655
ext.onResponse({
660656
device_lists: {
661657
changed: ["@alice:localhost"],
662658
left: ["@bob:localhost"],
663659
},
664660
});
665-
expect(client!.crypto!.processDeviceLists).toHaveBeenCalledWith({
661+
expect(syncCryptoCallback!.processDeviceLists).toHaveBeenCalledWith({
666662
changed: ["@alice:localhost"],
667663
left: ["@bob:localhost"],
668664
});
669665
});
670666

671667
it("can update OTK counts and unused fallback keys", () => {
672-
client!.crypto!.processKeyCounts = jest.fn();
668+
syncCryptoCallback!.processKeyCounts = jest.fn();
673669
ext.onResponse({
674670
device_one_time_keys_count: {
675671
signed_curve25519: 42,
676672
},
677673
device_unused_fallback_key_types: ["signed_curve25519"],
678674
});
679-
expect(client!.crypto!.processKeyCounts).toHaveBeenCalledWith({ signed_curve25519: 42 }, [
675+
expect(syncCryptoCallback!.processKeyCounts).toHaveBeenCalledWith({ signed_curve25519: 42 }, [
680676
"signed_curve25519",
681677
]);
682678
});

src/sliding-sync-sdk.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
SetPresence,
3131
} from "./sync.ts";
3232
import { MatrixEvent } from "./models/event.ts";
33-
import { Crypto } from "./crypto/index.ts";
3433
import { IMinimalEvent, IRoomEvent, IStateEvent, IStrippedState, ISyncResponse } from "./sync-accumulator.ts";
3534
import { MatrixError } from "./http-api/index.ts";
3635
import {
@@ -66,7 +65,7 @@ type ExtensionE2EEResponse = Pick<
6665
>;
6766

6867
class ExtensionE2EE implements Extension<ExtensionE2EERequest, ExtensionE2EEResponse> {
69-
public constructor(private readonly crypto: Crypto) {}
68+
public constructor(private readonly crypto: SyncCryptoCallbacks) {}
7069

7170
public name(): string {
7271
return "e2ee";
@@ -373,8 +372,8 @@ export class SlidingSyncSdk {
373372
new ExtensionTyping(this.client),
374373
new ExtensionReceipts(this.client),
375374
];
376-
if (this.syncOpts.crypto) {
377-
extensions.push(new ExtensionE2EE(this.syncOpts.crypto));
375+
if (this.syncOpts.cryptoCallbacks) {
376+
extensions.push(new ExtensionE2EE(this.syncOpts.cryptoCallbacks));
378377
}
379378
extensions.forEach((ext) => {
380379
this.slidingSync.registerExtension(ext);

0 commit comments

Comments
 (0)