This commit is contained in:
Josh Harvey 2016-11-27 16:54:08 +02:00
parent 4184e99273
commit 493e669e6d
5 changed files with 198 additions and 196 deletions

View file

@ -8,6 +8,7 @@ const logger = require('./logger')
const configManager = require('./config-manager')
const db = require('./db')
const pairing = require('./pairing')
const settingsLoader = require('./settings')
let plugins
@ -15,68 +16,31 @@ module.exports = {
init
}
const STALE_TICKER = 3 * 60 * 1000
const STALE_BALANCE = 3 * 60 * 1000
const CLOCK_SKEW = 60 * 1000
const REQUEST_TTL = 3 * 60 * 1000
const pids = {}
const reboots = {}
function buildRates (deviceId) {
const cryptoCodes = plugins.getCryptoCodes()
const config = plugins.getConfig(deviceId)
const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100).plus(1)
const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100).plus(1)
const rates = {}
cryptoCodes.forEach(cryptoCode => {
const _rate = plugins.getDeviceRate(cryptoCode)
if (!_rate) return logger.warn('No rate for ' + cryptoCode + ' yet')
if (Date.now() - _rate.timestamp > STALE_TICKER) return logger.warn('Stale rate for ' + cryptoCode)
const rate = _rate.rates
rates[cryptoCode] = {
cashIn: rate.ask.times(cashInCommission),
cashOut: rate.bid.div(cashOutCommission)
}
function loadSettings (req, res, next) {
settingsLoader.settings()
.then(settings => {
req.settings = settings
next()
})
return rates
.catch(next)
}
function buildBalances (deviceId) {
const cryptoCodes = plugins.getCryptoCodes(deviceId)
const fiatCode = plugins.getFiatCode(deviceId)
const _balances = {}
cryptoCodes.forEach(cryptoCode => {
const balanceRec = plugins.fiatBalance(fiatCode, cryptoCode, deviceId)
if (!balanceRec) return logger.warn('No balance for ' + cryptoCode + ' yet')
if (Date.now() - balanceRec.timestamp > STALE_BALANCE) return logger.warn('Stale balance for ' + cryptoCode)
_balances[cryptoCode] = balanceRec.balance
})
return _balances
}
function poll (req, res) {
function poll (req, res, next) {
const deviceId = req.deviceId
const deviceTime = req.deviceTime
const pid = req.query.pid
const settings = req.settings
const config = configManager.machineScoped(deviceId, settings.config)
pids[deviceId] = {pid, ts: Date.now()}
let rates = {}
let balances = {}
rates = buildRates(deviceId)
balances = buildBalances(deviceId)
const config = plugins.getConfig(deviceId)
plugins.pollQueries(deviceId)
plugins.pollQueries(settings, deviceTime, deviceId, req.query)
.then(results => {
const cartridges = results.cartridges
@ -102,8 +66,8 @@ function poll (req, res) {
zeroConfLimit: config.commissions.zeroConfLimit,
fiatTxLimit: config.limits.cashOutTransactionLimit,
reboot,
rates,
balances,
rates: results.rates,
balances: results.balances,
coins: config.currencies.cryptoCurrencies
}
@ -111,32 +75,31 @@ function poll (req, res) {
response.idVerificationLimit = config.compliance.idVerificationLimit
}
res.json(response)
return res.json(response)
})
.catch(e => { console.log(e); logger.error(e) })
plugins.recordPing(deviceId, deviceTime, req.query)
.catch(logger.error)
.catch(next)
}
function trade (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
plugins.trade(req.deviceId, tx)
plugins.trade(req.settings, req.deviceId, tx)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function stateChange (req, res) {
plugins.stateChange(req.deviceId, req.deviceTime, req.body)
function stateChange (req, res, next) {
plugins.stateChange(req.settings, req.deviceId, req.deviceTime, req.body)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function send (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.sendCoins(req.deviceId, tx)
return plugins.sendCoins(req.settings, req.deviceId, tx)
.then(status => {
const body = {txId: status && status.txId}
return cacheAndRespond(req, res, body)
@ -144,33 +107,38 @@ function send (req, res, next) {
.catch(next)
}
function cashOut (req, res) {
function cashOut (req, res, next) {
logger.info({tx: req.body, cmd: 'cashOut'})
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.cashOut(req.deviceId, tx)
return plugins.cashOut(req.settings, req.deviceId, tx)
.then(cryptoAddress => cacheAndRespond(req, res, {toAddress: cryptoAddress}))
.catch(next)
}
function dispenseAck (req, res) {
plugins.dispenseAck(req.deviceId, req.body.tx)
function dispenseAck (req, res, next) {
plugins.dispenseAck(req.settings, req.deviceId, req.body.tx)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function deviceEvent (req, res) {
function deviceEvent (req, res, next) {
plugins.logEvent(req.deviceId, req.body)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function verifyUser (req, res) {
plugins.verifyUser(req.body)
function verifyUser (req, res, next) {
plugins.verifyUser(req.settings, req.body)
.then(idResult => cacheAndRespond(req, res, idResult))
.catch(next)
}
function verifyTx (req, res) {
plugins.verifyTransaction(req.body)
function verifyTx (req, res, next) {
plugins.verifyTransaction(req.settings, req.body)
.then(idResult => cacheAndRespond(req, res, idResult))
.catch(next)
}
function ca (req, res) {
@ -193,36 +161,38 @@ function pair (req, res, next) {
.catch(next)
}
function phoneCode (req, res) {
function phoneCode (req, res, next) {
const phone = req.body.phone
logger.debug('Phone code requested for: ' + phone)
return plugins.getPhoneCode(phone)
return plugins.getPhoneCode(req.settings, phone)
.then(code => cacheAndRespond(req, res, {code}))
.catch(err => {
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
throw err
})
.catch(next)
}
function updatePhone (req, res) {
function updatePhone (req, res, next) {
const notified = req.query.notified === 'true'
const tx = req.body
return plugins.updatePhone(tx, notified)
.then(r => cacheAndRespond(req, res, r))
.catch(next)
}
function fetchPhoneTx (req, res) {
function fetchPhoneTx (req, res, next) {
return plugins.fetchPhoneTx(req.query.phone)
.then(r => res.json(r))
.catch(next)
}
function registerRedeem (req, res) {
function registerRedeem (req, res, next) {
const txId = req.params.txId
return plugins.registerRedeem(txId)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function waitForDispense (req, res, next) {
@ -238,11 +208,12 @@ function waitForDispense (req, res, next) {
.catch(next)
}
function dispense (req, res) {
function dispense (req, res, next) {
const tx = req.body.tx
return plugins.requestDispense(tx)
.then(dispenseRec => cacheAndRespond(req, res, dispenseRec))
.catch(next)
}
function isUniqueViolation (err) {
@ -363,19 +334,19 @@ function init (opts) {
app.post('/pair', pair)
app.get('/ca', ca)
app.get('/poll', authMiddleware, poll)
app.get('/poll', authMiddleware, loadSettings, poll)
app.post('/trade', authMiddleware, trade)
app.post('/send', authMiddleware, send)
app.post('/state', authMiddleware, stateChange)
app.post('/cash_out', authMiddleware, cashOut)
app.post('/dispense_ack', authMiddleware, dispenseAck)
app.post('/trade', authMiddleware, loadSettings, trade)
app.post('/send', authMiddleware, loadSettings, send)
app.post('/state', authMiddleware, loadSettings, stateChange)
app.post('/cash_out', authMiddleware, loadSettings, cashOut)
app.post('/dispense_ack', authMiddleware, loadSettings, dispenseAck)
app.post('/event', authMiddleware, deviceEvent)
app.post('/verify_user', authMiddleware, verifyUser)
app.post('/verify_transaction', authMiddleware, verifyTx)
app.post('/verify_user', authMiddleware, loadSettings, verifyUser)
app.post('/verify_transaction', authMiddleware, loadSettings, verifyTx)
app.post('/phone_code', authMiddleware, phoneCode)
app.post('/phone_code', authMiddleware, loadSettings, phoneCode)
app.post('/update_phone', authMiddleware, updatePhone)
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
app.post('/register_redeem/:txId', authMiddleware, registerRedeem)