Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactor dependencies #314

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .babelrc

This file was deleted.

4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"parser": "@babel/eslint-parser",
"plugins": [
"prettier"
],
Expand All @@ -17,5 +16,6 @@
"env":{
"es6": true
},
"globals": { "fetch": false }
"globals": { "fetch": false },
"sourceType": "module"
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WORKDIR /lndhub
COPY package.json package-lock.json ./

# Install dependencies
RUN npm i
RUN npm i --only=production

# Copy project files and folders to the current working directory
COPY . .
Expand Down
10 changes: 5 additions & 5 deletions bitcoin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// setup bitcoind rpc
const config = require('./config');
let jayson = require('jayson/promise');
let url = require('url');
import config from './config.js';
import jayson from 'jayson/promise/index.js';
import url from 'url';
if (config.bitcoind) {
let rpc = url.parse(config.bitcoind.rpc);
rpc.timeout = 15000;
module.exports = jayson.client.http(rpc);
export default jayson.client.http(rpc);
} else {
module.exports = {};
export default {};
}
6 changes: 3 additions & 3 deletions btc-decoder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const bitcoin = require('bitcoinjs-lib');
const classify = require('bitcoinjs-lib/src/classify');
import bitcoin from 'bitcoinjs-lib';
import classify from 'bitcoinjs-lib/src/classify.js';

const decodeFormat = (tx) => ({
txid: tx.getId(),
Expand Down Expand Up @@ -74,6 +74,6 @@ class TxDecoder {
}
}

module.exports.decodeRawHex = (rawTx, network = bitcoin.networks.bitcoin) => {
export function decodeRawHex(rawTx, network = bitcoin.networks.bitcoin) {
return new TxDecoder(rawTx, network).decode();
};
6 changes: 3 additions & 3 deletions class/Invo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var crypto = require('crypto');
var lightningPayReq = require('bolt11');
import crypto from 'crypto';
import lightningPayReq from 'bolt11';

export class Invo {
constructor(redis, bitcoindrpc, lightning) {
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Invo {
}

async savePreimage(preimageHex) {
const paymentHashHex = require('crypto').createHash('sha256').update(Buffer.from(preimageHex, 'hex')).digest('hex');
const paymentHashHex = crypto.createHash('sha256').update(Buffer.from(preimageHex, 'hex')).digest('hex');
const key = 'preimage_for_' + paymentHashHex;
await this._redis.set(key, preimageHex);
await this._redis.expire(key, 3600 * 24 * 30); // 1 month
Expand Down
6 changes: 2 additions & 4 deletions class/Paym.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var crypto = require('crypto');
var lightningPayReq = require('bolt11');
import { BigNumber } from 'bignumber.js';
import lightningPayReq from 'bolt11';

export class Paym {
constructor(redis, bitcoindrpc, lightning) {
Expand Down Expand Up @@ -31,7 +29,7 @@ export class Paym {
if (!this._bolt11) throw new Error('bolt11 is not provided');
if (!this._decoded) await this.decodePayReqViaRpc(this._bolt11);

var request = {
const request = {
pub_key: this._decoded.destination,
amt: this._decoded.num_satoshis,
final_cltv_delta: 144,
Expand Down
8 changes: 4 additions & 4 deletions class/User.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Lock } from './Lock';
import { Lock } from './Lock.js';

var crypto = require('crypto');
var lightningPayReq = require('bolt11');
import * as crypto from 'crypto';
import lightningPayReq from 'bolt11';
import { BigNumber } from 'bignumber.js';
import { decodeRawHex } from '../btc-decoder';
const config = require('../config');
import config from '../config.js';

// static cache:
let _invoice_ispaid_cache = {};
Expand Down
8 changes: 4 additions & 4 deletions class/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './User';
export * from './Lock';
export * from './Paym';
export * from './Invo';
export * from './User.js';
export * from './Lock.js';
export * from './Paym.js';
export * from './Invo.js';
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ if (process.env.CONFIG) {
config = JSON.parse(process.env.CONFIG);
}

module.exports = config;
export default config;
23 changes: 12 additions & 11 deletions controllers/api.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { User, Lock, Paym, Invo } from '../class/';
import Frisbee from 'frisbee';
const config = require('../config');
let express = require('express');
let router = express.Router();
let logger = require('../utils/logger');
import express from 'express';
import Redis from 'ioredis';
import rateLimit from 'express-rate-limit';
import bitcoinclient from '../bitcoin.js';
import lightning from '../lightning.js';
import logger from '../utils/logger.js';
import config from '../config.js';
const MIN_BTC_BLOCK = 670000;
const router = express.Router();
console.log('using config', JSON.stringify(config));

var Redis = require('ioredis');
var redis = new Redis(config.redis);
const redis = new Redis(config.redis);
redis.monitor(function (err, monitor) {
monitor.on('monitor', function (time, args, source, database) {
// console.log('REDIS', JSON.stringify(args));
});
});


/****** START SET FEES FROM CONFIG AT STARTUP ******/
/** GLOBALS */
global.forwardFee = config.forwardReserveFee || 0.01;
global.internalFee = config.intraHubFee || 0.003;
/****** END SET FEES FROM CONFIG AT STARTUP ******/

let bitcoinclient = require('../bitcoin');
let lightning = require('../lightning');
let identity_pubkey = false;
// ###################### SMOKE TESTS ########################

Expand Down Expand Up @@ -131,7 +133,6 @@ if (config.enableUpdateDescribeGraph) {

// ######################## ROUTES ########################

const rateLimit = require('express-rate-limit');
const postLimiter = rateLimit({
windowMs: 30 * 60 * 1000,
max: config.postRateLimit || 100,
Expand Down Expand Up @@ -299,7 +300,7 @@ router.post('/payinvoice', async function (req, res) {

// else - regular lightning network payment:

var call = lightning.sendPayment();
const call = lightning.sendPayment();
call.on('data', async function (payment) {
// payment callback
await u.unlockFunds(req.body.invoice);
Expand Down Expand Up @@ -533,7 +534,7 @@ router.get('/getchaninfo/:chanid', async function (req, res) {
res.send('');
});

module.exports = router;
export default router;

// ################# HELPERS ###########################

Expand Down
16 changes: 8 additions & 8 deletions controllers/website.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const express = require('express');
import express from 'express';
import * as fs from 'fs';
import mustache from 'mustache';
import lightning from '../lightning';
import logger from '../utils/logger';
import qr from 'qr-image';
const router = express.Router();
const fs = require('fs');
const mustache = require('mustache');
const lightning = require('../lightning');
const logger = require('../utils/logger');
const qr = require('qr-image');

let lightningGetInfo = {};
let lightningListChannels = {};
Expand Down Expand Up @@ -107,7 +107,7 @@ router.get('/qr', function (req, res) {
}
const customPath = req.url.replace('/qr', '');
const url = 'bluewallet:setlndhuburl?url=' + encodeURIComponent(req.protocol + '://' + host + customPath);
var code = qr.image(url, { type: 'png' });
const code = qr.image(url, { type: 'png' });
res.setHeader('Content-type', 'image/png');
code.pipe(res);
});
Expand All @@ -116,4 +116,4 @@ router.use(function (req, res) {
res.status(404).send('404');
});

module.exports = router;
export default router;
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import express from 'express';
import helmet from 'helmet';
import morgan from 'morgan';
import { v4 as uuidv4 } from 'uuid';
import logger from './utils/logger.js';
import config from './config.js';
import rateLimit from 'express-rate-limit';
import apiController from './controllers/api.js';
import siteController from './controllers/website.js';

process.on('uncaughtException', function (err) {
console.error(err);
console.log('Node NOT Exiting...');
});

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
let express = require('express');
const helmet = require('helmet');
let morgan = require('morgan');
import { v4 as uuidv4 } from 'uuid';
let logger = require('./utils/logger');
const config = require('./config');

morgan.token('id', function getId(req) {
return req.id;
Expand All @@ -20,7 +24,6 @@ app.enable('trust proxy');
app.use(helmet.hsts());
app.use(helmet.hidePoweredBy());

const rateLimit = require('express-rate-limit');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: config.rateLimit || 200,
Expand All @@ -38,19 +41,18 @@ app.use(
),
);

let bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json(null)); // parse application/json
app.use(express.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(express.json(null)); // parse application/json

app.use('/static', express.static('static'));
app.use(require('./controllers/api'));
app.use(require('./controllers/website'));
app.use(apiController);
app.use(siteController);

const bindHost = process.env.HOST || '0.0.0.0';
const bindPort = process.env.PORT || 3000;

let server = app.listen(bindPort, bindHost, function () {
logger.log('BOOTING UP', 'Listening on ' + bindHost + ':' + bindPort);
});
module.exports = server;
export default server;
18 changes: 10 additions & 8 deletions lightning.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// setup lnd rpc
const config = require('./config');
var fs = require('fs');
var grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
import config from './config.js';
import fs from 'fs';
import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';

const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};

const packageDefinition = protoLoader.loadSync('rpc.proto', loaderOptions);
var lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc;
const lnrpc = grpc.loadPackageDefinition(packageDefinition).lnrpc;

process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
var lndCert;
let lndCert;
if (process.env.TLSCERT) {
lndCert = Buffer.from(process.env.TLSCERT, 'hex');
} else {
Expand All @@ -39,7 +41,7 @@ let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
// trying to unlock the wallet:
if (config.lnd.password) {
process.env.VERBOSE && console.log('trying to unlock the wallet');
var walletUnlocker = new lnrpc.WalletUnlocker(config.lnd.url, creds);
const walletUnlocker = new lnrpc.WalletUnlocker(config.lnd.url, creds);
walletUnlocker.unlockWallet(
{
wallet_password: Buffer.from(config.lnd.password).toString('base64'),
Expand All @@ -54,4 +56,4 @@ if (config.lnd.password) {
);
}

module.exports = new lnrpc.Lightning(config.lnd.url, creds, { 'grpc.max_receive_message_length': 1024 * 1024 * 1024 });
export default new lnrpc.Lightning(config.lnd.url, creds, { 'grpc.max_receive_message_length': 1024 * 1024 * 1024 });
Loading