-
Notifications
You must be signed in to change notification settings - Fork 295
/
index.js
268 lines (237 loc) · 8.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// @flow
import os from 'os';
import path from 'path';
import { app, BrowserWindow, shell } from 'electron';
import { client } from 'electron-connect';
import { logger } from './utils/logging';
import {
setupLogging,
logSystemInfo,
logStateSnapshot,
generateWalletMigrationReport,
} from './utils/setupLogging';
import { handleDiskSpace } from './utils/handleDiskSpace';
import { handleCheckBlockReplayProgress } from './utils/handleCheckBlockReplayProgress';
import { createMainWindow } from './windows/main';
import { installChromeExtensions } from './utils/installChromeExtensions';
import { environment } from './environment';
import mainErrorHandler from './utils/mainErrorHandler';
import {
launcherConfig,
pubLogsFolderPath,
stateDirectoryPath,
} from './config';
import { setupCardanoNode } from './cardano/setup';
import { CardanoNode } from './cardano/CardanoNode';
import { safeExitWithCode } from './utils/safeExitWithCode';
import { buildAppMenus } from './utils/buildAppMenus';
import { getLocale } from './utils/getLocale';
import { detectSystemLocale } from './utils/detectSystemLocale';
import { ensureXDGDataIsSet } from './cardano/config';
import { rebuildApplicationMenu } from './ipc/rebuild-application-menu';
import { getStateDirectoryPathChannel } from './ipc/getStateDirectoryPathChannel';
import { getDesktopDirectoryPathChannel } from './ipc/getDesktopDirectoryPathChannel';
import { getSystemLocaleChannel } from './ipc/getSystemLocaleChannel';
import { CardanoNodeStates } from '../common/types/cardano-node.types';
import type { CheckDiskSpaceResponse } from '../common/types/no-disk-space.types';
import { logUsedVersion } from './utils/logUsedVersion';
import { setStateSnapshotLogChannel } from './ipc/set-log-state-snapshot';
import { generateWalletMigrationReportChannel } from './ipc/generateWalletMigrationReportChannel';
import { enableApplicationMenuNavigationChannel } from './ipc/enableApplicationMenuNavigationChannel';
import { pauseActiveDownloads } from './ipc/downloadManagerChannel';
// import { isHardwareWalletSupportEnabled, isLedgerEnabled } from '../renderer/app/config/hardwareWalletsConfig';
/* eslint-disable consistent-return */
// Global references to windows to prevent them from being garbage collected
let mainWindow: BrowserWindow;
let cardanoNode: ?CardanoNode;
const {
isDev,
isWatchMode,
isBlankScreenFixActive,
network,
os: osName,
version: daedalusVersion,
nodeVersion: cardanoNodeVersion,
apiVersion: cardanoWalletVersion,
} = environment;
if (isBlankScreenFixActive) {
// Run "location.assign('chrome://gpu')" in JavaScript console to see if the flag is active
app.disableHardwareAcceleration();
}
app.allowRendererProcessReuse = true;
const safeExit = async () => {
pauseActiveDownloads();
if (!cardanoNode || cardanoNode.state === CardanoNodeStates.STOPPED) {
logger.info('Daedalus:safeExit: exiting Daedalus with code 0', { code: 0 });
return safeExitWithCode(0);
}
if (cardanoNode.state === CardanoNodeStates.STOPPING) {
logger.info('Daedalus:safeExit: waiting for cardano-node to stop...');
cardanoNode.exitOnStop();
return;
}
try {
const pid = cardanoNode.pid || 'null';
logger.info(`Daedalus:safeExit: stopping cardano-node with PID: ${pid}`, {
pid,
});
await cardanoNode.stop();
logger.info('Daedalus:safeExit: exiting Daedalus with code 0', { code: 0 });
safeExitWithCode(0);
} catch (error) {
logger.error('Daedalus:safeExit: cardano-node did not exit correctly', {
error,
});
safeExitWithCode(0);
}
};
const onAppReady = async () => {
setupLogging();
logUsedVersion(
environment.version,
path.join(pubLogsFolderPath, 'Daedalus-versions.json')
);
const cpu = os.cpus();
const platformVersion = os.release();
const ram = JSON.stringify(os.totalmem(), null, 2);
const startTime = new Date().toISOString();
// first checks for japanese locale, otherwise returns english
const systemLocale = detectSystemLocale();
const systemInfo = logSystemInfo({
cardanoNodeVersion,
cardanoWalletVersion,
cpu,
daedalusVersion,
isBlankScreenFixActive,
network,
osName,
platformVersion,
ram,
startTime,
});
logger.info(`Daedalus is starting at ${startTime}`, { startTime });
logger.info('Updating System-info.json file', { ...systemInfo.data });
// We need DAEDALUS_INSTALL_DIRECTORY in PATH
// in order for the cardano-launcher to find wallet and node bins
process.env.PATH = [
process.env.PATH,
process.env.DAEDALUS_INSTALL_DIRECTORY,
].join(path.delimiter);
ensureXDGDataIsSet();
await installChromeExtensions(isDev);
// Detect locale
let locale = getLocale(network);
mainWindow = createMainWindow(locale);
const onCheckDiskSpace = ({
isNotEnoughDiskSpace,
}: CheckDiskSpaceResponse) => {
if (cardanoNode) {
if (isNotEnoughDiskSpace) {
if (
cardanoNode.state !== CardanoNodeStates.STOPPING &&
cardanoNode.state !== CardanoNodeStates.STOPPED
) {
try {
cardanoNode.stop();
} catch (e) {} // eslint-disable-line
}
} else if (
cardanoNode.state !== CardanoNodeStates.STARTING &&
cardanoNode.state !== CardanoNodeStates.RUNNING
) {
cardanoNode.restart();
}
}
};
const handleCheckDiskSpace = handleDiskSpace(mainWindow, onCheckDiskSpace);
const onMainError = (error: string) => {
if (error.indexOf('ENOSPC') > -1) {
handleCheckDiskSpace();
return false;
}
};
mainErrorHandler(onMainError);
await handleCheckDiskSpace();
await handleCheckBlockReplayProgress(mainWindow, launcherConfig.logsPrefix);
cardanoNode = setupCardanoNode(launcherConfig, mainWindow);
if (isWatchMode) {
// Connect to electron-connect server which restarts / reloads windows on file changes
client.create(mainWindow);
}
setStateSnapshotLogChannel.onReceive((data) => {
return Promise.resolve(logStateSnapshot(data));
});
generateWalletMigrationReportChannel.onReceive((data) => {
return Promise.resolve(generateWalletMigrationReport(data));
});
getStateDirectoryPathChannel.onRequest(() =>
Promise.resolve(stateDirectoryPath)
);
getDesktopDirectoryPathChannel.onRequest(() =>
Promise.resolve(app.getPath('desktop'))
);
getSystemLocaleChannel.onRequest(() => Promise.resolve(systemLocale));
mainWindow.on('close', async (event) => {
logger.info(
'mainWindow received <close> event. Safe exiting Daedalus now.'
);
event.preventDefault();
await safeExit();
});
buildAppMenus(mainWindow, cardanoNode, locale, {
isUpdateAvailable: false,
isNavigationEnabled: false,
});
await enableApplicationMenuNavigationChannel.onReceive(
() =>
new Promise((resolve) => {
buildAppMenus(mainWindow, cardanoNode, locale, {
isUpdateAvailable: false,
isNavigationEnabled: true,
});
resolve();
})
);
await rebuildApplicationMenu.onReceive(
(data) =>
new Promise((resolve) => {
locale = getLocale(network);
buildAppMenus(mainWindow, cardanoNode, locale, {
isUpdateAvailable: data.isUpdateAvailable,
isNavigationEnabled: data.isNavigationEnabled,
});
mainWindow.updateTitle(locale);
resolve();
})
);
// Security feature: Prevent creation of new browser windows
// https://github.com/electron/electron/blob/master/docs/tutorial/security.md#14-disable-or-limit-creation-of-new-windows
app.on('web-contents-created', (_, contents) => {
contents.on('new-window', (event, url) => {
// Prevent creation of new BrowserWindows via links / window.open
event.preventDefault();
logger.info('Prevented creation of new browser window', { url });
// Open these links with the default browser
shell.openExternal(url);
});
});
// Wait for controlled cardano-node shutdown before quitting the app
app.on('before-quit', async (event) => {
logger.info('app received <before-quit> event. Safe exiting Daedalus now.');
event.preventDefault(); // prevent Daedalus from quitting immediately
await safeExit();
});
};
// Make sure this is the only Daedalus instance running per cluster before doing anything else
const isSingleInstance = app.requestSingleInstanceLock();
if (!isSingleInstance) {
app.quit();
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
app.on('ready', onAppReady);
}