This repository was archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathgrpc-client.js
148 lines (133 loc) · 4.52 KB
/
grpc-client.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
const fs = require('fs');
const path = require('path');
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
process.env.GRPC_SSL_CIPHER_SUITES =
'ECDHE-RSA-AES128-GCM-SHA256:' +
'ECDHE-RSA-AES128-SHA256:' +
'ECDHE-RSA-AES256-SHA384:' +
'ECDHE-RSA-AES256-GCM-SHA384:' +
'ECDHE-ECDSA-AES128-GCM-SHA256:' +
'ECDHE-ECDSA-AES128-SHA256:' +
'ECDHE-ECDSA-AES256-SHA384:' +
'ECDHE-ECDSA-AES256-GCM-SHA384';
async function waitForCertPath(certPath) {
let intervalId;
return new Promise(resolve => {
intervalId = setInterval(() => {
if (!fs.existsSync(certPath)) return;
clearInterval(intervalId);
resolve();
}, 500);
});
}
async function getCredentials(lndSettingsDir) {
let certPath = path.join(lndSettingsDir, 'tls.cert');
await waitForCertPath(certPath);
const lndCert = fs.readFileSync(certPath);
return grpc.credentials.createSsl(lndCert);
}
function getMacaroonCreds(lndSettingsDir, network) {
return grpc.credentials.createFromMetadataGenerator((args, callback) => {
const metadata = new grpc.Metadata();
const macaroonPath = path.join(
lndSettingsDir,
`data/chain/bitcoin/${network}/admin.macaroon`
);
const macaroonHex = fs.readFileSync(macaroonPath).toString('hex');
metadata.add('macaroon', macaroonHex);
callback(null, metadata);
});
}
module.exports.init = async function({
ipcMain,
lndPort,
lndSettingsDir,
network,
}) {
let credentials;
let protoPath;
let lnrpc;
let unlocker;
let lnd;
let autopilot;
const protoOptions = {
keepCase: false,
longs: Number,
enums: String,
defaults: true,
oneofs: true,
};
ipcMain.on('unlockInit', async event => {
credentials = await getCredentials(lndSettingsDir);
protoPath = path.join(__dirname, '..', 'assets', 'rpc.proto');
const packageDef = protoLoader.loadSync(protoPath, protoOptions);
lnrpc = grpc.loadPackageDefinition(packageDef).lnrpc;
unlocker = new lnrpc.WalletUnlocker(`localhost:${lndPort}`, credentials);
grpc.waitForClientReady(unlocker, Infinity, err => {
event.sender.send('unlockReady', { err });
});
});
ipcMain.on('unlockClose', event => {
unlocker.close();
event.sender.send('unlockClosed', {});
});
ipcMain.on('lndInit', event => {
const macaroonCreds = getMacaroonCreds(lndSettingsDir, network);
credentials = grpc.credentials.combineChannelCredentials(
credentials,
macaroonCreds
);
lnd = new lnrpc.Lightning(`localhost:${lndPort}`, credentials);
grpc.waitForClientReady(lnd, Infinity, err => {
event.sender.send('lndReady', { err });
});
});
ipcMain.on('lndAtplInit', async event => {
const atplProto = path.join(__dirname, '..', 'assets', 'autopilot.proto');
const packageDef = protoLoader.loadSync(atplProto, protoOptions);
let autopilotRpc = grpc.loadPackageDefinition(packageDef).autopilotrpc;
autopilot = new autopilotRpc.Autopilot(`localhost:${lndPort}`, credentials);
grpc.waitForClientReady(autopilot, Infinity, err => {
event.sender.send('lndAtplReady', { err });
});
});
ipcMain.on('lndClose', event => {
lnd.close();
event.sender.send('lndClosed', {});
});
ipcMain.on('unlockRequest', (event, { method, body = {} }) => {
const handleResponse = (err, response) => {
event.sender.send(`unlockResponse_${method}`, { err, response });
};
unlocker[method](body, handleResponse);
});
ipcMain.on('lndRequest', (event, { method, body = {} }) => {
const handleResponse = (err, response) => {
event.sender.send(`lndResponse_${method}`, { err, response });
};
lnd[method](body, handleResponse);
});
ipcMain.on('lndAtplRequest', (event, { method, body = {} }) => {
const handleResponse = (err, response) => {
event.sender.send(`lndAtplResponse_${method}`, { err, response });
};
autopilot[method](body, handleResponse);
});
const streams = {};
ipcMain.on('lndStreamRequest', (event, { method, body = {} }) => {
let stream;
stream = lnd[method](body);
const send = res => event.sender.send(`lndStreamEvent_${method}`, res);
stream.on('data', data => send({ event: 'data', data }));
stream.on('end', () => send({ event: 'end' }));
stream.on('error', err => send({ event: 'error', err }));
stream.on('status', data => send({ event: 'status', data }));
streams[method] = stream;
});
ipcMain.on('lndStreamWrite', (event, { method, data }) => {
const stream = streams[method];
if (!stream) return;
stream.write(data);
});
};