relay wip

This commit is contained in:
poma 2019-07-16 18:38:55 +03:00
parent 6e05a678dd
commit f3b1b7f96e
3 changed files with 46 additions and 1 deletions

View File

@ -26,6 +26,7 @@
"circom": "0.0.30", "circom": "0.0.30",
"circomlib": "^0.0.10", "circomlib": "^0.0.10",
"dotenv": "^8.0.0", "dotenv": "^8.0.0",
"express": "^4.17.1",
"ganache-cli": "^6.4.5", "ganache-cli": "^6.4.5",
"snarkjs": "git+https://github.com/iden3/snarkjs.git#5fe2bd4642ec567c75ad5ac3f73687999c412e73", "snarkjs": "git+https://github.com/iden3/snarkjs.git#5fe2bd4642ec567c75ad5ac3f73687999c412e73",
"truffle": "^5.0.27", "truffle": "^5.0.27",

44
relay/relay.js Normal file
View File

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

View File

@ -10,7 +10,7 @@ const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBi
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)); const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes));
const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0]; const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0];
async function snarkVerify(proof) { function snarkVerify(proof) {
proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof)); proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof));
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json')); const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'));
return groth.isValid(verification_key, proof, proof.publicSignals); return groth.isValid(verification_key, proof, proof.publicSignals);