diff --git a/bin/lamassu-server b/bin/lamassu-server index e5e82712..53879f8e 100755 --- a/bin/lamassu-server +++ b/bin/lamassu-server @@ -30,6 +30,8 @@ options.mock = argv.mock console.log('DEBUG23') +process.on('unhandledRejection', err => console.log(err.stack)) + createServer(options) .then(server => { console.log('DEBUG22') diff --git a/lib/app.js b/lib/app.js index fb5f9fbf..5dc9503f 100644 --- a/lib/app.js +++ b/lib/app.js @@ -28,14 +28,10 @@ module.exports = function (options) { return configManager.load() .then(config => { - console.log('DEBUG5: %j', config) - console.log('DEBUG8 ****************') - plugins.configure(config) - console.log('DEBUG9.1 ****************') plugins.startPolling() - console.log('DEBUG9.2 ****************') plugins.startCheckingNotification() + console.log('DEBUG9.3 ****************') app.use(bodyParser.json()) @@ -56,12 +52,6 @@ module.exports = function (options) { server = https.createServer(serverOptions, app) - authMiddleware = function (req, res, next) { - next() // TODO: authentication - } - } else { - server = http.createServer(app) - authMiddleware = function (req, res, next) { const deviceId = req.connection.getPeerCertificate().fingerprint @@ -76,13 +66,19 @@ module.exports = function (options) { }) .catch(e => res.status(403).end()) } + } else { + server = http.createServer(app) + + authMiddleware = function (req, res, next) { + return next() + } } if (options.mock) logger.info('In mock mode') var localApp = express() localApp.use(bodyParser.json()) - var localServer = http.createServer({localAddress: 'localhost'}, localApp) + var localServer = http.createServer(localApp) var localPort = 7070 console.log('DEBUG7 ****************') @@ -95,9 +91,9 @@ module.exports = function (options) { mock: options.mock }) - localServer.listen(7070, function () { - console.log('lamassu-server is listening on local port %d', localPort) - }) + // localServer.listen(7070, 'localhost', function () { + // console.log('lamassu-server is listening on local port %d', localPort) + // }) return server }) diff --git a/lib/notifier.js b/lib/notifier.js index 3a323973..894550ec 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -36,8 +36,8 @@ function checkBalance (rec) { } function checkBalances () { - var balances = getBalances() - return R.reject(R.isNil, balances.map(checkBalance)) + return getBalances() + .then(balances => R.reject(R.isNil, balances.map(checkBalance))) } function checkPing (deviceEvents) { @@ -87,13 +87,13 @@ function devicesAndEvents () { function checkStatus () { var alerts = {devices: {}, deviceNames: {}} - alerts.general = checkBalances() - return devicesAndEvents() - .then(function (rec) { + return Promise.all([checkBalances(), devicesAndEvents()]) + .then(([balances, rec]) => { var devices = rec.devices var events = rec.events + alerts.general = balances devices.forEach(function (deviceRow) { var deviceId = deviceRow.device_id var deviceName = deviceRow.name || deviceId diff --git a/lib/plugins.js b/lib/plugins.js index 735d5e46..88fe5ca6 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -134,7 +134,6 @@ function pp (o) { function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, config, accounts, options, onChangeCallback) { - pp(config) const currentName = config.cryptoServices[pluginType] currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {} @@ -168,16 +167,10 @@ exports.loadOrConfigPlugin = loadOrConfigPlugin // Note: this whole function gets called every time there's a config update exports.configure = function configure (config) { - console.log('DEBUG40') - console.log('DEBUG4: %j', config) - cachedConfig = config - console.log('DEBUG41') - const accounts = configManager.loadAccounts() - console.log('DEBUG42') const cryptoCodes = getCryptoCodes() console.log('DEBUG30') @@ -216,7 +209,6 @@ exports.configure = function configure (config) { accounts, {masterSeed: cryptoSeed}, function onWalletChange (newWallet) { - console.log('DEBUG34: %s, %j', cryptoCode, newWallet) walletPlugins[cryptoCode] = newWallet pollBalance(cryptoCode) } @@ -239,8 +231,6 @@ exports.configure = function configure (config) { ) }) - console.log('DEBUG32') - const unscopedCfg = configManager.unscoped(cachedConfig) // ID VERIFIER [optional] configure (or load) @@ -577,7 +567,6 @@ function pollBalance (cryptoCode, cb) { function pollRate (cryptoCode, cb) { const tickerPlugin = tickerPlugins[cryptoCode] - pp(tickerPlugins) logger.debug('[%s] polling for rates (%s)', cryptoCode, tickerPlugin.NAME) let currencies = deviceCurrency @@ -710,8 +699,6 @@ exports.verifyTx = function verifyTx (data, cb) { function sendMessage (rec) { const pluginPromises = [] const config = configManager.unscoped(cachedConfig) - console.log('DEBUG35') - pp(config) if (!config.notifications.notificationsEnabled) return Promise.all([]) @@ -789,19 +776,25 @@ exports.getCryptoCodes = getCryptoCodes function getAllCryptoCodes () { return db.devices() .then(rows => { - return rows.reduce((acc, r) => getCryptoCodes(r.device_id).forEach(c => acc.add(c)), new Set()) + return rows.reduce((acc, r) => { + getCryptoCodes(r.device_id).forEach(c => acc.add(c)) + return acc + }, new Set()) }) } function checkBalances () { - return Promise.all(getAllCryptoCodes(), db.devices()) + return Promise.all([getAllCryptoCodes(), db.devices()]) .then(arr => { const cryptoCodes = arr[0] const deviceIds = arr[1].map(r => r.device_id) const balances = [] cryptoCodes.forEach(cryptoCode => { - const minBalance = deviceIds.map(deviceId => exports.fiatBalance(cryptoCode, deviceId).balance) + const minBalance = deviceIds.map(deviceId => { + const fiatBalanceRec = exports.fiatBalance(cryptoCode, deviceId) + return fiatBalanceRec ? fiatBalanceRec.balance : Infinity + }) .reduce((min, cur) => Math.min(min, cur), Infinity) const rec = {fiatBalance: minBalance, cryptoCode, fiatCode: deviceCurrency} @@ -814,12 +807,9 @@ function checkBalances () { exports.startCheckingNotification = function startCheckingNotification () { const config = configManager.unscoped(cachedConfig) - return checkBalances() - .then(balances => { - notifier.init(db, balances, config.notifications) - checkNotification() - setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL) - }) + notifier.init(db, checkBalances, config.notifications) + checkNotification() + setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL) } exports.getPhoneCode = function getPhoneCode (phone) { diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 80a63bf9..4849a6d9 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -279,10 +279,10 @@ exports.machineEvent = function machineEvent (rec) { } exports.devices = function devices () { - const sql = 'SELECT device_id, name FROM devices WHERE authorized=$1' + const sql = 'SELECT device_id, name FROM devices' const db = connect() - return db.any(sql, [true]) + return db.any(sql) } exports.machineEvents = function machineEvents () { diff --git a/lib/routes.js b/lib/routes.js index 78b82548..43ef06b5 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -95,8 +95,6 @@ function poll (req, res) { } } - console.log('DEBUG34') - const response = { err: null, locale, @@ -112,14 +110,10 @@ function poll (req, res) { coins: config.currencies.cryptos } - console.log('DEBUG35') - if (response.idVerificationEnabled) { response.idVerificationLimit = config.compliance.idVerificationLimit } - console.log('DEBUG32') - res.json(response) }) .catch(e => { console.log(e); logger.error(e) }) @@ -223,7 +217,7 @@ function verifyTx (req, res) { } function ca (req, res) { - const token = req.body.token + const token = req.query.token return pair.authorizeCaDownload(token) .then(valid => { @@ -234,7 +228,7 @@ function ca (req, res) { } function pair (req, res) { - const token = req.body.token + const token = req.query.token const deviceId = getDeviceId(req) return pair.pair(token, deviceId) @@ -349,7 +343,7 @@ function init (opts) { const localApp = opts.localApp app.post('/pair', pair) - app.post('/ca', ca) + app.get('/ca', ca) app.get('/poll', authMiddleware, poll) diff --git a/package.json b/package.json index 91be6488..cccf5cd6 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "numeral": "^1.5.3", "pg": "^4.5.5", "pg-promise": "^4.3.3", + "pify": "^2.3.0", "pretty-ms": "^2.1.0", "ramda": "^0.21.0", "u-promised": "^0.2.4",