From 011a439870a256ff0d851c80c36375b51207ef1d Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 13 Dec 2022 14:38:02 -0800 Subject: [PATCH] unsealing own channels as well in webapp --- net/web/src/context/useChannelContext.hook.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/net/web/src/context/useChannelContext.hook.js b/net/web/src/context/useChannelContext.hook.js index ce327476..d6f0d42f 100644 --- a/net/web/src/context/useChannelContext.hook.js +++ b/net/web/src/context/useChannelContext.hook.js @@ -131,9 +131,24 @@ export function useChannelContext() { return await addChannel(access.current, 'sealed', cards, data); }, unsealChannelSubject: (channelId, sealKey) => { - console.log("unseal: ", channelId); - let sealed = channels.current.get(channelId); - console.log(sealed); + const channel = channels.current.get(channelId); + + const { subjectEncrypted, subjectIv, seals } = JSON.parse(channel.data.channelDetail.data); + seals.forEach(seal => { + if (seal.publicKey === sealKey.public) { + let crypto = new JSEncrypt(); + crypto.setPrivateKey(sealKey.private); + const unsealedKey = crypto.decrypt(seal.sealedKey); + const iv = CryptoJS.enc.Hex.parse(subjectIv); + const key = CryptoJS.enc.Hex.parse(unsealedKey); + const enc = CryptoJS.enc.Base64.parse(subjectEncrypted); + let cipher = CryptoJS.lib.CipherParams.create({ ciphertext: enc, iv: iv }); + const dec = CryptoJS.AES.decrypt(cipher, key, { iv: iv }); + channel.data.unsealedChannel = JSON.parse(dec.toString(CryptoJS.enc.Utf8)); + channels.current.set(channel.id, { ...channel }); + updateState({ channels: channels.current }); + } + }); }, setChannelSubject: async (channelId, subject) => { return await setChannelSubject(access.current, channelId, subject);