merge lightning
This commit is contained in:
commit
6fa6ac1647
92 changed files with 2688 additions and 2651 deletions
251
lib/routes.js
251
lib/routes.js
|
|
@ -49,62 +49,61 @@ function poll (req, res, next) {
|
|||
pids[deviceId] = {pid, ts: Date.now()}
|
||||
|
||||
return pi.pollQueries(serialNumber, deviceTime, req.query)
|
||||
.then(results => {
|
||||
const cassettes = results.cassettes
|
||||
.then(results => {
|
||||
const cassettes = results.cassettes
|
||||
|
||||
const reboot = pid && reboots[deviceId] && reboots[deviceId] === pid
|
||||
const langs = config.machineLanguages
|
||||
const reboot = pid && reboots[deviceId] && reboots[deviceId] === pid
|
||||
const langs = config.machineLanguages
|
||||
|
||||
const locale = {
|
||||
fiatCode: config.fiatCurrency,
|
||||
localeInfo: {
|
||||
primaryLocale: langs[0],
|
||||
primaryLocales: langs,
|
||||
country: config.country
|
||||
const locale = {
|
||||
fiatCode: config.fiatCurrency,
|
||||
localeInfo: {
|
||||
primaryLocale: langs[0],
|
||||
primaryLocales: langs,
|
||||
country: config.country
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const response = {
|
||||
error: null,
|
||||
locale,
|
||||
txLimit: config.cashInTransactionLimit,
|
||||
idVerificationEnabled: config.idVerificationEnabled,
|
||||
smsVerificationActive: config.smsVerificationActive,
|
||||
smsVerificationThreshold: config.smsVerificationThreshold,
|
||||
hardLimitVerificationActive: config.hardLimitVerificationActive,
|
||||
hardLimitVerificationThreshold: config.hardLimitVerificationThreshold,
|
||||
idCardDataVerificationActive: config.idCardDataVerificationActive,
|
||||
idCardDataVerificationThreshold: config.idCardDataVerificationThreshold,
|
||||
idCardPhotoVerificationActive: config.idCardPhotoVerificationActive,
|
||||
idCardPhotoVerificationThreshold: config.idCardPhotoVerificationThreshold,
|
||||
sanctionsVerificationActive: config.sanctionsVerificationActive,
|
||||
sanctionsVerificationThreshold: config.sanctionsVerificationThreshold,
|
||||
crossRefVerificationActive: config.crossRefVerificationActive,
|
||||
crossRefVerificationThreshold: config.crossRefVerificationThreshold,
|
||||
frontCameraVerificationActive: config.frontCameraVerificationActive,
|
||||
frontCameraVerificationThreshold: config.frontCameraVerificationThreshold,
|
||||
cassettes,
|
||||
twoWayMode: config.cashOutEnabled,
|
||||
zeroConfLimit: config.zeroConfLimit,
|
||||
reboot,
|
||||
hasLightning
|
||||
}
|
||||
const response = {
|
||||
error: null,
|
||||
locale,
|
||||
txLimit: config.cashInTransactionLimit,
|
||||
idVerificationEnabled: config.idVerificationEnabled,
|
||||
smsVerificationActive: config.smsVerificationActive,
|
||||
smsVerificationThreshold: config.smsVerificationThreshold,
|
||||
hardLimitVerificationActive: config.hardLimitVerificationActive,
|
||||
hardLimitVerificationThreshold: config.hardLimitVerificationThreshold,
|
||||
idCardDataVerificationActive: config.idCardDataVerificationActive,
|
||||
idCardDataVerificationThreshold: config.idCardDataVerificationThreshold,
|
||||
idCardPhotoVerificationActive: config.idCardPhotoVerificationActive,
|
||||
idCardPhotoVerificationThreshold: config.idCardPhotoVerificationThreshold,
|
||||
sanctionsVerificationActive: config.sanctionsVerificationActive,
|
||||
sanctionsVerificationThreshold: config.sanctionsVerificationThreshold,
|
||||
crossRefVerificationActive: config.crossRefVerificationActive,
|
||||
crossRefVerificationThreshold: config.crossRefVerificationThreshold,
|
||||
frontCameraVerificationActive: config.frontCameraVerificationActive,
|
||||
frontCameraVerificationThreshold: config.frontCameraVerificationThreshold,
|
||||
cassettes,
|
||||
twoWayMode: config.cashOutEnabled,
|
||||
zeroConfLimit: config.zeroConfLimit,
|
||||
reboot,
|
||||
hasLightning
|
||||
}
|
||||
|
||||
if (response.idVerificationEnabled) {
|
||||
response.idVerificationLimit = config.idVerificationLimit
|
||||
}
|
||||
if (response.idVerificationEnabled) {
|
||||
response.idVerificationLimit = config.idVerificationLimit
|
||||
}
|
||||
|
||||
console.log(response)
|
||||
return res.json(_.assign(response, results))
|
||||
})
|
||||
.catch(next)
|
||||
return res.json(_.assign(response, results))
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function getTx (req, res, next) {
|
||||
if (req.query.status) {
|
||||
return helpers.fetchStatusTx(req.params.id, req.query.status)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
return next(httpError('Not Found', 404))
|
||||
|
|
@ -113,8 +112,8 @@ function getTx (req, res, next) {
|
|||
function getPhoneTx (req, res, next) {
|
||||
if (req.query.phone) {
|
||||
return helpers.fetchPhoneTx(req.query.phone)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
return next(httpError('Not Found', 404))
|
||||
|
|
@ -124,87 +123,99 @@ function postTx (req, res, next) {
|
|||
const pi = plugins(req.settings, req.deviceId)
|
||||
|
||||
return Tx.post(_.set('deviceId', req.deviceId, req.body), pi)
|
||||
.then(tx => {
|
||||
if (tx.errorCode) {
|
||||
logger.error(tx.error)
|
||||
throw httpError(tx.error, 500)
|
||||
}
|
||||
.then(tx => {
|
||||
if (tx.errorCode) {
|
||||
logger.error(tx.error)
|
||||
throw httpError(tx.error, 500)
|
||||
}
|
||||
|
||||
return res.json(tx)
|
||||
})
|
||||
.catch(err => {
|
||||
if (err instanceof E.StaleTxError) return res.status(409).json({})
|
||||
if (err instanceof E.RatchetError) return res.status(409).json({})
|
||||
return res.json(tx)
|
||||
})
|
||||
.catch(err => {
|
||||
if (err instanceof E.StaleTxError) return res.status(409).json({})
|
||||
if (err instanceof E.RatchetError) return res.status(409).json({})
|
||||
|
||||
throw err
|
||||
})
|
||||
.catch(next)
|
||||
throw err
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function stateChange (req, res, next) {
|
||||
helpers.stateChange(req.deviceId, req.deviceTime, req.body)
|
||||
.then(() => respond(req, res))
|
||||
.catch(next)
|
||||
.then(() => respond(req, res))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function deviceEvent (req, res, next) {
|
||||
const pi = plugins(req.settings, req.deviceId)
|
||||
pi.logEvent(req.body)
|
||||
.then(() => respond(req, res))
|
||||
.catch(next)
|
||||
.then(() => respond(req, res))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function verifyUser (req, res, next) {
|
||||
const pi = plugins(req.settings, req.deviceId)
|
||||
pi.verifyUser(req.body)
|
||||
.then(idResult => respond(req, res, idResult))
|
||||
.catch(next)
|
||||
.then(idResult => respond(req, res, idResult))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function verifyTx (req, res, next) {
|
||||
const pi = plugins(req.settings, req.deviceId)
|
||||
pi.verifyTransaction(req.body)
|
||||
.then(idResult => respond(req, res, idResult))
|
||||
.catch(next)
|
||||
.then(idResult => respond(req, res, idResult))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function getCustomerWithPhoneCode (req, res, next) {
|
||||
const pi = plugins(req.settings, req.deviceId)
|
||||
const phone = req.body.phone
|
||||
return pi.getPhoneCode(phone)
|
||||
.then(code => {
|
||||
return customers.get(phone)
|
||||
.then(customer => {
|
||||
if (customer) return respond(req, res, {code, customer})
|
||||
return customers.add(req.body)
|
||||
.then(customer => respond(req, res, {code, customer}))
|
||||
.then(code => {
|
||||
return customers.get(phone)
|
||||
.then(customer => {
|
||||
if (customer) return respond(req, res, {code, customer})
|
||||
return customers.add(req.body)
|
||||
.then(customer => respond(req, res, {code, customer}))
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
|
||||
throw err
|
||||
})
|
||||
.catch(next)
|
||||
.catch(err => {
|
||||
if (err.name === 'BadNumberError') throw httpError('Bad number', 401)
|
||||
throw err
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function updateCustomer (req, res, next) {
|
||||
const id = req.params.id
|
||||
const patch = req.body
|
||||
customers.getById(id)
|
||||
.then(customer => {
|
||||
if (!customer) { throw httpError('Not Found', 404) }
|
||||
return customers.update(id, patch)
|
||||
})
|
||||
.then(customer => respond(req, res, {customer}))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function getLastSeen (req, res, next) {
|
||||
return logs.getLastSeen(req.deviceId)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function updateLogs (req, res, next) {
|
||||
return logs.update(req.deviceId, req.body.logs)
|
||||
.then(status => res.json({success: status}))
|
||||
.catch(next)
|
||||
.then(status => res.json({success: status}))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function ca (req, res) {
|
||||
const token = req.query.token
|
||||
|
||||
return pairing.authorizeCaDownload(token)
|
||||
.then(ca => res.json({ca}))
|
||||
.catch(() => res.status(403).json({error: 'forbidden'}))
|
||||
.then(ca => res.json({ca}))
|
||||
.catch(() => res.status(403).json({error: 'forbidden'}))
|
||||
}
|
||||
|
||||
function pair (req, res, next) {
|
||||
|
|
@ -213,21 +224,21 @@ function pair (req, res, next) {
|
|||
const model = req.query.model
|
||||
|
||||
return pairing.pair(token, deviceId, model)
|
||||
.then(valid => {
|
||||
if (valid) {
|
||||
return helpers.updateMachineDefaults(deviceId)
|
||||
.then(() => res.json({status: 'paired'}))
|
||||
}
|
||||
.then(valid => {
|
||||
if (valid) {
|
||||
return helpers.updateMachineDefaults(deviceId)
|
||||
.then(() => res.json({status: 'paired'}))
|
||||
}
|
||||
|
||||
throw httpError('Pairing failed')
|
||||
})
|
||||
.catch(next)
|
||||
throw httpError('Pairing failed')
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function errorHandler (err, req, res, next) {
|
||||
const statusCode = err.name === 'HTTPError'
|
||||
? err.code || 500
|
||||
: 500
|
||||
? err.code || 500
|
||||
: 500
|
||||
|
||||
const json = {error: err.message}
|
||||
|
||||
|
|
@ -267,15 +278,15 @@ function authorize (req, res, next) {
|
|||
const deviceId = req.deviceId
|
||||
|
||||
return pairing.isPaired(deviceId)
|
||||
.then(r => {
|
||||
if (r) {
|
||||
req.deviceId = deviceId
|
||||
return next()
|
||||
}
|
||||
.then(r => {
|
||||
if (r) {
|
||||
req.deviceId = deviceId
|
||||
return next()
|
||||
}
|
||||
|
||||
return res.status(403).json({error: 'Forbidden'})
|
||||
})
|
||||
.catch(next)
|
||||
return res.status(403).json({error: 'Forbidden'})
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
const skip = (req, res) => _.includes(req.path, ['/poll', '/state', '/logs']) &&
|
||||
|
|
@ -314,6 +325,8 @@ app.post('/verify_user', verifyUser)
|
|||
app.post('/verify_transaction', verifyTx)
|
||||
|
||||
app.post('/phone_code', getCustomerWithPhoneCode)
|
||||
app.patch('/customer/:id', updateCustomer)
|
||||
|
||||
app.post('/tx', postTx)
|
||||
app.get('/tx/:id', getTx)
|
||||
app.get('/tx', getPhoneTx)
|
||||
|
|
@ -345,12 +358,12 @@ localApp.post('/reboot', (req, res) => {
|
|||
|
||||
localApp.post('/dbChange', (req, res, next) => {
|
||||
return settingsLoader.loadLatest()
|
||||
.then(poller.reload)
|
||||
.then(() => logger.info('Config reloaded'))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
.then(poller.reload)
|
||||
.then(() => logger.info('Config reloaded'))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
})
|
||||
|
||||
function sha256 (buf) {
|
||||
|
|
@ -363,8 +376,8 @@ function sha256 (buf) {
|
|||
|
||||
function populateDeviceId (req, res, next) {
|
||||
const deviceId = _.isFunction(req.connection.getPeerCertificate)
|
||||
? sha256(req.connection.getPeerCertificate().raw)
|
||||
: null
|
||||
? sha256(req.connection.getPeerCertificate().raw)
|
||||
: null
|
||||
|
||||
req.deviceId = deviceId
|
||||
req.deviceTime = req.get('date')
|
||||
|
|
@ -382,16 +395,16 @@ function populateSettings (req, res, next) {
|
|||
|
||||
if (!versionId) {
|
||||
return settingsLoader.loadLatest()
|
||||
.then(settings => { req.settings = settings })
|
||||
.then(() => next())
|
||||
.catch(next)
|
||||
.then(settings => { req.settings = settings })
|
||||
.then(() => next())
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
settingsLoader.load(versionId)
|
||||
.then(settings => { req.settings = settings })
|
||||
.then(() => helpers.updateDeviceConfigVersion(versionId))
|
||||
.then(() => next())
|
||||
.catch(next)
|
||||
.then(settings => { req.settings = settings })
|
||||
.then(() => helpers.updateDeviceConfigVersion(versionId))
|
||||
.then(() => next())
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
module.exports = {app, localApp}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue