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

4
.gitignore vendored
View File

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

View File

@ -6,5 +6,5 @@ RUN npm install && npm cache clean --force
COPY . . COPY . .
EXPOSE 8000 EXPOSE 8000
HEALTHCHECK CMD curl -f http://localhost:8000/ HEALTHCHECK CMD curl -f http://localhost:8000/status
CMD ["npm", "run", "start"] 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, "constant": true,
"inputs": [ "inputs": [
{ {
"internalType": "uint256", "internalType": "bytes32",
"name": "", "name": "",
"type": "uint256" "type": "bytes32"
} }
], ],
"name": "nullifierHashes", "name": "nullifierHashes",
@ -52,13 +37,84 @@
}, },
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [
"name": "roots", {
"internalType": "bytes32",
"name": "_left",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "_right",
"type": "bytes32"
}
],
"name": "hashLeftRight",
"outputs": [ "outputs": [
{ {
"internalType": "uint256[]", "internalType": "bytes32",
"name": "", "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, "payable": false,
@ -69,9 +125,30 @@
"constant": true, "constant": true,
"inputs": [ "inputs": [
{ {
"internalType": "uint256", "internalType": "bytes32",
"name": "_root",
"type": "bytes32"
}
],
"name": "isKnownRoot",
"outputs": [
{
"internalType": "bool",
"name": "", "name": "",
"type": "uint256" "type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
} }
], ],
"name": "commitments", "name": "commitments",
@ -89,22 +166,7 @@
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [],
"name": "zeros", "name": "denomination",
"outputs": [
{
"internalType": "uint256[]",
"name": "",
"type": "uint256[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "levels",
"outputs": [ "outputs": [
{ {
"internalType": "uint256", "internalType": "uint256",
@ -119,63 +181,7 @@
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [],
"name": "operator", "name": "currentRootIndex",
"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",
"outputs": [ "outputs": [
{ {
"internalType": "uint32", "internalType": "uint32",
@ -190,7 +196,79 @@
{ {
"constant": true, "constant": true,
"inputs": [], "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": [ "outputs": [
{ {
"internalType": "uint256", "internalType": "uint256",
@ -204,14 +282,8 @@
}, },
{ {
"constant": true, "constant": true,
"inputs": [ "inputs": [],
{ "name": "isDepositsDisabled",
"internalType": "uint256",
"name": "root",
"type": "uint256"
}
],
"name": "isKnownRoot",
"outputs": [ "outputs": [
{ {
"internalType": "bool", "internalType": "bool",
@ -225,15 +297,21 @@
}, },
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [
"name": "getLastRoot",
"outputs": [
{ {
"internalType": "uint256", "internalType": "uint256",
"name": "", "name": "",
"type": "uint256" "type": "uint256"
} }
], ],
"name": "filledSubtrees",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"payable": false, "payable": false,
"stateMutability": "view", "stateMutability": "view",
"type": "function" "type": "function"
@ -241,12 +319,12 @@
{ {
"constant": true, "constant": true,
"inputs": [], "inputs": [],
"name": "mixDenomination", "name": "nextIndex",
"outputs": [ "outputs": [
{ {
"internalType": "uint256", "internalType": "uint32",
"name": "", "name": "",
"type": "uint256" "type": "uint32"
} }
], ],
"payable": false, "payable": false,
@ -256,27 +334,22 @@
{ {
"inputs": [ "inputs": [
{ {
"internalType": "address", "internalType": "contract IVerifier",
"name": "_verifier", "name": "_verifier",
"type": "address" "type": "address"
}, },
{ {
"internalType": "uint256", "internalType": "uint256",
"name": "_mixDenomination", "name": "_denomination",
"type": "uint256" "type": "uint256"
}, },
{ {
"internalType": "uint8", "internalType": "uint32",
"name": "_merkleTreeHeight", "name": "_merkleTreeHeight",
"type": "uint8" "type": "uint32"
}, },
{ {
"internalType": "uint256", "internalType": "address",
"name": "_emptyElement",
"type": "uint256"
},
{
"internalType": "address payable",
"name": "_operator", "name": "_operator",
"type": "address" "type": "address"
} }
@ -290,15 +363,15 @@
"inputs": [ "inputs": [
{ {
"indexed": true, "indexed": true,
"internalType": "uint256", "internalType": "bytes32",
"name": "commitment", "name": "commitment",
"type": "uint256" "type": "bytes32"
}, },
{ {
"indexed": false, "indexed": false,
"internalType": "uint256", "internalType": "uint32",
"name": "leafIndex", "name": "leafIndex",
"type": "uint256" "type": "uint32"
}, },
{ {
"indexed": false, "indexed": false,
@ -321,9 +394,9 @@
}, },
{ {
"indexed": false, "indexed": false,
"internalType": "uint256", "internalType": "bytes32",
"name": "nullifierHash", "name": "nullifierHash",
"type": "uint256" "type": "bytes32"
}, },
{ {
"indexed": true, "indexed": true,
@ -338,16 +411,16 @@
"type": "uint256" "type": "uint256"
} }
], ],
"name": "Withdraw", "name": "Withdrawal",
"type": "event" "type": "event"
}, },
{ {
"constant": false, "constant": false,
"inputs": [ "inputs": [
{ {
"internalType": "uint256", "internalType": "bytes32",
"name": "commitment", "name": "_commitment",
"type": "uint256" "type": "bytes32"
} }
], ],
"name": "deposit", "name": "deposit",
@ -360,63 +433,54 @@
"constant": false, "constant": false,
"inputs": [ "inputs": [
{ {
"internalType": "uint256[2]", "internalType": "bytes",
"name": "a", "name": "_proof",
"type": "uint256[2]" "type": "bytes"
}, },
{ {
"internalType": "uint256[2][2]", "internalType": "bytes32",
"name": "b", "name": "_root",
"type": "uint256[2][2]" "type": "bytes32"
}, },
{ {
"internalType": "uint256[2]", "internalType": "bytes32",
"name": "c", "name": "_nullifierHash",
"type": "uint256[2]" "type": "bytes32"
}, },
{ {
"internalType": "uint256[5]", "internalType": "address payable",
"name": "input", "name": "_recipient",
"type": "uint256[5]" "type": "address"
},
{
"internalType": "address payable",
"name": "_relayer",
"type": "address"
},
{
"internalType": "uint256",
"name": "_fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_refund",
"type": "uint256"
} }
], ],
"name": "withdraw", "name": "withdraw",
"outputs": [], "outputs": [],
"payable": false, "payable": true,
"stateMutability": "nonpayable", "stateMutability": "payable",
"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",
"type": "function" "type": "function"
}, },
{ {
"constant": true, "constant": true,
"inputs": [ "inputs": [
{ {
"internalType": "uint256", "internalType": "bytes32",
"name": "nullifier", "name": "_nullifierHash",
"type": "uint256" "type": "bytes32"
} }
], ],
"name": "isSpent", "name": "isSpent",
@ -430,5 +494,59 @@
"payable": false, "payable": false,
"stateMutability": "view", "stateMutability": "view",
"type": "function" "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) { 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) => { app.post('/relay', async (req, resp) => {
let { valid , reason } = isValidProof(req.body.proof) let { valid , reason } = isValidProof(req.body.proof)
if (!valid) { if (!valid) {
console.log('Proof is invalid:', reason) 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 let currency
@ -55,51 +55,55 @@ app.post('/relay', async (req, resp) => {
} }
let { proof, publicSignals } = req.body.proof let { proof, publicSignals } = req.body.proof
const args = {
const relayer = toChecksumAddress(`0x${publicSignals[3].slice(26)}`) root: publicSignals[0],
if (relayer !== web3.eth.defaultAccount) { nullifierHash: publicSignals[1],
console.log('This proof is for different relayer:', relayer) 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)
return resp.status(400).json({ error: 'Relayer address is invalid' }) 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')) const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000'))
let desiredFee let desiredFee
switch (currency) { switch (currency) {
case 'eth': { case 'eth': {
if (!refund.isZero()) { if (!args.refund.isZero()) {
return resp.status(400).json({ error: 'Cannot send refund for eth currency.' }) return resp.status(400).json({ error: 'Cannot send refund for eth currency.' })
} }
desiredFee = expense desiredFee = expense
break break
} }
case 'dai': { 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 break
} }
} }
if (fee.lt(desiredFee)) { if (args.fee.lt(desiredFee)) {
console.log('Fee is too low') console.log('Fee is too low')
return resp.status(400).json({ error: 'Fee is too low. Try to resend.' }) return resp.status(400).json({ error: 'Fee is too low. Try to resend.' })
} }
try { try {
const mixer = new web3.eth.Contract(mixerABI, req.body.contract) const mixer = new web3.eth.Contract(mixerABI, req.body.contract)
const nullifier = publicSignals[1] const isSpent = await mixer.methods.isSpent(args.nullifierHash).call()
const isSpent = await mixer.methods.isSpent(nullifier).call()
if (isSpent) { if (isSpent) {
return resp.status(400).json({ error: 'The note has been spent.' }) return resp.status(400).json({ error: 'The note has been spent.' })
} }
const root = publicSignals[0] const isKnownRoot = await mixer.methods.isKnownRoot(args.root).call()
const isKnownRoot = await mixer.methods.isKnownRoot(root).call()
if (!isKnownRoot) { if (!isKnownRoot) {
return resp.status(400).json({ error: 'The merkle root is too old or invalid.' }) 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 gas = await mixer.methods.withdraw(proof, ...publicSignals).estimateGas({ from: web3.eth.defaultAccount, value: args.refund })
const result = mixer.methods.withdraw(proof, publicSignals).send({ const result = mixer.methods.withdraw(proof, ...publicSignals).send({
value: refund, from: web3.eth.defaultAccount,
value: args.refund,
gas: numberToHex(gas + 50000), gas: numberToHex(gas + 50000),
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')), gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
// TODO: nonce // 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)", "author": "Alexey Pertsev <alexey@peppersec.com> (https://peppersec.com)",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"dotenv": "^8.0.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"web3": "^1.0.0-beta.55", "web3": "^1.2.2",
"web3-utils": "^1.0.0" "web3-utils": "^1.2.2"
}, },
"devDependencies": { "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 // 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' } return { valid: false, reason: 'One of inputs is empty. There must be proof and publicSignals' }
} }
Object.keys(proof).forEach(key => { if (!isHexStrict(data.proof) || data.proof.length !== 2 + 2 * 8 * 32) {
if (!Array.isArray(proof[key])) {
return { valid: false, reason: `Corrupted ${key}` }
}
})
if (proof.proof.length !== 8) {
return { valid: false, reason: 'Corrupted proof' } return { valid: false, reason: 'Corrupted proof' }
} }
if (proof.publicSignals.length !== 6) { if (data.publicSignals.length !== 6) {
return { valid: false, reason: 'Corrupted publicSignals' } return { valid: false, reason: 'Corrupted publicSignals' }
} }
for (let [key, input] of Object.entries(proof)) { for(let signal of data.publicSignals) {
for (let i = 0; i < input.length; i++ ) { if (!isHexStrict(signal)) {
if (!isHexStrict(input[i]) || input[i].length !== 66) { return { valid: false, reason: 'Corrupted publicSignals' }
return { valid: false, reason: `Corrupted ${key}` }
}
} }
} }
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 } return { valid: true }
} }