feat: scorechain address analysis

This commit is contained in:
Rafael Taranto 2024-04-30 17:18:36 +01:00
parent 5ae9b76c3b
commit 501da5f54a
15 changed files with 158 additions and 308 deletions

View file

@ -36,8 +36,15 @@ function post (machineTx, pi) {
let addressReuse = false
let walletScore = {}
return Promise.all([settingsLoader.loadLatest(), checkForBlacklisted(updatedTx), doesTxReuseAddress(updatedTx), getWalletScore(updatedTx, pi)])
.then(([{ config }, blacklistItems, isReusedAddress, fetchedWalletScore]) => {
const promises = [settingsLoader.loadLatest()]
const isFirstPost = !r.tx.fiat || r.tx.fiat.isZero()
if (isFirstPost) {
promises.push(checkForBlacklisted(updatedTx), doesTxReuseAddress(updatedTx), getWalletScore(updatedTx, pi))
}
return Promise.all(promises)
.then(([{ config }, blacklistItems = false, isReusedAddress = false, fetchedWalletScore = null]) => {
const rejectAddressReuse = configManager.getCompliance(config).rejectAddressReuse
walletScore = fetchedWalletScore
@ -87,11 +94,7 @@ function logActionById (action, _rec, txId) {
}
function checkForBlacklisted (tx) {
// Check only on addressScan and avoid testing for blacklist on every bill inserted
if (!tx.fiat || tx.fiat.isZero()) {
return blacklist.blocked(tx.toAddress, tx.cryptoCode)
}
return Promise.resolve(false)
return blacklist.blocked(tx.toAddress, tx.cryptoCode)
}
function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
@ -113,7 +116,7 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
return Promise.resolve({
walletScore: walletScore.score,
operatorCompleted: true,
error: 'Ciphertrace score is above defined threshold',
error: 'Chain analysis score is above defined threshold',
errorCode: 'scoreThresholdReached'
})
}
@ -167,32 +170,20 @@ function postProcess (r, pi, isBlacklisted, addressReuse, walletScore) {
}
function doesTxReuseAddress (tx) {
if (!tx.fiat || tx.fiat.isZero()) {
const sql = `
SELECT EXISTS (
SELECT DISTINCT to_address FROM (
SELECT to_address FROM cash_in_txs WHERE id != $1
) AS x WHERE to_address = $2
)`
return db.one(sql, [tx.id, tx.toAddress]).then(({ exists }) => exists)
}
return Promise.resolve(false)
const sql = `
SELECT EXISTS (
SELECT DISTINCT to_address FROM (
SELECT to_address FROM cash_in_txs WHERE id != $1
) AS x WHERE to_address = $2
)`
return db.one(sql, [tx.id, tx.toAddress]).then(({ exists }) => exists)
}
function getWalletScore (tx, pi) {
return pi.isWalletScoringEnabled(tx)
.then(isEnabled => {
if (!isEnabled) return null
if (!tx.fiat || tx.fiat.isZero()) {
return pi.rateWallet(tx.cryptoCode, tx.toAddress)
}
// Passthrough the previous result
return pi.isValidWalletScore(tx.walletScore)
.then(isValid => ({
address: tx.toAddress,
score: tx.walletScore,
isValid
}))
return pi.rateAddress(tx.cryptoCode, tx.toAddress)
})
}