2019-07-16 18:34:34 +02:00
|
|
|
let bigInt = require('snarkjs/src/bigint')
|
2019-07-16 17:38:55 +02:00
|
|
|
|
2019-07-16 18:34:34 +02:00
|
|
|
const express = require('express')
|
|
|
|
const app = express()
|
|
|
|
app.use(express.json())
|
2019-07-16 17:38:55 +02:00
|
|
|
|
2019-07-16 18:34:34 +02:00
|
|
|
const Web3 = require('web3')
|
|
|
|
const web3 = new Web3('http://localhost:8545', null, {transactionConfirmationBlocks: 1})
|
|
|
|
const contractJson = require('../build/contracts/Mixer.json')
|
|
|
|
let netId = 42
|
|
|
|
const mixer = new web3.eth.Contract(contractJson.abi, contractJson.networks[netId].address)
|
2019-07-16 17:38:55 +02:00
|
|
|
|
|
|
|
function getMinimumFee() {
|
|
|
|
// todo calc acceptable fee
|
2019-07-16 18:34:34 +02:00
|
|
|
return 1e16
|
2019-07-16 17:38:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
app.post('/deposit', async (req, resp) => {
|
2019-07-16 18:34:34 +02:00
|
|
|
let proof = req.body
|
2019-07-16 17:38:55 +02:00
|
|
|
if (!(proof.pi_a && proof.pi_b && proof.pi_c && proof.publicSignals)) { // check that it's kinda well formed
|
2019-07-16 18:34:34 +02:00
|
|
|
resp.status(400).end()
|
2019-07-16 17:38:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bigInt(proof.publicSignals[3]) < getMinimumFee()) {
|
2019-07-16 18:34:34 +02:00
|
|
|
resp.status(403).send('Fee is too low')
|
2019-07-16 17:38:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!utils.snarkVerify(proof)) {
|
2019-07-16 18:34:34 +02:00
|
|
|
resp.status(403).send('Invalid snark proof')
|
2019-07-16 17:38:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-07-16 18:34:34 +02:00
|
|
|
let receipt = await mixer.withdraw(proof.pi_a, proof.pi_b, proof.pi_b, proof.publicSignals)
|
|
|
|
console.log(receipt)
|
|
|
|
resp.send({ transaction: receipt.transactionHash })
|
2019-07-16 17:38:55 +02:00
|
|
|
} catch (e) {
|
2019-07-16 18:34:34 +02:00
|
|
|
console.log(e)
|
|
|
|
resp.status(400).send('Transaction was reverted')
|
2019-07-16 17:38:55 +02:00
|
|
|
}
|
2019-07-16 18:34:34 +02:00
|
|
|
})
|
|
|
|
app.listen(3000)
|