You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.3 KiB

  1. const Verifier = artifacts.require("../../contracts/Verifier");
  2. const Miksi = artifacts.require("../../contracts/Miksi.sol");
  3. const chai = require("chai");
  4. const expect = chai.expect;
  5. const fs = require("fs");
  6. const { groth } = require('snarkjs');
  7. const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
  8. const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
  9. const circomlib = require("circomlib");
  10. contract("miksi", (accounts) => {
  11. const {
  12. 0: owner,
  13. 1: addr1, // used for the deposit
  14. 2: addr2, // used for the withdraw
  15. 3: addr3,
  16. } = accounts;
  17. let insVerifier;
  18. let insMiksi;
  19. before(async () => {
  20. insVerifier = await Verifier.new();
  21. insMiksi = await Miksi.new(insVerifier.address);
  22. });
  23. it("miksi flow", async () => {
  24. const secret = "123456789";
  25. const coinCode = "0"; // refearing to ETH
  26. const ethAmount = '0.5';
  27. const amount = web3.utils.toWei(ethAmount, 'ether');
  28. let balance_wei = await web3.eth.getBalance(addr1);
  29. console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether'));
  30. expect(balance_wei).to.be.equal('100000000000000000000');
  31. const poseidon = circomlib.poseidon.createHash(6, 8, 57);
  32. const commitment = poseidon([coinCode, amount, secret]).toString();
  33. // deposit
  34. console.log("Deposit of " + ethAmount + " ETH from " + addr1);
  35. await insMiksi.deposit(coinCode, commitment, {from: addr1, value: amount});
  36. balance_wei = await web3.eth.getBalance(addr1);
  37. console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether'));
  38. expect(balance_wei).to.be.equal('99499141300000000000');
  39. // getDeposit data
  40. const res = await insMiksi.getDeposit(commitment);
  41. expect(res[0].toString()).to.be.equal(coinCode);
  42. expect(res[1].toString()).to.be.equal(amount);
  43. // calculate witness
  44. const wasm = await fs.promises.readFile("./build/withdraw.wasm");
  45. const input = unstringifyBigInts({
  46. "coinCode": coinCode,
  47. "amount": amount,
  48. "commitment": commitment,
  49. "secret": secret,
  50. "address": addr2
  51. });
  52. const options = {};
  53. console.log("Calculate witness");
  54. const wc = await WitnessCalculatorBuilder(wasm, options);
  55. const w = await wc.calculateWitness(input);
  56. const witness = unstringifyBigInts(stringifyBigInts(w));
  57. // generate zkproof of commitment using snarkjs (as is a test)
  58. const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/proving_key.json", "utf8")));
  59. console.log("Generate zkSNARK proof");
  60. const {proof, publicSignals} = groth.genProof(provingKey, witness);
  61. // withdraw
  62. console.log("Withdraw of " + ethAmount + " ETH to " + addr2);
  63. const resW = await insMiksi.withdraw(
  64. commitment,
  65. addr2,
  66. [proof.pi_a[0].toString(), proof.pi_a[1].toString()],
  67. [
  68. [proof.pi_b[0][1].toString(), proof.pi_b[0][0].toString()],
  69. [proof.pi_b[1][1].toString(), proof.pi_b[1][0].toString()]
  70. ],
  71. [proof.pi_c[0].toString(), proof.pi_c[1].toString()]
  72. );
  73. // console.log("resW", resW);
  74. balance_wei = await web3.eth.getBalance(addr2);
  75. console.log("Balance at " + addr2, web3.utils.fromWei(balance_wei, 'ether'));
  76. expect(balance_wei).to.be.equal('100500000000000000000');
  77. });
  78. });