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-plusplus": "off",
"no-undefined": "off" "no-undefined": "off",
"no-process-env": "off"
}, },
"parser": "babel-eslint", "parser": "babel-eslint",
"env": { "env": {

View file

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

View file

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

View file

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

596
yarn.lock

File diff suppressed because it is too large Load diff