From 0c1343cad50b831a5d4b3fbad5b930d8ac9bac74 Mon Sep 17 00:00:00 2001
From: Audric Ackermann <audric@loki.network>
Date: Fri, 12 Feb 2021 11:00:22 +1100
Subject: [PATCH] add a way to share currently distributing keypair to added
 members

---
 ts/receiver/closedGroups.ts | 18 ++++++++++++++++--
 ts/session/group/index.ts   |  5 +++++
 2 files changed, 21 insertions(+), 2 deletions(-)

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<string>
 ) {
+  // 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()