From e220aeea91c7841f1a11b1dc40ac1ffd08fd3cd2 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 21 Sep 2023 15:39:00 +1000 Subject: [PATCH] feat: add working encrypt/decrypt for 03 group with libsession --- ts/receiver/configMessage.ts | 4 +- ts/receiver/contentMessage.ts | 22 +++++--- ts/session/crypto/MessageEncrypter.ts | 2 +- ts/session/types/PubKey.ts | 52 ------------------- ....ts => libsession_wrapper_user_profile.ts} | 0 .../browser/libsession_worker_interface.ts | 11 ++-- 6 files changed, 23 insertions(+), 68 deletions(-) rename ts/test/session/unit/libsession_wrapper/{libsession_wrapper_test.ts => libsession_wrapper_user_profile.ts} (100%) diff --git a/ts/receiver/configMessage.ts b/ts/receiver/configMessage.ts index a992c77e1..709390bbc 100644 --- a/ts/receiver/configMessage.ts +++ b/ts/receiver/configMessage.ts @@ -679,8 +679,8 @@ async function handleGroupUpdate(latestEnvelopeTimestamp: number) { const allGoupsIdsInWrapper = allGoupsInWrapper.map(m => m.pubkeyHex); const allGoupsIdsInDb = allGoupsInDb.map(m => m.id as string); - console.warn('allGoupsIdsInWrapper', stringify(allGoupsIdsInWrapper)); - console.warn('allGoupsIdsInDb', stringify(allGoupsIdsInDb)); + window.log.debug('allGoupsIdsInWrapper', stringify(allGoupsIdsInWrapper)); + window.log.debug('allGoupsIdsInDb', stringify(allGoupsIdsInDb)); const userEdKeypair = await UserUtils.getUserED25519KeyPairBytes(); if (!userEdKeypair) { diff --git a/ts/receiver/contentMessage.ts b/ts/receiver/contentMessage.ts index f25e3b151..a0daf1c07 100644 --- a/ts/receiver/contentMessage.ts +++ b/ts/receiver/contentMessage.ts @@ -60,16 +60,26 @@ export async function handleSwarmContentMessage(envelope: EnvelopePlus, messageH async function decryptForGroupV2(envelope: EnvelopePlus) { window?.log?.info('received closed group message v2'); - const groupPk = envelope.source; - if (!PubKey.isClosedGroupV2(groupPk)) { - throw new PreConditionFailed('decryptForGroupV2: not a 03 prefixed group'); - } + try { + const groupPk = envelope.source; + if (!PubKey.isClosedGroupV2(groupPk)) { + throw new PreConditionFailed('decryptForGroupV2: not a 03 prefixed group'); + } - return MetaGroupWrapperActions.decryptMessage(groupPk, envelope.content); + const decrypted = await MetaGroupWrapperActions.decryptMessage(groupPk, envelope.content); + + // the receiving pipeline relies on the envelope.senderIdentity field to know who is the author of a message + // eslint-disable-next-line no-param-reassign + envelope.senderIdentity = decrypted.pubkeyHex; + + return decrypted.plaintext; + } catch (e) { + window.log.warn('failed to decrypt message with error: ', e.message); + return null; + } } async function decryptForClosedGroup(envelope: EnvelopePlus) { - // case .closedGroupCiphertext: for ios window?.log?.info('received closed group message'); try { const hexEncodedGroupPublicKey = envelope.source; diff --git a/ts/session/crypto/MessageEncrypter.ts b/ts/session/crypto/MessageEncrypter.ts index 29d09804e..b491c31ea 100644 --- a/ts/session/crypto/MessageEncrypter.ts +++ b/ts/session/crypto/MessageEncrypter.ts @@ -19,7 +19,7 @@ type EncryptResult = { async function encryptWithLibSession(destination: GroupPubkeyType, plainText: Uint8Array) { try { - return MetaGroupWrapperActions.encryptMessage(destination, plainText, true); + return MetaGroupWrapperActions.encryptMessage(destination, plainText); } catch (e) { window.log.warn('encrypt message for group failed with', e.message); throw new SigningFailed(e.message); diff --git a/ts/session/types/PubKey.ts b/ts/session/types/PubKey.ts index 2ad1912a9..f473fcade 100644 --- a/ts/session/types/PubKey.ts +++ b/ts/session/types/PubKey.ts @@ -26,58 +26,6 @@ export enum KeyPrefixType { groupV3 = '03', } -// export type GroupV2PubKey = { -// key: GroupPubkeyType; // 03 prefix for groups v2 -// isGroupV2: true; -// isLegacyGroup: false; -// isPrivate: false; -// isUS: false; -// isBlinded: false; -// }; - -// export type PrivatePubkey = { -// key: PubkeyType; // 05 prefix for private conversations -// isGroupV2: false; -// isLegacyGroup: false; -// isPrivate: true; -// isUS: false; -// isBlinded: false; -// }; - -// export type UsPubkey = { -// key: PubkeyType; // 05 prefix for note to self -// isGroupV2: false; -// isLegacyGroup: false; -// isPrivate: false; -// isUS: true; -// isBlinded: false; -// }; - -// export type PrivateBlindedPubkey = { -// key: BlindedPubkeyType; // 15 prefix for blinded pubkeys -// isGroupV2: false; -// isLegacyGroup: false; -// isPrivate: true; -// isUS: false; -// isBlinded: true; -// }; - -// export type LegacyGroupPubkey = { -// key: PubkeyType; // 05 prefix for legacy closed group -// isGroupV2: false; -// isLegacyGroup: true; -// isPrivate: false; -// isUS: false; -// isBlinded: false; -// }; - -// export type PubKeyRecord = -// | UsPubkey -// | PrivatePubkey -// | GroupV2PubKey -// | LegacyGroupPubkey -// | PrivateBlindedPubkey; - // TODO make that Pubkey class more useful, add fields for what types of pubkey it is (group, legacy group, private) export class PubKey { diff --git a/ts/test/session/unit/libsession_wrapper/libsession_wrapper_test.ts b/ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_profile.ts similarity index 100% rename from ts/test/session/unit/libsession_wrapper/libsession_wrapper_test.ts rename to ts/test/session/unit/libsession_wrapper/libsession_wrapper_user_profile.ts diff --git a/ts/webworker/workers/browser/libsession_worker_interface.ts b/ts/webworker/workers/browser/libsession_worker_interface.ts index 096eeb7d2..0215fe6e0 100644 --- a/ts/webworker/workers/browser/libsession_worker_interface.ts +++ b/ts/webworker/workers/browser/libsession_worker_interface.ts @@ -510,13 +510,10 @@ export const MetaGroupWrapperActions: MetaGroupWrapperActionsCalls = { data, timestampMs, ]) as Promise>, - encryptMessage: async (groupPk: GroupPubkeyType, plaintext: Uint8Array, compress: boolean) => - callLibSessionWorker([ - `MetaGroupConfig-${groupPk}`, - 'encryptMessage', - plaintext, - compress, - ]) as Promise>, + encryptMessage: async (groupPk: GroupPubkeyType, plaintext: Uint8Array) => + callLibSessionWorker([`MetaGroupConfig-${groupPk}`, 'encryptMessage', plaintext]) as Promise< + ReturnType + >, decryptMessage: async (groupPk: GroupPubkeyType, ciphertext: Uint8Array) => callLibSessionWorker([`MetaGroupConfig-${groupPk}`, 'decryptMessage', ciphertext]) as Promise< ReturnType