Merge branch 'erc20_support' of github.com:peppersec/tornado-mixer-relayer into erc20_support

This commit is contained in:
Alexey 2019-11-14 14:27:23 +03:00
commit d8987e060c
7 changed files with 1873 additions and 3607 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.vscode
node_modules/
.env
.env.kovan
.env.mainnet

View File

@ -6,5 +6,5 @@ RUN npm install && npm cache clean --force
COPY . .
EXPOSE 8000
HEALTHCHECK CMD curl -f http://localhost:8000/
HEALTHCHECK CMD curl -f http://localhost:8000/status
CMD ["npm", "run", "start"]

View File

@ -1,26 +1,11 @@
[
{
"constant": true,
"inputs": [],
"name": "filled_subtrees",
"outputs": [
{
"internalType": "uint256[]",
"name": "",
"type": "uint256[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"internalType": "bytes32",
"name": "",
"type": "uint256"
"type": "bytes32"
}
],
"name": "nullifierHashes",
@ -52,13 +37,84 @@
},
{
"constant": true,
"inputs": [],
"name": "roots",
"inputs": [
{
"internalType": "bytes32",
"name": "_left",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "_right",
"type": "bytes32"
}
],
"name": "hashLeftRight",
"outputs": [
{
"internalType": "uint256[]",
"internalType": "bytes32",
"name": "",
"type": "uint256[]"
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "FIELD_SIZE",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isVerifierUpdateDisabled",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "levels",
"outputs": [
{
"internalType": "uint32",
"name": "",
"type": "uint32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "operator",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"payable": false,
@ -69,9 +125,30 @@
"constant": true,
"inputs": [
{
"internalType": "uint256",
"internalType": "bytes32",
"name": "_root",
"type": "bytes32"
}
],
"name": "isKnownRoot",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "uint256"
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"name": "commitments",
@ -89,22 +166,7 @@
{
"constant": true,
"inputs": [],
"name": "zeros",
"outputs": [
{
"internalType": "uint256[]",
"name": "",
"type": "uint256[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "levels",
"name": "denomination",
"outputs": [
{
"internalType": "uint256",
@ -119,63 +181,7 @@
{
"constant": true,
"inputs": [],
"name": "operator",
"outputs": [
{
"internalType": "address payable",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isDepositsEnabled",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "left",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "right",
"type": "uint256"
}
],
"name": "hashLeftRight",
"outputs": [
{
"internalType": "uint256",
"name": "mimc_hash",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "next_index",
"name": "currentRootIndex",
"outputs": [
{
"internalType": "uint32",
@ -190,7 +196,79 @@
{
"constant": true,
"inputs": [],
"name": "current_root",
"name": "getLastRoot",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "roots",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ROOT_HISTORY_SIZE",
"outputs": [
{
"internalType": "uint32",
"name": "",
"type": "uint32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "zeros",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ZERO_VALUE",
"outputs": [
{
"internalType": "uint256",
@ -204,14 +282,8 @@
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "root",
"type": "uint256"
}
],
"name": "isKnownRoot",
"inputs": [],
"name": "isDepositsDisabled",
"outputs": [
{
"internalType": "bool",
@ -225,15 +297,21 @@
},
{
"constant": true,
"inputs": [],
"name": "getLastRoot",
"outputs": [
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "filledSubtrees",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
@ -241,12 +319,12 @@
{
"constant": true,
"inputs": [],
"name": "mixDenomination",
"name": "nextIndex",
"outputs": [
{
"internalType": "uint256",
"internalType": "uint32",
"name": "",
"type": "uint256"
"type": "uint32"
}
],
"payable": false,
@ -256,27 +334,22 @@
{
"inputs": [
{
"internalType": "address",
"internalType": "contract IVerifier",
"name": "_verifier",
"type": "address"
},
{
"internalType": "uint256",
"name": "_mixDenomination",
"name": "_denomination",
"type": "uint256"
},
{
"internalType": "uint8",
"internalType": "uint32",
"name": "_merkleTreeHeight",
"type": "uint8"
"type": "uint32"
},
{
"internalType": "uint256",
"name": "_emptyElement",
"type": "uint256"
},
{
"internalType": "address payable",
"internalType": "address",
"name": "_operator",
"type": "address"
}
@ -290,15 +363,15 @@
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"internalType": "bytes32",
"name": "commitment",
"type": "uint256"
"type": "bytes32"
},
{
"indexed": false,
"internalType": "uint256",
"internalType": "uint32",
"name": "leafIndex",
"type": "uint256"
"type": "uint32"
},
{
"indexed": false,
@ -321,9 +394,9 @@
},
{
"indexed": false,
"internalType": "uint256",
"internalType": "bytes32",
"name": "nullifierHash",
"type": "uint256"
"type": "bytes32"
},
{
"indexed": true,
@ -338,16 +411,16 @@
"type": "uint256"
}
],
"name": "Withdraw",
"name": "Withdrawal",
"type": "event"
},
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "commitment",
"type": "uint256"
"internalType": "bytes32",
"name": "_commitment",
"type": "bytes32"
}
],
"name": "deposit",
@ -360,63 +433,54 @@
"constant": false,
"inputs": [
{
"internalType": "uint256[2]",
"name": "a",
"type": "uint256[2]"
"internalType": "bytes",
"name": "_proof",
"type": "bytes"
},
{
"internalType": "uint256[2][2]",
"name": "b",
"type": "uint256[2][2]"
"internalType": "bytes32",
"name": "_root",
"type": "bytes32"
},
{
"internalType": "uint256[2]",
"name": "c",
"type": "uint256[2]"
"internalType": "bytes32",
"name": "_nullifierHash",
"type": "bytes32"
},
{
"internalType": "uint256[5]",
"name": "input",
"type": "uint256[5]"
"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": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "toggleDeposits",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address payable",
"name": "_newAccount",
"type": "address"
}
],
"name": "changeOperator",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "uint256",
"name": "nullifier",
"type": "uint256"
"internalType": "bytes32",
"name": "_nullifierHash",
"type": "bytes32"
}
],
"name": "isSpent",
@ -430,5 +494,59 @@
"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

@ -37,14 +37,14 @@ app.get('/', function (req, res) {
})
app.get('/status', function (req, res) {
res.json({ relayerAddress: web3.eth.defaultAccount, gasPrices, netId })
res.json({ relayerAddress: web3.eth.defaultAccount, gasPrices, netId, ethPriceInDai })
})
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,55 @@ app.post('/relay', async (req, resp) => {
}
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 relayer = toChecksumAddress(`0x${publicSignals[3].slice(26)}`)
if (relayer !== web3.eth.defaultAccount) {
console.log('This proof is for different relayer:', relayer)
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({ from: web3.eth.defaultAccount, value: args.refund })
const result = mixer.methods.withdraw(proof, ...publicSignals).send({
from: web3.eth.defaultAccount,
value: args.refund,
gas: numberToHex(gas + 50000),
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
// TODO: nonce

4930
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,13 +11,13 @@
"author": "Alexey Pertsev <alexey@peppersec.com> (https://peppersec.com)",
"license": "MIT",
"dependencies": {
"dotenv": "^8.0.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"node-fetch": "^2.6.0",
"web3": "^1.0.0-beta.55",
"web3-utils": "^1.0.0"
"web3": "^1.2.2",
"web3-utils": "^1.2.2"
},
"devDependencies": {
"eslint": "^6.0.1"
"eslint": "^6.6.0"
}
}

View File

@ -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 }
}