diff --git a/.gitignore b/.gitignore index d06a9ff..37f18f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .vscode node_modules/ -.env \ No newline at end of file +.env +.env.kovan +.env.mainnet \ No newline at end of file diff --git a/index.js b/index.js index 5172e62..2185282 100644 --- a/index.js +++ b/index.js @@ -44,7 +44,7 @@ app.post('/relay', async (req, resp) => { let { valid , reason } = isValidProof(req.body.proof) if (!valid) { console.log('Proof is invalid:', reason) - return resp.status(400).json({ error: 'Proof is invalid' }) + return resp.status(400).json({ error: 'Proof format is invalid' }) } let currency @@ -55,51 +55,54 @@ app.post('/relay', async (req, resp) => { } let { proof, publicSignals } = req.body.proof - - const relayer = toChecksumAddress(`0x${publicSignals[3].slice(26)}`) - if (relayer !== web3.eth.defaultAccount) { - console.log('This proof is for different relayer:', relayer) + const args = { + root: publicSignals[0], + nullifierHash: publicSignals[1], + recipient: toChecksumAddress(publicSignals[2]), + relayer: toChecksumAddress(publicSignals[3]), + fee: toBN(publicSignals[4]), + refund: toBN(publicSignals[5]), + } + + if (args.relayer !== web3.eth.defaultAccount) { + console.log('This proof is for different relayer:', args.relayer) return resp.status(400).json({ error: 'Relayer address is invalid' }) } - const fee = toBN(publicSignals[4]) - const refund = toBN(publicSignals[5]) const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000')) let desiredFee switch (currency) { case 'eth': { - if (!refund.isZero()) { + if (!args.refund.isZero()) { return resp.status(400).json({ error: 'Cannot send refund for eth currency.' }) } desiredFee = expense break } case 'dai': { - desiredFee = expense.add(refund).mul(toBN(ethPriceInDai)).div(toBN(10 ** 18)) + desiredFee = expense.add(args.refund).mul(toBN(ethPriceInDai)).div(toBN(10 ** 18)) break } } - if (fee.lt(desiredFee)) { + if (args.fee.lt(desiredFee)) { console.log('Fee is too low') return resp.status(400).json({ error: 'Fee is too low. Try to resend.' }) } try { const mixer = new web3.eth.Contract(mixerABI, req.body.contract) - const nullifier = publicSignals[1] - const isSpent = await mixer.methods.isSpent(nullifier).call() + const isSpent = await mixer.methods.isSpent(args.nullifierHash).call() if (isSpent) { return resp.status(400).json({ error: 'The note has been spent.' }) } - const root = publicSignals[0] - const isKnownRoot = await mixer.methods.isKnownRoot(root).call() + const isKnownRoot = await mixer.methods.isKnownRoot(args.root).call() if (!isKnownRoot) { return resp.status(400).json({ error: 'The merkle root is too old or invalid.' }) } - const gas = await mixer.methods.withdraw(proof, publicSignals).estimateGas({ value: refund }) - const result = mixer.methods.withdraw(proof, publicSignals).send({ - value: refund, + const gas = await mixer.methods.withdraw(proof, ...publicSignals).estimateGas({ value: args.refund }) + const result = mixer.methods.withdraw(proof, ...publicSignals).send({ + value: args.refund, gas: numberToHex(gas + 50000), gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')), // TODO: nonce diff --git a/utils.js b/utils.js index 2c99f8d..af8299b 100644 --- a/utils.js +++ b/utils.js @@ -42,34 +42,36 @@ async function fetchDAIprice({ ethPriceInDai, web3 }) { } } -function isValidProof(proof) { +function isValidProof(data) { // validator expects `websnarkUtils.toSolidityInput(proof)` output - if (!(proof.proof && proof.publicSignals)) { + if (!(data.proof && data.publicSignals)) { return { valid: false, reason: 'One of inputs is empty. There must be proof and publicSignals' } } - Object.keys(proof).forEach(key => { - if (!Array.isArray(proof[key])) { - return { valid: false, reason: `Corrupted ${key}` } - } - }) - - if (proof.proof.length !== 8) { + if (!isHexStrict(data.proof) || data.proof.length !== 2 + 2 * 8 * 32) { return { valid: false, reason: 'Corrupted proof' } } - if (proof.publicSignals.length !== 6) { + if (data.publicSignals.length !== 6) { return { valid: false, reason: 'Corrupted publicSignals' } } - for (let [key, input] of Object.entries(proof)) { - for (let i = 0; i < input.length; i++ ) { - if (!isHexStrict(input[i]) || input[i].length !== 66) { - return { valid: false, reason: `Corrupted ${key}` } - } + for(let signal of data.publicSignals) { + if (!isHexStrict(signal)) { + return { valid: false, reason: 'Corrupted publicSignals' } } } + + if (data.publicSignals[0].length !== 66 || + data.publicSignals[1].length !== 66 || + data.publicSignals[2].length !== 42 || + data.publicSignals[3].length !== 42 || + data.publicSignals[4].length !== 66 || + data.publicSignals[5].length !== 66) { + return { valid: false, reason: 'Corrupted publicSignals' } + } + return { valid: true } }