|
| 1 | +"use strict"; |
| 2 | +const crypto = require("crypto"); |
| 3 | +const Plugin = require("ilp-plugin-lightning"); |
| 4 | +const { convert, Unit } = require("ilp-plugin-lightning/build/account"); |
| 5 | +const connectorList = require("./connector_list.json"); |
| 6 | +const util = require("util"); |
| 7 | +const parentBtpHmacKey = "parent_btp_uri"; |
| 8 | +const inquirer = require("inquirer"); |
| 9 | +const base64url = buf => |
| 10 | + buf |
| 11 | + .toString("base64") |
| 12 | + .replace(/=/g, "") |
| 13 | + .replace(/\+/g, "-") |
| 14 | + .replace(/\//g, "_"); |
| 15 | + |
| 16 | +async function configure({ testnet, advanced }) { |
| 17 | + const servers = connectorList[testnet ? "test" : "live"]; |
| 18 | + const defaultParent = servers[Math.floor(Math.random() * servers.length)]; |
| 19 | + const res = {}; |
| 20 | + const fields = [ |
| 21 | + { |
| 22 | + type: "input", |
| 23 | + name: "pubkey", |
| 24 | + message: "LND Pubkey:" |
| 25 | + }, |
| 26 | + { |
| 27 | + type: "input", |
| 28 | + name: "host", |
| 29 | + message: "LND Host IP:" |
| 30 | + }, |
| 31 | + { |
| 32 | + type: "input", |
| 33 | + name: "tlscert", |
| 34 | + message: "LND TLS Cert (Path to file or base64 stiring):" |
| 35 | + }, |
| 36 | + { |
| 37 | + type: "input", |
| 38 | + name: "macaroon", |
| 39 | + message: "LND Admin Macaroon (Path to file or base64 stiring):" |
| 40 | + }, |
| 41 | + { |
| 42 | + type: "input", |
| 43 | + name: "parent", |
| 44 | + message: "BTP host of parent connector:", |
| 45 | + default: defaultParent |
| 46 | + }, |
| 47 | + { |
| 48 | + type: "input", |
| 49 | + name: "name", |
| 50 | + message: "Name to assign to this channel:", |
| 51 | + default: base64url(crypto.randomBytes(32)) |
| 52 | + } |
| 53 | + ]; |
| 54 | + for (const field of fields) { |
| 55 | + res[field.name] = (await inquirer.prompt(field))[field.name]; |
| 56 | + } |
| 57 | + |
| 58 | + // create btp server uri for upstream |
| 59 | + const btpName = res.name || ""; |
| 60 | + const btpSecret = hmac( |
| 61 | + hmac(parentBtpHmacKey, res.parent + btpName), |
| 62 | + res.pubkey |
| 63 | + ).toString("hex"); |
| 64 | + const btpServer = "btp+wss://" + btpName + ":" + btpSecret + "@" + res.parent; |
| 65 | + |
| 66 | + return { |
| 67 | + relation: "parent", |
| 68 | + plugin: require.resolve("ilp-plugin-lightning"), |
| 69 | + assetCode: "BTC", |
| 70 | + assetScale: 8, |
| 71 | + sendRoutes: false, |
| 72 | + receiveRoutes: false, |
| 73 | + options: { |
| 74 | + role: "client", |
| 75 | + server: btpServer, |
| 76 | + lndIdentityPubkey: res.pubkey, |
| 77 | + lndHost: res.host, |
| 78 | + lnd: { |
| 79 | + tlsCertPath: res.tlscert, |
| 80 | + macaroonPath: res.macaroon, |
| 81 | + lndHost: res.host |
| 82 | + }, |
| 83 | + balance: { |
| 84 | + maximum: convert(".0005", Unit.BTC, Unit.Satoshi), |
| 85 | + settleTo: convert(".0001", Unit.BTC, Unit.Satoshi), |
| 86 | + settleThreshold: convert("0.00009", Unit.BTC, Unit.Satoshi) |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +const commands = [ |
| 93 | + { |
| 94 | + command: "info", |
| 95 | + describe: "Get info about your lnd node", |
| 96 | + builder: {}, |
| 97 | + handler: (config, argv) => makeUplink(config)._printInfo() |
| 98 | + } |
| 99 | + // { |
| 100 | + // command: 'channels', |
| 101 | + // describe: 'Get info about any channels you have with your connector', |
| 102 | + // builder: {}, |
| 103 | + // handler: (con) |
| 104 | + // } |
| 105 | + // { |
| 106 | + // command: 'getRoutes', |
| 107 | + // describe: 'Get routing information between you and the connector', |
| 108 | + // builder: { |
| 109 | + // amount: { |
| 110 | + // description: 'The amount (in satoshis) each route must contain.', |
| 111 | + // demandOption: true |
| 112 | + // } |
| 113 | + // }, |
| 114 | + // handler: (config, {amount}) => makeUplink(config).getRoutes(amount) |
| 115 | + // } |
| 116 | +]; |
| 117 | + |
| 118 | +function makeUplink(config) { |
| 119 | + return new LightningUplink(config); |
| 120 | +} |
| 121 | + |
| 122 | +class LightningUplink { |
| 123 | + constructor(config) { |
| 124 | + this.config = config; |
| 125 | + this.pluginOpts = config.options; |
| 126 | + this.plugin = null; |
| 127 | + } |
| 128 | + |
| 129 | + async _printInfo() { |
| 130 | + const api = await this.api(); |
| 131 | + const info = await api.getInfo(); |
| 132 | + console.log(util.inspect(object, { colors: true })); |
| 133 | + } |
| 134 | + |
| 135 | + async _api() { |
| 136 | + if (!this.plugin) { |
| 137 | + this.plugin = new Plugin(this.pluginOpts); |
| 138 | + await this.plugin.connect(); |
| 139 | + } |
| 140 | + return this.plugin.lnd; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +function hmac(key, message) { |
| 145 | + const h = crypto.createHmac("sha256", key); |
| 146 | + h.update(message); |
| 147 | + return h.digest(); |
| 148 | +} |
| 149 | + |
| 150 | +module.exports = { |
| 151 | + configure, |
| 152 | + commands |
| 153 | +}; |
0 commit comments