-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecutor.js
129 lines (116 loc) · 3.24 KB
/
executor.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
const { BrowserWindow, Notification } = require('electron')
const lnService = require('ln-service');
const Store = require('electron-store');
const sendMessage = require('./sendMessage');
const commands = require('./commands');
module.exports = class Executor {
constructor(app) {
this.app = app;
this.initStore();
this.currentAccount = this.store.get('currentAccount', null);
this.initLnd();
this.sendMessage = sendMessage;
}
quit () {
console.log('Bye');
this.app.quit();
}
ln(command, args) {
console.log(`Calling LND: ${command}`);
// TODO: nice notifications for each lnd call
new Notification({title: 'Lightning', body: `Executing ${command} - ${JSON.stringify(args)}`}).show();
return lnService[command]({
lnd: this.lnd,
...args
});
}
get accounts () {
if (!this._accounts) {
this._accounts = this.store.get('accounts', {});
}
return this._accounts;
}
initStore () {
this.store = new Store();
this.store.onDidChange('currentAccount', (newValue, oldValue) => {
this.currentAccount = newValue;
this.initLnd();
});
this.store.onDidChange('accounts', (newValue, oldValue) => {
this._accounts = newValue;
});
}
initLnd () {
let account = this.accounts[this.currentAccount];
if (account) {
this.lnd = lnService.authenticatedLndGrpc({
cert: account.cert,
macaroon: account.macaroon,
socket: account.socket
}).lnd;
} else {
this.lnd = null;
}
}
processMessage () {
if (!this.currentBrowserMessage) {
return;
}
if (Object.keys(this.accounts).length === 0) {
console.log(`No accounts found. Starting Setup`);
return this.show('setup');
}
const cmd = commands[this.currentBrowserMessage.command];
if (cmd) {
return cmd.apply(this, [this.currentBrowserMessage]);
} else {
return this.show(this.currentBrowserMessage.command, this.currentBrowserMessage);
}
}
handleBrowserMessage(message) {
this.currentBrowserMessage = message;
this.processMessage();
}
show(route, message) {
if (this.window) {
this.window.loadURL(`http://localhost:3000/#/${route}`);
if (message) {
this.window.webContents.send('main', message);
}
} else {
this.launchWindow(route, message);
}
}
launchWindow(route, message) {
console.log(`Launching ${route}`);
return this.app.whenReady().then(() => {
this.window = new BrowserWindow({
width: 800,
height: 600,
modal: true,
minimizable: false,
maximizable: false,
title: 'Joule',
backgroundColor: "#D6D8DC",
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
devTools: true
}
});
this.window.webContents.openDevTools();
this.window.removeMenu();
this.window.loadURL(`http://localhost:3000/#/${route}`);
this.window.once('ready-to-show', () => {
this.window.show()
});
if (message) {
this.window.webContents.on('did-finish-load', () => {
this.window.webContents.send('main', message);
});
}
return this.window;
});
}
}