Handle set/getEncryptionKey in preload

This commit is contained in:
Manav Rathi 2024-03-25 12:19:19 +05:30
parent 106ba270fe
commit dd7e87274a
No known key found for this signature in database
5 changed files with 35 additions and 47 deletions

View file

@ -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");

View file

@ -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),
);
};

View file

@ -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,

View file

@ -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);
});

View file

@ -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,