-
Notifications
You must be signed in to change notification settings - Fork 200
/
lightning.js
57 lines (54 loc) · 1.88 KB
/
lightning.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
// setup lnd rpc
const config = require('./config');
var fs = require('fs');
var grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync('rpc.proto', loaderOptions);
var lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
var lndCert;
if (process.env.TLSCERT) {
lndCert = Buffer.from(process.env.TLSCERT, 'hex');
} else {
lndCert = fs.readFileSync('tls.cert');
}
process.env.VERBOSE && console.log('using tls.cert', lndCert.toString('hex'));
let sslCreds = grpc.credentials.createSsl(lndCert);
let macaroon;
if (process.env.MACAROON) {
macaroon = process.env.MACAROON;
} else {
macaroon = fs.readFileSync('admin.macaroon').toString('hex');
}
process.env.VERBOSE && console.log('using macaroon', macaroon);
let macaroonCreds = grpc.credentials.createFromMetadataGenerator(function (args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
// trying to unlock the wallet:
if (config.lnd.password) {
process.env.VERBOSE && console.log('trying to unlock the wallet');
var walletUnlocker = new lnrpc.WalletUnlocker(config.lnd.url, creds);
walletUnlocker.unlockWallet(
{
wallet_password: Buffer.from(config.lnd.password).toString('base64'),
},
function (err, response) {
if (err) {
process.env.VERBOSE && console.log('unlockWallet failed, probably because its been aleady unlocked');
} else {
console.log('unlockWallet:', response);
}
},
);
}
module.exports = new lnrpc.Lightning(config.lnd.url, creds, { 'grpc.max_receive_message_length': 1024 * 1024 * 1024 });