2019-07-19 19:34:51 +02:00
|
|
|
const { numberToHex, toWei, toHex, toBN } = require('web3-utils')
|
2019-07-18 16:45:33 +02:00
|
|
|
const Web3 = require('web3')
|
2019-07-18 14:43:26 +02:00
|
|
|
const express = require('express')
|
2019-07-18 16:45:33 +02:00
|
|
|
|
2019-07-18 14:43:26 +02:00
|
|
|
const app = express()
|
|
|
|
app.use(express.json())
|
2019-07-18 18:00:07 +02:00
|
|
|
app.use(function(req, res, next) {
|
2019-07-19 19:34:51 +02:00
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
2019-07-18 20:15:59 +02:00
|
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
|
|
|
|
next()
|
|
|
|
})
|
2019-07-18 14:43:26 +02:00
|
|
|
|
2019-07-19 19:34:51 +02:00
|
|
|
const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice, desiredFee } = require('./config')
|
2019-07-18 16:45:33 +02:00
|
|
|
const { fetchGasPrice, isValidProof } = require('./utils')
|
2019-07-18 14:43:26 +02: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-07-18 16:45:33 +02:00
|
|
|
const mixerABI = require('./mixerABI.json')
|
2019-07-18 14:43:26 +02:00
|
|
|
const mixer = new web3.eth.Contract(mixerABI, mixerAddress)
|
|
|
|
const gasPrices = { fast: defaultGasPrice }
|
|
|
|
|
|
|
|
app.get('/', function (req, res) {
|
|
|
|
// just for testing purposes
|
|
|
|
res.send(`Tornado mixer relayer. Gas Price is ${JSON.stringify(gasPrices)}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/relay', async (req, resp) => {
|
|
|
|
const { valid , reason } = isValidProof(req.body)
|
|
|
|
if (!valid) {
|
|
|
|
console.log('Proof is invalid:', reason)
|
|
|
|
return resp.status(400).send('Proof is invalid')
|
|
|
|
}
|
|
|
|
|
|
|
|
let { pi_a, pi_b, pi_c, publicSignals } = req.body
|
|
|
|
|
2019-07-19 19:34:51 +02:00
|
|
|
const fee = toBN(publicSignals[3])
|
|
|
|
if (fee.lt(toBN(desiredFee))) {
|
|
|
|
console.log('Fee is too low')
|
|
|
|
return resp.status(400).send('Fee is too low')
|
|
|
|
}
|
2019-07-18 14:43:26 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const nullifier = publicSignals[1]
|
|
|
|
const isSpent = await mixer.methods.isSpent(nullifier).call()
|
|
|
|
if (isSpent) {
|
|
|
|
throw new Error('The note has been spent')
|
|
|
|
}
|
2019-07-18 15:30:24 +02:00
|
|
|
const root = publicSignals[0]
|
|
|
|
const isKnownRoot = await mixer.methods.isKnownRoot(root).call()
|
|
|
|
if (!isKnownRoot) {
|
|
|
|
throw new Error('The merkle root is too old or invalid')
|
|
|
|
}
|
2019-07-18 14:43:26 +02:00
|
|
|
const gas = await mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).estimateGas()
|
|
|
|
const result = mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).send({
|
|
|
|
gas: numberToHex(gas + 50000),
|
|
|
|
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
|
|
|
|
// TODO: nonce
|
|
|
|
})
|
|
|
|
result.once('transactionHash', function(hash){
|
2019-07-18 18:00:07 +02:00
|
|
|
resp.json({ txHash: hash })
|
2019-07-18 14:43:26 +02:00
|
|
|
}).on('error', function(e){
|
|
|
|
console.log(e)
|
|
|
|
resp.status(400).send('Proof is malformed')
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
resp.status(400).send('Proof is malformed or spent')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.listen(8000)
|
|
|
|
|
|
|
|
if (netId === 1) {
|
|
|
|
fetchGasPrice({ gasPrices })
|
|
|
|
console.log('Gas price oracle started.')
|
|
|
|
}
|