From c2bfa215c0d4b246ce3e1c626e8a8f0239021518 Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 5 Aug 2014 00:20:35 +0200 Subject: [PATCH 1/4] fix(module): info about missing exchange module added --- lib/trader.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/trader.js b/lib/trader.js index ee181f71..48d4c5f5 100644 --- a/lib/trader.js +++ b/lib/trader.js @@ -21,7 +21,12 @@ var Trader = module.exports = function (db) { }; Trader.prototype._findExchange = function (name) { - return require('lamassu-' + name); + try { + return require('lamassu-' + name); + + } catch(_) { + throw new Error(name + ' module is not installed. Try running `npm install --save lamassu-' + name + '` first'); + } }; Trader.prototype._findTicker = function (name) { From 443a4814876b5b449f053bbc9882ffa00f5f7cdc Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 5 Aug 2014 00:58:23 +0200 Subject: [PATCH 2/4] fix(fingerprint): check for getPeerCertificate fn existence added --- lib/routes.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index 83a3b9f5..780dfcc2 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -18,13 +18,13 @@ Error.prototype.toJSON = function () { return ret; }; -var poll = function(req, res) { +function poll(req, res) { var rateRec = _trader.rate(); var balanceRec = _trader.balance; var fingerprint = getFingerprint(req); logger.debug('poll request from: %s', fingerprint); - + // `rateRec` and `balanceRec` are both objects, so there's no danger // of misinterpreting rate or balance === 0 as 'Server initializing'. if (!rateRec || !balanceRec) { @@ -54,13 +54,13 @@ var poll = function(req, res) { }); }; -var trade = function (req, res) { +function trade(req, res) { var fingerprint = getFingerprint(req); _trader.trade(req.body, fingerprint); res.json({err: null}); }; -var send = function(req, res) { +function send(req, res) { var fingerprint = getFingerprint(req); _trader.sendBitcoins(fingerprint, req.body, function(err, txHash) { res.json({ @@ -71,7 +71,7 @@ var send = function(req, res) { }); }; -var pair = function(req, res) { +function pair(req, res) { var token = req.body.token; var name = req.body.name; @@ -89,7 +89,7 @@ var pair = function(req, res) { ); }; -exports.init = function(app, config, trader, authMiddleware) { +function init(app, config, trader, authMiddleware) { _lamassuConfig = config; _trader = trader; @@ -102,6 +102,11 @@ exports.init = function(app, config, trader, authMiddleware) { }; function getFingerprint(req) { - return req.connection.getPeerCertificate && + return typeof req.connection.getPeerCertificate === 'function' && req.connection.getPeerCertificate().fingerprint; -} \ No newline at end of file +}; + +module.exports = { + init: init, + getFingerprint: getFingerprint +}; From 21c957cf0f9f9eb82b83dd4f62e8d8fe9a51b5ab Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 5 Aug 2014 00:59:08 +0200 Subject: [PATCH 3/4] style(server): server setup cleanup --- lib/app.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/app.js b/lib/app.js index 2dedc726..854e0dd8 100644 --- a/lib/app.js +++ b/lib/app.js @@ -47,10 +47,9 @@ module.exports = function (options) { app.use(express.bodyParser()); - if (!options.https) { - server = http.createServer(app); - } - else { + var authMiddleware; + + if (options.https) { var serverOptions = { key: options.https.key, cert: options.https.cert, @@ -61,30 +60,28 @@ module.exports = function (options) { }; server = https.createServer(serverOptions, app); - } - var authMiddleware = function (req, res, next) { - req.device = {}; - return next(); - }; - - if (options.https) { authMiddleware = function(req, res, next) { - var fingerprint = req.connection.getPeerCertificate().fingerprint; - - config.isAuthorized(fingerprint, function (err, device) { - if (err) { + config.isAuthorized(routes.getFingerprint(req), function (err, device) { + if (err) { res.json({err: 'Internal Server Error'}); return next(err); } if (!device) { - res.statusCode = 404; - res.json({err: 'Not Found'}); - return next(new Error('Device is unpaired')); + res.json(404, {err: 'Not Found'}); + return next(new Error('Device is unpaired')); } next(); }); }; + + } else { + server = http.createServer(app); + + authMiddleware = function (req, res, next) { + req.device = {}; + return next(); + }; } routes.init(app, config, trader, authMiddleware); From 95330cc39ddb85c954bc604adec3d87ab1720638 Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 5 Aug 2014 01:14:41 +0200 Subject: [PATCH 4/4] style(exports): module.exports moved to the top of a file --- lib/routes.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index 780dfcc2..7a162d28 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -4,6 +4,11 @@ var _trader; var _lamassuConfig; var logger = require('./logger'); +module.exports = { + init: init, + getFingerprint: getFingerprint +}; + // Make sure these are higher than polling interval // or there will be a lot of errors var STALE_TICKER = 180000; @@ -105,8 +110,3 @@ function getFingerprint(req) { return typeof req.connection.getPeerCertificate === 'function' && req.connection.getPeerCertificate().fingerprint; }; - -module.exports = { - init: init, - getFingerprint: getFingerprint -};