Skip to content

armgit / ente

Public Imported from GitHub
0

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

Handle set/getEncryptionKey in preload

Manav Rathidd7e87274a07
diff --git a/desktop/src/api/safeStorage.ts b/desktop/src/api/safeStorage.ts
index cffb23302c..e3d6749515 100644
--- a/desktop/src/api/safeStorage.ts
+++ b/desktop/src/api/safeStorage.ts
@@ -1,13 +1,11 @@
-import { ipcRenderer } from "electron";
+import { safeStorage } from "electron/main";
import { logError } from "../main/log";
import { safeStorageStore } from "../stores/safeStorage.store";
export async function setEncryptionKey(encryptionKey: string) {
try {
- const encryptedKey: Buffer = await ipcRenderer.invoke(
- "safeStorage-encrypt",
- encryptionKey,
- );
+ const encryptedKey: Buffer =
+ await safeStorage.encryptString(encryptionKey);
const b64EncryptedKey = Buffer.from(encryptedKey).toString("base64");
safeStorageStore.set("encryptionKey", b64EncryptedKey);
} catch (e) {
@@ -20,10 +18,8 @@ export async function getEncryptionKey(): Promise<string> {
try {
const b64EncryptedKey = safeStorageStore.get("encryptionKey");
if (b64EncryptedKey) {
- const keyBuffer = new Uint8Array(
- Buffer.from(b64EncryptedKey, "base64"),
- );
- return await ipcRenderer.invoke("safeStorage-decrypt", keyBuffer);
+ const keyBuffer = Buffer.from(b64EncryptedKey, "base64");
+ return await safeStorage.decryptString(keyBuffer);
}
} catch (e) {
logError(e, "getEncryptionKey failed");
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index 394221ead2..5173198c0d 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -7,18 +7,19 @@
*/
import { ipcMain } from "electron/main";
-import {
- computeImageEmbedding,
- computeTextEmbedding,
-} from "services/clipService";
-import type { Model } from "types";
import { clearElectronStore } from "../api/electronStore";
+import { getEncryptionKey, setEncryptionKey } from "../api/safeStorage";
import {
appVersion,
muteUpdateNotification,
skipAppUpdate,
updateAndRestart,
} from "../services/appUpdater";
+import {
+ computeImageEmbedding,
+ computeTextEmbedding,
+} from "../services/clipService";
+import type { Model } from "../types";
import { checkExistsAndCreateDir, fsExists } from "./fs";
import { openDirectory, openLogDirectory } from "./general";
import { logToDisk } from "./log";
@@ -51,16 +52,16 @@ export const attachIPCHandlers = () => {
// See: [Note: Catching exception during .send/.on]
ipcMain.on("logToDisk", (_, message) => logToDisk(message));
- ipcMain.handle("fsExists", (_, path) => fsExists(path));
-
- ipcMain.handle("checkExistsAndCreateDir", (_, dirPath) =>
- checkExistsAndCreateDir(dirPath),
- );
-
ipcMain.on("clear-electron-store", (_) => {
clearElectronStore();
});
+ ipcMain.handle("setEncryptionKey", (_, encryptionKey) =>
+ setEncryptionKey(encryptionKey),
+ );
+
+ ipcMain.handle("getEncryptionKey", (_) => getEncryptionKey());
+
ipcMain.on("update-and-restart", (_) => {
updateAndRestart();
});
@@ -82,4 +83,10 @@ export const attachIPCHandlers = () => {
ipcMain.handle("computeTextEmbedding", (_, model: Model, text: string) =>
computeTextEmbedding(model, text),
);
+
+ ipcMain.handle("fsExists", (_, path) => fsExists(path));
+
+ ipcMain.handle("checkExistsAndCreateDir", (_, dirPath) =>
+ checkExistsAndCreateDir(dirPath),
+ );
};
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 8a9f1a01e3..a0f0f4308f 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -34,7 +34,6 @@ import path from "path";
import { runFFmpegCmd } from "./api/ffmpeg";
import { getDirFiles } from "./api/fs";
import { convertToJPEG, generateImageThumbnail } from "./api/imageProcessor";
-import { getEncryptionKey, setEncryptionKey } from "./api/safeStorage";
import {
getElectronFilesFromGoogleZip,
getPendingUploads,
@@ -122,6 +121,12 @@ const clearElectronStore = () => {
ipcRenderer.send("clear-electron-store");
};
+const setEncryptionKey = (encryptionKey: string): Promise<void> =>
+ ipcRenderer.invoke("setEncryptionKey", encryptionKey);
+
+const getEncryptionKey = (): Promise<string> =>
+ ipcRenderer.invoke("getEncryptionKey");
+
// - App update
const updateAndRestart = () => {
@@ -364,6 +369,8 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
openDirectory,
registerForegroundEventListener,
clearElectronStore,
+ getEncryptionKey,
+ setEncryptionKey,
// Logging
openLogDirectory,
@@ -401,8 +408,6 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
showUploadZipDialog,
getElectronFilesFromGoogleZip,
setToUploadCollection,
- getEncryptionKey,
- setEncryptionKey,
getDirFiles,
getWatchMappings,
addWatchMapping,
diff --git a/desktop/src/utils/ipcComms.ts b/desktop/src/utils/ipcComms.ts
index 3f42244e86..3c64f0e40a 100644
--- a/desktop/src/utils/ipcComms.ts
+++ b/desktop/src/utils/ipcComms.ts
@@ -1,20 +1,7 @@
import chokidar from "chokidar";
-import {
- app,
- BrowserWindow,
- dialog,
- ipcMain,
- safeStorage,
- shell,
- Tray,
-} from "electron";
+import { BrowserWindow, dialog, ipcMain, Tray } from "electron";
import path from "path";
import { attachIPCHandlers } from "../main/ipc";
-import {
- muteUpdateNotification,
- skipAppUpdate,
- updateAndRestart,
-} from "../services/appUpdater";
import {
computeImageEmbedding,
computeTextEmbedding,
@@ -79,14 +66,6 @@ export default function setupIpcComs(
watcher.unwatch(args.dir);
});
- ipcMain.handle("safeStorage-encrypt", (_, message) => {
- return safeStorage.encryptString(message);
- });
-
- ipcMain.handle("safeStorage-decrypt", (_, message) => {
- return safeStorage.decryptString(message);
- });
-
ipcMain.handle("convert-to-jpeg", (_, fileData, filename) => {
return convertToJPEG(fileData, filename);
});
diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.ts
index 76e8994d2c..b751ae40f2 100644
--- a/web/packages/shared/electron/types.ts
+++ b/web/packages/shared/electron/types.ts
@@ -83,6 +83,9 @@ export interface ElectronAPIsType {
registerForegroundEventListener: (onForeground: () => void) => void;
clearElectronStore: () => void;
+ setEncryptionKey: (encryptionKey: string) => Promise<void>;
+ getEncryptionKey: () => Promise<string>;
+
// - FS legacy
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
@@ -149,8 +152,6 @@ export interface ElectronAPIsType {
removeFolder: (folderPath: string) => Promise<void>,
) => void;
isFolder: (dirPath: string) => Promise<boolean>;
- setEncryptionKey: (encryptionKey: string) => Promise<void>;
- getEncryptionKey: () => Promise<string>;
convertToJPEG: (
fileData: Uint8Array,
filename: string,