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
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');
}

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 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};

View File

@ -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) {

View File

@ -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) {