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.

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