-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (143 loc) · 3.72 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
"use strict";
const crypto = require("crypto");
const Plugin = require("ilp-plugin-lightning");
const { convert, Unit } = require("ilp-plugin-lightning/build/account");
const connectorList = require("./connector_list.json");
const util = require("util");
const parentBtpHmacKey = "parent_btp_uri";
const inquirer = require("inquirer");
const base64url = buf =>
buf
.toString("base64")
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
async function configure({ testnet, advanced }) {
const servers = connectorList[testnet ? "test" : "live"];
const defaultParent = servers[Math.floor(Math.random() * servers.length)];
const res = {};
const fields = [
{
type: "input",
name: "pubkey",
message: "LND Pubkey:"
},
{
type: "input",
name: "host",
message: "LND Host IP:"
},
{
type: "input",
name: "tlscert",
message: "LND TLS Cert (Path to file or base64 stiring):"
},
{
type: "input",
name: "macaroon",
message: "LND Admin Macaroon (Path to file or base64 stiring):"
},
{
type: "input",
name: "parent",
message: "BTP host of parent connector:",
default: defaultParent
},
{
type: "input",
name: "name",
message: "Name to assign to this channel:",
default: base64url(crypto.randomBytes(32))
}
];
for (const field of fields) {
res[field.name] = (await inquirer.prompt(field))[field.name];
}
// create btp server uri for upstream
const btpName = res.name || "";
const btpSecret = hmac(
hmac(parentBtpHmacKey, res.parent + btpName),
res.pubkey
).toString("hex");
const btpServer = "btp+wss://" + btpName + ":" + btpSecret + "@" + res.parent;
return {
relation: "parent",
plugin: require.resolve("ilp-plugin-lightning"),
assetCode: "BTC",
assetScale: 8,
sendRoutes: false,
receiveRoutes: false,
options: {
role: "client",
server: btpServer,
lndIdentityPubkey: res.pubkey,
lndHost: res.host,
lnd: {
tlsCertPath: res.tlscert,
macaroonPath: res.macaroon,
lndHost: res.host
},
balance: {
maximum: convert(".0005", Unit.BTC, Unit.Satoshi),
settleTo: convert(".0001", Unit.BTC, Unit.Satoshi),
settleThreshold: convert("0.00009", Unit.BTC, Unit.Satoshi)
}
}
};
}
const commands = [
{
command: "info",
describe: "Get info about your lnd node",
builder: {},
handler: (config, argv) => makeUplink(config)._printInfo()
}
// {
// command: 'channels',
// describe: 'Get info about any channels you have with your connector',
// builder: {},
// handler: (con)
// }
// {
// command: 'getRoutes',
// describe: 'Get routing information between you and the connector',
// builder: {
// amount: {
// description: 'The amount (in satoshis) each route must contain.',
// demandOption: true
// }
// },
// handler: (config, {amount}) => makeUplink(config).getRoutes(amount)
// }
];
function makeUplink(config) {
return new LightningUplink(config);
}
class LightningUplink {
constructor(config) {
this.config = config;
this.pluginOpts = config.options;
this.plugin = null;
}
async _printInfo() {
const api = await this.api();
const info = await api.getInfo();
console.log(util.inspect(object, { colors: true }));
}
async _api() {
if (!this.plugin) {
this.plugin = new Plugin(this.pluginOpts);
await this.plugin.connect();
}
return this.plugin.lnd;
}
}
function hmac(key, message) {
const h = crypto.createHmac("sha256", key);
h.update(message);
return h.digest();
}
module.exports = {
configure,
commands
};