@@ -80,8 +80,8 @@ export class EncryptionManager implements IEncryptionManager {
8080 // if it looks like a membership has been updated.
8181 private lastMembershipFingerprints : Set < string > | undefined ;
8282
83- private currentEncryptionKeyIndex = - 1 ;
84-
83+ private mediaTrailerKeyIndexInUse = - 1 ;
84+ private latestGeneratedKeyIndex = - 1 ;
8585 private joinConfig : EncryptionConfig | undefined ;
8686
8787 public constructor (
@@ -254,28 +254,29 @@ export class EncryptionManager implements IEncryptionManager {
254254
255255 if ( ! this . joined ) return ;
256256
257- logger . info ( `Sending encryption keys event. indexToSend=${ indexToSend } ` ) ;
258-
259257 const myKeys = this . getKeysForParticipant ( this . userId , this . deviceId ) ;
260258
261259 if ( ! myKeys ) {
262260 logger . warn ( "Tried to send encryption keys event but no keys found!" ) ;
263261 return ;
264262 }
265263
266- if ( typeof indexToSend !== "number" && this . currentEncryptionKeyIndex === - 1 ) {
264+ if ( typeof indexToSend !== "number" && this . latestGeneratedKeyIndex === - 1 ) {
267265 logger . warn ( "Tried to send encryption keys event but no current key index found!" ) ;
268266 return ;
269267 }
270268
271- const keyIndexToSend = indexToSend ?? this . currentEncryptionKeyIndex ;
269+ const keyIndexToSend = indexToSend ?? this . latestGeneratedKeyIndex ;
270+ logger . info (
271+ `Try sending encryption keys event. keyIndexToSend=${ keyIndexToSend } (method parameter: ${ indexToSend } )` ,
272+ ) ;
272273 const keyToSend = myKeys [ keyIndexToSend ] ;
273274
274275 try {
275276 this . statistics . counters . roomEventEncryptionKeysSent += 1 ;
276277 await this . transport . sendKey ( encodeUnpaddedBase64 ( keyToSend ) , keyIndexToSend , this . getMemberships ( ) ) ;
277278 logger . debug (
278- `Embedded-E2EE-LOG updateEncryptionKeyEvent participantId=${ this . userId } :${ this . deviceId } numKeys=${ myKeys . length } currentKeyIndex=${ this . currentEncryptionKeyIndex } keyIndexToSend=${ keyIndexToSend } ` ,
279+ `sendEncryptionKeysEvent participantId=${ this . userId } :${ this . deviceId } numKeys=${ myKeys . length } currentKeyIndex=${ this . latestGeneratedKeyIndex } keyIndexToSend=${ keyIndexToSend } ` ,
279280 this . encryptionKeys ,
280281 ) ;
281282 } catch ( error ) {
@@ -290,6 +291,7 @@ export class EncryptionManager implements IEncryptionManager {
290291 } ;
291292
292293 public onNewKeyReceived : KeyTransportEventListener = ( userId , deviceId , keyBase64Encoded , index , timestamp ) => {
294+ logger . debug ( `Received key over key transport ${ userId } :${ deviceId } at index ${ index } ` ) ;
293295 this . setEncryptionKey ( userId , deviceId , index , keyBase64Encoded , timestamp ) ;
294296 } ;
295297
@@ -302,12 +304,12 @@ export class EncryptionManager implements IEncryptionManager {
302304 }
303305
304306 private getNewEncryptionKeyIndex ( ) : number {
305- if ( this . currentEncryptionKeyIndex === - 1 ) {
307+ if ( this . latestGeneratedKeyIndex === - 1 ) {
306308 return 0 ;
307309 }
308310
309311 // maximum key index is 255
310- return ( this . currentEncryptionKeyIndex + 1 ) % 256 ;
312+ return ( this . latestGeneratedKeyIndex + 1 ) % 256 ;
311313 }
312314
313315 /**
@@ -357,6 +359,7 @@ export class EncryptionManager implements IEncryptionManager {
357359 }
358360 }
359361
362+ this . latestGeneratedKeyIndex = encryptionKeyIndex ;
360363 participantKeys [ encryptionKeyIndex ] = {
361364 key : keyBin ,
362365 timestamp,
@@ -365,18 +368,18 @@ export class EncryptionManager implements IEncryptionManager {
365368 if ( delayBeforeUse ) {
366369 const useKeyTimeout = setTimeout ( ( ) => {
367370 this . setNewKeyTimeouts . delete ( useKeyTimeout ) ;
368- logger . info ( `Delayed-emitting key changed event for ${ participantId } idx ${ encryptionKeyIndex } ` ) ;
371+ logger . info ( `Delayed-emitting key changed event for ${ participantId } index ${ encryptionKeyIndex } ` ) ;
369372 if ( userId === this . userId && deviceId === this . deviceId ) {
370- this . currentEncryptionKeyIndex = encryptionKeyIndex ;
373+ this . mediaTrailerKeyIndexInUse = encryptionKeyIndex ;
371374 }
372- this . onEncryptionKeysChanged ( keyBin , encryptionKeyIndex , participantId ) ;
375+ this . onEncryptionKeysChanged ( keyBin , this . mediaTrailerKeyIndexInUse , participantId ) ;
373376 } , this . useKeyDelay ) ;
374377 this . setNewKeyTimeouts . add ( useKeyTimeout ) ;
375378 } else {
376379 if ( userId === this . userId && deviceId === this . deviceId ) {
377- this . currentEncryptionKeyIndex = encryptionKeyIndex ;
380+ this . mediaTrailerKeyIndexInUse = encryptionKeyIndex ;
378381 }
379- this . onEncryptionKeysChanged ( keyBin , encryptionKeyIndex , participantId ) ;
382+ this . onEncryptionKeysChanged ( keyBin , this . mediaTrailerKeyIndexInUse , participantId ) ;
380383 }
381384 }
382385
0 commit comments