diff --git a/ts/receiver/closedGroups.ts b/ts/receiver/closedGroups.ts index 449e1ac67..eb48040c5 100644 --- a/ts/receiver/closedGroups.ts +++ b/ts/receiver/closedGroups.ts @@ -33,6 +33,11 @@ import { MessageController } from '../session/messages'; import { ClosedGroupEncryptionPairReplyMessage } from '../session/messages/outgoing/content/data/group'; import { queueAllCachedFromSource } from './receiver'; +export const distributingClosedGroupEncryptionKeyPairs = new Map< + string, + ECKeyPair +>(); + export async function handleClosedGroupControlMessage( envelope: EnvelopePlus, groupUpdate: SignalService.DataMessage.ClosedGroupControlMessage @@ -456,6 +461,9 @@ async function handleClosedGroupEncryptionKeyPair( ); if (isKeyPairAlreadyHere) { + const existingKeyPairs = await getAllEncryptionKeyPairsForGroup( + groupPublicKey + ); window.log.info('Dropping already saved keypair for group', groupPublicKey); await removeFromCache(envelope); return; @@ -764,11 +772,17 @@ async function sendLatestKeyPairToUsers( groupPubKey: string, targetUsers: Array ) { + // use the inMemory keypair if found + + const inMemoryKeyPair = distributingClosedGroupEncryptionKeyPairs.get( + groupPubKey + ); + // Get the latest encryption key pair const latestKeyPair = await getLatestClosedGroupEncryptionKeyPair( groupPubKey ); - if (!latestKeyPair) { + if (!inMemoryKeyPair && !latestKeyPair) { window.log.info( 'We do not have the keypair ourself, so dropping this message.' ); @@ -789,7 +803,7 @@ async function sendLatestKeyPairToUsers( const wrappers = await ClosedGroup.buildEncryptionKeyPairWrappers( [member], - ECKeyPair.fromHexKeyPair(latestKeyPair) + inMemoryKeyPair || ECKeyPair.fromHexKeyPair(latestKeyPair) ); const keypairsMessage = new ClosedGroupEncryptionPairReplyMessage({ diff --git a/ts/session/group/index.ts b/ts/session/group/index.ts index 4be9e19a6..d5cc8de88 100644 --- a/ts/session/group/index.ts +++ b/ts/session/group/index.ts @@ -35,6 +35,7 @@ import { ClosedGroupUpdateMessage, } from '../messages/outgoing/content/data/group'; import { MessageController } from '../messages'; +import { distributingClosedGroupEncryptionKeyPairs } from '../../receiver/closedGroups'; export interface GroupInfo { id: string; @@ -572,11 +573,15 @@ export async function generateAndSendNewEncryptionKeyPair( expireTimer, }); + distributingClosedGroupEncryptionKeyPairs.set(toHex(groupId), newKeyPair); + const messageSentCallback = async () => { window.log.info( `KeyPairMessage for ClosedGroup ${groupPublicKey} is sent. Saving the new encryptionKeyPair.` ); + distributingClosedGroupEncryptionKeyPairs.delete(toHex(groupId)); + await addClosedGroupEncryptionKeyPair( toHex(groupId), newKeyPair.toHexKeyPair()