merged in latest from master
This commit is contained in:
commit
142dbe7f8b
6 changed files with 85 additions and 72 deletions
37
lib/app.js
37
lib/app.js
|
|
@ -8,7 +8,7 @@ var routes = require('./routes');
|
||||||
var Trader = require('./trader');
|
var Trader = require('./trader');
|
||||||
var PostgresqlInterface = require('./postgresql_interface');
|
var PostgresqlInterface = require('./postgresql_interface');
|
||||||
var logger = require('./logger');
|
var logger = require('./logger');
|
||||||
|
|
||||||
module.exports = function (options) {
|
module.exports = function (options) {
|
||||||
var app = express();
|
var app = express();
|
||||||
var connectionString;
|
var connectionString;
|
||||||
|
|
@ -47,10 +47,9 @@ module.exports = function (options) {
|
||||||
|
|
||||||
app.use(express.bodyParser());
|
app.use(express.bodyParser());
|
||||||
|
|
||||||
if (!options.https) {
|
var authMiddleware;
|
||||||
server = http.createServer(app);
|
|
||||||
}
|
if (options.https) {
|
||||||
else {
|
|
||||||
var serverOptions = {
|
var serverOptions = {
|
||||||
key: options.https.key,
|
key: options.https.key,
|
||||||
cert: options.https.cert,
|
cert: options.https.cert,
|
||||||
|
|
@ -61,30 +60,28 @@ module.exports = function (options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
server = https.createServer(serverOptions, app);
|
server = https.createServer(serverOptions, app);
|
||||||
}
|
|
||||||
|
|
||||||
var authMiddleware = function (req, res, next) {
|
|
||||||
req.device = {};
|
|
||||||
return next();
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options.https) {
|
|
||||||
authMiddleware = function(req, res, next) {
|
authMiddleware = function(req, res, next) {
|
||||||
var fingerprint = req.connection.getPeerCertificate().fingerprint;
|
config.isAuthorized(routes.getFingerprint(req), function (err, device) {
|
||||||
|
if (err) {
|
||||||
config.isAuthorized(fingerprint, function (err, device) {
|
|
||||||
if (err) {
|
|
||||||
res.json({err: 'Internal Server Error'});
|
res.json({err: 'Internal Server Error'});
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
if (!device) {
|
if (!device) {
|
||||||
res.statusCode = 404;
|
res.json(404, {err: 'Not Found'});
|
||||||
res.json({err: 'Not Found'});
|
return next(new Error('Device is unpaired'));
|
||||||
return next(new Error('Device is unpaired'));
|
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} else {
|
||||||
|
server = http.createServer(app);
|
||||||
|
|
||||||
|
authMiddleware = function (req, res, next) {
|
||||||
|
req.device = {};
|
||||||
|
return next();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
routes.init({
|
routes.init({
|
||||||
|
|
|
||||||
|
|
@ -39,16 +39,16 @@ PostgresqlInterface.prototype.recordDeviceEvent =
|
||||||
cb);
|
cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
PostgresqlInterface.prototype.summonTransaction =
|
PostgresqlInterface.prototype.summonTransaction =
|
||||||
function summonTransaction(deviceFingerprint, tx, cb) {
|
function summonTransaction(deviceFingerprint, tx, cb) {
|
||||||
// First do an INSERT
|
// First do an INSERT
|
||||||
// If it worked, go ahead with transaction
|
// If it worked, go ahead with transaction
|
||||||
// If duplicate, fetch status and return
|
// If duplicate, fetch status and return
|
||||||
var self = this;
|
var self = this;
|
||||||
this.client.query('INSERT INTO transactions (id, status, device_fingerprint, ' +
|
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,
|
'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) {
|
function (err) {
|
||||||
if (err && PG_ERRORS[err.code] === 'uniqueViolation')
|
if (err && PG_ERRORS[err.code] === 'uniqueViolation')
|
||||||
return self._fetchTransaction(tx.txId, cb);
|
return self._fetchTransaction(tx.txId, cb);
|
||||||
|
|
@ -57,30 +57,30 @@ PostgresqlInterface.prototype.summonTransaction =
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
PostgresqlInterface.prototype.reportTransactionError =
|
PostgresqlInterface.prototype.reportTransactionError =
|
||||||
function reportTransactionError(tx, errString, status) {
|
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]);
|
[status, errString, tx.txId]);
|
||||||
};
|
};
|
||||||
|
|
||||||
PostgresqlInterface.prototype.completeTransaction =
|
PostgresqlInterface.prototype.completeTransaction =
|
||||||
function completeTransaction(tx, txHash) {
|
function completeTransaction(tx, txHash) {
|
||||||
if (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]);
|
[txHash, 'completed', tx.txId]);
|
||||||
else
|
else
|
||||||
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',
|
||||||
['failed', 'No txHash received', tx.txId]);
|
['failed', 'No txHash received', tx.txId]);
|
||||||
};
|
};
|
||||||
|
|
||||||
PostgresqlInterface.prototype._fetchTransaction =
|
PostgresqlInterface.prototype._fetchTransaction =
|
||||||
function _fetchTransaction(txId, cb) {
|
function _fetchTransaction(txId, cb) {
|
||||||
this.client.query('SELECT status, tx_hash, error FROM transactions WHERE id=$1',
|
this.client.query('SELECT status, tx_hash, error FROM transactions WHERE id=$1',
|
||||||
[txId], function (err, results) {
|
[txId], function (err, results) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
// This should never happen, since we already checked for existence
|
// 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];
|
var result = results.rows[0];
|
||||||
cb(null, {txHash: result.tx_hash, err: result.error, status: result.status});
|
cb(null, {txHash: result.tx_hash, err: result.error, status: result.status});
|
||||||
|
|
|
||||||
|
|
@ -3,82 +3,90 @@
|
||||||
var _trader;
|
var _trader;
|
||||||
var _lamassuConfig;
|
var _lamassuConfig;
|
||||||
var _idVerifier = null;
|
var _idVerifier = null;
|
||||||
|
var _trader = null;
|
||||||
var logger = require('./logger');
|
var logger = require('./logger');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
init: init,
|
||||||
|
getFingerprint: getFingerprint
|
||||||
|
};
|
||||||
|
|
||||||
// Make sure these are higher than polling interval
|
// Make sure these are higher than polling interval
|
||||||
// or there will be a lot of errors
|
// or there will be a lot of errors
|
||||||
var STALE_TICKER = 180000;
|
var STALE_TICKER = 180000;
|
||||||
var STALE_BALANCE = 180000;
|
var STALE_BALANCE = 180000;
|
||||||
|
|
||||||
var poll = function(req, res) {
|
function poll(req, res) {
|
||||||
var rateRec = _trader.rate();
|
var rateRec = _trader.rate();
|
||||||
var balanceRec = _trader.balance;
|
var balanceRec = _trader.balance;
|
||||||
var fingerprint = getFingerprint(req);
|
var fingerprint = getFingerprint(req);
|
||||||
var api = prepareApi(req, res);
|
|
||||||
|
|
||||||
logger.debug('poll request from: %s', fingerprint);
|
logger.debug('poll request from: %s', fingerprint);
|
||||||
|
|
||||||
// `rateRec` and `balanceRec` are both objects, so there's no danger
|
// `rateRec` and `balanceRec` are both objects, so there's no danger
|
||||||
// of misinterpreting rate or balance === 0 as 'Server initializing'.
|
// of misinterpreting rate or balance === 0 as 'Server initializing'.
|
||||||
if (!rateRec || !balanceRec) {
|
if (!rateRec || !balanceRec) {
|
||||||
return api.respond('Server initializing');
|
return res.json({err: 'Server initializing'});
|
||||||
}
|
}
|
||||||
|
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
if (now - rateRec.timestamp > STALE_TICKER) {
|
if (now - rateRec.timestamp > STALE_TICKER) {
|
||||||
return api.respond('Stale ticker');
|
return res.json({err: 'Stale ticker'});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (now - balanceRec.timestamp > STALE_BALANCE) {
|
if (now - balanceRec.timestamp > STALE_BALANCE) {
|
||||||
return api.respond('Stale balance');
|
return res.json({err: 'Stale balance'});
|
||||||
}
|
}
|
||||||
|
|
||||||
var rate = rateRec.rate;
|
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);
|
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,
|
rate: rate * _trader.config.exchanges.settings.commission,
|
||||||
fiat: fiatBalance,
|
fiat: fiatBalance,
|
||||||
locale: _trader.config.brain.locale,
|
locale: _trader.config.brain.locale,
|
||||||
txLimit: parseInt(_trader.config.exchanges.settings.compliance.maximum.limit, 10),
|
txLimit: parseInt(_trader.config.exchanges.settings.compliance.maximum.limit, 10),
|
||||||
idVerificationLimit: 0 // DEBUG
|
idVerificationLimit: 0 // DEBUG
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
var trade = function (req, res) {
|
function trade(req, res) {
|
||||||
var fingerprint = getFingerprint(req);
|
var fingerprint = getFingerprint(req);
|
||||||
var api = prepareApi(req, res);
|
|
||||||
_trader.trade(req.body, fingerprint);
|
_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 fingerprint = req.connection.getPeerCertificate().fingerprint;
|
||||||
var api = prepareApi(req, res);
|
|
||||||
_trader.event(req.body, fingerprint);
|
_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 fingerprint = req.connection.getPeerCertificate().fingerprint;
|
||||||
var api = prepareApi(req, res);
|
|
||||||
_idVerifier.verify(req.body, function (err, idResult) {
|
_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 fingerprint = getFingerprint(req);
|
||||||
var api = prepareApi(req, res);
|
|
||||||
_trader.sendBitcoins(fingerprint, req.body, function(err, txHash) {
|
_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) {
|
function pair(req, res) {
|
||||||
var api = prepareApi(req, res);
|
|
||||||
var token = req.body.token;
|
var token = req.body.token;
|
||||||
var name = req.body.name;
|
var name = req.body.name;
|
||||||
|
|
||||||
|
|
@ -87,13 +95,16 @@ var pair = function(req, res) {
|
||||||
getFingerprint(req),
|
getFingerprint(req),
|
||||||
name,
|
name,
|
||||||
function(err) {
|
function(err) {
|
||||||
if (err) return api.respond(err, null, 500);
|
if (err) {
|
||||||
api.respond();
|
return res.json(500, { err: err.message });
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(200);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
exports.init = function(config) {
|
function init(config) {
|
||||||
_lamassuConfig = config.lamassuConfig;
|
_lamassuConfig = config.lamassuConfig;
|
||||||
_trader = config.trader;
|
_trader = config.trader;
|
||||||
|
|
||||||
|
|
@ -113,9 +124,9 @@ exports.init = function(config) {
|
||||||
app.post('/pair', pair);
|
app.post('/pair', pair);
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
};
|
}
|
||||||
|
|
||||||
function getFingerprint(req) {
|
function getFingerprint(req) {
|
||||||
return req.connection.getPeerCertificate &&
|
return typeof req.connection.getPeerCertificate === 'function' &&
|
||||||
req.connection.getPeerCertificate().fingerprint;
|
req.connection.getPeerCertificate().fingerprint;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,12 @@ var Trader = module.exports = function (db) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Trader.prototype._findExchange = function (name) {
|
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) {
|
Trader.prototype._findTicker = function (name) {
|
||||||
|
|
@ -98,7 +103,7 @@ Trader.prototype.configure = function (config) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// IMPORTANT: This function returns the estimated minimum available balance
|
// 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
|
// session starts when a user presses then Start button and ends when we
|
||||||
// send the bitcoins.
|
// send the bitcoins.
|
||||||
Trader.prototype.fiatBalance = function (deviceFingerprint) {
|
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
|
// `balance.transferBalance` is the balance of our transfer account (the one
|
||||||
// we use to send Bitcoins to clients) in satoshis.
|
// we use to send Bitcoins to clients) in satoshis.
|
||||||
var transferBalance = balance.transferBalance;
|
var transferBalance = balance.transferBalance;
|
||||||
|
|
||||||
// Since `transferBalance` is in satoshis, we need to turn it into
|
// Since `transferBalance` is in satoshis, we need to turn it into
|
||||||
// bitcoins and then fiat to learn how much fiat currency we can exchange.
|
// 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';
|
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
|
// in which case ATM should continue polling
|
||||||
self.pollBalance();
|
self.pollBalance();
|
||||||
cb(txErr, txRec.txHash);
|
cb(txErr, txRec.txHash);
|
||||||
|
|
@ -218,7 +223,7 @@ Trader.prototype.trade = function (rec, deviceFingerprint) {
|
||||||
delete self._sessionInfo[deviceFingerprint];
|
delete self._sessionInfo[deviceFingerprint];
|
||||||
}, SESSION_TIMEOUT)
|
}, SESSION_TIMEOUT)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
this._tradeQueue.push({fiat: rec.fiat, satoshis: rec.satoshis, currency: rec.currency});
|
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) {
|
Trader.prototype._tradeBalanceFunc = function _tradeBalanceFunc(callback) {
|
||||||
if (!this.tradeExchange) return callback(null, null);
|
if (!this.tradeExchange) return callback(null, null);
|
||||||
var forexMultiplier = this._tradeForexMultiplier();
|
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) {
|
this.tradeExchange.balance(function (err, localBalance) {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
callback(null, localBalance * forexMultiplier);
|
callback(null, localBalance * forexMultiplier);
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ describe('send test', function() {
|
||||||
'txs': []
|
'txs': []
|
||||||
};
|
};
|
||||||
|
|
||||||
var payment_response = {
|
var payment_response = {
|
||||||
'message': 'Sent 0.1 BTC to 1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64',
|
'message': 'Sent 0.1 BTC to 1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64',
|
||||||
'tx_hash': 'f322d01ad784e5deeb25464a5781c3b20971c1863679ca506e702e3e33c18e9c',
|
'tx_hash': 'f322d01ad784e5deeb25464a5781c3b20971c1863679ca506e702e3e33c18e9c',
|
||||||
'notice': 'Some funds are pending confirmation and cannot be spent yet (Value 0.001 BTC)'
|
'notice': 'Some funds are pending confirmation and cannot be spent yet (Value 0.001 BTC)'
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ describe('trader/fiatBalance', function() {
|
||||||
assert.equal(fiatBalance, 150 / LOW_BALANCE_MARGIN);
|
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() {
|
'trade exchange with different currencies', function() {
|
||||||
var trader = new Trader(db);
|
var trader = new Trader(db);
|
||||||
trader.configure({
|
trader.configure({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue