This commit is contained in:
Daniel Lugo 2020-08-20 15:41:45 -04:00
parent 9f180859ce
commit bd01b3cbce

View file

@ -1,5 +1,8 @@
const FS = require("../utils/fs"); /**
const lnrpc = require("../services/lnd/lightning"); * @format
*/
const FS = require('../utils/fs')
const lnrpc = require('../services/lnd/lightning')
/** /**
* @typedef {import('commander').Command} Command * @typedef {import('commander').Command} Command
@ -43,9 +46,9 @@ class LightningServices {
/** /**
* @type {Config} * @type {Config}
*/ */
const newDefaults = require("../config/defaults")(program.mainnet) const newDefaults = require('../config/defaults')(program.mainnet)
this.defaults = newDefaults; this.defaults = newDefaults
this._config = { this._config = {
...newDefaults, ...newDefaults,
@ -55,11 +58,11 @@ class LightningServices {
lndHost: program.lndhost || newDefaults.lndHost, lndHost: program.lndhost || newDefaults.lndHost,
lndCertPath: program.lndCertPath || newDefaults.lndCertPath, lndCertPath: program.lndCertPath || newDefaults.lndCertPath,
macaroonPath: program.macaroonPath || newDefaults.macaroonPath macaroonPath: program.macaroonPath || newDefaults.macaroonPath
}; }
} }
isInitialized = () => { isInitialized = () => {
return !!(this.lightning && this.walletUnlocker); return !!(this.lightning && this.walletUnlocker)
} }
get services() { get services() {
@ -67,11 +70,11 @@ class LightningServices {
lightning: this.lightning, lightning: this.lightning,
walletUnlocker: this.walletUnlocker, walletUnlocker: this.walletUnlocker,
router: this.router router: this.router
}; }
} }
get servicesData() { get servicesData() {
return this.lnServicesData; return this.lnServicesData
} }
/** /**
@ -82,7 +85,9 @@ class LightningServices {
return this._config return this._config
} }
throw new Error('Tried to access LightningServices.servicesConfig without setting defaults first.') throw new Error(
'Tried to access LightningServices.servicesConfig without setting defaults first.'
)
} }
get config() { get config() {
@ -90,7 +95,9 @@ class LightningServices {
return this._config return this._config
} }
throw new Error('Tried to access LightningServices.config without setting defaults first.') throw new Error(
'Tried to access LightningServices.config without setting defaults first.'
)
} }
/** /**
@ -101,38 +108,38 @@ class LightningServices {
return this._defaults return this._defaults
} }
throw new Error('Tried to access LightningServices.defaults without setting them first.') throw new Error(
'Tried to access LightningServices.defaults without setting them first.'
)
} }
init = async () => { init = async () => {
const { macaroonPath, lndHost, lndCertPath } = this.config; const { macaroonPath, lndHost, lndCertPath } = this.config
const macaroonExists = await FS.access(macaroonPath); const macaroonExists = await FS.access(macaroonPath)
const lnServices = await lnrpc( const lnServices = await lnrpc({
{ lnrpcProtoPath: this.defaults.lndProto,
lnrpcProtoPath: this.defaults.lndProto, routerProtoPath: this.defaults.routerProto,
routerProtoPath: this.defaults.routerProto, walletUnlockerProtoPath: this.defaults.walletUnlockerProto,
walletUnlockerProtoPath: this.defaults.walletUnlockerProto, lndHost,
lndHost, lndCertPath,
lndCertPath, macaroonPath: macaroonExists ? macaroonPath : null
macaroonPath: macaroonExists ? macaroonPath : null })
}
);
if (!lnServices) { if (!lnServices) {
throw new Error(`Could not init lnServices`) throw new Error(`Could not init lnServices`)
} }
const { lightning, walletUnlocker, router } = lnServices; const { lightning, walletUnlocker, router } = lnServices
this.lightning = lightning; this.lightning = lightning
this.walletUnlocker = walletUnlocker this.walletUnlocker = walletUnlocker
this.router = router; this.router = router
this.lnServicesData = { this.lnServicesData = {
lndProto: this.defaults.lndProto, lndProto: this.defaults.lndProto,
lndHost, lndHost,
lndCertPath, lndCertPath,
macaroonPath: macaroonExists ? macaroonPath : null macaroonPath: macaroonExists ? macaroonPath : null
}; }
} }
} }
const lightningServices = new LightningServices(); const lightningServices = new LightningServices()
module.exports = lightningServices; module.exports = lightningServices