From 91e1077daa350d617b356ee9cb6fe96f547e9743 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Mon, 24 Apr 2017 14:08:12 +0300 Subject: [PATCH] move to sha-256 fingerprints --- lib/cash-out-tx.js | 37 ++++++++++++++------- lib/plugins.js | 52 ------------------------------ lib/routes.js | 13 ++++++-- migrations/028-cash_out_actions.js | 2 ++ 4 files changed, 39 insertions(+), 65 deletions(-) diff --git a/lib/cash-out-tx.js b/lib/cash-out-tx.js index 4adf9fa2..d9fb363e 100644 --- a/lib/cash-out-tx.js +++ b/lib/cash-out-tx.js @@ -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) { diff --git a/lib/plugins.js b/lib/plugins.js index b89d0f13..faad0d39 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -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 } diff --git a/lib/routes.js b/lib/routes.js index 4d7e8fad..939fbdf2 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -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')) diff --git a/migrations/028-cash_out_actions.js b/migrations/028-cash_out_actions.js index 4c19beaa..6442f81b 100644 --- a/migrations/028-cash_out_actions.js +++ b/migrations/028-cash_out_actions.js @@ -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' ]