tornado-relayer/index.js

129 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-09-20 00:46:20 +03:00
const { numberToHex, toWei, toHex, toBN, toChecksumAddress } = require('web3-utils')
2019-10-26 21:16:31 +03:00
const { netId, rpcUrl, privateKey, mixers, defaultGasPrice } = require('./config')
const { fetchGasPrice, isValidProof, fetchDAIprice, isKnownContract } = require('./utils')
2019-07-18 17:45:33 +03:00
const Web3 = require('web3')
2019-07-18 15:43:26 +03:00
const express = require('express')
2019-07-18 17:45:33 +03:00
2019-07-18 15:43:26 +03:00
const app = express()
app.use(express.json())
2019-07-24 20:19:00 +03:00
app.use((err, req, res, next) => {
if (err) {
console.log('Invalid Request data')
res.send('Invalid Request data')
} else {
next()
}
})
2019-07-18 19:00:07 +03:00
app.use(function(req, res, next) {
2019-07-19 20:34:51 +03:00
res.header('Access-Control-Allow-Origin', '*')
2019-07-18 21:15:59 +03:00
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
2019-07-18 15:43:26 +03:00
const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 })
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey)
web3.eth.accounts.wallet.add('0x' + privateKey)
web3.eth.defaultAccount = account.address
2019-09-19 18:21:57 +03:00
const mixerABI = require('./abis/mixerABI.json')
2019-07-18 15:43:26 +03:00
const gasPrices = { fast: defaultGasPrice }
2019-09-19 18:21:57 +03:00
const ethPriceInDai = toWei('200')
2019-07-18 15:43:26 +03:00
app.get('/', function (req, res) {
// just for testing purposes
2019-10-28 21:39:38 +03:00
res.send('This is <a href=https://tornado.cash>tornado.cash</a> Relayer service. Check the /status for settings')
})
app.get('/status', function (req, res) {
res.json({ relayerAddress: web3.eth.defaultAccount, gasPrices, netId })
2019-07-18 15:43:26 +03:00
})
app.post('/relay', async (req, resp) => {
2019-09-19 22:56:45 +03:00
let { valid , reason } = isValidProof(req.body.proof)
2019-07-18 15:43:26 +03:00
if (!valid) {
console.log('Proof is invalid:', reason)
2019-11-08 04:25:43 +03:00
return resp.status(400).json({ error: 'Proof format is invalid' })
2019-07-18 15:43:26 +03:00
}
2019-09-20 00:46:20 +03:00
2019-09-19 22:56:45 +03:00
let currency
( { valid, currency } = isKnownContract(req.body.contract))
2019-09-20 00:46:20 +03:00
if (!valid) {
console.log('Contract does not exist:', req.body.contract)
return resp.status(400).json({ error: 'This relayer does not support the token' })
}
2019-07-18 15:43:26 +03:00
2019-10-06 10:55:24 +03:00
let { proof, publicSignals } = req.body.proof
2019-11-08 04:25:43 +03:00
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)
2019-09-20 00:46:20 +03:00
return resp.status(400).json({ error: 'Relayer address is invalid' })
}
2019-07-18 15:43:26 +03:00
2019-09-19 22:56:45 +03:00
const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000'))
let desiredFee
switch (currency) {
case 'eth': {
2019-11-08 04:25:43 +03:00
if (!args.refund.isZero()) {
2019-10-06 10:54:48 +03:00
return resp.status(400).json({ error: 'Cannot send refund for eth currency.' })
}
2019-09-19 22:56:45 +03:00
desiredFee = expense
break
}
case 'dai': {
2019-11-08 04:25:43 +03:00
desiredFee = expense.add(args.refund).mul(toBN(ethPriceInDai)).div(toBN(10 ** 18))
2019-09-19 22:56:45 +03:00
break
}
}
2019-11-08 04:25:43 +03:00
if (args.fee.lt(desiredFee)) {
2019-07-19 20:34:51 +03:00
console.log('Fee is too low')
2019-07-23 19:07:33 +03:00
return resp.status(400).json({ error: 'Fee is too low. Try to resend.' })
2019-07-19 20:34:51 +03:00
}
2019-07-18 15:43:26 +03:00
try {
2019-09-19 22:56:45 +03:00
const mixer = new web3.eth.Contract(mixerABI, req.body.contract)
2019-11-08 04:25:43 +03:00
const isSpent = await mixer.methods.isSpent(args.nullifierHash).call()
2019-07-18 15:43:26 +03:00
if (isSpent) {
2019-07-23 19:07:33 +03:00
return resp.status(400).json({ error: 'The note has been spent.' })
2019-07-18 15:43:26 +03:00
}
2019-11-08 04:25:43 +03:00
const isKnownRoot = await mixer.methods.isKnownRoot(args.root).call()
2019-07-18 16:30:24 +03:00
if (!isKnownRoot) {
2019-07-23 19:07:33 +03:00
return resp.status(400).json({ error: 'The merkle root is too old or invalid.' })
2019-07-18 16:30:24 +03:00
}
2019-11-08 04:25:43 +03:00
const gas = await mixer.methods.withdraw(proof, ...publicSignals).estimateGas({ value: args.refund })
const result = mixer.methods.withdraw(proof, ...publicSignals).send({
value: args.refund,
2019-07-18 15:43:26 +03:00
gas: numberToHex(gas + 50000),
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
// TODO: nonce
})
result.once('transactionHash', function(hash){
2019-07-18 19:00:07 +03:00
resp.json({ txHash: hash })
2019-07-18 15:43:26 +03:00
}).on('error', function(e){
console.log(e)
2019-07-23 19:07:33 +03:00
return resp.status(400).json({ error: 'Proof is malformed.' })
2019-07-18 15:43:26 +03:00
})
} catch (e) {
console.log(e)
2019-07-23 19:07:33 +03:00
return resp.status(400).json({ error: 'Proof is malformed or spent.' })
2019-07-18 15:43:26 +03:00
}
})
app.listen(8000)
2019-08-03 23:36:22 +03:00
if (Number(netId) === 1) {
2019-07-18 15:43:26 +03:00
fetchGasPrice({ gasPrices })
2019-09-19 18:21:57 +03:00
fetchDAIprice({ ethPriceInDai, web3 })
2019-07-18 15:43:26 +03:00
console.log('Gas price oracle started.')
}