diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts index 98f577eaa7..a43c7673d7 100644 --- a/web/packages/base/crypto/index.ts +++ b/web/packages/base/crypto/index.ts @@ -111,7 +111,7 @@ let _comlinkWorker: ComlinkWorker | undefined; /** * Lazily created, cached, instance of a CryptoWorker web worker. */ -export const sharedCryptoWorker = async () => +export const sharedCryptoWorker = () => (_comlinkWorker ??= createComlinkCryptoWorker()).remote; /** A shorter alias of {@link sharedCryptoWorker} for use within this file. */ @@ -144,7 +144,7 @@ export const toB64URLSafe = (bytes: Uint8Array): Promise => /** * Convert a base64 string to bytes ({@link Uint8Array}). */ -export const fromB64 = (b64String: string) => +export const fromB64 = (b64String: string): Promise => inWorker() ? ei._fromB64(b64String) : sharedWorker().then((w) => w.fromB64(b64String)); @@ -160,7 +160,7 @@ export const toHex = (b64String: string): Promise => /** * Convert a hex string to the base64 representation of the underlying bytes. */ -export const fromHex = (hexString: string) => +export const fromHex = (hexString: string): Promise => inWorker() ? ei._fromHex(hexString) : sharedWorker().then((w) => w.fromHex(hexString)); @@ -286,7 +286,7 @@ export const encryptMetadataJSON = ( * Encrypt the given data using chunked streaming encryption, but process all * the chunks in one go. */ -export const encryptStreamBytes = async ( +export const encryptStreamBytes = ( data: Uint8Array, key: BytesOrB64, ): Promise => @@ -305,11 +305,11 @@ export const initChunkEncryption = async (key: BytesOrB64) => /** * Encrypt a chunk as part of a chunked streaming encryption. */ -export const encryptStreamChunk = async ( +export const encryptStreamChunk = ( data: Uint8Array, state: StateAddress, isFinalChunk: boolean, -) => +): Promise => inWorker() ? ei._encryptStreamChunk(data, state, isFinalChunk) : sharedWorker().then((w) => @@ -332,7 +332,10 @@ export const decryptBox = ( * Variant of {@link decryptBox} that returns the decrypted bytes as it is * (without encoding them to base64). */ -export const decryptBoxBytes = (box: EncryptedBox, key: BytesOrB64) => +export const decryptBoxBytes = ( + box: EncryptedBox, + key: BytesOrB64, +): Promise => inWorker() ? ei._decryptBoxBytes(box, key) : sharedWorker().then((w) => w.decryptBoxBytes(box, key)); @@ -366,7 +369,10 @@ export const decryptBlob = ( * A variant of {@link decryptBlobBytes} that returns the result bytes directly * (instead of encoding them as a base64 string). */ -export const decryptBlobBytes = (blob: EncryptedBlob, key: BytesOrB64) => +export const decryptBlobBytes = ( + blob: EncryptedBlob, + key: BytesOrB64, +): Promise => inWorker() ? ei._decryptBlobBytes(blob, key) : sharedWorker().then((w) => w.decryptBlobBytes(blob, key)); @@ -374,10 +380,10 @@ export const decryptBlobBytes = (blob: EncryptedBlob, key: BytesOrB64) => /** * Decrypt the result of {@link encryptStreamBytes}. */ -export const decryptStreamBytes = async ( +export const decryptStreamBytes = ( file: EncryptedFile, key: BytesOrB64, -) => +): Promise => inWorker() ? ei._decryptStreamBytes(file, key) : sharedWorker().then((w) => w.decryptStreamBytes(file, key)); @@ -396,10 +402,10 @@ export const initChunkDecryption = async (header: string, key: BytesOrB64) => * * This function is used in tandem with {@link initChunkDecryption}. */ -export const decryptStreamChunk = async ( +export const decryptStreamChunk = ( data: Uint8Array, state: StateAddress, -) => +): Promise => inWorker() ? ei._decryptStreamChunk(data, state) : sharedWorker().then((w) => w.decryptStreamChunk(data, state)); diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts index 601bbeebd7..f2305398f3 100644 --- a/web/packages/base/crypto/libsodium.ts +++ b/web/packages/base/crypto/libsodium.ts @@ -36,7 +36,7 @@ export const toB64 = async (input: Uint8Array) => { * * This is the converse of {@link toBase64}. */ -export const fromB64 = async (input: string) => { +export const fromB64 = async (input: string): Promise => { await sodium.ready; return sodium.from_base64(input, sodium.base64_variants.ORIGINAL); }; @@ -117,7 +117,7 @@ export const toHex = async (input: string) => { * * This is the inverse of {@link toHex}. */ -export const fromHex = async (input: string) => { +export const fromHex = async (input: string): Promise => { await sodium.ready; return await toB64(sodium.from_hex(input)); }; @@ -478,7 +478,7 @@ export const encryptStreamChunk = async ( data: Uint8Array, pushState: sodium.StateAddress, isFinalChunk: boolean, -) => { +): Promise => { await sodium.ready; const tag = isFinalChunk ? sodium.crypto_secretstream_xchacha20poly1305_TAG_FINAL @@ -577,7 +577,7 @@ export const decryptMetadataJSON = async ( export const decryptStreamBytes = async ( { encryptedData, decryptionHeader }: EncryptedFile, key: BytesOrB64, -) => { +): Promise => { await sodium.ready; const pullState = sodium.crypto_secretstream_xchacha20poly1305_init_pull( await fromB64(decryptionHeader), @@ -647,7 +647,7 @@ export const initChunkDecryption = async ( export const decryptStreamChunk = async ( data: Uint8Array, pullState: StateAddress, -) => { +): Promise => { await sodium.ready; const pullResult = sodium.crypto_secretstream_xchacha20poly1305_pull( pullState,