π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Clarify wasm loader API
Manav Rathi41267b07fbba
diff --git a/web/apps/locker/src/services/crypto.ts b/web/apps/locker/src/services/crypto.tsindex 782cf89713..a6f5e76879 100644--- a/web/apps/locker/src/services/crypto.ts+++ b/web/apps/locker/src/services/crypto.ts@@ -15,7 +15,7 @@ import type {EncryptedFile,KeyPair,} from "ente-base/crypto/types";-import { ensureWasmCryptoInit } from "ente-wasm/load";+import { loadCryptoReadyEnteWasm } from "ente-wasm/load";const shouldFallbackToLegacyBlobDecrypt = (error: unknown): boolean => {if (!error || typeof error !== "object") {@@ -70,7 +70,7 @@ export const decryptBox = async (box: EncryptedBox,key: Uint8Array | string,): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_decrypt_box(toB64String(box.encryptedData),toB64String(box.nonce),@@ -97,7 +97,7 @@ export const decryptMetadataJSON = async (blob: EncryptedBlob,key: Uint8Array | string,): Promise<unknown> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const encryptedData = toB64String(blob.encryptedData);const decryptionHeader = toB64String(blob.decryptionHeader);const keyB64 = toB64String(key);@@ -129,7 +129,7 @@ export const boxSealOpen = async (encryptedData: string,keyPair: KeyPair,): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_box_seal_open(encryptedData,keyPair.publicKey,@@ -141,7 +141,7 @@ export const boxSeal = async (dataB64: string,publicKeyB64: string,): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_box_seal(dataB64, publicKeyB64);};@@ -159,12 +159,12 @@ export const boxSeal = async (* Generate a random 32-byte key (base64).*/export const generateKey = async (): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_generate_key();};export const md5Base64 = async (data: Uint8Array): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_md5_base64(data);};@@ -176,7 +176,7 @@ export const encryptBox = async (dataB64: string,keyB64: string,): Promise<{ encryptedData: string; nonce: string }> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const result = wasm.crypto_encrypt_box(dataB64, keyB64);return { encryptedData: result.encrypted_data, nonce: result.nonce };};@@ -189,7 +189,7 @@ export const encryptBlob = async (dataB64: string,keyB64: string,): Promise<{ encryptedData: string; decryptionHeader: string }> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const result = wasm.crypto_encrypt_blob(dataB64, keyB64);return {encryptedData: result.encrypted_data,@@ -228,7 +228,7 @@ export interface StreamDecryptorHandle {export const createStreamEncryptor =async (): Promise<StreamEncryptorHandle> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const encryptor = new wasm.CryptoStreamEncryptor();return {@@ -244,7 +244,7 @@ export const createStreamDecryptor = async (decryptionHeader: string,keyB64: string,): Promise<StreamDecryptorHandle> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const decryptor = new wasm.CryptoStreamDecryptor(decryptionHeader, keyB64);return {@@ -280,7 +280,7 @@ export interface EncryptedFileResult {export const encryptFileStream = async (dataB64: string,): Promise<EncryptedFileResult> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const result = wasm.crypto_encrypt_stream(dataB64);return {encryptedData: result.encrypted_data,@@ -300,7 +300,7 @@ export const encryptFileStreamWithKey = async (dataB64: string,keyB64: string,): Promise<EncryptedFileResult> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const result = wasm.crypto_encrypt_stream_with_key(dataB64, keyB64);return {encryptedData: result.encrypted_data,@@ -324,7 +324,7 @@ export const decryptStreamBytes = async (file: EncryptedFile,key: Uint8Array | string,): Promise<Uint8Array> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const plaintextB64 = wasm.crypto_decrypt_stream(toB64String(file.encryptedData),toB64String(file.decryptionHeader),diff --git a/web/packages/accounts-rs/services/crypto.ts b/web/packages/accounts-rs/services/crypto.tsindex 516f6da1ec..bea25a11f8 100644--- a/web/packages/accounts-rs/services/crypto.ts+++ b/web/packages/accounts-rs/services/crypto.ts@@ -1,4 +1,4 @@-import { ensureWasmCryptoInit } from "ente-wasm/load";+import { loadCryptoReadyEnteWasm } from "ente-wasm/load";export const deriveKeyInsufficientMemoryErrorMessage ="Failed to derive key (insufficient memory)";@@ -84,7 +84,7 @@ export const deriveKey = async (opsLimit: number,memLimit: number,) => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.auth_derive_kek(password, saltB64, memLimit, opsLimit);};@@ -98,7 +98,7 @@ const normalizeDerivedKey = (result: WasmDerivedKey): DerivedKey => ({export const deriveSensitiveKey = async (password: string,): Promise<DerivedKey> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();try {return normalizeDerivedKey(wasm.auth_generate_sensitive_kek(password) as WasmDerivedKey,@@ -111,7 +111,7 @@ export const deriveSensitiveKey = async (export const deriveInteractiveKey = async (password: string,): Promise<DerivedKey> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();try {return normalizeDerivedKey(wasm.auth_generate_interactive_kek(password) as WasmDerivedKey,@@ -122,12 +122,12 @@ export const deriveInteractiveKey = async (};export const generateKey = async (): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_generate_key();};export const generateKeyPair = async (): Promise<KeyPair> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const keyPair = wasm.crypto_generate_keypair();return { publicKey: keyPair.public_key, privateKey: keyPair.secret_key };};@@ -136,7 +136,7 @@ export const encryptBox = async (dataB64: string,keyB64: string,): Promise<EncryptedBox> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const box = wasm.crypto_encrypt_box(dataB64, keyB64);return { encryptedData: box.encrypted_data, nonce: box.nonce };};@@ -145,7 +145,7 @@ export const decryptBox = async (box: EncryptedBox,keyB64: string,): Promise<string> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.crypto_decrypt_box(box.encryptedData, box.nonce, keyB64);};@@ -153,7 +153,7 @@ export const boxSealOpenBytes = async (encryptedData: string,keyPair: KeyPair,): Promise<Uint8Array> => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return fromB64(wasm.crypto_box_seal_open(encryptedData,@@ -169,7 +169,7 @@ export const deriveSubKeyBytes = async (subKeyID: number,context: string,) => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return fromB64(wasm.crypto_derive_subkey(keyB64,@@ -184,7 +184,7 @@ export const generateSRPSetupAttributesRust = async (kekB64: string,srpUserID: string,) => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();const setup = wasm.auth_generate_srp_setup(kekB64,srpUserID,@@ -197,11 +197,11 @@ export const generateSRPSetupAttributesRust = async (};export const recoveryKeyFromMnemonicOrHex = async (value: string) => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.auth_recovery_key_from_mnemonic_or_hex(value);};export const recoveryKeyToMnemonicRust = async (recoveryKey: string) => {- const wasm = await ensureWasmCryptoInit();+ const wasm = await loadCryptoReadyEnteWasm();return wasm.auth_recovery_key_to_mnemonic(recoveryKey);};diff --git a/web/packages/wasm/load.ts b/web/packages/wasm/load.tsindex b63beb5a61..06f0136826 100644--- a/web/packages/wasm/load.ts+++ b/web/packages/wasm/load.ts@@ -16,11 +16,12 @@ export const loadEnteWasm = (): Promise<EnteWasmModule> =>}));/**- * Ensure the WASM crypto backend is initialised, then return the ready module.+ * Load `ente-wasm`, initialize the crypto backend, and return the ready+ * module.** The first call runs `crypto_init()`. Later calls reuse the same promise.*/-export const ensureWasmCryptoInit = (): Promise<EnteWasmModule> =>+export const loadCryptoReadyEnteWasm = (): Promise<EnteWasmModule> =>(cryptoReadyPromise ??= loadEnteWasm().then((wasm) => {wasm.crypto_init();diff --git a/web/packages/wasm/package.json b/web/packages/wasm/package.jsonindex 29dd1be9b5..b0ead6c573 100644--- a/web/packages/wasm/package.json+++ b/web/packages/wasm/package.json@@ -14,10 +14,6 @@},"module": "./pkg/ente_wasm.js","types": "./pkg/ente_wasm.d.ts",- "files": [- "pkg",- "load.ts"- ],"scripts": {"build": "wasm-pack build --target bundler","test": "vitest"