correct typings
This commit is contained in:
parent
c493eb47d5
commit
06e76cec45
3 changed files with 103 additions and 12 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* @param {string} path
|
||||||
|
*/
|
||||||
MACAROON_PATH: path => `
|
MACAROON_PATH: path => `
|
||||||
The specified macaroon path "${path}" was not found.
|
The specified macaroon path "${path}" was not found.
|
||||||
This issue can be caused by:
|
This issue can be caused by:
|
||||||
|
|
@ -6,6 +9,9 @@ module.exports = {
|
||||||
1. Setting an invalid path for your Macaroon file.
|
1. Setting an invalid path for your Macaroon file.
|
||||||
2. Not initializing your wallet before using the ShockAPI
|
2. Not initializing your wallet before using the ShockAPI
|
||||||
`,
|
`,
|
||||||
|
/**
|
||||||
|
* @param {string} path
|
||||||
|
*/
|
||||||
CERT_PATH: path => `
|
CERT_PATH: path => `
|
||||||
The specified LND certificate file "${path}" was not found.
|
The specified LND certificate file "${path}" was not found.
|
||||||
This issue can be caused by:
|
This issue can be caused by:
|
||||||
|
|
@ -15,6 +21,10 @@ module.exports = {
|
||||||
`,
|
`,
|
||||||
CERT_MISSING: () =>
|
CERT_MISSING: () =>
|
||||||
"Required LND certificate path missing from application configuration.",
|
"Required LND certificate path missing from application configuration.",
|
||||||
|
/**
|
||||||
|
* @param {string|null} macaroonPath
|
||||||
|
* @param {string} lndCertPath
|
||||||
|
*/
|
||||||
CERT_AND_MACAROON_MISSING: (macaroonPath, 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:
|
You neither specified an LND cert path nor a Macaroon path. Please make sure both files exist in the paths you've specified:
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,13 @@ const logger = require("winston");
|
||||||
const errorConstants = require("../../constants/errors");
|
const errorConstants = require("../../constants/errors");
|
||||||
|
|
||||||
// expose the routes to our app with module.exports
|
// 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) => {
|
module.exports = async (protoPath, lndHost, lndCertPath, macaroonPath) => {
|
||||||
try {
|
try {
|
||||||
process.env.GRPC_SSL_CIPHER_SUITES = "HIGH+ECDSA";
|
process.env.GRPC_SSL_CIPHER_SUITES = "HIGH+ECDSA";
|
||||||
|
|
@ -28,7 +35,7 @@ module.exports = async (protoPath, lndHost, lndCertPath, macaroonPath) => {
|
||||||
|
|
||||||
if (macaroonExists) {
|
if (macaroonExists) {
|
||||||
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(
|
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(
|
||||||
async (args, callback) => {
|
async (_, callback) => {
|
||||||
const adminMacaroon = await fs.readFile(macaroonPath);
|
const adminMacaroon = await fs.readFile(macaroonPath);
|
||||||
const metadata = new grpc.Metadata();
|
const metadata = new grpc.Metadata();
|
||||||
metadata.add("macaroon", adminMacaroon.toString("hex"));
|
metadata.add("macaroon", adminMacaroon.toString("hex"));
|
||||||
|
|
@ -55,7 +62,9 @@ module.exports = async (protoPath, lndHost, lndCertPath, macaroonPath) => {
|
||||||
if (certExists) {
|
if (certExists) {
|
||||||
const credentials = await getCredentials();
|
const credentials = await getCredentials();
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
const lightning = new lnrpc.Lightning(lndHost, credentials);
|
const lightning = new lnrpc.Lightning(lndHost, credentials);
|
||||||
|
// @ts-ignore
|
||||||
const walletUnlocker = new lnrpc.WalletUnlocker(lndHost, credentials);
|
const walletUnlocker = new lnrpc.WalletUnlocker(lndHost, credentials);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,61 @@
|
||||||
const FS = require("../utils/fs");
|
const FS = require("../utils/fs");
|
||||||
const lnrpc = require("../services/lnd/lightning");
|
const lnrpc = require("../services/lnd/lightning");
|
||||||
|
|
||||||
class LightningServices {
|
/**
|
||||||
setDefaults = program => {
|
* @typedef {import('commander').Command} Command
|
||||||
const defaults = require("../config/defaults")(program.mainnet);
|
*/
|
||||||
|
|
||||||
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,
|
useTLS: program.usetls,
|
||||||
serverPort: program.serverport || defaults.serverPort,
|
serverPort: program.serverport || newDefaults.serverPort,
|
||||||
serverHost: program.serverhost || defaults.serverHost,
|
serverHost: program.serverhost || newDefaults.serverHost,
|
||||||
lndHost: program.lndhost || defaults.lndHost,
|
lndHost: program.lndhost || newDefaults.lndHost,
|
||||||
lndCertPath: program.lndCertPath || defaults.lndCertPath,
|
lndCertPath: program.lndCertPath || newDefaults.lndCertPath,
|
||||||
macaroonPath: program.macaroonPath || defaults.macaroonPath
|
macaroonPath: program.macaroonPath || newDefaults.macaroonPath,
|
||||||
|
lndProto: newDefaults.lndProto
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,8 +74,34 @@ class LightningServices {
|
||||||
return this.lnServicesData;
|
return this.lnServicesData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Config}
|
||||||
|
*/
|
||||||
get servicesConfig() {
|
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 () => {
|
init = async () => {
|
||||||
|
|
@ -44,6 +113,9 @@ class LightningServices {
|
||||||
lndCertPath,
|
lndCertPath,
|
||||||
macaroonExists ? macaroonPath : null
|
macaroonExists ? macaroonPath : null
|
||||||
);
|
);
|
||||||
|
if (!lnServices) {
|
||||||
|
throw new Error(`Could not init lnServices`)
|
||||||
|
}
|
||||||
const { lightning, walletUnlocker } = lnServices;
|
const { lightning, walletUnlocker } = lnServices;
|
||||||
this.lightning = lightning;
|
this.lightning = lightning;
|
||||||
this.walletUnlocker = walletUnlocker
|
this.walletUnlocker = walletUnlocker
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue