fix isKnownContract validation

This commit is contained in:
smart_ex 2022-04-18 17:18:57 +10:00 committed by Danil Kovtonyuk
parent ed9f003d50
commit ecdcb545fc
4 changed files with 26 additions and 12 deletions

@ -19,7 +19,7 @@ ajv.addKeyword('isAddress', {
ajv.addKeyword('isKnownContract', { ajv.addKeyword('isKnownContract', {
validate: (schema, data) => { validate: (schema, data) => {
try { try {
return getInstance(data) !== null return !!getInstance(data)
} catch (e) { } catch (e) {
return false return false
} }

@ -1,16 +1,26 @@
const { instances } = require('./config') const { instances } = require('./config')
const { toChecksumAddress, BN } = require('web3-utils') const { toChecksumAddress, BN } = require('web3-utils')
const addressMap = new Map()
for (const [currency, { instanceAddress, symbol, decimals }] of Object.entries(instances)) {
Object.entries(instanceAddress).forEach(([amount, address]) =>
addressMap.set(address, {
currency,
amount,
symbol,
decimals,
}),
)
}
function getInstance(address) { function getInstance(address) {
address = toChecksumAddress(address) address = toChecksumAddress(address)
for (const currency of Object.keys(instances)) { const key = toChecksumAddress(address)
for (const amount of Object.keys(instances[currency].instanceAddress)) { if (addressMap.has(key)) {
if (instances[currency].instanceAddress[amount] === address) { return addressMap.get(key)
return { currency, amount } } else {
} throw new Error('Unknown contact address')
}
} }
return null
} }
function setSafeInterval(func, interval) { function setSafeInterval(func, interval) {

@ -10,7 +10,6 @@ const {
netId, netId,
gasPrices, gasPrices,
gasLimits, gasLimits,
instances,
privateKey, privateKey,
proxyLight, proxyLight,
httpRpcUrl, httpRpcUrl,
@ -74,8 +73,7 @@ function getGasLimit() {
} }
async function checkTornadoFee({ args, contract }) { async function checkTornadoFee({ args, contract }) {
const { currency, amount } = getInstance(contract) const { amount, decimals } = getInstance(contract)
const { decimals } = instances[currency]
const fee = toBN(args[4]) const fee = toBN(args[4])
const { fast } = await getGasPrices() const { fast } = await getGasPrices()

@ -15,7 +15,13 @@ describe('Validator', () => {
'.proof should match pattern "^0x[a-fA-F0-9]{512}$"', '.proof should match pattern "^0x[a-fA-F0-9]{512}$"',
) )
}) })
it('should throw if unknown contract', () => {
const malformedData = { ...withdrawData }
malformedData.contract = '0xf17f52151ebef6c7334fad080c5704d77216b732'
getTornadoWithdrawInputError(malformedData).should.be.equal(
'.contract should pass "isKnownContract" keyword validation',
)
})
it('should throw something is missing', () => { it('should throw something is missing', () => {
const malformedData = { ...withdrawData } const malformedData = { ...withdrawData }
delete malformedData.proof delete malformedData.proof