update for new mixer changes

This commit is contained in:
poma 2019-11-08 04:25:43 +03:00
parent 2e4977a12b
commit ff2202e2b0
3 changed files with 40 additions and 33 deletions

2
.gitignore vendored
View File

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

View File

@ -44,7 +44,7 @@ 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,54 @@ app.post('/relay', async (req, resp) => {
} }
let { proof, publicSignals } = req.body.proof 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 (args.relayer !== web3.eth.defaultAccount) {
if (relayer !== web3.eth.defaultAccount) { console.log('This proof is for different relayer:', args.relayer)
console.log('This proof is for different relayer:', 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({ value: args.refund })
const result = mixer.methods.withdraw(proof, publicSignals).send({ const result = mixer.methods.withdraw(proof, ...publicSignals).send({
value: refund, 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

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