From b8abef126a680b43e13acbe5dae72ce4789560b8 Mon Sep 17 00:00:00 2001 From: balzack Date: Fri, 8 Nov 2024 23:45:17 -0800 Subject: [PATCH] fix parsing of decrypted content --- app/client/mobile/src/NativeCrypto.ts | 7 +++--- .../mobile/src/content/useContent.hook.ts | 25 ++++++++++++++++++- app/sdk/src/contact.ts | 7 +++--- app/sdk/src/stream.ts | 9 +++---- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/app/client/mobile/src/NativeCrypto.ts b/app/client/mobile/src/NativeCrypto.ts index 386bef9b..d588749e 100644 --- a/app/client/mobile/src/NativeCrypto.ts +++ b/app/client/mobile/src/NativeCrypto.ts @@ -85,9 +85,10 @@ export class NativeCrypto implements Crypto { // decrypt data with private rsa key public async rsaDecrypt(encryptedDataB64: string, privateKeyB64: string): Promise<{data: string}> { - const crypto = new JSEncrypt(); - crypto.setPrivateKey(privateKeyB64); - const data = await RSA.decrypt(encryptedDataB64, privateKeyB64); + const begin = '-----BEGIN RSA PRIVATE KEY-----\n'; + const end = '\n-----END RSA PRIVATE KEY-----'; + const key = `${begin}${privateKeyB64}${end}`; + const data = await RSA.decrypt(encryptedDataB64, key); if (!data) { throw new Error('rsaDecrypt failed'); } diff --git a/app/client/mobile/src/content/useContent.hook.ts b/app/client/mobile/src/content/useContent.hook.ts index da0a5285..ffab358d 100644 --- a/app/client/mobile/src/content/useContent.hook.ts +++ b/app/client/mobile/src/content/useContent.hook.ts @@ -95,9 +95,32 @@ export function useContent() { } }; + const getMessage = () => { + if (!lastTopic) { + return ''; + } + if (lastTopic.dataType === 'superbasictopic') { + if (lastTopic.data?.text) { + return lastTopic.data.text; + } else { + return '' + } + } else if (lastTopic.dataType === 'sealedtopic') { + if (lastTopic.data) { + if (lastTopic.data.message?.text) { + return lastTopic.data.message.text; + } else { + return ''; + } + } else { + return null; + } + } + } + const hosted = cardId == null; const subject = data?.subject ? [data.subject] : buildSubject(); - const message = lastTopic ? (lastTopic.data ? lastTopic.data.text : null) : ''; + const message = getMessage(); const imageUrl = selectImage(); return {cardId, channelId, sealed, hosted, unread, imageUrl, subject, message}; diff --git a/app/sdk/src/contact.ts b/app/sdk/src/contact.ts index bf205bba..c25d0d7f 100644 --- a/app/sdk/src/contact.ts +++ b/app/sdk/src/contact.ts @@ -447,6 +447,7 @@ export class ContactModule implements Contact { this.log.warn(err); } } + this.emitChannels(cardId); } this.unsealAll = false; } @@ -985,8 +986,7 @@ export class ContactModule implements Contact { } if (item.channelKey) { const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey); - const { subject } = JSON.parse(data); - item.unsealedDetail = subject; + item.unsealedDetail = data; return true; } } catch (err) { @@ -1006,8 +1006,7 @@ export class ContactModule implements Contact { if (item.channelKey) { const { messageEncrypted, messageIv } = JSON.parse(item.summary.data); const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey); - const { message } = JSON.parse(data); - item.unsealedSummary = message; + item.unsealedSummary = data; return true; } } catch (err) { diff --git a/app/sdk/src/stream.ts b/app/sdk/src/stream.ts index ad13c89d..6340bda3 100644 --- a/app/sdk/src/stream.ts +++ b/app/sdk/src/stream.ts @@ -95,7 +95,7 @@ export class StreamModule { try { return JSON.parse(data); } catch (err) { - console.log('invalid channel data'); + this.log.warn('invalid channel data'); } } return null; @@ -204,6 +204,7 @@ export class StreamModule { } } this.unsealAll = false; + this.emitChannels(); } this.syncing = false; @@ -498,8 +499,7 @@ export class StreamModule { } if (item.channelKey) { const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey); - const { subject } = JSON.parse(data); - item.unsealedDetail = subject; + item.unsealedDetail = data; return true; } } catch (err) { @@ -519,8 +519,7 @@ export class StreamModule { if (item.channelKey) { const { messageEncrypted, messageIv } = JSON.parse(item.summary.data); const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey); - const { message } = JSON.parse(data); - item.unsealedSummary = message; + item.unsealedSummary = data; return true; } } catch (err) {