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