-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreload.js
87 lines (76 loc) · 2.89 KB
/
preload.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
/*
* Copyright (C) 2017-2018 The Particl developers
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// This file is loaded whenever a javascript context is created. It runs in a
// private scope that can access a subset of electron renderer APIs. We must be
// careful to not leak any objects into the global scope!
const { ipcRenderer, remote } = require('electron');
const flatten = (obj) => Object.keys(obj)
.reduce((acc, key) => {
const val = obj[key];
return acc.concat(typeof val === 'object' ? flatten(val) : val);
}, []);
/**
* SafeIpcRenderer
*
* This class wraps electron's ipcRenderer an prevents
* invocations to channels passed to the constructor. The instance methods
* are all created in the constructor to ensure that the protect method
* and validEvents array cannot be overridden.
*
*/
class SafeIpcRenderer {
constructor (events) {
const validEvents = flatten(events);
const protect = (fn) => {
return (channel, ...args) => {
let validChannel = channel;
if (channel.indexOf(':') !== -1) {
if (Number.isInteger(+channel.substr(channel.indexOf(':') + 1))) {
validChannel = channel.substr(0, channel.indexOf(':'));
}
}
if (!validEvents.includes(validChannel)) {
throw new Error(`Blocked access to unknown channel
${channel} ${validChannel} from the renderer`);
}
return fn.apply(ipcRenderer, [channel].concat(args));
};
};
this.on = protect(ipcRenderer.on);
this.once = protect(ipcRenderer.once);
this.send = protect(ipcRenderer.send);
this.sendSync = protect(ipcRenderer.sendSync);
this.sendToHost = protect(ipcRenderer.sendToHost);
this.removeListener = protect(ipcRenderer.removeListener);
this.removeAllListeners = protect(ipcRenderer.removeAllListeners);
this.listenerCount = protect(ipcRenderer.listenerCount);
}
}
window.ipc = new SafeIpcRenderer([
'front-choosewallet',
'front-walletready',
'daemon',
'zmq',
'rpc-channel',
'hl-rpc-channel',
'rx-ipc-check-reply',
'rx-ipc-check-reply:rpc-channel',
'rx-ipc-check-listener'
]);
window.remote = remote;
window.electron = true;