1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Use processKeyCounts in sliding sync (#3253)

This commit is contained in:
Florian Duros
2023-04-05 17:39:46 +02:00
committed by GitHub
parent 5854af0eae
commit 72a2b6d571
3 changed files with 27 additions and 62 deletions

View File

@ -671,32 +671,17 @@ describe("SlidingSyncSdk", () => {
// TODO: more assertions?
});
it("can update OTK counts", () => {
client!.crypto!.updateOneTimeKeyCount = jest.fn();
it("can update OTK counts and unused fallback keys", () => {
client!.crypto!.processKeyCounts = jest.fn();
ext.onResponse({
device_one_time_keys_count: {
signed_curve25519: 42,
},
});
expect(client!.crypto!.updateOneTimeKeyCount).toHaveBeenCalledWith(42);
ext.onResponse({
device_one_time_keys_count: {
not_signed_curve25519: 42,
// missing field -> default to 0
},
});
expect(client!.crypto!.updateOneTimeKeyCount).toHaveBeenCalledWith(0);
});
it("can update fallback keys", () => {
ext.onResponse({
device_unused_fallback_key_types: ["signed_curve25519"],
});
expect(client!.crypto!.getNeedsNewFallback()).toEqual(false);
ext.onResponse({
device_unused_fallback_key_types: ["not_signed_curve25519"],
});
expect(client!.crypto!.getNeedsNewFallback()).toEqual(true);
expect(client!.crypto!.processKeyCounts).toHaveBeenCalledWith({ signed_curve25519: 42 }, [
"signed_curve25519",
]);
});
});

View File

@ -1817,31 +1817,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
});
}
/**
* Stores the current one_time_key count which will be handled later (in a call of
* onSyncCompleted). The count is e.g. coming from a /sync response.
*
* @param currentCount - The current count of one_time_keys to be stored
*
* TODO This method is called in `processKeyCounts` method and in the `sliding-sync-sdk`.
* TODO The `sliding-sync-sdk` should call `processKeyCounts` directly instead of `updateOneTimeKeyCount`
* TODO Move the content of `updateOneTimeKeyCount` in `processKeyCounts` after the `sliding-sync-sdk` change
*/
public updateOneTimeKeyCount(currentCount: number): void {
if (isFinite(currentCount)) {
this.oneTimeKeyCount = currentCount;
} else {
throw new TypeError("Parameter for updateOneTimeKeyCount has to be a number");
}
}
// TODO This method is called in `processKeyCounts`, `uploadOneTimeKeys` methods and in the `sliding-sync-sdk`.
// TODO The `sliding-sync-sdk` should call `processKeyCounts` directly instead of `setNeedsNewFallback`
// TODO Move the content of `setNeedsNewFallback` in `processKeyCounts` after the `sliding-sync-sdk` change
public setNeedsNewFallback(needsNewFallback: boolean): void {
this.needsNewFallback = needsNewFallback;
}
public getNeedsNewFallback(): boolean {
return !!this.needsNewFallback;
}
@ -1977,7 +1952,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
fallbackJson["signed_curve25519:" + keyId] = k;
promises.push(this.signObject(k));
}
this.setNeedsNewFallback(false);
this.needsNewFallback = false;
}
const oneTimeKeys = await this.olmDevice.getOneTimeKeys();
@ -3222,6 +3197,20 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
});
}
/**
* Stores the current one_time_key count which will be handled later (in a call of
* onSyncCompleted).
*
* @param currentCount - The current count of one_time_keys to be stored
*/
private updateOneTimeKeyCount(currentCount: number): void {
if (isFinite(currentCount)) {
this.oneTimeKeyCount = currentCount;
} else {
throw new TypeError("Parameter for updateOneTimeKeyCount has to be a number");
}
}
public processKeyCounts(oneTimeKeysCounts?: Record<string, number>, unusedFallbackKeys?: string[]): Promise<void> {
if (oneTimeKeysCounts !== undefined) {
this.updateOneTimeKeyCount(oneTimeKeysCounts["signed_curve25519"] || 0);
@ -3232,7 +3221,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
// is present in the sync response, which indicates that the server supports fallback keys.
//
// If there's no unused signed_curve25519 fallback key, we need a new one.
this.setNeedsNewFallback(!unusedFallbackKeys.includes("signed_curve25519"));
this.needsNewFallback = !unusedFallbackKeys.includes("signed_curve25519");
}
return Promise.resolve();

View File

@ -94,21 +94,12 @@ class ExtensionE2EE implements Extension<ExtensionE2EERequest, ExtensionE2EEResp
);
}
// Handle one_time_keys_count
if (data["device_one_time_keys_count"]) {
const currentCount = data["device_one_time_keys_count"].signed_curve25519 || 0;
this.crypto.updateOneTimeKeyCount(currentCount);
}
if (data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"]) {
// The presence of device_unused_fallback_key_types indicates that the
// server supports fallback keys. If there's no unused
// signed_curve25519 fallback key we need a new one.
const unusedFallbackKeys =
data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"];
this.crypto.setNeedsNewFallback(
Array.isArray(unusedFallbackKeys) && !unusedFallbackKeys.includes("signed_curve25519"),
// Handle one_time_keys_count and unused_fallback_key_types
await this.crypto.processKeyCounts(
data.device_one_time_keys_count,
data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"],
);
}
this.crypto.onSyncCompleted({});
}
}