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.

84 lines
2.2 KiB

  1. pragma solidity ^0.6.0;
  2. import './deposit-verifier.sol';
  3. import './withdraw-verifier.sol';
  4. contract Miksi {
  5. DepositVerifier dVerifier;
  6. WithdrawVerifier wVerifier;
  7. uint256 key = 0;
  8. uint256 amount = uint256(1000000000000000000);
  9. uint256 root ;
  10. uint256[] commitments;
  11. mapping(uint256 => bool) nullifiers;
  12. constructor( address _depositVerifierContractAddr, address _withdrawVerifierContractAddr) public {
  13. dVerifier = DepositVerifier(_depositVerifierContractAddr);
  14. wVerifier = WithdrawVerifier(_withdrawVerifierContractAddr);
  15. root = uint256(7191590165524151132621032034309259185021876706372059338263145339926209741311);
  16. }
  17. function deposit(
  18. uint256 _commitment,
  19. uint256 _root,
  20. uint[2] memory a,
  21. uint[2][2] memory b,
  22. uint[2] memory c
  23. ) public payable {
  24. // check root state transition update with zkp
  25. uint256[6] memory input = [
  26. 0,
  27. msg.value,
  28. root, // rootOld
  29. _root, // rootNew
  30. _commitment,
  31. key+1
  32. ];
  33. require(dVerifier.verifyProof(a, b, c, input), "zkProof deposit could not be verified");
  34. require(msg.value==amount, "value should be 1 ETH"); // this can be flexible with a wrapper with preset fixed amounts
  35. commitments.push(_commitment);
  36. root = _root;
  37. key += 1;
  38. }
  39. function getCommitments() public view returns (uint256[] memory, uint256, uint256) {
  40. return (commitments, root, key+1);
  41. }
  42. function withdraw(
  43. address payable _address,
  44. uint256 nullifier,
  45. uint[2] memory a,
  46. uint[2][2] memory b,
  47. uint[2] memory c
  48. ) public {
  49. uint256[5] memory input = [
  50. 0,
  51. amount,
  52. nullifier,
  53. root,
  54. uint256(_address)
  55. ];
  56. require(wVerifier.verifyProof(a, b, c, input), "zkProof withdraw could not be verified");
  57. // zk verification passed
  58. require(useNullifier(nullifier), "nullifier already used");
  59. // nullifier check passed
  60. // proceed with the withdraw
  61. _address.send(amount);
  62. // _address.call.value(amount).gas(20317)();
  63. }
  64. function useNullifier(
  65. uint256 nullifier
  66. ) internal returns (bool) {
  67. if (nullifiers[nullifier]) {
  68. return false;
  69. }
  70. nullifiers[nullifier] = true;
  71. return true;
  72. }
  73. }