use the js-sdk where the hashed rtcSessionIdeintity is already part of

the event (no need to compute it in the encryption manager)
This commit is contained in:
Timo K
2025-12-28 21:04:46 +01:00
parent 55d18f10fe
commit 3ba2d13e27
3 changed files with 8 additions and 59 deletions

View File

@@ -10,7 +10,6 @@ import {
type MatrixRTCSession,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/lib/matrixrtc";
import { type CallMembershipIdentityParts } from "matrix-js-sdk/lib/matrixrtc/EncryptionManager";
import { logger as rootLogger } from "matrix-js-sdk/lib/logger";
const logger = rootLogger.getChild("[MatrixKeyProvider]");
@@ -27,10 +26,6 @@ export class MatrixKeyProvider extends BaseKeyProvider {
MatrixRTCSessionEvent.EncryptionKeyChanged,
this.onEncryptionKeyChanged,
);
this.rtcSession.off(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMembershipsChanged,
);
}
this.rtcSession = rtcSession;
@@ -39,63 +34,17 @@ export class MatrixKeyProvider extends BaseKeyProvider {
MatrixRTCSessionEvent.EncryptionKeyChanged,
this.onEncryptionKeyChanged,
);
this.rtcSession.on(
MatrixRTCSessionEvent.MembershipsChanged,
this.onMembershipsChanged,
);
// The new session could be aware of keys of which the old session wasn't,
// so emit key changed events
this.rtcSession.reemitEncryptionKeys();
}
private keyCache = new Array<{
membership: CallMembershipIdentityParts;
encryptionKey: Uint8Array;
encryptionKeyIndex: number;
}>();
private onMembershipsChanged = (): void => {
const duplicatedArray = this.keyCache;
// Reset key cache first. It will get repopulated when calling `onEncryptionKeyChanged`
this.keyCache = [];
let next = duplicatedArray.pop();
while (next !== undefined) {
logger.debug(
"[KeyCache] remove key event from the cache and try adding it again. For membership: ",
next.membership,
);
this.onEncryptionKeyChanged(
next.encryptionKey,
next.encryptionKeyIndex,
next.membership,
);
next = duplicatedArray.pop();
}
};
private onEncryptionKeyChanged = (
encryptionKey: Uint8Array,
encryptionKeyIndex: number,
membership: CallMembershipIdentityParts,
rtcBackendIdentity: string,
): void => {
// This is the only way we can get the kind of the membership event we just received the key for.
// best case we want to recompute this once the memberships change (you can receive the key before the participant...)
const membershipFull = this.rtcSession?.memberships.find(
(m) =>
m.userId === membership.userId &&
m.deviceId === membership.deviceId &&
m.memberId === membership.memberId,
);
if (!membershipFull) {
logger.debug(
"[KeyCache] Added key event to the cache because we do not have a membership for it (yet): ",
membership,
);
this.keyCache.push({ membership, encryptionKey, encryptionKeyIndex });
return;
}
crypto.subtle
.importKey("raw", encryptionKey, "HKDF", false, [
"deriveBits",
@@ -105,7 +54,7 @@ export class MatrixKeyProvider extends BaseKeyProvider {
(keyMaterial) => {
this.onSetEncryptionKey(
keyMaterial,
membershipFull.rtcBackendIdentity,
rtcBackendIdentity,
encryptionKeyIndex,
);