2019-09-19 17:46:20 -04:00
|
|
|
const { numberToHex, toWei, toHex, toBN, toChecksumAddress } = require('web3-utils')
|
2019-11-15 04:01:59 -05:00
|
|
|
const { netId, rpcUrl, privateKey, mixers, defaultGasPrice, port } = require('./config')
|
|
|
|
const { fetchGasPrice, isValidProof, isValidArgs, fetchDAIprice, isKnownContract, isEnoughFee } = require('./utils')
|
2019-07-18 10:45:33 -04:00
|
|
|
const Web3 = require('web3')
|
2019-07-18 08:43:26 -04:00
|
|
|
const express = require('express')
|
2019-07-18 10:45:33 -04:00
|
|
|
|
2019-07-18 08:43:26 -04:00
|
|
|
const app = express()
|
|
|
|
app.use(express.json())
|
2019-07-24 13:19:00 -04:00
|
|
|
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
if (err) {
|
|
|
|
console.log('Invalid Request data')
|
|
|
|
res.send('Invalid Request data')
|
|
|
|
} else {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-07-18 12:00:07 -04:00
|
|
|
app.use(function(req, res, next) {
|
2019-07-19 13:34:51 -04:00
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
2019-07-18 14:15:59 -04:00
|
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
|
|
|
|
next()
|
|
|
|
})
|
2019-07-18 08:43:26 -04: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 11:21:57 -04:00
|
|
|
const mixerABI = require('./abis/mixerABI.json')
|
2019-07-18 08:43:26 -04:00
|
|
|
const gasPrices = { fast: defaultGasPrice }
|
2019-09-19 11:21:57 -04:00
|
|
|
const ethPriceInDai = toWei('200')
|
2019-07-18 08:43:26 -04:00
|
|
|
|
|
|
|
app.get('/', function (req, res) {
|
|
|
|
// just for testing purposes
|
2019-10-28 14:39:38 -04: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) {
|
2019-11-14 08:24:01 -05:00
|
|
|
res.json({ relayerAddress: web3.eth.defaultAccount, mixers, gasPrices, netId, ethPriceInDai })
|
2019-07-18 08:43:26 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/relay', async (req, resp) => {
|
2019-11-14 08:24:01 -05:00
|
|
|
const { proof, args, contract } = req.body
|
|
|
|
let { valid , reason } = isValidProof(proof)
|
2019-07-18 08:43:26 -04:00
|
|
|
if (!valid) {
|
|
|
|
console.log('Proof is invalid:', reason)
|
2019-11-07 20:25:43 -05:00
|
|
|
return resp.status(400).json({ error: 'Proof format is invalid' })
|
2019-07-18 08:43:26 -04:00
|
|
|
}
|
2019-09-19 17:46:20 -04:00
|
|
|
|
2019-11-14 08:24:01 -05:00
|
|
|
({ valid , reason } = isValidArgs(args))
|
2019-09-19 17:46:20 -04:00
|
|
|
if (!valid) {
|
2019-11-14 08:24:01 -05:00
|
|
|
console.log('Args are invalid:', reason)
|
|
|
|
return resp.status(400).json({ error: 'Withdraw arguments are invalid' })
|
2019-09-19 17:46:20 -04:00
|
|
|
}
|
2019-07-18 08:43:26 -04:00
|
|
|
|
2019-11-14 08:24:01 -05:00
|
|
|
let currency
|
|
|
|
( { valid, currency } = isKnownContract(contract))
|
|
|
|
if (!valid) {
|
|
|
|
console.log('Contract does not exist:', contract)
|
|
|
|
return resp.status(400).json({ error: 'This relayer does not support the token' })
|
2019-11-07 20:25:43 -05:00
|
|
|
}
|
|
|
|
|
2019-11-14 08:24:01 -05:00
|
|
|
const [ root, nullifierHash, recipient, relayer, fee, refund ] = [
|
|
|
|
args[0],
|
|
|
|
args[1],
|
|
|
|
toChecksumAddress(args[2]),
|
|
|
|
toChecksumAddress(args[3]),
|
|
|
|
toBN(args[4]),
|
|
|
|
toBN(args[5])
|
|
|
|
]
|
|
|
|
|
2019-11-15 04:01:59 -05:00
|
|
|
if (currency === 'eth' && !refund.isZero()) {
|
|
|
|
return resp.status(400).json({ error: 'Cannot send refund for eth currency.' })
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:24:01 -05:00
|
|
|
if (relayer !== web3.eth.defaultAccount) {
|
|
|
|
console.log('This proof is for different relayer:', relayer)
|
2019-09-19 17:46:20 -04:00
|
|
|
return resp.status(400).json({ error: 'Relayer address is invalid' })
|
|
|
|
}
|
2019-07-18 08:43:26 -04:00
|
|
|
|
|
|
|
try {
|
2019-09-19 15:56:45 -04:00
|
|
|
const mixer = new web3.eth.Contract(mixerABI, req.body.contract)
|
2019-11-14 08:24:01 -05:00
|
|
|
const isSpent = await mixer.methods.isSpent(nullifierHash).call()
|
2019-07-18 08:43:26 -04:00
|
|
|
if (isSpent) {
|
2019-07-23 12:07:33 -04:00
|
|
|
return resp.status(400).json({ error: 'The note has been spent.' })
|
2019-07-18 08:43:26 -04:00
|
|
|
}
|
2019-11-14 08:24:01 -05:00
|
|
|
const isKnownRoot = await mixer.methods.isKnownRoot(root).call()
|
2019-07-18 09:30:24 -04:00
|
|
|
if (!isKnownRoot) {
|
2019-07-23 12:07:33 -04:00
|
|
|
return resp.status(400).json({ error: 'The merkle root is too old or invalid.' })
|
2019-07-18 09:30:24 -04:00
|
|
|
}
|
2019-11-15 04:01:59 -05:00
|
|
|
|
2019-11-14 08:24:01 -05:00
|
|
|
const withdrawArgs = [
|
|
|
|
proof,
|
|
|
|
root,
|
|
|
|
nullifierHash,
|
|
|
|
recipient,
|
|
|
|
relayer,
|
|
|
|
fee.toString(),
|
|
|
|
refund.toString()
|
|
|
|
]
|
2019-11-15 04:01:59 -05:00
|
|
|
let gas = await mixer.methods.withdraw(...withdrawArgs).estimateGas({
|
2019-11-14 08:24:01 -05:00
|
|
|
from: web3.eth.defaultAccount,
|
|
|
|
value: refund
|
|
|
|
})
|
2019-11-15 04:01:59 -05:00
|
|
|
|
|
|
|
gas += 50000
|
|
|
|
|
|
|
|
const { isEnough, reason } = isEnoughFee({ gas, gasPrices, currency, refund, ethPriceInDai, fee })
|
|
|
|
if (!isEnough) {
|
|
|
|
console.log(`Wrong fee: ${reason}`)
|
|
|
|
return resp.status(400).json({ error: reason })
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:24:01 -05:00
|
|
|
const result = mixer.methods.withdraw(...withdrawArgs).send({
|
2019-11-09 20:03:46 -05:00
|
|
|
from: web3.eth.defaultAccount,
|
2019-11-14 08:24:01 -05:00
|
|
|
value: refund,
|
2019-11-15 04:01:59 -05:00
|
|
|
gas: numberToHex(gas),
|
2019-07-18 08:43:26 -04:00
|
|
|
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
|
|
|
|
// TODO: nonce
|
|
|
|
})
|
2019-11-14 08:24:01 -05:00
|
|
|
result.once('transactionHash', function(txHash){
|
|
|
|
resp.json({ txHash })
|
|
|
|
console.log(`A new successfuly sent tx ${txHash} for the ${recipient}`)
|
2019-07-18 08:43:26 -04:00
|
|
|
}).on('error', function(e){
|
|
|
|
console.log(e)
|
2019-07-23 12:07:33 -04:00
|
|
|
return resp.status(400).json({ error: 'Proof is malformed.' })
|
2019-07-18 08:43:26 -04:00
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
2019-07-23 12:07:33 -04:00
|
|
|
return resp.status(400).json({ error: 'Proof is malformed or spent.' })
|
2019-07-18 08:43:26 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-11-15 04:01:59 -05:00
|
|
|
app.listen(port || 8000)
|
2019-07-18 08:43:26 -04:00
|
|
|
|
2019-08-03 16:36:22 -04:00
|
|
|
if (Number(netId) === 1) {
|
2019-07-18 08:43:26 -04:00
|
|
|
fetchGasPrice({ gasPrices })
|
2019-09-19 11:21:57 -04:00
|
|
|
fetchDAIprice({ ethPriceInDai, web3 })
|
2019-07-18 08:43:26 -04:00
|
|
|
console.log('Gas price oracle started.')
|
|
|
|
}
|
2019-11-14 08:24:01 -05:00
|
|
|
|
2019-11-15 04:01:59 -05:00
|
|
|
console.log('Relayer started on port', port || 8000)
|
2019-11-14 08:24:01 -05:00
|
|
|
console.log(`relayerAddress: ${web3.eth.defaultAccount}`)
|
|
|
|
console.log(`mixers: ${JSON.stringify(mixers)}`)
|
|
|
|
console.log(`gasPrices: ${JSON.stringify(gasPrices)}`)
|
|
|
|
console.log(`netId: ${netId}`)
|
|
|
|
console.log(`ethPriceInDai: ${ethPriceInDai}`)
|