move to sha-256 fingerprints

This commit is contained in:
Josh Harvey 2017-04-24 14:08:12 +03:00
parent 8674816d27
commit 91e1077daa
4 changed files with 39 additions and 65 deletions

View file

@ -19,7 +19,7 @@ module.exports = {
const mapValuesWithKey = _.mapValues.convert({cap: false})
const UPDATEABLE_FIELDS = ['txHash', 'status', 'dispense', 'notified', 'redeem',
'phone', 'error', 'confirmationTime', 'swept']
'phone', 'error', 'swept']
const STALE_INCOMING_TX_AGE = T.week
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
@ -203,6 +203,21 @@ function nextHd (isHd, tx) {
.then(row => _.set('hdIndex', row.hd_index, tx))
}
function updateCassettes (tx) {
const sql = `update devices set
cassette1 = cassette1 - $1,
cassette2 = cassette2 - $2
where device_id = $3`
const values = [
tx.bills[0].dispensed + tx.bills[0].rejected,
tx.bills[1].dispensed + tx.bills[1].rejected,
tx.deviceId
]
return db.none(sql, values)
}
function preProcess (oldTx, newTx, pi) {
if (!oldTx) {
return pi.isHd(newTx)
@ -226,14 +241,18 @@ function preProcess (oldTx, newTx, pi) {
if (!oldTx) return updatedTx
if (updatedTx.status !== oldTx.status) {
return logAction(updatedTx.status, {}, updatedTx)
const rec = {
to_address: updatedTx.toAddress,
tx_hash: updatedTx.txHash
}
return logAction(updatedTx.status, rec, updatedTx)
}
console.log('DEBUG120: %j', [oldTx, updatedTx])
if (!oldTx.dispenseConfirmed && updatedTx.dispenseConfirmed) {
console.log('DEBUG121')
return logDispense(updatedTx)
.then(pi.updateCassettes(updatedTx))
.then(updateCassettes(updatedTx))
}
if (!oldTx.phone && newTx.phone) {
@ -249,9 +268,10 @@ function preProcess (oldTx, newTx, pi) {
}
function postProcess (txVector, pi) {
const [, newTx] = txVector
const [oldTx, newTx] = txVector
if (newTx.dispense && !newTx.bills) {
if (newTx.dispense && !oldTx.dispense) {
console.log('DEBUG130')
return pi.buildCassettes()
.then(cassettes => {
pi.sell(newTx)
@ -277,12 +297,7 @@ function postProcess (txVector, pi) {
}
function updateStatus (oldTx, newTx) {
const tx = _.set('status', ratchetStatus(oldTx.status, newTx.status), newTx)
const isConfirmed = _.includes(tx.status, ['instant', 'confirmed'])
if (tx.status === oldTx.status || !isConfirmed) return tx
return _.set('confirmationTime', 'now()^', tx)
return _.set('status', ratchetStatus(oldTx.status, newTx.status), newTx)
}
function ratchetStatus (oldStatus, newStatus) {

View file

@ -15,7 +15,6 @@ const wallet = require('./wallet')
const exchange = require('./exchange')
const sms = require('./sms')
const email = require('./email')
const settingsLoader = require('./settings-loader')
const mapValuesWithKey = _.mapValues.convert({cap: false})
@ -510,56 +509,6 @@ function plugins (settings, deviceId) {
.catch(err => logger.error(err))
}
function updateCassettes (tx) {
// Note: This is the only place we update config from outside admin,
// so should be safe even though it's not an atomic operation.
//
// However, we should make all config changes atomic in the future.
const config = configManager.machineScoped(deviceId, settings.config)
const topCashOutDenomination = config.topCashOutDenomination -
(tx.bills[0].dispensed + tx.bills[0].rejected)
const bottomCashOutDenomination = config.bottomCashOutDenomination -
(tx.bills[1].dispensed + tx.bills[1].rejected)
const newFields = [
{
fieldLocator: {
fieldScope: {
crypto: 'global',
machine: deviceId
},
code: 'topCashOutDenomination',
fieldType: 'integer',
fieldClass: null
},
fieldValue: {
fieldType: 'integer',
value: topCashOutDenomination
}
},
{
fieldLocator: {
fieldScope: {
crypto: 'global',
machine: deviceId
},
code: 'bottomCashOutDenomination',
fieldType: 'integer',
fieldClass: null
},
fieldValue: {
fieldType: 'integer',
value: bottomCashOutDenomination
}
}
]
return settingsLoader.loadLatest()
.then(settings => {
return settingsLoader.save({config: settingsLoader.mergeValues(settings.config, newFields)})
})
}
return {
pollQueries,
sendCoins,
@ -576,7 +525,6 @@ function plugins (settings, deviceId) {
sendMessage,
checkBalances,
buildCassettes,
updateCassettes,
buy,
sell
}

View file

@ -297,9 +297,18 @@ localApp.post('/dbChange', (req, res, next) => {
})
})
function sha256 (buf) {
const crypto = require('crypto')
const hash = crypto.createHash('sha256')
hash.update(buf)
return hash.digest('hex').toString('hex')
}
function populateDeviceId (req, res, next) {
const deviceId = ((typeof req.connection.getPeerCertificate === 'function' &&
req.connection.getPeerCertificate().fingerprint)) || null
const deviceId = _.isFunction(req.connection.getPeerCertificate)
? sha256(req.connection.getPeerCertificate().raw)
: null
req.deviceId = deviceId
req.deviceTime = Date.parse(req.get('date'))

View file

@ -29,6 +29,8 @@ exports.up = function (next) {
'alter table cash_out_txs drop column denomination_1',
'alter table cash_out_txs drop column denomination_2',
'alter table cash_out_txs drop column dispense_error',
'alter table cash_out_txs drop column dispense_time',
'alter table cash_out_txs drop column confirmation_time',
'alter table cash_out_txs add column dispense_confirmed boolean default false',
'alter table cash_out_txs rename column dispensed to dispense'
]