fix parsing of decrypted content

This commit is contained in:
balzack 2024-11-08 23:45:17 -08:00
parent 0315ccc464
commit b8abef126a
4 changed files with 35 additions and 13 deletions

View File

@ -85,9 +85,10 @@ export class NativeCrypto implements Crypto {
// decrypt data with private rsa key // decrypt data with private rsa key
public async rsaDecrypt(encryptedDataB64: string, privateKeyB64: string): Promise<{data: string}> { public async rsaDecrypt(encryptedDataB64: string, privateKeyB64: string): Promise<{data: string}> {
const crypto = new JSEncrypt(); const begin = '-----BEGIN RSA PRIVATE KEY-----\n';
crypto.setPrivateKey(privateKeyB64); const end = '\n-----END RSA PRIVATE KEY-----';
const data = await RSA.decrypt(encryptedDataB64, privateKeyB64); const key = `${begin}${privateKeyB64}${end}`;
const data = await RSA.decrypt(encryptedDataB64, key);
if (!data) { if (!data) {
throw new Error('rsaDecrypt failed'); throw new Error('rsaDecrypt failed');
} }

View File

@ -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 hosted = cardId == null;
const subject = data?.subject ? [data.subject] : buildSubject(); const subject = data?.subject ? [data.subject] : buildSubject();
const message = lastTopic ? (lastTopic.data ? lastTopic.data.text : null) : ''; const message = getMessage();
const imageUrl = selectImage(); const imageUrl = selectImage();
return {cardId, channelId, sealed, hosted, unread, imageUrl, subject, message}; return {cardId, channelId, sealed, hosted, unread, imageUrl, subject, message};

View File

@ -447,6 +447,7 @@ export class ContactModule implements Contact {
this.log.warn(err); this.log.warn(err);
} }
} }
this.emitChannels(cardId);
} }
this.unsealAll = false; this.unsealAll = false;
} }
@ -985,8 +986,7 @@ export class ContactModule implements Contact {
} }
if (item.channelKey) { if (item.channelKey) {
const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey); const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey);
const { subject } = JSON.parse(data); item.unsealedDetail = data;
item.unsealedDetail = subject;
return true; return true;
} }
} catch (err) { } catch (err) {
@ -1006,8 +1006,7 @@ export class ContactModule implements Contact {
if (item.channelKey) { if (item.channelKey) {
const { messageEncrypted, messageIv } = JSON.parse(item.summary.data); const { messageEncrypted, messageIv } = JSON.parse(item.summary.data);
const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey); const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey);
const { message } = JSON.parse(data); item.unsealedSummary = data;
item.unsealedSummary = message;
return true; return true;
} }
} catch (err) { } catch (err) {

View File

@ -95,7 +95,7 @@ export class StreamModule {
try { try {
return JSON.parse(data); return JSON.parse(data);
} catch (err) { } catch (err) {
console.log('invalid channel data'); this.log.warn('invalid channel data');
} }
} }
return null; return null;
@ -204,6 +204,7 @@ export class StreamModule {
} }
} }
this.unsealAll = false; this.unsealAll = false;
this.emitChannels();
} }
this.syncing = false; this.syncing = false;
@ -498,8 +499,7 @@ export class StreamModule {
} }
if (item.channelKey) { if (item.channelKey) {
const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey); const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey);
const { subject } = JSON.parse(data); item.unsealedDetail = data;
item.unsealedDetail = subject;
return true; return true;
} }
} catch (err) { } catch (err) {
@ -519,8 +519,7 @@ export class StreamModule {
if (item.channelKey) { if (item.channelKey) {
const { messageEncrypted, messageIv } = JSON.parse(item.summary.data); const { messageEncrypted, messageIv } = JSON.parse(item.summary.data);
const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey); const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey);
const { message } = JSON.parse(data); item.unsealedSummary = data;
item.unsealedSummary = message;
return true; return true;
} }
} catch (err) { } catch (err) {