diff --git a/lib/routes.js b/lib/routes.js index 14714570..0490f491 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,38 +1,38 @@ 'use strict' -var BigNumber = require('bignumber.js') -var logger = require('./logger') +const BigNumber = require('bignumber.js') +const logger = require('./logger') -var mock = false +let mock = false -var plugins -var lamassuConfig +let plugins +let lamassuConfig module.exports = { - init: init, - getFingerprint: getFingerprint + init, + getFingerprint } -var STALE_TICKER = 3 * 60 * 1000 -var STALE_BALANCE = 3 * 60 * 1000 +const STALE_TICKER = 3 * 60 * 1000 +const STALE_BALANCE = 3 * 60 * 1000 -var pids = {} -var reboots = {} +const pids = {} +const reboots = {} function buildRates () { - var cryptoCodes = plugins.getcryptoCodes() - var config = plugins.getConfig() - var settings = config.exchanges.settings + const cryptoCodes = plugins.getcryptoCodes() + const config = plugins.getConfig() + const settings = config.exchanges.settings - var cashInCommission = settings.commission - var cashOutCommission = settings.fiatCommission || cashInCommission + const cashInCommission = settings.commission + const cashOutCommission = settings.fiatCommission || cashInCommission - var rates = {} - cryptoCodes.forEach(function (cryptoCode) { - var _rate = plugins.getDeviceRate(cryptoCode) + const rates = {} + cryptoCodes.forEach(cryptoCode => { + const _rate = plugins.getDeviceRate(cryptoCode) if (!_rate) return logger.warn('No rate for ' + cryptoCode + ' yet') if (Date.now() - _rate.timestamp > STALE_TICKER) return logger.warn('Stale rate for ' + cryptoCode) - var rate = _rate.rates + const rate = _rate.rates rates[cryptoCode] = { cashIn: rate.ask.times(cashInCommission), cashOut: rate.bid.div(cashOutCommission) @@ -43,11 +43,11 @@ function buildRates () { } function buildBalances () { - var cryptoCodes = plugins.getcryptoCodes() + const cryptoCodes = plugins.getcryptoCodes() - var _balances = {} - cryptoCodes.forEach(function (cryptoCode) { - var balanceRec = plugins.fiatBalance(cryptoCode) + const _balances = {} + cryptoCodes.forEach(cryptoCode => { + const balanceRec = plugins.fiatBalance(cryptoCode) if (!balanceRec) return logger.warn('No balance for ' + cryptoCode + ' yet') if (Date.now() - balanceRec.timestamp > STALE_BALANCE) return logger.warn('Stale balance for ' + cryptoCode) _balances[cryptoCode] = balanceRec.balance @@ -57,112 +57,112 @@ function buildBalances () { } function poll (req, res) { - var fingerprint = getFingerprint(req) - var pid = req.query.pid + const fingerprint = getFingerprint(req) + const pid = req.query.pid - pids[fingerprint] = {pid: pid, ts: Date.now()} + pids[fingerprint] = {pid, ts: Date.now()} logger.debug('poll request from: %s', fingerprint) - var rates = {} - var balances = {} + let rates = {} + let balances = {} rates = buildRates() balances = buildBalances() - var config = plugins.getConfig() - var settings = config.exchanges.settings - var complianceSettings = settings.compliance + const config = plugins.getConfig() + const settings = config.exchanges.settings + const complianceSettings = settings.compliance plugins.pollQueries(session(req)) - .then(results => { - var cartridges = results.cartridges + .then(results => { + const cartridges = results.cartridges - var reboot = reboots[fingerprint] === pid + const reboot = reboots[fingerprint] === pid - var response = { - err: null, - locale: config.brain.locale, - txLimit: parseInt(complianceSettings.maximum.limit, 10), - idVerificationEnabled: complianceSettings.idVerificationEnabled, - cartridges: cartridges, - twoWayMode: !!cartridges, - zeroConfLimit: settings.zeroConfLimit, - fiatTxLimit: settings.fiatTxLimit, - reboot: reboot, - rates: rates, - balances: balances, - coins: settings.coins - } + const response = { + err: null, + locale: config.brain.locale, + txLimit: parseInt(complianceSettings.maximum.limit, 10), + idVerificationEnabled: complianceSettings.idVerificationEnabled, + cartridges, + twoWayMode: !!cartridges, + zeroConfLimit: settings.zeroConfLimit, + fiatTxLimit: settings.fiatTxLimit, + reboot, + rates, + balances, + coins: settings.coins + } - if (response.idVerificationEnabled) { - response.idVerificationLimit = complianceSettings.idVerificationLimit - } + if (response.idVerificationEnabled) { + response.idVerificationLimit = complianceSettings.idVerificationLimit + } - res.json(response) - }) - .catch(logger.error) + res.json(response) + }) + .catch(logger.error) plugins.recordPing(session(req), req.query) - .catch(logger.error) + .catch(logger.error) } function trade (req, res) { - var tx = req.body + const tx = req.body tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) plugins.trade(session(req), tx) - .then(() => res.status(201).json({})) - .catch(err => { - logger.error(err) - res.status(500).json({err: err}) - }) + .then(() => res.status(201).json({})) + .catch(err => { + logger.error(err) + res.status(500).json({err}) + }) } function stateChange (req, res) { plugins.stateChange(session(req), req.body) - .then(() => res.json({success: true})) - .catch(err => { - console.error(err) - res.json({success: false}) - }) + .then(() => res.json({success: true})) + .catch(err => { + console.error(err) + res.json({success: false}) + }) } function send (req, res) { - var tx = req.body + const tx = req.body tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) // TODO: use status.statusCode here after confirming machine compatibility // FIX: (joshm) set txHash to status.txId instead of previous status.txHash which wasn't being set // Need to clean up txHash vs txId return plugins.sendCoins(session(req), tx) - .then(status => res.json({ - txHash: status && status.txHash, - txId: status && status.txId - })) - .catch(err => { - logger.error(err) - res.json({ - err: err.message, - errType: err.name + .then(status => res.json({ + txHash: status && status.txHash, + txId: status && status.txId + })) + .catch(err => { + logger.error(err) + res.json({ + err: err.message, + errType: err.name + }) }) - }) } function cashOut (req, res) { logger.info({tx: req.body, cmd: 'cashOut'}) - var tx = req.body + const tx = req.body tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) return plugins.cashOut(session(req), req.body) - .then(cryptoAddress => res.json({bitcoinAddress: cryptoAddress})) - .catch(err => { - res.json({ - err: err.message, - errType: err.name + .then(cryptoAddress => res.json({bitcoinAddress: cryptoAddress})) + .catch(err => { + res.json({ + err: err.message, + errType: err.name + }) + logger.error(err) }) - logger.error(err) - }) } function dispenseAck (req, res) { @@ -178,7 +178,7 @@ function deviceEvent (req, res) { function verifyUser (req, res) { if (mock) return res.json({success: true}) - plugins.verifyUser(req.body, function (err, idResult) { + plugins.verifyUser(req.body, (err, idResult) => { if (err) { logger.error(err) return res.json({err: 'Verification failed'}) @@ -191,7 +191,7 @@ function verifyUser (req, res) { function verifyTx (req, res) { if (mock) return res.json({success: true}) - plugins.verifyTx(req.body, function (err, idResult) { + plugins.verifyTx(req.body, (err, idResult) => { if (err) { logger.error(err) return res.json({err: 'Verification failed'}) @@ -202,14 +202,14 @@ function verifyTx (req, res) { } function pair (req, res) { - var token = req.body.token - var name = req.body.name + const token = req.body.token + const name = req.body.name lamassuConfig.pair( token, getFingerprint(req), name, - function (err) { + err => { if (err) { logger.error(err) return res.json({err: err.message}) @@ -221,74 +221,74 @@ function pair (req, res) { } function phoneCode (req, res) { - var phone = req.body.phone + const phone = req.body.phone logger.debug('Phone code requested for: ' + phone) return plugins.getPhoneCode(phone) - .then(code => res.json({code: code})) - .catch(err => { - logger.error(err) - if (err.name === 'BadNumberError') return res.sendStatus(410) - return res.sendStatus(500) - }) + .then(code => res.json({code})) + .catch(err => { + logger.error(err) + if (err.name === 'BadNumberError') return res.sendStatus(410) + return res.sendStatus(500) + }) } function updatePhone (req, res) { const notified = req.query.notified === 'true' return plugins.updatePhone(session(req), req.body, notified) - .then(r => res.json(r)) - .catch(err => { - logger.error(err) - res.sendStatus(500) - }) + .then(r => res.json(r)) + .catch(err => { + logger.error(err) + res.sendStatus(500) + }) } function fetchPhoneTx (req, res) { return plugins.fetchPhoneTx(req.query.phone) - .then(r => res.json(r)) - .catch(err => { - logger.error(err) - res.sendStatus(500) - }) + .then(r => res.json(r)) + .catch(err => { + logger.error(err) + res.sendStatus(500) + }) } function registerRedeem (req, res) { return plugins.registerRedeem(session(req)) - .then(() => res.json({success: true})) - .catch(err => { - logger.error(err) - res.sendStatus(500) - }) + .then(() => res.json({success: true})) + .catch(err => { + logger.error(err) + res.sendStatus(500) + }) } function waitForDispense (req, res) { logger.debug('waitForDispense') return plugins.fetchTx(session(req)) - .then(tx => { - logger.debug('tx fetched') - logger.debug(tx) - if (!tx) return res.sendStatus(404) - if (tx.status === req.query.status) return res.sendStatus(304) - res.json({tx: tx}) - }) - .catch(err => { - logger.error(err) - res.sendStatus(500) - }) + .then(tx => { + logger.debug('tx fetched') + logger.debug(tx) + if (!tx) return res.sendStatus(404) + if (tx.status === req.query.status) return res.sendStatus(304) + res.json({tx}) + }) + .catch(err => { + logger.error(err) + res.sendStatus(500) + }) } function dispense (req, res) { const tx = req.body.tx return plugins.requestDispense(tx) - .then(r => { - return cacheResponse(req, r) - .then(() => res.json(r)) - }) - .catch(err => { - logger.error(err) - res.sendStatus(500) - }) + .then(r => { + return cacheResponse(req, r) + .then(() => res.json(r)) + }) + .catch(err => { + logger.error(err) + res.sendStatus(500) + }) } function init (localConfig) { @@ -296,9 +296,9 @@ function init (localConfig) { plugins = localConfig.plugins mock = localConfig.mock - var authMiddleware = localConfig.authMiddleware - var app = localConfig.app - var localApp = localConfig.localApp + const authMiddleware = localConfig.authMiddleware + const app = localConfig.app + const localApp = localConfig.localApp app.get('/poll', authMiddleware, poll) @@ -320,15 +320,15 @@ function init (localConfig) { app.get('/await_dispense', authMiddleware, waitForDispense) app.post('/dispense', authMiddleware, cachedResponse, dispense) - localApp.get('/pid', function (req, res) { - var machineFingerprint = req.query.fingerprint - var pidRec = pids[machineFingerprint] + localApp.get('/pid', (req, res) => { + const machineFingerprint = req.query.fingerprint + const pidRec = pids[machineFingerprint] res.json(pidRec) }) - localApp.post('/reboot', function (req, res) { - var pid = req.body.pid - var fingerprint = req.body.fingerprint + localApp.post('/reboot', (req, res) => { + const pid = req.body.pid + const fingerprint = req.body.fingerprint console.log('pid: %s, fingerprint: %s', pid, fingerprint) if (!fingerprint || !pid) { @@ -357,11 +357,11 @@ function getFingerprint (req) { function cachedResponse (req, res, next) { return plugins.cachedResponse(session(req), req.path, req.method) - .then(r => { - if (!r.body) return next() - if (r.body.pendingRequest) return res.sendStatus(409) - res.json(r.body) - }) + .then(r => { + if (!r.body) return next() + if (r.body.pendingRequest) return res.sendStatus(409) + res.json(r.body) + }) } function cacheResponse (req, body) {