diff --git a/packages/server/lib/cash-in/cash-in-tx.js b/packages/server/lib/cash-in/cash-in-tx.js index e454efa5..4ed8616e 100644 --- a/packages/server/lib/cash-in/cash-in-tx.js +++ b/packages/server/lib/cash-in/cash-in-tx.js @@ -30,6 +30,7 @@ module.exports = { post, monitorPending, cancel, + doesTxReuseAddress, PENDING_INTERVAL, TRANSACTION_STATES, } @@ -201,7 +202,7 @@ function postProcess(r, pi, isBlacklisted, addressReuse, walletScore) { // If the current customer is anon, we can still allow one other customer to use the address, // So we count distinct customers plus the current customer if they are not anonymous. // To prevent malicious blocking of address, we only check for txs with actual fiat -function doesTxReuseAddress(tx) { +function doesTxReuseAddress({ toAddress, customerId }) { const sql = ` SELECT COUNT(*) > 1 as exists FROM (SELECT DISTINCT customer_id @@ -214,7 +215,7 @@ function doesTxReuseAddress(tx) { WHERE $2 != $3) t; ` return db - .one(sql, [tx.toAddress, tx.customerId, constants.anonymousCustomer.uuid]) + .one(sql, [toAddress, customerId, constants.anonymousCustomer.uuid]) .then(({ exists }) => exists) } diff --git a/packages/server/lib/routes/customerRoutes.js b/packages/server/lib/routes/customerRoutes.js index a37f3a37..b7490fd0 100644 --- a/packages/server/lib/routes/customerRoutes.js +++ b/packages/server/lib/routes/customerRoutes.js @@ -25,6 +25,7 @@ const Tx = require('../tx') const loyalty = require('../loyalty') const logger = require('../logger') const externalCompliance = require('../compliance-external') +const { doesTxReuseAddress } = require('../cash-in/cash-in-tx') function updateCustomerCustomInfoRequest(customerId, patch) { const promise = _.isNil(patch.data) @@ -364,6 +365,18 @@ function addOrUpdateCustomer( return { ...customer, lastUsedAddress } }) }) + .then(customer => { + const { rejectAddressReuse } = configManager.getCompliance(config) + if (!rejectAddressReuse || !customer.lastUsedAddress) return customer + + return doesTxReuseAddress({ + toAddress: customer.lastUsedAddress, + customerId: customer.id, + }).then(isReused => { + const newAddress = isReused ? null : customer.lastUsedAddress + return { ...customer, lastUsedAddress: newAddress } + }) + }) } function getOrAddCustomerPhone(req, res, next) {