From 76da94cf7891d4468d48028b08d0252732295aab Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 17 Aug 2024 21:41:07 +0530 Subject: [PATCH] Add the variant --- web/packages/base/crypto/ente-impl.ts | 22 ++++++++------- web/packages/base/crypto/ente.ts | 39 ++++++++++++++++++--------- web/packages/base/crypto/types.ts | 19 ------------- web/packages/base/crypto/worker.ts | 1 + 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/web/packages/base/crypto/ente-impl.ts b/web/packages/base/crypto/ente-impl.ts index 1c28e3d124..6e3115795b 100644 --- a/web/packages/base/crypto/ente-impl.ts +++ b/web/packages/base/crypto/ente-impl.ts @@ -1,6 +1,6 @@ /** Careful when adding add other imports! */ import * as libsodium from "./libsodium"; -import type { BytesOrB64, EncryptedBlob_2, EncryptJSON } from "./types"; +import type { BytesOrB64, EncryptedBlob_2 } from "./types"; export const _encryptBoxB64 = libsodium.encryptBoxB64; @@ -16,16 +16,18 @@ export const _encryptThumbnail = async (data: BytesOrB64, key: BytesOrB64) => { }; }; -export const _encryptMetadataJSON_New = ({ jsonValue, keyB64 }: EncryptJSON) => - _encryptBlobB64( - new TextEncoder().encode(JSON.stringify(jsonValue)), - keyB64, - ); +export const _encryptMetadataJSON_New = (jsonValue: unknown, key: BytesOrB64) => + _encryptBlobB64(new TextEncoder().encode(JSON.stringify(jsonValue)), key); -export const _encryptMetadataJSON = async (r: EncryptJSON) => { - // Deprecated. Keep the old API for now. - const { encryptedData, decryptionHeader } = - await _encryptMetadataJSON_New(r); +// Deprecated, translates to the old API for now. +export const _encryptMetadataJSON = async (r: { + jsonValue: unknown; + keyB64: string; +}) => { + const { encryptedData, decryptionHeader } = await _encryptMetadataJSON_New( + r.jsonValue, + r.keyB64, + ); return { encryptedDataB64: encryptedData, decryptionHeaderB64: decryptionHeader, diff --git a/web/packages/base/crypto/ente.ts b/web/packages/base/crypto/ente.ts index 8feb0ccdce..7585f961bd 100644 --- a/web/packages/base/crypto/ente.ts +++ b/web/packages/base/crypto/ente.ts @@ -51,12 +51,7 @@ import { sharedCryptoWorker } from "."; import { assertionFailed } from "../assert"; import { inWorker } from "../env"; import * as ei from "./ente-impl"; -import type { - BytesOrB64, - EncryptedBlob_2, - EncryptedBox2, - EncryptJSON, -} from "./types"; +import type { BytesOrB64, EncryptedBlob_2, EncryptedBox2 } from "./types"; /** * Some of these functions have not yet been needed on the main thread, and for @@ -129,18 +124,36 @@ export const encryptThumbnail = (data: BytesOrB64, key: BytesOrB64) => * Encrypt the JSON metadata associated with an Ente object (file, collection or * entity) using the object's key. * - * This is a variant of {@link encryptAssociatedData} tailored for encrypting - * any arbitrary metadata associated with an Ente object. For example, it is - * used for encrypting the various metadata fields (See: [Note: Metadatum]) - * associated with a file, using that file's key. + * This is a variant of {@link encryptBlobB64} tailored for encrypting any + * arbitrary metadata associated with an Ente object. For example, it is used + * for encrypting the various metadata fields associated with a file, using that + * file's key. * * Instead of raw bytes, it takes as input an arbitrary JSON object which it - * encodes into a string, and encrypts that. And instead of returning the raw - * encrypted bytes, it returns their base64 string representation. + * encodes into a string, and encrypts that. * * Use {@link decryptMetadataJSON} to decrypt the result. + * + * @param jsonValue The JSON value to encrypt. This can be an arbitrary JSON + * value, but since TypeScript currently doesn't have a native JSON type, it is + * typed as {@link unknown}. + * + * @param key The encryption key. */ -export const encryptMetadataJSON = async (r: EncryptJSON) => +export const encryptMetadataJSON_New = (jsonValue: unknown, key: BytesOrB64) => + inWorker() + ? ei._encryptMetadataJSON_New(jsonValue, key) + : sharedCryptoWorker().then((w) => + w.encryptMetadataJSON_New(jsonValue, key), + ); + +/** + * Deprecated, use {@link encryptMetadataJSON_New} instead. + */ +export const encryptMetadataJSON = async (r: { + jsonValue: unknown; + keyB64: string; +}) => inWorker() ? ei._encryptMetadataJSON(r) : sharedCryptoWorker().then((w) => w.encryptMetadataJSON(r)); diff --git a/web/packages/base/crypto/types.ts b/web/packages/base/crypto/types.ts index fdc9542062..d4c972e8c6 100644 --- a/web/packages/base/crypto/types.ts +++ b/web/packages/base/crypto/types.ts @@ -4,25 +4,6 @@ */ export type BytesOrB64 = Uint8Array | string; -/** - * Deprecated. - * - * A variant of {@link EncryptBytes} with the data as a JSON value. - */ -export interface EncryptJSON { - /** - * The JSON value to encrypt. - * - * This can be an arbitrary JSON value, but since TypeScript currently - * doesn't have a native JSON type, it is typed as {@link unknown}. - */ - jsonValue: unknown; - /** - * A base64 string containing the encryption key. - */ - keyB64: string; -} - /** * The result of encryption using the secretbox APIs. * diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts index c87ce36b42..7ce5b56775 100644 --- a/web/packages/base/crypto/worker.ts +++ b/web/packages/base/crypto/worker.ts @@ -14,6 +14,7 @@ import * as libsodium from "./libsodium"; export class CryptoWorker { encryptBoxB64 = ei._encryptBoxB64; encryptThumbnail = ei._encryptThumbnail; + encryptMetadataJSON_New = ei._encryptMetadataJSON_New; encryptMetadataJSON = ei._encryptMetadataJSON; decryptBox = ei._decryptBox; decryptBoxB64 = ei._decryptBoxB64;