WIP
This commit is contained in:
parent
b62cd590ef
commit
5312086622
3 changed files with 127 additions and 131 deletions
139
lib/routes.js
139
lib/routes.js
|
|
@ -10,7 +10,7 @@ let lamassuConfig
|
|||
|
||||
module.exports = {
|
||||
init,
|
||||
getFingerprint
|
||||
getDeviceId
|
||||
}
|
||||
|
||||
const STALE_TICKER = 3 * 60 * 1000
|
||||
|
|
@ -57,12 +57,13 @@ function buildBalances () {
|
|||
}
|
||||
|
||||
function poll (req, res) {
|
||||
const fingerprint = getFingerprint(req)
|
||||
const deviceId = getDeviceId(req)
|
||||
const deviceTime = getDeviceTime(req)
|
||||
const pid = req.query.pid
|
||||
|
||||
pids[fingerprint] = {pid, ts: Date.now()}
|
||||
pids[deviceId] = {pid, ts: Date.now()}
|
||||
|
||||
logger.debug('poll request from: %s', fingerprint)
|
||||
logger.debug('poll request from: %s', deviceId)
|
||||
|
||||
let rates = {}
|
||||
let balances = {}
|
||||
|
|
@ -74,36 +75,36 @@ function poll (req, res) {
|
|||
const settings = config.exchanges.settings
|
||||
const complianceSettings = settings.compliance
|
||||
|
||||
plugins.pollQueries(session(req))
|
||||
.then(results => {
|
||||
const cartridges = results.cartridges
|
||||
plugins.pollQueries(deviceId)
|
||||
.then(results => {
|
||||
const cartridges = results.cartridges
|
||||
|
||||
const reboot = reboots[fingerprint] === pid
|
||||
const reboot = reboots[deviceId] === pid
|
||||
|
||||
const response = {
|
||||
err: null,
|
||||
locale: config.brain.locale,
|
||||
txLimit: parseInt(complianceSettings.maximum.limit, 10),
|
||||
idVerificationEnabled: complianceSettings.idVerificationEnabled,
|
||||
cartridges,
|
||||
twoWayMode: !!cartridges,
|
||||
zeroConfLimit: settings.zeroConfLimit,
|
||||
fiatTxLimit: settings.fiatTxLimit,
|
||||
reboot,
|
||||
rates,
|
||||
balances,
|
||||
coins: settings.coins
|
||||
}
|
||||
const response = {
|
||||
err: null,
|
||||
locale: config.brain.locale,
|
||||
txLimit: parseInt(complianceSettings.maximum.limit, 10),
|
||||
idVerificationEnabled: complianceSettings.idVerificationEnabled,
|
||||
cartridges,
|
||||
twoWayMode: !!cartridges,
|
||||
zeroConfLimit: settings.zeroConfLimit,
|
||||
fiatTxLimit: settings.fiatTxLimit,
|
||||
reboot,
|
||||
rates,
|
||||
balances,
|
||||
coins: settings.coins
|
||||
}
|
||||
|
||||
if (response.idVerificationEnabled) {
|
||||
response.idVerificationLimit = complianceSettings.idVerificationLimit
|
||||
}
|
||||
if (response.idVerificationEnabled) {
|
||||
response.idVerificationLimit = complianceSettings.idVerificationLimit
|
||||
}
|
||||
|
||||
res.json(response)
|
||||
})
|
||||
.catch(logger.error)
|
||||
res.json(response)
|
||||
})
|
||||
.catch(logger.error)
|
||||
|
||||
plugins.recordPing(session(req), req.query)
|
||||
plugins.recordPing(deviceId, deviceTime, req.query)
|
||||
.catch(logger.error)
|
||||
}
|
||||
|
||||
|
|
@ -111,7 +112,7 @@ function trade (req, res) {
|
|||
const tx = req.body
|
||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||
|
||||
plugins.trade(session(req), tx)
|
||||
plugins.trade(getDeviceId(req), tx)
|
||||
.then(() => res.status(201).json({}))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
|
|
@ -120,7 +121,7 @@ function trade (req, res) {
|
|||
}
|
||||
|
||||
function stateChange (req, res) {
|
||||
plugins.stateChange(session(req), req.body)
|
||||
plugins.stateChange(getDeviceId(req), getDeviceTime(req), req.body)
|
||||
.then(() => res.json({success: true}))
|
||||
.catch(err => {
|
||||
console.error(err)
|
||||
|
|
@ -135,7 +136,7 @@ function send (req, res) {
|
|||
// TODO: use status.statusCode here after confirming machine compatibility
|
||||
// FIX: (joshm) set txHash to status.txId instead of previous status.txHash which wasn't being set
|
||||
// Need to clean up txHash vs txId
|
||||
return plugins.sendCoins(session(req), tx)
|
||||
return plugins.sendCoins(getDeviceId(req), tx)
|
||||
.then(status => res.json({
|
||||
txHash: status && status.txHash,
|
||||
txId: status && status.txId
|
||||
|
|
@ -154,24 +155,24 @@ function cashOut (req, res) {
|
|||
const tx = req.body
|
||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||
|
||||
return plugins.cashOut(session(req), req.body)
|
||||
.then(cryptoAddress => res.json({bitcoinAddress: cryptoAddress}))
|
||||
.catch(err => {
|
||||
res.json({
|
||||
err: err.message,
|
||||
errType: err.name
|
||||
})
|
||||
logger.error(err)
|
||||
return plugins.cashOut(tx)
|
||||
.then(cryptoAddress => res.json({toAddress: cryptoAddress}))
|
||||
.catch(err => {
|
||||
res.json({
|
||||
err: err.message,
|
||||
errType: err.name
|
||||
})
|
||||
logger.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
function dispenseAck (req, res) {
|
||||
plugins.dispenseAck(session(req), req.body)
|
||||
plugins.dispenseAck(req.body)
|
||||
res.json({success: true})
|
||||
}
|
||||
|
||||
function deviceEvent (req, res) {
|
||||
plugins.logEvent(session(req), req.body)
|
||||
plugins.logEvent(getDeviceId(req), req.body)
|
||||
res.json({err: null})
|
||||
}
|
||||
|
||||
|
|
@ -207,7 +208,7 @@ function pair (req, res) {
|
|||
|
||||
lamassuConfig.pair(
|
||||
token,
|
||||
getFingerprint(req),
|
||||
getDeviceId(req),
|
||||
name,
|
||||
err => {
|
||||
if (err) {
|
||||
|
|
@ -235,7 +236,9 @@ function phoneCode (req, res) {
|
|||
|
||||
function updatePhone (req, res) {
|
||||
const notified = req.query.notified === 'true'
|
||||
return plugins.updatePhone(session(req), req.body, notified)
|
||||
const tx = req.body
|
||||
|
||||
return plugins.updatePhone(tx, notified)
|
||||
.then(r => res.json(r))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
|
|
@ -253,7 +256,8 @@ function fetchPhoneTx (req, res) {
|
|||
}
|
||||
|
||||
function registerRedeem (req, res) {
|
||||
return plugins.registerRedeem(session(req))
|
||||
const txId = req.params.txId
|
||||
return plugins.registerRedeem(txId)
|
||||
.then(() => res.json({success: true}))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
|
|
@ -263,7 +267,7 @@ function registerRedeem (req, res) {
|
|||
|
||||
function waitForDispense (req, res) {
|
||||
logger.debug('waitForDispense')
|
||||
return plugins.fetchTx(session(req))
|
||||
return plugins.fetchTx(req.params.txId)
|
||||
.then(tx => {
|
||||
logger.debug('tx fetched')
|
||||
logger.debug(tx)
|
||||
|
|
@ -279,16 +283,25 @@ function waitForDispense (req, res) {
|
|||
|
||||
function dispense (req, res) {
|
||||
const tx = req.body.tx
|
||||
const deviceId = getDeviceId(req)
|
||||
|
||||
return plugins.requestDispense(tx)
|
||||
return cachedResponse(deviceId, tx.id, req)
|
||||
.then(r => {
|
||||
// Cache hit
|
||||
if (r.body && r.body.pendingRequest) return res.sendStatus(409)
|
||||
if (r.body) res.json(r.body)
|
||||
|
||||
// No cache hit
|
||||
return plugins.requestDispense(tx)
|
||||
.then(r => {
|
||||
return cacheResponse(req, r)
|
||||
.then(() => res.json(r))
|
||||
.then(() => res.json(r))
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function init (localConfig) {
|
||||
|
|
@ -316,9 +329,9 @@ function init (localConfig) {
|
|||
app.post('/phone_code', authMiddleware, phoneCode)
|
||||
app.post('/update_phone', authMiddleware, updatePhone)
|
||||
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
|
||||
app.post('/register_redeem', authMiddleware, registerRedeem)
|
||||
app.get('/await_dispense', authMiddleware, waitForDispense)
|
||||
app.post('/dispense', authMiddleware, cachedResponse, dispense)
|
||||
app.post('/register_redeem/:txId', authMiddleware, registerRedeem)
|
||||
app.get('/await_dispense/:txId', authMiddleware, waitForDispense)
|
||||
app.post('/dispense', authMiddleware, dispense)
|
||||
|
||||
localApp.get('/pid', (req, res) => {
|
||||
const machineFingerprint = req.query.fingerprint
|
||||
|
|
@ -342,28 +355,20 @@ function init (localConfig) {
|
|||
return app
|
||||
}
|
||||
|
||||
function session (req) {
|
||||
return {
|
||||
fingerprint: getFingerprint(req),
|
||||
id: req.get('session-id'),
|
||||
deviceTime: Date.parse(req.get('date'))
|
||||
}
|
||||
function getDeviceTime (req) {
|
||||
return Date.parse(req.get('date'))
|
||||
}
|
||||
|
||||
function getFingerprint (req) {
|
||||
function getDeviceId (req) {
|
||||
return (typeof req.connection.getPeerCertificate === 'function' &&
|
||||
req.connection.getPeerCertificate().fingerprint) || 'unknown'
|
||||
}
|
||||
|
||||
function cachedResponse (req, res, next) {
|
||||
return plugins.cachedResponse(session(req), req.path, req.method)
|
||||
.then(r => {
|
||||
if (!r.body) return next()
|
||||
if (r.body.pendingRequest) return res.sendStatus(409)
|
||||
res.json(r.body)
|
||||
})
|
||||
function cachedResponse (deviceId, txId, req) {
|
||||
return plugins.cachedResponse(deviceId, txId, req.path, req.method)
|
||||
.then(r => r.body)
|
||||
}
|
||||
|
||||
function cacheResponse (req, body) {
|
||||
return plugins.cacheResponse(session(req), req.path, req.method, body)
|
||||
function cacheResponse (deviceId, txId, req, body) {
|
||||
return plugins.cacheResponse(deviceId, txId, req.path, req.method, body)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue