Skip to content

Commit

Permalink
Encrypt options & drop alt
Browse files Browse the repository at this point in the history
  • Loading branch information
narze committed Aug 8, 2023
1 parent 90187a4 commit ff43ef7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/lib/commands/chatlog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const command: Command = {

reply('(wip) Here are the chat logs:');

reply(lines.join('\n'));
reply({
message: lines.join('\n'),
type: 'text',
encrypted: true
});
}
};

Expand Down
26 changes: 24 additions & 2 deletions src/lib/commands/components/ChatMessage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@
}
}
function getMeta() {
if (message.encrypted) {
// Tries to decrypt the message, if failed, return error message
const key = localStorage.getItem('chat-os-encryption-key');
if (!key) {
return '[no encryption key set]';
}
try {
const decrypted = decryptMessage(message.meta as string, key);
return decrypted;
} catch (e) {
console.info(e);
return '[decryption failed]';
}
} else {
return message.meta;
}
}
const decryptMessage = (messageWithNonce: string, key: string) => {
const keyUint8Array = decodeBase64(key);
const messageWithNonceAsUint8Array = decodeBase64(messageWithNonce);
Expand Down Expand Up @@ -194,13 +216,13 @@
{/if}

{#if message.type == 'image'}
<img src={getMessage()} alt={message.alt} />
<img src={getMessage()} alt={getMeta().alt} />
{:else if message.type == 'link'}
<a href={getMessage()} target="_blank" rel="noreferrer" class="link">{getMessage()}</a>
{:else if message.type == 'component'}
{#if getMessage() in components}
<!-- <Renderer component={components[getMessage()]} props={{}} /> -->
<svelte:component this={components[getMessage()]} options={message.meta} />
<svelte:component this={components[getMessage()]} options={getMeta()} />
{/if}
{:else}
{#each getMessage().split('\n') as line}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/commands/promptpay-qr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const command: Command = {
message: await QRCode.toDataURL(ppqr(args[1], { amount: args[3] ? +args[3] : undefined }), {
scale: 6
}),
options: { alt }
options: { alt },
encrypted: true
});
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/lib/commands/qr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const command: Command = {
reply({
type: 'image',
message: await QRCode.toDataURL(args[1], { scale: 6 }),
options: { alt: args[1] }
options: { alt: args[1] },
encrypted: true
});
}
};
Expand Down
1 change: 1 addition & 0 deletions src/lib/commands/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const command: Command = {
reply({
type: 'component',
message: 'timer',
encrypted: true,
options: { seconds, startAt: Date.now(), name: args.name || null }
});
}
Expand Down
16 changes: 7 additions & 9 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script lang="ts" context="module">
type EncryptedMeta = string;
export interface Log {
id: string;
message: string;
self: boolean;
time: Timestamp;
type: string;
alt?: string;
meta?: Record<string, any>;
meta?: Record<string, any> | EncryptedMeta;
guestSession?: boolean;
encrypted?: boolean;
}
Expand Down Expand Up @@ -62,7 +63,6 @@
message: log.message,
time: Timestamp.fromDate(log.time),
type: log.type,
alt: log.alt,
meta: log.meta,
guestSession: log.guestSession
})) satisfies Log[];
Expand Down Expand Up @@ -206,7 +206,7 @@
}
}
const encryptMessage = (json: string, key: string) => {
const encryptMessage = (json: string | Record<string, any>, key: string) => {
const keyUint8Array = decodeBase64(key);
const nonce = randomBytes(secretbox.nonceLength);
const messageUint8 = decodeUTF8(JSON.stringify(json));
Expand All @@ -225,29 +225,28 @@
const options = payload?.options || {};
const encrypted = payload?.encrypted || false;
console.log({ payload });
setTimeout(async () => {
if ($user) {
const encryptionKey = localStorage.getItem('chat-os-encryption-key');
if (encrypted && encryptionKey) {
const encryptedMessage = encryptMessage(message, encryptionKey);
const encryptedOptions = encryptMessage(options, encryptionKey);
await messagesCollection.add({
self: false,
message: encryptedMessage,
time: Timestamp.now(),
type,
alt: options.alt || null,
meta: encryptedOptions as unknown as Record<string, any>,
encrypted: true
});
} else {
await messagesCollection.add({
self: false,
message,
time: Timestamp.now(),
alt: options.alt || null,
meta: options,
type
});
}
Expand All @@ -258,7 +257,6 @@
message,
time: new Date(),
type: type,
alt: options.alt,
meta: options
});
}
Expand Down

0 comments on commit ff43ef7

Please sign in to comment.