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.

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