From 727544344be84d130f121cfbd4193098b93a3c11 Mon Sep 17 00:00:00 2001 From: josepfo Date: Fri, 4 Nov 2022 15:56:15 +0000 Subject: [PATCH] fix: verify if wallet scoring is enabled --- lib/cash-in/cash-in-tx.js | 24 +++++---- lib/cash-out/cash-out-tx.js | 50 ++++++++++--------- lib/plugins.js | 7 ++- .../wallet-scoring/ciphertrace/ciphertrace.js | 11 +++- .../mock-scoring/mock-scoring.js | 12 ++++- lib/wallet-scoring.js | 12 ++++- 6 files changed, 79 insertions(+), 37 deletions(-) diff --git a/lib/cash-in/cash-in-tx.js b/lib/cash-in/cash-in-tx.js index 728c4b86..7e5efb0f 100644 --- a/lib/cash-in/cash-in-tx.js +++ b/lib/cash-in/cash-in-tx.js @@ -174,16 +174,20 @@ function doesTxReuseAddress (tx) { } function getWalletScore (tx, pi) { - 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 - })) + 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 + })) + }) } function monitorPending (settings) { diff --git a/lib/cash-out/cash-out-tx.js b/lib/cash-out/cash-out-tx.js index 838971cb..15204e5a 100644 --- a/lib/cash-out/cash-out-tx.js +++ b/lib/cash-out/cash-out-tx.js @@ -122,30 +122,34 @@ function getWalletScore (tx, pi) { return tx // Transaction shows up on the blockchain, we can request the sender address - return pi.getTransactionHash(tx) - .then(rejectEmpty("No transaction hashes")) - .then(txHashes => pi.getInputAddresses(tx, txHashes)) - .then(rejectEmpty("No input addresses")) - .then(addresses => Promise.all(_.map(it => pi.rateWallet(tx.cryptoCode, it), addresses))) - .then(rejectEmpty("No score ratings")) - .then(_.maxBy(_.get(['score']))) - .then(highestScore => - // Conservatively assign the highest risk of all input addresses to the risk of this transaction - highestScore.isValid - ? _.assign(tx, { walletScore: highestScore.score }) - : _.assign(tx, { - walletScore: highestScore.score, - error: 'Address score is above defined threshold', - errorCode: 'scoreThresholdReached', + return pi.isWalletScoringEnabled(tx) + .then(isEnabled => { + if (!isEnabled) return tx + return pi.getTransactionHash(tx) + .then(rejectEmpty("No transaction hashes")) + .then(txHashes => pi.getInputAddresses(tx, txHashes)) + .then(rejectEmpty("No input addresses")) + .then(addresses => Promise.all(_.map(it => pi.rateWallet(tx.cryptoCode, it), addresses))) + .then(rejectEmpty("No score ratings")) + .then(_.maxBy(_.get(['score']))) + .then(highestScore => + // Conservatively assign the highest risk of all input addresses to the risk of this transaction + highestScore.isValid + ? _.assign(tx, { walletScore: highestScore.score }) + : _.assign(tx, { + walletScore: highestScore.score, + error: 'Address score is above defined threshold', + errorCode: 'scoreThresholdReached', + dispense: true + }) + ) + .catch(error => _.assign(tx, { + walletScore: 10, + error: `Failure getting address score: ${error.message}`, + errorCode: 'ciphertraceError', dispense: true - }) - ) - .catch(error => _.assign(tx, { - walletScore: 10, - error: `Failure getting address score: ${error.message}`, - errorCode: 'ciphertraceError', - dispense: true - })) + })) + }) } function monitorLiveIncoming (settings) { diff --git a/lib/plugins.js b/lib/plugins.js index 5b65ad56..ce85123c 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -850,6 +850,10 @@ function plugins (settings, deviceId) { return walletScoring.getInputAddresses(settings, tx.cryptoCode, txHashes) } + function isWalletScoringEnabled (tx) { + return walletScoring.isWalletScoringEnabled(settings, tx.cryptoCode) + } + return { getRates, recordPing, @@ -882,7 +886,8 @@ function plugins (settings, deviceId) { rateWallet, isValidWalletScore, getTransactionHash, - getInputAddresses + getInputAddresses, + isWalletScoringEnabled } } diff --git a/lib/plugins/wallet-scoring/ciphertrace/ciphertrace.js b/lib/plugins/wallet-scoring/ciphertrace/ciphertrace.js index 87a867ef..74d9a9c8 100644 --- a/lib/plugins/wallet-scoring/ciphertrace/ciphertrace.js +++ b/lib/plugins/wallet-scoring/ciphertrace/ciphertrace.js @@ -126,10 +126,19 @@ function getInputAddresses (account, cryptoCode, txHashes) { }) } +function isWalletScoringEnabled (account, cryptoCode) { + if (!SUPPORTED_COINS.includes(cryptoCode)) { + return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode)) + } + + return Promise.resolve(!_.isNil(account) && account.enabled) +} + module.exports = { NAME, rateWallet, isValidWalletScore, getTransactionHash, - getInputAddresses + getInputAddresses, + isWalletScoringEnabled } diff --git a/lib/plugins/wallet-scoring/mock-scoring/mock-scoring.js b/lib/plugins/wallet-scoring/mock-scoring/mock-scoring.js index d3dfbf03..455ff4bf 100644 --- a/lib/plugins/wallet-scoring/mock-scoring/mock-scoring.js +++ b/lib/plugins/wallet-scoring/mock-scoring/mock-scoring.js @@ -36,10 +36,20 @@ function getInputAddresses (account, cryptoCode, txHashes) { }) } +function isWalletScoringEnabled (account, cryptoCode) { + return new Promise((resolve, _) => { + setTimeout(() => { + return resolve(true) + }, 100) + }) +} + + module.exports = { NAME, rateWallet, isValidWalletScore, getTransactionHash, - getInputAddresses + getInputAddresses, + isWalletScoringEnabled } diff --git a/lib/wallet-scoring.js b/lib/wallet-scoring.js index 0281c25a..74de6130 100644 --- a/lib/wallet-scoring.js +++ b/lib/wallet-scoring.js @@ -47,9 +47,19 @@ function getInputAddresses (settings, cryptoCode, txHashes) { }) } +function isWalletScoringEnabled (settings, cryptoCode) { + return Promise.resolve() + .then(() => { + const { plugin, account } = loadWalletScoring(settings) + + return plugin.isWalletScoringEnabled(account, cryptoCode) + }) +} + module.exports = { rateWallet, isValidWalletScore, getTransactionHash, - getInputAddresses + getInputAddresses, + isWalletScoringEnabled }