correct typings

This commit is contained in:
Daniel Lugo 2020-05-17 17:25:02 -04:00
parent c493eb47d5
commit 06e76cec45
3 changed files with 103 additions and 12 deletions

View file

@ -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:

View file

@ -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<any>}
*/
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 {

View file

@ -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