Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Return wasm module from init helper

Manav Rathib8bafaf28b52
diff --git a/web/apps/locker/src/services/crypto.ts b/web/apps/locker/src/services/crypto.ts
index 570e526264..782cf89713 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, loadEnteWasm } from "ente-wasm/load";
+import { ensureWasmCryptoInit } from "ente-wasm/load";
const shouldFallbackToLegacyBlobDecrypt = (error: unknown): boolean => {
if (!error || typeof error !== "object") {
@@ -70,8 +70,7 @@ export const decryptBox = async (
box: EncryptedBox,
key: Uint8Array | string,
): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_decrypt_box(
toB64String(box.encryptedData),
toB64String(box.nonce),
@@ -98,8 +97,7 @@ export const decryptMetadataJSON = async (
blob: EncryptedBlob,
key: Uint8Array | string,
): Promise<unknown> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const encryptedData = toB64String(blob.encryptedData);
const decryptionHeader = toB64String(blob.decryptionHeader);
const keyB64 = toB64String(key);
@@ -131,8 +129,7 @@ export const boxSealOpen = async (
encryptedData: string,
keyPair: KeyPair,
): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_box_seal_open(
encryptedData,
keyPair.publicKey,
@@ -144,8 +141,7 @@ export const boxSeal = async (
dataB64: string,
publicKeyB64: string,
): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_box_seal(dataB64, publicKeyB64);
};
@@ -163,14 +159,12 @@ export const boxSeal = async (
* Generate a random 32-byte key (base64).
*/
export const generateKey = async (): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_generate_key();
};
export const md5Base64 = async (data: Uint8Array): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_md5_base64(data);
};
@@ -182,8 +176,7 @@ export const encryptBox = async (
dataB64: string,
keyB64: string,
): Promise<{ encryptedData: string; nonce: string }> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const result = wasm.crypto_encrypt_box(dataB64, keyB64);
return { encryptedData: result.encrypted_data, nonce: result.nonce };
};
@@ -196,8 +189,7 @@ export const encryptBlob = async (
dataB64: string,
keyB64: string,
): Promise<{ encryptedData: string; decryptionHeader: string }> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const result = wasm.crypto_encrypt_blob(dataB64, keyB64);
return {
encryptedData: result.encrypted_data,
@@ -236,8 +228,7 @@ export interface StreamDecryptorHandle {
export const createStreamEncryptor =
async (): Promise<StreamEncryptorHandle> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const encryptor = new wasm.CryptoStreamEncryptor();
return {
@@ -253,8 +244,7 @@ export const createStreamDecryptor = async (
decryptionHeader: string,
keyB64: string,
): Promise<StreamDecryptorHandle> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const decryptor = new wasm.CryptoStreamDecryptor(decryptionHeader, keyB64);
return {
@@ -290,8 +280,7 @@ export interface EncryptedFileResult {
export const encryptFileStream = async (
dataB64: string,
): Promise<EncryptedFileResult> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const result = wasm.crypto_encrypt_stream(dataB64);
return {
encryptedData: result.encrypted_data,
@@ -311,8 +300,7 @@ export const encryptFileStreamWithKey = async (
dataB64: string,
keyB64: string,
): Promise<EncryptedFileResult> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const result = wasm.crypto_encrypt_stream_with_key(dataB64, keyB64);
return {
encryptedData: result.encrypted_data,
@@ -336,8 +324,7 @@ export const decryptStreamBytes = async (
file: EncryptedFile,
key: Uint8Array | string,
): Promise<Uint8Array> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
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.ts
index 495613f1dc..516f6da1ec 100644
--- a/web/packages/accounts-rs/services/crypto.ts
+++ b/web/packages/accounts-rs/services/crypto.ts
@@ -1,4 +1,4 @@
-import { ensureWasmCryptoInit, loadEnteWasm } from "ente-wasm/load";
+import { ensureWasmCryptoInit } from "ente-wasm/load";
export const deriveKeyInsufficientMemoryErrorMessage =
"Failed to derive key (insufficient memory)";
@@ -84,8 +84,7 @@ export const deriveKey = async (
opsLimit: number,
memLimit: number,
) => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.auth_derive_kek(password, saltB64, memLimit, opsLimit);
};
@@ -99,9 +98,8 @@ const normalizeDerivedKey = (result: WasmDerivedKey): DerivedKey => ({
export const deriveSensitiveKey = async (
password: string,
): Promise<DerivedKey> => {
- await ensureWasmCryptoInit();
+ const wasm = await ensureWasmCryptoInit();
try {
- const wasm = await loadEnteWasm();
return normalizeDerivedKey(
wasm.auth_generate_sensitive_kek(password) as WasmDerivedKey,
);
@@ -113,9 +111,8 @@ export const deriveSensitiveKey = async (
export const deriveInteractiveKey = async (
password: string,
): Promise<DerivedKey> => {
- await ensureWasmCryptoInit();
+ const wasm = await ensureWasmCryptoInit();
try {
- const wasm = await loadEnteWasm();
return normalizeDerivedKey(
wasm.auth_generate_interactive_kek(password) as WasmDerivedKey,
);
@@ -125,14 +122,12 @@ export const deriveInteractiveKey = async (
};
export const generateKey = async (): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_generate_key();
};
export const generateKeyPair = async (): Promise<KeyPair> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const keyPair = wasm.crypto_generate_keypair();
return { publicKey: keyPair.public_key, privateKey: keyPair.secret_key };
};
@@ -141,8 +136,7 @@ export const encryptBox = async (
dataB64: string,
keyB64: string,
): Promise<EncryptedBox> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const box = wasm.crypto_encrypt_box(dataB64, keyB64);
return { encryptedData: box.encrypted_data, nonce: box.nonce };
};
@@ -151,8 +145,7 @@ export const decryptBox = async (
box: EncryptedBox,
keyB64: string,
): Promise<string> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.crypto_decrypt_box(box.encryptedData, box.nonce, keyB64);
};
@@ -160,8 +153,7 @@ export const boxSealOpenBytes = async (
encryptedData: string,
keyPair: KeyPair,
): Promise<Uint8Array> => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return fromB64(
wasm.crypto_box_seal_open(
encryptedData,
@@ -177,8 +169,7 @@ export const deriveSubKeyBytes = async (
subKeyID: number,
context: string,
) => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return fromB64(
wasm.crypto_derive_subkey(
keyB64,
@@ -193,8 +184,7 @@ export const generateSRPSetupAttributesRust = async (
kekB64: string,
srpUserID: string,
) => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
const setup = wasm.auth_generate_srp_setup(
kekB64,
srpUserID,
@@ -207,13 +197,11 @@ export const generateSRPSetupAttributesRust = async (
};
export const recoveryKeyFromMnemonicOrHex = async (value: string) => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.auth_recovery_key_from_mnemonic_or_hex(value);
};
export const recoveryKeyToMnemonicRust = async (recoveryKey: string) => {
- await ensureWasmCryptoInit();
- const wasm = await loadEnteWasm();
+ const wasm = await ensureWasmCryptoInit();
return wasm.auth_recovery_key_to_mnemonic(recoveryKey);
};
diff --git a/web/packages/wasm/load.ts b/web/packages/wasm/load.ts
index 2d6c559068..b63beb5a61 100644
--- a/web/packages/wasm/load.ts
+++ b/web/packages/wasm/load.ts
@@ -1,7 +1,7 @@
type EnteWasmModule = typeof import("ente-wasm");
let wasmPromise: Promise<EnteWasmModule> | undefined;
-let cryptoInitDone = false;
+let cryptoReadyPromise: Promise<EnteWasmModule> | undefined;
/**
* Load `ente-wasm` once and return the shared module instance.
@@ -9,18 +9,24 @@ let cryptoInitDone = false;
* The first call performs the dynamic import. Later calls reuse the same
* promise, so all consumers share one loader path.
*/
-export const loadEnteWasm = async (): Promise<EnteWasmModule> => {
- wasmPromise ??= import("ente-wasm");
- return wasmPromise;
-};
+export const loadEnteWasm = (): Promise<EnteWasmModule> =>
+ (wasmPromise ??= import("ente-wasm").catch((error: unknown) => {
+ wasmPromise = undefined;
+ throw error;
+ }));
/**
- * Ensure the WASM crypto backend has been initialised exactly once for the
- * current page runtime.
+ * Ensure the WASM crypto backend is initialised, then return the ready module.
+ *
+ * The first call runs `crypto_init()`. Later calls reuse the same promise.
*/
-export const ensureWasmCryptoInit = async () => {
- if (cryptoInitDone) return;
- const wasm = await loadEnteWasm();
- wasm.crypto_init();
- cryptoInitDone = true;
-};
+export const ensureWasmCryptoInit = (): Promise<EnteWasmModule> =>
+ (cryptoReadyPromise ??= loadEnteWasm()
+ .then((wasm) => {
+ wasm.crypto_init();
+ return wasm;
+ })
+ .catch((error: unknown) => {
+ cryptoReadyPromise = undefined;
+ throw error;
+ }));