Merge pull request #66 from shocknet/minor-fixes

Minor fixes
This commit is contained in:
Emad-salah 2020-05-22 18:48:46 +00:00 committed by GitHub
commit 8a0381fdec
6 changed files with 635 additions and 102 deletions

View file

@ -81,7 +81,9 @@
"no-plusplus": "off",
"no-undefined": "off"
"no-undefined": "off",
"no-process-env": "off"
},
"parser": "babel-eslint",
"env": {

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

@ -10,15 +10,9 @@
"test": "jest --no-cache",
"test:watch": "jest --no-cache --watch",
"typecheck": "tsc",
"eslint": "eslint services/**/*.js src/**/*.js utils/**/*.js config/**/*.js constants/**/*.js",
"lint": "eslint \"services/gunDB/**/*.js\"",
"format": "prettier --write \"./**/*.js\""
},
"husky": {
"hooks": {
"precommit": "eslint"
}
},
"author": "",
"license": "ISC",
"dependencies": {
@ -40,7 +34,7 @@
"graphviz": "0.0.8",
"grpc": "^1.21.1",
"gun": "git://github.com/amark/gun#c59e0e95f92779ce6bb3aab823d318bc16b20c33",
"husky": "^3.0.9",
"husky": "^4.2.5",
"jsonfile": "^4.0.0",
"jsonwebtoken": "^8.3.0",
"localtunnel": "^1.9.0",
@ -60,6 +54,7 @@
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@types/bluebird": "*",
"@types/dotenv": "^6.1.1",
"@types/express": "^4.17.1",
"@types/gun": "^0.9.1",
@ -76,8 +71,21 @@
"eslint-plugin-jest": "^22.20.1",
"eslint-plugin-prettier": "^3.1.1",
"jest": "^24.9.0",
"lint-staged": "^10.2.2",
"nodemon": "^1.19.3",
"prettier": "^1.18.2",
"ts-type": "^1.2.16",
"typescript": "^3.6.3"
},
"lint-staged": {
"*.{js,ts}": [
"prettier --check",
"eslint"
]
},
"husky": {
"hooks": {
"pre-commit": "yarn lint && yarn typecheck && yarn lint-staged"
}
}
}

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

596
yarn.lock

File diff suppressed because it is too large Load diff