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