ghost-portal/packages/apps-electron/src/electron.ts
Krzysztof Jelski f8f7eedad3
Improve Electron App security (#2923)
* WIP on storing accounts without electron remote modules.

* Improve security by isolating main and renderer.

* Apply suggestions from code review

Co-authored-by: Jaco Greeff <jacogr@gmail.com>

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
2020-06-10 18:09:55 +02:00

68 lines
1.7 KiB
TypeScript

// Copyright 2017-2020 @polkadot/apps authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { app, BrowserWindow, dialog, screen } from 'electron';
import { autoUpdater } from 'electron-updater';
import path from 'path';
import { features } from './featureToggles';
import { registerAccountStoreHandlers } from './main/account-store';
const ENV = process.env.NODE_ENV || 'production';
const isDev = ENV === 'development';
function createWindow (): Promise<unknown> {
const { height, width } = screen.getPrimaryDisplay().workAreaSize;
const win = new BrowserWindow({
height,
webPreferences: {
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
},
width
});
if (isDev) {
win.webContents.openDevTools();
return win.loadURL('http://0.0.0.0:3000/');
}
const mainFilePath = path.resolve(__dirname, 'index.html');
return win.loadFile(mainFilePath);
}
const onReady = async () => {
registerAccountStoreHandlers();
await createWindow();
if (features.autoUpdater) {
await autoUpdater.checkForUpdatesAndNotify();
}
};
app.whenReady().then(onReady).catch(console.error);
if (features.autoUpdater) {
autoUpdater.on('update-not-available', () => {
dialog.showMessageBox({
message: 'Current version is up-to-date.',
title: 'No Updates'
}).catch(console.error);
});
autoUpdater.on('error', (error) => {
if (!error) {
return;
}
const err: Error = error as Error;
dialog.showErrorBox('Auto update error: ', (err.stack || err).toString());
});
}