WIP
This commit is contained in:
parent
594228e871
commit
defb8d0f34
7 changed files with 36 additions and 53 deletions
|
|
@ -30,6 +30,8 @@ options.mock = argv.mock
|
||||||
|
|
||||||
console.log('DEBUG23')
|
console.log('DEBUG23')
|
||||||
|
|
||||||
|
process.on('unhandledRejection', err => console.log(err.stack))
|
||||||
|
|
||||||
createServer(options)
|
createServer(options)
|
||||||
.then(server => {
|
.then(server => {
|
||||||
console.log('DEBUG22')
|
console.log('DEBUG22')
|
||||||
|
|
|
||||||
26
lib/app.js
26
lib/app.js
|
|
@ -28,14 +28,10 @@ module.exports = function (options) {
|
||||||
|
|
||||||
return configManager.load()
|
return configManager.load()
|
||||||
.then(config => {
|
.then(config => {
|
||||||
console.log('DEBUG5: %j', config)
|
|
||||||
console.log('DEBUG8 ****************')
|
|
||||||
|
|
||||||
plugins.configure(config)
|
plugins.configure(config)
|
||||||
console.log('DEBUG9.1 ****************')
|
|
||||||
plugins.startPolling()
|
plugins.startPolling()
|
||||||
console.log('DEBUG9.2 ****************')
|
|
||||||
plugins.startCheckingNotification()
|
plugins.startCheckingNotification()
|
||||||
|
|
||||||
console.log('DEBUG9.3 ****************')
|
console.log('DEBUG9.3 ****************')
|
||||||
|
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
|
|
@ -56,12 +52,6 @@ module.exports = function (options) {
|
||||||
|
|
||||||
server = https.createServer(serverOptions, app)
|
server = https.createServer(serverOptions, app)
|
||||||
|
|
||||||
authMiddleware = function (req, res, next) {
|
|
||||||
next() // TODO: authentication
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
server = http.createServer(app)
|
|
||||||
|
|
||||||
authMiddleware = function (req, res, next) {
|
authMiddleware = function (req, res, next) {
|
||||||
const deviceId = req.connection.getPeerCertificate().fingerprint
|
const deviceId = req.connection.getPeerCertificate().fingerprint
|
||||||
|
|
||||||
|
|
@ -76,13 +66,19 @@ module.exports = function (options) {
|
||||||
})
|
})
|
||||||
.catch(e => res.status(403).end())
|
.catch(e => res.status(403).end())
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
server = http.createServer(app)
|
||||||
|
|
||||||
|
authMiddleware = function (req, res, next) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.mock) logger.info('In mock mode')
|
if (options.mock) logger.info('In mock mode')
|
||||||
|
|
||||||
var localApp = express()
|
var localApp = express()
|
||||||
localApp.use(bodyParser.json())
|
localApp.use(bodyParser.json())
|
||||||
var localServer = http.createServer({localAddress: 'localhost'}, localApp)
|
var localServer = http.createServer(localApp)
|
||||||
var localPort = 7070
|
var localPort = 7070
|
||||||
|
|
||||||
console.log('DEBUG7 ****************')
|
console.log('DEBUG7 ****************')
|
||||||
|
|
@ -95,9 +91,9 @@ module.exports = function (options) {
|
||||||
mock: options.mock
|
mock: options.mock
|
||||||
})
|
})
|
||||||
|
|
||||||
localServer.listen(7070, function () {
|
// localServer.listen(7070, 'localhost', function () {
|
||||||
console.log('lamassu-server is listening on local port %d', localPort)
|
// console.log('lamassu-server is listening on local port %d', localPort)
|
||||||
})
|
// })
|
||||||
|
|
||||||
return server
|
return server
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,8 @@ function checkBalance (rec) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkBalances () {
|
function checkBalances () {
|
||||||
var balances = getBalances()
|
return getBalances()
|
||||||
return R.reject(R.isNil, balances.map(checkBalance))
|
.then(balances => R.reject(R.isNil, balances.map(checkBalance)))
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkPing (deviceEvents) {
|
function checkPing (deviceEvents) {
|
||||||
|
|
@ -87,13 +87,13 @@ function devicesAndEvents () {
|
||||||
|
|
||||||
function checkStatus () {
|
function checkStatus () {
|
||||||
var alerts = {devices: {}, deviceNames: {}}
|
var alerts = {devices: {}, deviceNames: {}}
|
||||||
alerts.general = checkBalances()
|
|
||||||
|
|
||||||
return devicesAndEvents()
|
return Promise.all([checkBalances(), devicesAndEvents()])
|
||||||
.then(function (rec) {
|
.then(([balances, rec]) => {
|
||||||
var devices = rec.devices
|
var devices = rec.devices
|
||||||
var events = rec.events
|
var events = rec.events
|
||||||
|
|
||||||
|
alerts.general = balances
|
||||||
devices.forEach(function (deviceRow) {
|
devices.forEach(function (deviceRow) {
|
||||||
var deviceId = deviceRow.device_id
|
var deviceId = deviceRow.device_id
|
||||||
var deviceName = deviceRow.name || deviceId
|
var deviceName = deviceRow.name || deviceId
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,6 @@ function pp (o) {
|
||||||
|
|
||||||
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, config, accounts, options,
|
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, config, accounts, options,
|
||||||
onChangeCallback) {
|
onChangeCallback) {
|
||||||
pp(config)
|
|
||||||
const currentName = config.cryptoServices[pluginType]
|
const currentName = config.cryptoServices[pluginType]
|
||||||
|
|
||||||
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
|
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
|
||||||
|
|
@ -168,16 +167,10 @@ exports.loadOrConfigPlugin = loadOrConfigPlugin
|
||||||
|
|
||||||
// Note: this whole function gets called every time there's a config update
|
// Note: this whole function gets called every time there's a config update
|
||||||
exports.configure = function configure (config) {
|
exports.configure = function configure (config) {
|
||||||
console.log('DEBUG40')
|
|
||||||
console.log('DEBUG4: %j', config)
|
|
||||||
|
|
||||||
cachedConfig = config
|
cachedConfig = config
|
||||||
|
|
||||||
console.log('DEBUG41')
|
|
||||||
|
|
||||||
const accounts = configManager.loadAccounts()
|
const accounts = configManager.loadAccounts()
|
||||||
|
|
||||||
console.log('DEBUG42')
|
|
||||||
const cryptoCodes = getCryptoCodes()
|
const cryptoCodes = getCryptoCodes()
|
||||||
|
|
||||||
console.log('DEBUG30')
|
console.log('DEBUG30')
|
||||||
|
|
@ -216,7 +209,6 @@ exports.configure = function configure (config) {
|
||||||
accounts,
|
accounts,
|
||||||
{masterSeed: cryptoSeed},
|
{masterSeed: cryptoSeed},
|
||||||
function onWalletChange (newWallet) {
|
function onWalletChange (newWallet) {
|
||||||
console.log('DEBUG34: %s, %j', cryptoCode, newWallet)
|
|
||||||
walletPlugins[cryptoCode] = newWallet
|
walletPlugins[cryptoCode] = newWallet
|
||||||
pollBalance(cryptoCode)
|
pollBalance(cryptoCode)
|
||||||
}
|
}
|
||||||
|
|
@ -239,8 +231,6 @@ exports.configure = function configure (config) {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('DEBUG32')
|
|
||||||
|
|
||||||
const unscopedCfg = configManager.unscoped(cachedConfig)
|
const unscopedCfg = configManager.unscoped(cachedConfig)
|
||||||
|
|
||||||
// ID VERIFIER [optional] configure (or load)
|
// ID VERIFIER [optional] configure (or load)
|
||||||
|
|
@ -577,7 +567,6 @@ function pollBalance (cryptoCode, cb) {
|
||||||
|
|
||||||
function pollRate (cryptoCode, cb) {
|
function pollRate (cryptoCode, cb) {
|
||||||
const tickerPlugin = tickerPlugins[cryptoCode]
|
const tickerPlugin = tickerPlugins[cryptoCode]
|
||||||
pp(tickerPlugins)
|
|
||||||
logger.debug('[%s] polling for rates (%s)', cryptoCode, tickerPlugin.NAME)
|
logger.debug('[%s] polling for rates (%s)', cryptoCode, tickerPlugin.NAME)
|
||||||
|
|
||||||
let currencies = deviceCurrency
|
let currencies = deviceCurrency
|
||||||
|
|
@ -710,8 +699,6 @@ exports.verifyTx = function verifyTx (data, cb) {
|
||||||
function sendMessage (rec) {
|
function sendMessage (rec) {
|
||||||
const pluginPromises = []
|
const pluginPromises = []
|
||||||
const config = configManager.unscoped(cachedConfig)
|
const config = configManager.unscoped(cachedConfig)
|
||||||
console.log('DEBUG35')
|
|
||||||
pp(config)
|
|
||||||
|
|
||||||
if (!config.notifications.notificationsEnabled) return Promise.all([])
|
if (!config.notifications.notificationsEnabled) return Promise.all([])
|
||||||
|
|
||||||
|
|
@ -789,19 +776,25 @@ exports.getCryptoCodes = getCryptoCodes
|
||||||
function getAllCryptoCodes () {
|
function getAllCryptoCodes () {
|
||||||
return db.devices()
|
return db.devices()
|
||||||
.then(rows => {
|
.then(rows => {
|
||||||
return rows.reduce((acc, r) => getCryptoCodes(r.device_id).forEach(c => acc.add(c)), new Set())
|
return rows.reduce((acc, r) => {
|
||||||
|
getCryptoCodes(r.device_id).forEach(c => acc.add(c))
|
||||||
|
return acc
|
||||||
|
}, new Set())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkBalances () {
|
function checkBalances () {
|
||||||
return Promise.all(getAllCryptoCodes(), db.devices())
|
return Promise.all([getAllCryptoCodes(), db.devices()])
|
||||||
.then(arr => {
|
.then(arr => {
|
||||||
const cryptoCodes = arr[0]
|
const cryptoCodes = arr[0]
|
||||||
const deviceIds = arr[1].map(r => r.device_id)
|
const deviceIds = arr[1].map(r => r.device_id)
|
||||||
const balances = []
|
const balances = []
|
||||||
|
|
||||||
cryptoCodes.forEach(cryptoCode => {
|
cryptoCodes.forEach(cryptoCode => {
|
||||||
const minBalance = deviceIds.map(deviceId => exports.fiatBalance(cryptoCode, deviceId).balance)
|
const minBalance = deviceIds.map(deviceId => {
|
||||||
|
const fiatBalanceRec = exports.fiatBalance(cryptoCode, deviceId)
|
||||||
|
return fiatBalanceRec ? fiatBalanceRec.balance : Infinity
|
||||||
|
})
|
||||||
.reduce((min, cur) => Math.min(min, cur), Infinity)
|
.reduce((min, cur) => Math.min(min, cur), Infinity)
|
||||||
|
|
||||||
const rec = {fiatBalance: minBalance, cryptoCode, fiatCode: deviceCurrency}
|
const rec = {fiatBalance: minBalance, cryptoCode, fiatCode: deviceCurrency}
|
||||||
|
|
@ -814,12 +807,9 @@ function checkBalances () {
|
||||||
|
|
||||||
exports.startCheckingNotification = function startCheckingNotification () {
|
exports.startCheckingNotification = function startCheckingNotification () {
|
||||||
const config = configManager.unscoped(cachedConfig)
|
const config = configManager.unscoped(cachedConfig)
|
||||||
return checkBalances()
|
notifier.init(db, checkBalances, config.notifications)
|
||||||
.then(balances => {
|
checkNotification()
|
||||||
notifier.init(db, balances, config.notifications)
|
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
|
||||||
checkNotification()
|
|
||||||
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getPhoneCode = function getPhoneCode (phone) {
|
exports.getPhoneCode = function getPhoneCode (phone) {
|
||||||
|
|
|
||||||
|
|
@ -279,10 +279,10 @@ exports.machineEvent = function machineEvent (rec) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.devices = function devices () {
|
exports.devices = function devices () {
|
||||||
const sql = 'SELECT device_id, name FROM devices WHERE authorized=$1'
|
const sql = 'SELECT device_id, name FROM devices'
|
||||||
const db = connect()
|
const db = connect()
|
||||||
|
|
||||||
return db.any(sql, [true])
|
return db.any(sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.machineEvents = function machineEvents () {
|
exports.machineEvents = function machineEvents () {
|
||||||
|
|
|
||||||
|
|
@ -95,8 +95,6 @@ function poll (req, res) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('DEBUG34')
|
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
err: null,
|
err: null,
|
||||||
locale,
|
locale,
|
||||||
|
|
@ -112,14 +110,10 @@ function poll (req, res) {
|
||||||
coins: config.currencies.cryptos
|
coins: config.currencies.cryptos
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('DEBUG35')
|
|
||||||
|
|
||||||
if (response.idVerificationEnabled) {
|
if (response.idVerificationEnabled) {
|
||||||
response.idVerificationLimit = config.compliance.idVerificationLimit
|
response.idVerificationLimit = config.compliance.idVerificationLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('DEBUG32')
|
|
||||||
|
|
||||||
res.json(response)
|
res.json(response)
|
||||||
})
|
})
|
||||||
.catch(e => { console.log(e); logger.error(e) })
|
.catch(e => { console.log(e); logger.error(e) })
|
||||||
|
|
@ -223,7 +217,7 @@ function verifyTx (req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function ca (req, res) {
|
function ca (req, res) {
|
||||||
const token = req.body.token
|
const token = req.query.token
|
||||||
|
|
||||||
return pair.authorizeCaDownload(token)
|
return pair.authorizeCaDownload(token)
|
||||||
.then(valid => {
|
.then(valid => {
|
||||||
|
|
@ -234,7 +228,7 @@ function ca (req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function pair (req, res) {
|
function pair (req, res) {
|
||||||
const token = req.body.token
|
const token = req.query.token
|
||||||
const deviceId = getDeviceId(req)
|
const deviceId = getDeviceId(req)
|
||||||
|
|
||||||
return pair.pair(token, deviceId)
|
return pair.pair(token, deviceId)
|
||||||
|
|
@ -349,7 +343,7 @@ function init (opts) {
|
||||||
const localApp = opts.localApp
|
const localApp = opts.localApp
|
||||||
|
|
||||||
app.post('/pair', pair)
|
app.post('/pair', pair)
|
||||||
app.post('/ca', ca)
|
app.get('/ca', ca)
|
||||||
|
|
||||||
app.get('/poll', authMiddleware, poll)
|
app.get('/poll', authMiddleware, poll)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@
|
||||||
"numeral": "^1.5.3",
|
"numeral": "^1.5.3",
|
||||||
"pg": "^4.5.5",
|
"pg": "^4.5.5",
|
||||||
"pg-promise": "^4.3.3",
|
"pg-promise": "^4.3.3",
|
||||||
|
"pify": "^2.3.0",
|
||||||
"pretty-ms": "^2.1.0",
|
"pretty-ms": "^2.1.0",
|
||||||
"ramda": "^0.21.0",
|
"ramda": "^0.21.0",
|
||||||
"u-promised": "^0.2.4",
|
"u-promised": "^0.2.4",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue