123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- const { isAddress, toChecksumAddress } = require('web3-utils')
- const { getInstance } = require('./utils')
- const { rewardAccount } = require('./config')
- const Ajv = require('ajv')
- const ajv = new Ajv({ format: 'fast' })
- ajv.addKeyword('isAddress', {
- validate: (schema, data) => {
- try {
- return isAddress(data)
- } catch (e) {
- return false
- }
- },
- errors: true,
- })
- ajv.addKeyword('isKnownContract', {
- validate: (schema, data) => {
- try {
- return getInstance(data) !== null
- } catch (e) {
- return false
- }
- },
- errors: true,
- })
- ajv.addKeyword('isFeeRecipient', {
- validate: (schema, data) => {
- try {
- return toChecksumAddress(rewardAccount) === toChecksumAddress(data)
- } catch (e) {
- return false
- }
- },
- errors: true,
- })
- const addressType = { type: 'string', pattern: '^0x[a-fA-F0-9]{40}$', isAddress: true }
- const proofType = { type: 'string', pattern: '^0x[a-fA-F0-9]{512}$' }
- const encryptedAccountType = { type: 'string', pattern: '^0x[a-fA-F0-9]{392}$' }
- const bytes32Type = { type: 'string', pattern: '^0x[a-fA-F0-9]{64}$' }
- const instanceType = { ...addressType, isKnownContract: true }
- const relayerType = { ...addressType, isFeeRecipient: true }
- const tornadoWithdrawSchema = {
- type: 'object',
- properties: {
- proof: proofType,
- contract: instanceType,
- args: {
- type: 'array',
- maxItems: 6,
- minItems: 6,
- items: [bytes32Type, bytes32Type, addressType, relayerType, bytes32Type, bytes32Type],
- },
- },
- additionalProperties: false,
- required: ['proof', 'contract', 'args'],
- }
- const miningRewardSchema = {
- type: 'object',
- properties: {
- proof: proofType,
- args: {
- type: 'object',
- properties: {
- rate: bytes32Type,
- fee: bytes32Type,
- instance: instanceType,
- rewardNullifier: bytes32Type,
- extDataHash: bytes32Type,
- depositRoot: bytes32Type,
- withdrawalRoot: bytes32Type,
- extData: {
- type: 'object',
- properties: {
- relayer: relayerType,
- encryptedAccount: encryptedAccountType,
- },
- additionalProperties: false,
- required: ['relayer', 'encryptedAccount'],
- },
- account: {
- type: 'object',
- properties: {
- inputRoot: bytes32Type,
- inputNullifierHash: bytes32Type,
- outputRoot: bytes32Type,
- outputPathIndices: bytes32Type,
- outputCommitment: bytes32Type,
- },
- additionalProperties: false,
- required: [
- 'inputRoot',
- 'inputNullifierHash',
- 'outputRoot',
- 'outputPathIndices',
- 'outputCommitment',
- ],
- },
- },
- additionalProperties: false,
- required: [
- 'rate',
- 'fee',
- 'instance',
- 'rewardNullifier',
- 'extDataHash',
- 'depositRoot',
- 'withdrawalRoot',
- 'extData',
- 'account',
- ],
- },
- },
- additionalProperties: false,
- required: ['proof', 'args'],
- }
- const miningWithdrawSchema = {
- type: 'object',
- properties: {
- proof: proofType,
- args: {
- type: 'object',
- properties: {
- amount: bytes32Type,
- extDataHash: bytes32Type,
- extData: {
- type: 'object',
- properties: {
- fee: bytes32Type,
- recipient: addressType,
- relayer: relayerType,
- encryptedAccount: encryptedAccountType,
- },
- additionalProperties: false,
- required: ['fee', 'relayer', 'encryptedAccount', 'recipient'],
- },
- account: {
- type: 'object',
- properties: {
- inputRoot: bytes32Type,
- inputNullifierHash: bytes32Type,
- outputRoot: bytes32Type,
- outputPathIndices: bytes32Type,
- outputCommitment: bytes32Type,
- },
- additionalProperties: false,
- required: [
- 'inputRoot',
- 'inputNullifierHash',
- 'outputRoot',
- 'outputPathIndices',
- 'outputCommitment',
- ],
- },
- },
- additionalProperties: false,
- required: ['amount', 'extDataHash', 'extData', 'account'],
- },
- },
- additionalProperties: false,
- required: ['proof', 'args'],
- }
- const validateTornadoWithdraw = ajv.compile(tornadoWithdrawSchema)
- const validateMiningReward = ajv.compile(miningRewardSchema)
- const validateMiningWithdraw = ajv.compile(miningWithdrawSchema)
- function getInputError(validator, data) {
- validator(data)
- if (validator.errors) {
- const error = validator.errors[0]
- return `${error.dataPath} ${error.message}`
- }
- return null
- }
- function getTornadoWithdrawInputError(data) {
- return getInputError(validateTornadoWithdraw, data)
- }
- function getMiningRewardInputError(data) {
- return getInputError(validateMiningReward, data)
- }
- function getMiningWithdrawInputError(data) {
- return getInputError(validateMiningWithdraw, data)
- }
- module.exports = {
- getTornadoWithdrawInputError,
- getMiningRewardInputError,
- getMiningWithdrawInputError,
- }
|