merged in latest from master

This commit is contained in:
Josh Harvey 2014-08-07 18:50:01 -04:00
commit 142dbe7f8b
6 changed files with 85 additions and 72 deletions

View file

@ -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;
@ -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);
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({

View file

@ -39,16 +39,16 @@ PostgresqlInterface.prototype.recordDeviceEvent =
cb);
};
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);
@ -57,30 +57,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});

View file

@ -3,82 +3,90 @@
var _trader;
var _lamassuConfig;
var _idVerifier = null;
var _trader = null;
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;
var STALE_BALANCE = 180000;
var poll = function(req, res) {
function poll(req, res) {
var rateRec = _trader.rate();
var balanceRec = _trader.balance;
var fingerprint = getFingerprint(req);
var api = prepareApi(req, res);
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) {
return api.respond('Server initializing');
return res.json({err: 'Server initializing'});
}
var now = Date.now();
if (now - rateRec.timestamp > STALE_TICKER) {
return api.respond('Stale ticker');
return res.json({err: 'Stale ticker'});
}
if (now - balanceRec.timestamp > STALE_BALANCE) {
return api.respond('Stale balance');
return res.json({err: 'Stale balance'});
}
var rate = rateRec.rate;
if (rate === null) return api.respond('No rate available');
if (rate === null) return res.json({err: 'No rate available'});
var fiatBalance = _trader.fiatBalance(fingerprint);
if (fiatBalance === null) return api.respond('No balance available');
if (fiatBalance === null) return res.json({err: 'No balance available'});
api.respond(null, {
res.json({
err: null,
rate: rate * _trader.config.exchanges.settings.commission,
fiat: fiatBalance,
locale: _trader.config.brain.locale,
txLimit: parseInt(_trader.config.exchanges.settings.compliance.maximum.limit, 10),
idVerificationLimit: 0 // DEBUG
});
};
}
var trade = function (req, res) {
function trade(req, res) {
var fingerprint = getFingerprint(req);
var api = prepareApi(req, res);
_trader.trade(req.body, fingerprint);
api.respond();
};
var deviceEvent = function deviceEvent(req, res) {
res.json({err: null});
}
function deviceEvent(req, res) {
var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
_trader.event(req.body, fingerprint);
api.respond();
};
res.json({err: null});
}
var idVerify = function idVerify(req, res) {
function idVerify(req, res) {
// var fingerprint = req.connection.getPeerCertificate().fingerprint;
var api = prepareApi(req, res);
_idVerifier.verify(req.body, function (err, idResult) {
api.respond(err, idResult);
if (err)
return res.json({err: 'Verification failed'});
res.json(idResult);
});
};
}
var send = function(req, res) {
function send(req, res) {
var fingerprint = getFingerprint(req);
var api = prepareApi(req, res);
_trader.sendBitcoins(fingerprint, req.body, function(err, txHash) {
api.respond(err, {txHash: txHash});
res.json({
err: err && err.message,
txHash: txHash,
errType: err && err.name
});
});
};
}
var pair = function(req, res) {
var api = prepareApi(req, res);
function pair(req, res) {
var token = req.body.token;
var name = req.body.name;
@ -87,13 +95,16 @@ var pair = function(req, res) {
getFingerprint(req),
name,
function(err) {
if (err) return api.respond(err, null, 500);
api.respond();
if (err) {
return res.json(500, { err: err.message });
}
res.json(200);
}
);
};
}
exports.init = function(config) {
function init(config) {
_lamassuConfig = config.lamassuConfig;
_trader = config.trader;
@ -113,9 +124,9 @@ exports.init = function(config) {
app.post('/pair', pair);
return app;
};
}
function getFingerprint(req) {
return req.connection.getPeerCertificate &&
return typeof req.connection.getPeerCertificate === 'function' &&
req.connection.getPeerCertificate().fingerprint;
}

View file

@ -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) {
@ -98,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) {
@ -120,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.
//
@ -188,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);
@ -218,7 +223,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});
};
@ -296,7 +301,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);

View file

@ -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)'

View file

@ -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({