From 10d4ae7dcce52cef1d191f4c861be1518f8fe4e9 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 17 May 2023 14:10:49 +0000 Subject: [PATCH] Decrypt messages in the ssb app. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4299 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/ssb/app.js | 3 +++ apps/ssb/tf-message.js | 21 +++++++++++++++++++-- core/core.js | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/apps/ssb/app.js b/apps/ssb/app.js index c42918e4..1a61fe79 100644 --- a/apps/ssb/app.js +++ b/apps/ssb/app.js @@ -85,6 +85,9 @@ tfrpc.register(async function store_message(message) { tfrpc.register(function apps() { return core.apps(); }); +tfrpc.register(async function try_decrypt(id, content) { + return await ssb.privateMessageDecrypt(id, content); +}); ssb.addEventListener('broadcasts', async function() { await tfrpc.rpc.set('broadcasts', await ssb.getBroadcasts()); }); diff --git a/apps/ssb/tf-message.js b/apps/ssb/tf-message.js index 9fde3cd4..92e26514 100644 --- a/apps/ssb/tf-message.js +++ b/apps/ssb/tf-message.js @@ -14,6 +14,7 @@ class TfMessageElement extends LitElement { raw: {type: Boolean}, blog_data: {type: String}, expanded: {type: Object}, + decrypted: {type: Object}, }; } @@ -28,6 +29,7 @@ class TfMessageElement extends LitElement { this.drafts = {}; this.raw = false; this.expanded = {}; + this.decrypted = undefined; } show_reply() { @@ -218,8 +220,15 @@ class TfMessageElement extends LitElement { } } + async try_decrypt(content) { + this.decrypted = JSON.parse(await tfrpc.rpc.try_decrypt(this.whoami, content)); + } + render() { let content = this.message?.content; + if (this.decrypted?.type == 'post') { + content = this.decrypted; + } let self = this; let raw_button = this.raw ? html` self.raw = false}>` : @@ -328,6 +337,8 @@ class TfMessageElement extends LitElement { ` : content_warning : content_html; + let is_encrypted = this.decrypted ? html`🔓` : undefined; + let style_background = this.decrypted ? 'rgba(255, 0, 0, 0.2)' : 'rgba(255, 255, 255, 0.1)'; return html` -
+
+ ${is_encrypted} % ${new Date(this.message.timestamp).toLocaleString()} ${raw_button} @@ -430,7 +442,12 @@ class TfMessageElement extends LitElement {
`); } else if (typeof(this.message.content) == 'string') { - return small_frame(html`🔒`); + if (this.decrypted !== undefined) { + return small_frame(html`🔓
${JSON.stringify(this.decrypted, null, 2)}
`); + } else { + this.try_decrypt(content); + return small_frame(html`🔐`); + } } else { return small_frame(html`
type: ${content.type}
`); } diff --git a/core/core.js b/core/core.js index d95779e6..a9359d01 100644 --- a/core/core.js +++ b/core/core.js @@ -759,7 +759,7 @@ async function blobHandler(request, response, blobId, uri) { sendData(response, data, type, headers); } } else { - sendData(response, data, type, {}); + sendData(response, data, undefined, {}); } } }