This commit is contained in:
Josh Harvey 2016-10-24 01:55:17 +03:00
parent 594228e871
commit defb8d0f34
7 changed files with 36 additions and 53 deletions

View file

@ -30,6 +30,8 @@ options.mock = argv.mock
console.log('DEBUG23')
process.on('unhandledRejection', err => console.log(err.stack))
createServer(options)
.then(server => {
console.log('DEBUG22')

View file

@ -28,14 +28,10 @@ module.exports = function (options) {
return configManager.load()
.then(config => {
console.log('DEBUG5: %j', config)
console.log('DEBUG8 ****************')
plugins.configure(config)
console.log('DEBUG9.1 ****************')
plugins.startPolling()
console.log('DEBUG9.2 ****************')
plugins.startCheckingNotification()
console.log('DEBUG9.3 ****************')
app.use(bodyParser.json())
@ -56,12 +52,6 @@ module.exports = function (options) {
server = https.createServer(serverOptions, app)
authMiddleware = function (req, res, next) {
next() // TODO: authentication
}
} else {
server = http.createServer(app)
authMiddleware = function (req, res, next) {
const deviceId = req.connection.getPeerCertificate().fingerprint
@ -76,13 +66,19 @@ module.exports = function (options) {
})
.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')
var localApp = express()
localApp.use(bodyParser.json())
var localServer = http.createServer({localAddress: 'localhost'}, localApp)
var localServer = http.createServer(localApp)
var localPort = 7070
console.log('DEBUG7 ****************')
@ -95,9 +91,9 @@ module.exports = function (options) {
mock: options.mock
})
localServer.listen(7070, function () {
console.log('lamassu-server is listening on local port %d', localPort)
})
// localServer.listen(7070, 'localhost', function () {
// console.log('lamassu-server is listening on local port %d', localPort)
// })
return server
})

View file

@ -36,8 +36,8 @@ function checkBalance (rec) {
}
function checkBalances () {
var balances = getBalances()
return R.reject(R.isNil, balances.map(checkBalance))
return getBalances()
.then(balances => R.reject(R.isNil, balances.map(checkBalance)))
}
function checkPing (deviceEvents) {
@ -87,13 +87,13 @@ function devicesAndEvents () {
function checkStatus () {
var alerts = {devices: {}, deviceNames: {}}
alerts.general = checkBalances()
return devicesAndEvents()
.then(function (rec) {
return Promise.all([checkBalances(), devicesAndEvents()])
.then(([balances, rec]) => {
var devices = rec.devices
var events = rec.events
alerts.general = balances
devices.forEach(function (deviceRow) {
var deviceId = deviceRow.device_id
var deviceName = deviceRow.name || deviceId

View file

@ -134,7 +134,6 @@ function pp (o) {
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, config, accounts, options,
onChangeCallback) {
pp(config)
const currentName = config.cryptoServices[pluginType]
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
@ -168,16 +167,10 @@ exports.loadOrConfigPlugin = loadOrConfigPlugin
// Note: this whole function gets called every time there's a config update
exports.configure = function configure (config) {
console.log('DEBUG40')
console.log('DEBUG4: %j', config)
cachedConfig = config
console.log('DEBUG41')
const accounts = configManager.loadAccounts()
console.log('DEBUG42')
const cryptoCodes = getCryptoCodes()
console.log('DEBUG30')
@ -216,7 +209,6 @@ exports.configure = function configure (config) {
accounts,
{masterSeed: cryptoSeed},
function onWalletChange (newWallet) {
console.log('DEBUG34: %s, %j', cryptoCode, newWallet)
walletPlugins[cryptoCode] = newWallet
pollBalance(cryptoCode)
}
@ -239,8 +231,6 @@ exports.configure = function configure (config) {
)
})
console.log('DEBUG32')
const unscopedCfg = configManager.unscoped(cachedConfig)
// ID VERIFIER [optional] configure (or load)
@ -577,7 +567,6 @@ function pollBalance (cryptoCode, cb) {
function pollRate (cryptoCode, cb) {
const tickerPlugin = tickerPlugins[cryptoCode]
pp(tickerPlugins)
logger.debug('[%s] polling for rates (%s)', cryptoCode, tickerPlugin.NAME)
let currencies = deviceCurrency
@ -710,8 +699,6 @@ exports.verifyTx = function verifyTx (data, cb) {
function sendMessage (rec) {
const pluginPromises = []
const config = configManager.unscoped(cachedConfig)
console.log('DEBUG35')
pp(config)
if (!config.notifications.notificationsEnabled) return Promise.all([])
@ -789,19 +776,25 @@ exports.getCryptoCodes = getCryptoCodes
function getAllCryptoCodes () {
return db.devices()
.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 () {
return Promise.all(getAllCryptoCodes(), db.devices())
return Promise.all([getAllCryptoCodes(), db.devices()])
.then(arr => {
const cryptoCodes = arr[0]
const deviceIds = arr[1].map(r => r.device_id)
const balances = []
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)
const rec = {fiatBalance: minBalance, cryptoCode, fiatCode: deviceCurrency}
@ -814,12 +807,9 @@ function checkBalances () {
exports.startCheckingNotification = function startCheckingNotification () {
const config = configManager.unscoped(cachedConfig)
return checkBalances()
.then(balances => {
notifier.init(db, balances, config.notifications)
checkNotification()
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
})
notifier.init(db, checkBalances, config.notifications)
checkNotification()
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
}
exports.getPhoneCode = function getPhoneCode (phone) {

View file

@ -279,10 +279,10 @@ exports.machineEvent = function machineEvent (rec) {
}
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()
return db.any(sql, [true])
return db.any(sql)
}
exports.machineEvents = function machineEvents () {

View file

@ -95,8 +95,6 @@ function poll (req, res) {
}
}
console.log('DEBUG34')
const response = {
err: null,
locale,
@ -112,14 +110,10 @@ function poll (req, res) {
coins: config.currencies.cryptos
}
console.log('DEBUG35')
if (response.idVerificationEnabled) {
response.idVerificationLimit = config.compliance.idVerificationLimit
}
console.log('DEBUG32')
res.json(response)
})
.catch(e => { console.log(e); logger.error(e) })
@ -223,7 +217,7 @@ function verifyTx (req, res) {
}
function ca (req, res) {
const token = req.body.token
const token = req.query.token
return pair.authorizeCaDownload(token)
.then(valid => {
@ -234,7 +228,7 @@ function ca (req, res) {
}
function pair (req, res) {
const token = req.body.token
const token = req.query.token
const deviceId = getDeviceId(req)
return pair.pair(token, deviceId)
@ -349,7 +343,7 @@ function init (opts) {
const localApp = opts.localApp
app.post('/pair', pair)
app.post('/ca', ca)
app.get('/ca', ca)
app.get('/poll', authMiddleware, poll)

View file

@ -37,6 +37,7 @@
"numeral": "^1.5.3",
"pg": "^4.5.5",
"pg-promise": "^4.3.3",
"pify": "^2.3.0",
"pretty-ms": "^2.1.0",
"ramda": "^0.21.0",
"u-promised": "^0.2.4",