This commit is contained in:
Alexey 2019-11-14 16:24:01 +03:00
parent 28c4860f54
commit 097bd13174
4 changed files with 207 additions and 237 deletions

View File

@ -5,40 +5,29 @@
2. `cp .env.example .env`
3. Modify `.env` as needed
## Deploy Kovan
1. `cp .env.example deploy/kovan/.env`
2. Modify `.env` as needed
3. `docker-compose -p kovan up -d`
## Run locally
1. `npm run start`
2. `curl -X POST -H 'content-type:application/json' --data '<PROOF>' http://127.0.0.1:8000/relay`
2. `curl -X POST -H 'content-type:application/json' --data '<input data>' http://127.0.0.1:8000/relay`
Relayer should return a transaction hash.
## Running in Docker
To run a relayer in Docker simply run this command:
```sh
docker run -d -e NET_ID=<...> -e RPC_URL=<...> -e PRIVATE_KEY=<...> -e ETH_MIXER_ADDRESS=<...> -e DAI_MIXER_ADDRESS=<...> peppersec/tornado-mixer-relayer
```
## Proof example
## Input data example
```json
{
"contract": "0xf15f151CEf196E4a2dC28De496811e435552E93e",
"proof": {
"proof":[
"0x0ed9b1afc791a551f5baa2f84786963b1463ca3f7c68eb0de3b267e6cb491f05",
"0x1335f2af3c71e442fd82f63f8f1c605ca2612b8d0fa22b4cbd1239cca839aa3d",
"0x1abb4b71da0efa67cbc76a97ac360826b17a88f07bd89151258bf076474a4804",
"0x000189f7f1067a768d116cd86980eae6963dd9bc6c1f8204ceacf90a94f60d81",
"0x15b11f9f5441adeea61534105902170a409b228e159fe7428abf6e863fc05273",
"0x0526b509ba2cda2b21b09401d70d23ea0225be4fdaa9097af842ff6783d1e0f4",
"0x2cd9a2305827f7da64aa1a3136c11ae1d3d7b3cb69832d8c04ab39d8b9393cda",
"0x2090cd3f9d09d66ca4e1e9bed2c72d5fa174b47599cb47e572324b1a98a3cb7a"
"proof": "0x0f8cb4c2ca9cbb23a5f21475773e19e39d3470436d7296f25c8730d19d88fcef2986ec694ad094f4c5fff79a4e5043bd553df20b23108bc023ec3670718143c20cc49c6d9798e1ae831fd32a878b96ff8897728f9b7963f0d5a4b5574426ac6203b2456d360b8e825d8f5731970bf1fc1b95b9713e3b24203667ecdd5939c2e40dec48f9e51d9cc8dc2f7f3916f0e9e31519c7df2bea8c51a195eb0f57beea4924cb846deaa78cdcbe361a6c310638af6f6157317bc27d74746bfaa2e1f8d2e9088fd10fa62100740874cdffdd6feb15c95c5a303f6bc226d5e51619c5b825471a17ddfeb05b250c0802261f7d05cf29a39a72c13e200e5bc721b0e4c50d55e6",
"args": [
"0x1579d41e5290ab5bcec9a7df16705e49b5c0b869095299196c19c5e14462c9e3",
"0x0cf7f49c5b35c48b9e1d43713e0b46a75977e3d10521e9ac1e4c3cd5e3da1c5d",
"0x03ebd0748aa4d1457cf479cce56309641e0a98f5",
"0xbd4369dc854c5d5b79fe25492e3a3cfcb5d02da5",
"0x000000000000000000000000000000000000000000000000058d15e176280000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
],
"publicSignals":[
"0x1e8a85160889dfb5c03a8e2a6cca18b4c476c0b486003e9ed666a33e04114658",
"0x00bfb0befe19eac571ecaf7858e50d70273fbe2952cc8431f59399bb28665796",
"0x00000000000000000000000003ebd0748aa4d1457cf479cce56309641e0a98f5",
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
}
"contract": "0xA27E34Ad97F171846bAf21399c370c9CE6129e0D"
}
```

View File

@ -1,4 +1,19 @@
[
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "_newOperator",
"type": "address"
}
],
"name": "changeOperator",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
@ -20,6 +35,51 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "bytes",
"name": "_proof",
"type": "bytes"
},
{
"internalType": "bytes32",
"name": "_root",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "_nullifierHash",
"type": "bytes32"
},
{
"internalType": "address payable",
"name": "_recipient",
"type": "address"
},
{
"internalType": "address payable",
"name": "_relayer",
"type": "address"
},
{
"internalType": "uint256",
"name": "_fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_refund",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": true,
"inputs": [],
@ -76,21 +136,6 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isVerifierUpdateDisabled",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
@ -193,6 +238,36 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "_newVerifier",
"type": "address"
}
],
"name": "updateVerifier",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "bytes32",
"name": "_commitment",
"type": "bytes32"
}
],
"name": "deposit",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": true,
"inputs": [],
@ -244,6 +319,27 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "bytes32",
"name": "_nullifierHash",
"type": "bytes32"
}
],
"name": "isSpent",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
@ -280,21 +376,6 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isDepositsDisabled",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
@ -413,140 +494,5 @@
],
"name": "Withdrawal",
"type": "event"
},
{
"constant": false,
"inputs": [
{
"internalType": "bytes32",
"name": "_commitment",
"type": "bytes32"
}
],
"name": "deposit",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "bytes",
"name": "_proof",
"type": "bytes"
},
{
"internalType": "bytes32",
"name": "_root",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "_nullifierHash",
"type": "bytes32"
},
{
"internalType": "address payable",
"name": "_recipient",
"type": "address"
},
{
"internalType": "address payable",
"name": "_relayer",
"type": "address"
},
{
"internalType": "uint256",
"name": "_fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_refund",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "bytes32",
"name": "_nullifierHash",
"type": "bytes32"
}
],
"name": "isSpent",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "bool",
"name": "_state",
"type": "bool"
}
],
"name": "toggleDeposits",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "_newVerifier",
"type": "address"
}
],
"name": "updateVerifier",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "disableVerifierUpdate",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "_newOperator",
"type": "address"
}
],
"name": "changeOperator",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
]

View File

@ -1,6 +1,6 @@
const { numberToHex, toWei, toHex, toBN, toChecksumAddress } = require('web3-utils')
const { netId, rpcUrl, privateKey, defaultGasPrice } = require('./config')
const { fetchGasPrice, isValidProof, fetchDAIprice, isKnownContract } = require('./utils')
const { netId, rpcUrl, privateKey, mixers, defaultGasPrice } = require('./config')
const { fetchGasPrice, isValidProof, isValidArgs, fetchDAIprice, isKnownContract } = require('./utils')
const Web3 = require('web3')
const express = require('express')
@ -37,35 +37,41 @@ app.get('/', function (req, res) {
})
app.get('/status', function (req, res) {
res.json({ relayerAddress: web3.eth.defaultAccount, gasPrices, netId, ethPriceInDai })
res.json({ relayerAddress: web3.eth.defaultAccount, mixers, gasPrices, netId, ethPriceInDai })
})
app.post('/relay', async (req, resp) => {
let { valid , reason } = isValidProof(req.body.proof)
const { proof, args, contract } = req.body
let { valid , reason } = isValidProof(proof)
if (!valid) {
console.log('Proof is invalid:', reason)
return resp.status(400).json({ error: 'Proof format is invalid' })
}
let currency
( { valid, currency } = isKnownContract(req.body.contract))
({ valid , reason } = isValidArgs(args))
if (!valid) {
console.log('Contract does not exist:', req.body.contract)
console.log('Args are invalid:', reason)
return resp.status(400).json({ error: 'Withdraw arguments are invalid' })
}
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' })
}
let { proof, publicSignals } = req.body.proof
const args = {
root: publicSignals[0],
nullifierHash: publicSignals[1],
recipient: toChecksumAddress(publicSignals[2]),
relayer: toChecksumAddress(publicSignals[3]),
fee: toBN(publicSignals[4]),
refund: toBN(publicSignals[5]),
}
const [ root, nullifierHash, recipient, relayer, fee, refund ] = [
args[0],
args[1],
toChecksumAddress(args[2]),
toChecksumAddress(args[3]),
toBN(args[4]),
toBN(args[5])
]
if (args.relayer !== web3.eth.defaultAccount) {
console.log('This proof is for different relayer:', args.relayer)
if (relayer !== web3.eth.defaultAccount) {
console.log('This proof is for different relayer:', relayer)
return resp.status(400).json({ error: 'Relayer address is invalid' })
}
@ -73,43 +79,56 @@ app.post('/relay', async (req, resp) => {
let desiredFee
switch (currency) {
case 'eth': {
if (!args.refund.isZero()) {
if (!refund.isZero()) {
return resp.status(400).json({ error: 'Cannot send refund for eth currency.' })
}
desiredFee = expense
break
}
case 'dai': {
desiredFee = expense.add(args.refund).mul(toBN(ethPriceInDai)).div(toBN(10 ** 18))
desiredFee = expense.add(refund).mul(toBN(ethPriceInDai)).div(toBN(10 ** 18))
break
}
}
if (args.fee.lt(desiredFee)) {
if (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 isSpent = await mixer.methods.isSpent(args.nullifierHash).call()
const isSpent = await mixer.methods.isSpent(nullifierHash).call()
if (isSpent) {
return resp.status(400).json({ error: 'The note has been spent.' })
}
const isKnownRoot = await mixer.methods.isKnownRoot(args.root).call()
const isKnownRoot = await mixer.methods.isKnownRoot(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({ from: web3.eth.defaultAccount, value: args.refund })
const result = mixer.methods.withdraw(proof, ...publicSignals).send({
const withdrawArgs = [
proof,
root,
nullifierHash,
recipient,
relayer,
fee.toString(),
refund.toString()
]
const gas = await mixer.methods.withdraw(...withdrawArgs).estimateGas({
from: web3.eth.defaultAccount,
value: args.refund,
value: refund
})
const result = mixer.methods.withdraw(...withdrawArgs).send({
from: web3.eth.defaultAccount,
value: refund,
gas: numberToHex(gas + 50000),
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
// TODO: nonce
})
result.once('transactionHash', function(hash){
resp.json({ txHash: hash })
result.once('transactionHash', function(txHash){
resp.json({ txHash })
console.log(`A new successfuly sent tx ${txHash} for the ${recipient}`)
}).on('error', function(e){
console.log(e)
return resp.status(400).json({ error: 'Proof is malformed.' })
@ -127,3 +146,10 @@ if (Number(netId) === 1) {
fetchDAIprice({ ethPriceInDai, web3 })
console.log('Gas price oracle started.')
}
console.log('Relayer started')
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}`)

View File

@ -42,34 +42,43 @@ async function fetchDAIprice({ ethPriceInDai, web3 }) {
}
}
function isValidProof(data) {
function isValidProof(proof) {
// validator expects `websnarkUtils.toSolidityInput(proof)` output
if (!(data.proof && data.publicSignals)) {
return { valid: false, reason: 'One of inputs is empty. There must be proof and publicSignals' }
if (!(proof)) {
return { valid: false, reason: 'The proof is empty.' }
}
if (!isHexStrict(data.proof) || data.proof.length !== 2 + 2 * 8 * 32) {
if (!isHexStrict(proof) || proof.length !== 2 + 2 * 8 * 32) {
return { valid: false, reason: 'Corrupted proof' }
}
if (data.publicSignals.length !== 6) {
return { valid: false, reason: 'Corrupted publicSignals' }
return { valid: true }
}
function isValidArgs(args) {
if (!(args)) {
return { valid: false, reason: 'Args are empty' }
}
for(let signal of data.publicSignals) {
if (args.length !== 6) {
return { valid: false, reason: 'Length of args is lower than 6' }
}
for(let signal of args) {
if (!isHexStrict(signal)) {
return { valid: false, reason: 'Corrupted publicSignals' }
return { valid: false, reason: `Corrupted signal ${signal}` }
}
}
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' }
if (args[0].length !== 66 ||
args[1].length !== 66 ||
args[2].length !== 42 ||
args[3].length !== 42 ||
args[4].length !== 66 ||
args[5].length !== 66) {
return { valid: false, reason: 'The length one of the signals is incorrect' }
}
return { valid: true }
@ -88,4 +97,4 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
module.exports = { fetchGasPrice, isValidProof, sleep, fetchDAIprice, isKnownContract }
module.exports = { fetchGasPrice, isValidProof, isValidArgs, sleep, fetchDAIprice, isKnownContract }