From 06e76cec453099ff70e98ac491f57de188d663a6 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 17 May 2020 17:25:02 -0400 Subject: [PATCH] correct typings --- constants/errors.js | 10 ++++ services/lnd/lightning.js | 11 ++++- utils/lightningServices.js | 94 +++++++++++++++++++++++++++++++++----- 3 files changed, 103 insertions(+), 12 deletions(-) diff --git a/constants/errors.js b/constants/errors.js index 8491c293..8f54d2dc 100644 --- a/constants/errors.js +++ b/constants/errors.js @@ -1,4 +1,7 @@ module.exports = { + /** + * @param {string} path + */ MACAROON_PATH: path => ` The specified macaroon path "${path}" was not found. This issue can be caused by: @@ -6,6 +9,9 @@ module.exports = { 1. Setting an invalid path for your Macaroon file. 2. Not initializing your wallet before using the ShockAPI `, + /** + * @param {string} path + */ CERT_PATH: path => ` The specified LND certificate file "${path}" was not found. This issue can be caused by: @@ -15,6 +21,10 @@ module.exports = { `, CERT_MISSING: () => "Required LND certificate path missing from application configuration.", + /** + * @param {string|null} macaroonPath + * @param {string} lndCertPath + */ CERT_AND_MACAROON_MISSING: (macaroonPath, lndCertPath) => ` You neither specified an LND cert path nor a Macaroon path. Please make sure both files exist in the paths you've specified: diff --git a/services/lnd/lightning.js b/services/lnd/lightning.js index 7494c0eb..ccacfbd6 100644 --- a/services/lnd/lightning.js +++ b/services/lnd/lightning.js @@ -5,6 +5,13 @@ const logger = require("winston"); const errorConstants = require("../../constants/errors"); // expose the routes to our app with module.exports +/** + * @param {string} protoPath + * @param {string} lndHost + * @param {string} lndCertPath + * @param {string|null} macaroonPath + * @returns {Promise} + */ module.exports = async (protoPath, lndHost, lndCertPath, macaroonPath) => { try { process.env.GRPC_SSL_CIPHER_SUITES = "HIGH+ECDSA"; @@ -28,7 +35,7 @@ module.exports = async (protoPath, lndHost, lndCertPath, macaroonPath) => { if (macaroonExists) { const macaroonCreds = grpc.credentials.createFromMetadataGenerator( - async (args, callback) => { + async (_, callback) => { const adminMacaroon = await fs.readFile(macaroonPath); const metadata = new grpc.Metadata(); metadata.add("macaroon", adminMacaroon.toString("hex")); @@ -55,7 +62,9 @@ module.exports = async (protoPath, lndHost, lndCertPath, macaroonPath) => { if (certExists) { const credentials = await getCredentials(); + // @ts-ignore const lightning = new lnrpc.Lightning(lndHost, credentials); + // @ts-ignore const walletUnlocker = new lnrpc.WalletUnlocker(lndHost, credentials); return { diff --git a/utils/lightningServices.js b/utils/lightningServices.js index 91a510ae..cd78f7a2 100644 --- a/utils/lightningServices.js +++ b/utils/lightningServices.js @@ -1,18 +1,61 @@ const FS = require("../utils/fs"); const lnrpc = require("../services/lnd/lightning"); -class LightningServices { - setDefaults = program => { - const defaults = require("../config/defaults")(program.mainnet); +/** + * @typedef {import('commander').Command} Command + */ - this.defaults = defaults; - this.config = { +/** + * @typedef {object} Config + * @prop {boolean} useTLS + * @prop {number} serverPort + * @prop {string} serverHost + * @prop {string} lndHost + * @prop {string} lndCertPath + * @prop {string} macaroonPath + * @prop {string} lndProto + */ + +class LightningServices { + /** + * @type {Config|null} + */ + _config = null + + /** + * @type {Config|null} + */ + _defaults = null + + /** + * @param {Config} newDefaults + */ + set defaults(newDefaults) { + this._defaults = newDefaults + } + + /** + * @param {Command} program + */ + setDefaults = program => { + /** + * @type {Config} + */ + const newDefaults = { + ...require("../config/defaults")(program.mainnet), + useTLS: false, + } + + this.defaults = newDefaults; + + this._config = { useTLS: program.usetls, - serverPort: program.serverport || defaults.serverPort, - serverHost: program.serverhost || defaults.serverHost, - lndHost: program.lndhost || defaults.lndHost, - lndCertPath: program.lndCertPath || defaults.lndCertPath, - macaroonPath: program.macaroonPath || defaults.macaroonPath + serverPort: program.serverport || newDefaults.serverPort, + serverHost: program.serverhost || newDefaults.serverHost, + lndHost: program.lndhost || newDefaults.lndHost, + lndCertPath: program.lndCertPath || newDefaults.lndCertPath, + macaroonPath: program.macaroonPath || newDefaults.macaroonPath, + lndProto: newDefaults.lndProto }; } @@ -31,8 +74,34 @@ class LightningServices { return this.lnServicesData; } + /** + * @returns {Config} + */ get servicesConfig() { - return this.config; + if (this._config) { + return this._config + } + + throw new Error('Tried to access LightningServices.servicesConfig without setting defaults first.') + } + + get config() { + if (this._config) { + return this._config + } + + throw new Error('Tried to access LightningServices.config without setting defaults first.') + } + + /** + * @returns {Config} + */ + get defaults() { + if (this._defaults) { + return this._defaults + } + + throw new Error('Tried to access LightningServices.defaults without setting them first.') } init = async () => { @@ -44,6 +113,9 @@ class LightningServices { lndCertPath, macaroonExists ? macaroonPath : null ); + if (!lnServices) { + throw new Error(`Could not init lnServices`) + } const { lightning, walletUnlocker } = lnServices; this.lightning = lightning; this.walletUnlocker = walletUnlocker