From c2bfa215c0d4b246ce3e1c626e8a8f0239021518 Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 5 Aug 2014 00:20:35 +0200 Subject: [PATCH 1/5] 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/5] 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/5] 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/5] 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 -}; From 2b64f3568929a7d590244a5c7164fc78f95cce87 Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 5 Aug 2014 17:21:39 +0200 Subject: [PATCH 5/5] style(whitespace): trailing whitespaces removed --- lib/app.js | 4 ++-- lib/postgresql_interface.js | 22 +++++++++++----------- lib/routes.js | 4 ++-- lib/trader.js | 10 +++++----- test/api/sendTest.js | 2 +- test/unit/traderFiatBalanceTest.js | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/app.js b/lib/app.js index 854e0dd8..46241570 100644 --- a/lib/app.js +++ b/lib/app.js @@ -8,7 +8,7 @@ var routes = require('./routes'); var Trader = require('./trader'); var PostgresqlInterface = require('./postgresql_interface'); var logger = require('./logger'); - + module.exports = function (options) { var app = express(); var connectionString; @@ -65,7 +65,7 @@ module.exports = function (options) { config.isAuthorized(routes.getFingerprint(req), function (err, device) { if (err) { res.json({err: 'Internal Server Error'}); - return next(err); + return next(err); } if (!device) { res.json(404, {err: 'Not Found'}); diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 2b7885eb..f1269445 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -20,16 +20,16 @@ var PostgresqlInterface = function (conString) { PostgresqlInterface.factory = function factory(conString) { return new PostgresqlInterface(conString); }; module.exports = PostgresqlInterface; -PostgresqlInterface.prototype.summonTransaction = +PostgresqlInterface.prototype.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb) { // First do an INSERT // If it worked, go ahead with transaction // If duplicate, fetch status and return var self = this; this.client.query('INSERT INTO transactions (id, status, device_fingerprint, ' + - 'to_address, satoshis, currency_code, fiat) ' + + 'to_address, satoshis, currency_code, fiat) ' + 'VALUES ($1, $2, $3, $4, $5, $6, $7)', [tx.txId, 'pending', deviceFingerprint, - tx.toAddress, tx.satoshis, tx.currencyCode, tx.fiat], + tx.toAddress, tx.satoshis, tx.currencyCode, tx.fiat], function (err) { if (err && PG_ERRORS[err.code] === 'uniqueViolation') return self._fetchTransaction(tx.txId, cb); @@ -38,30 +38,30 @@ PostgresqlInterface.prototype.summonTransaction = }); }; -PostgresqlInterface.prototype.reportTransactionError = +PostgresqlInterface.prototype.reportTransactionError = function reportTransactionError(tx, errString, status) { - this.client.query('UPDATE transactions SET status=$1, error=$2 WHERE id=$3', + this.client.query('UPDATE transactions SET status=$1, error=$2 WHERE id=$3', [status, errString, tx.txId]); }; -PostgresqlInterface.prototype.completeTransaction = +PostgresqlInterface.prototype.completeTransaction = function completeTransaction(tx, txHash) { if (txHash) - this.client.query('UPDATE transactions SET tx_hash=$1, status=$2, completed=now() WHERE id=$3', + this.client.query('UPDATE transactions SET tx_hash=$1, status=$2, completed=now() WHERE id=$3', [txHash, 'completed', tx.txId]); else - this.client.query('UPDATE transactions SET status=$1, error=$2 WHERE id=$3', - ['failed', 'No txHash received', tx.txId]); + this.client.query('UPDATE transactions SET status=$1, error=$2 WHERE id=$3', + ['failed', 'No txHash received', tx.txId]); }; -PostgresqlInterface.prototype._fetchTransaction = +PostgresqlInterface.prototype._fetchTransaction = function _fetchTransaction(txId, cb) { this.client.query('SELECT status, tx_hash, error FROM transactions WHERE id=$1', [txId], function (err, results) { if (err) return cb(err); // This should never happen, since we already checked for existence - if (results.rows.length === 0) return cb(new Error('Couldn\'t find transaction.')); + if (results.rows.length === 0) return cb(new Error('Couldn\'t find transaction.')); var result = results.rows[0]; cb(null, {txHash: result.tx_hash, err: result.error, status: result.status}); diff --git a/lib/routes.js b/lib/routes.js index 7a162d28..dfd7d6df 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -69,8 +69,8 @@ function send(req, res) { var fingerprint = getFingerprint(req); _trader.sendBitcoins(fingerprint, req.body, function(err, txHash) { res.json({ - err: err && err.message, - txHash: txHash, + err: err && err.message, + txHash: txHash, errType: err && err.name }); }); diff --git a/lib/trader.js b/lib/trader.js index 48d4c5f5..60b1e1c5 100644 --- a/lib/trader.js +++ b/lib/trader.js @@ -103,7 +103,7 @@ Trader.prototype.configure = function (config) { }; // IMPORTANT: This function returns the estimated minimum available balance -// in fiat as of the start of the current user session on the device. User +// in fiat as of the start of the current user session on the device. User // session starts when a user presses then Start button and ends when we // send the bitcoins. Trader.prototype.fiatBalance = function (deviceFingerprint) { @@ -125,7 +125,7 @@ Trader.prototype.fiatBalance = function (deviceFingerprint) { // `balance.transferBalance` is the balance of our transfer account (the one // we use to send Bitcoins to clients) in satoshis. var transferBalance = balance.transferBalance; - + // Since `transferBalance` is in satoshis, we need to turn it into // bitcoins and then fiat to learn how much fiat currency we can exchange. // @@ -193,7 +193,7 @@ Trader.prototype.sendBitcoins = function (deviceFingerprint, tx, cb) { if (txRec.status === 'insufficientFunds') txErr.name = 'InsufficientFunds'; } - // transaction exists, but txHash might be null, + // transaction exists, but txHash might be null, // in which case ATM should continue polling self.pollBalance(); cb(txErr, txRec.txHash); @@ -213,7 +213,7 @@ Trader.prototype.trade = function (rec, deviceFingerprint) { delete self._sessionInfo[deviceFingerprint]; }, SESSION_TIMEOUT) }; - } + } this._tradeQueue.push({fiat: rec.fiat, satoshis: rec.satoshis, currency: rec.currency}); }; @@ -291,7 +291,7 @@ Trader.prototype._tradeForexMultiplier = function _tradeForexMultiplier() { Trader.prototype._tradeBalanceFunc = function _tradeBalanceFunc(callback) { if (!this.tradeExchange) return callback(null, null); var forexMultiplier = this._tradeForexMultiplier(); - if (!forexMultiplier) return callback(new Error('Can\'t compute balance, no tickers yet.')); + if (!forexMultiplier) return callback(new Error('Can\'t compute balance, no tickers yet.')); this.tradeExchange.balance(function (err, localBalance) { if (err) return callback(err); callback(null, localBalance * forexMultiplier); diff --git a/test/api/sendTest.js b/test/api/sendTest.js index 182d7eb2..622e3d15 100644 --- a/test/api/sendTest.js +++ b/test/api/sendTest.js @@ -90,7 +90,7 @@ describe('send test', function() { 'txs': [] }; - var payment_response = { + var payment_response = { 'message': 'Sent 0.1 BTC to 1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64', 'tx_hash': 'f322d01ad784e5deeb25464a5781c3b20971c1863679ca506e702e3e33c18e9c', 'notice': 'Some funds are pending confirmation and cannot be spent yet (Value 0.001 BTC)' diff --git a/test/unit/traderFiatBalanceTest.js b/test/unit/traderFiatBalanceTest.js index 12536f26..923e3671 100644 --- a/test/unit/traderFiatBalanceTest.js +++ b/test/unit/traderFiatBalanceTest.js @@ -74,7 +74,7 @@ describe('trader/fiatBalance', function() { assert.equal(fiatBalance, 150 / LOW_BALANCE_MARGIN); }); - it('should calculate balance correctly with transfer and ' + + it('should calculate balance correctly with transfer and ' + 'trade exchange with different currencies', function() { var trader = new Trader(db); trader.configure({