2019-07-09 15:05:30 +02:00
|
|
|
pragma solidity ^0.5.8;
|
|
|
|
|
|
|
|
import "./MerkleTreeWithHistory.sol";
|
|
|
|
|
|
|
|
contract IVerifier {
|
2019-07-12 23:50:26 +02:00
|
|
|
function verifyProof(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public returns(bool);
|
2019-07-09 15:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
contract Mixer is MerkleTreeWithHistory {
|
|
|
|
uint256 public transferValue;
|
2019-08-01 10:41:22 +02:00
|
|
|
bool public isDepositsEnabled = true;
|
|
|
|
address public pauseAccount;
|
2019-07-25 15:58:21 +02:00
|
|
|
mapping(uint256 => bool) public nullifierHashes;
|
2019-07-15 17:04:48 +02:00
|
|
|
// we store all commitments just to prevent accidental deposits with the same commitment
|
|
|
|
mapping(uint256 => bool) public commitments;
|
2019-07-09 15:05:30 +02:00
|
|
|
IVerifier verifier;
|
|
|
|
|
2019-07-22 22:37:02 +02:00
|
|
|
event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp);
|
2019-07-25 15:58:21 +02:00
|
|
|
event Withdraw(address to, uint256 nullifierHash, uint256 fee);
|
2019-07-09 15:05:30 +02:00
|
|
|
|
2019-07-10 14:35:46 +02:00
|
|
|
/**
|
|
|
|
@dev The constructor
|
|
|
|
@param _verifier the address of SNARK verifier for this contract
|
|
|
|
@param _transferValue the value for all deposits in this contract in wei
|
|
|
|
*/
|
2019-07-12 17:04:45 +02:00
|
|
|
constructor(
|
|
|
|
address _verifier,
|
|
|
|
uint256 _transferValue,
|
|
|
|
uint8 _merkleTreeHeight,
|
2019-08-01 10:41:22 +02:00
|
|
|
uint256 _emptyElement,
|
|
|
|
address _pauseAccount
|
2019-07-12 17:04:45 +02:00
|
|
|
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
|
2019-07-09 15:05:30 +02:00
|
|
|
verifier = IVerifier(_verifier);
|
|
|
|
transferValue = _transferValue;
|
2019-08-01 10:41:22 +02:00
|
|
|
pauseAccount = _pauseAccount;
|
2019-07-09 15:05:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 14:35:46 +02:00
|
|
|
/**
|
|
|
|
@dev Deposit funds into mixer. The caller must send value equal to `transferValue` of this mixer.
|
|
|
|
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
|
|
|
|
*/
|
2019-07-09 15:05:30 +02:00
|
|
|
function deposit(uint256 commitment) public payable {
|
2019-08-01 10:41:22 +02:00
|
|
|
require(isDepositsEnabled, "deposits disabled");
|
2019-07-09 15:05:30 +02:00
|
|
|
require(msg.value == transferValue, "Please send `transferValue` ETH along with transaction");
|
2019-07-15 17:04:48 +02:00
|
|
|
require(!commitments[commitment], "The commitment has been submitted");
|
2019-07-10 18:58:21 +02:00
|
|
|
_insert(commitment);
|
2019-07-15 17:04:48 +02:00
|
|
|
commitments[commitment] = true;
|
2019-07-22 22:37:02 +02:00
|
|
|
emit Deposit(commitment, next_index - 1, block.timestamp);
|
2019-07-09 15:05:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 14:35:46 +02:00
|
|
|
/**
|
|
|
|
@dev Withdraw deposit from the mixer. `a`, `b`, and `c` are zkSNARK proof data, and input is an array of circuit public inputs
|
|
|
|
`input` array consists of:
|
|
|
|
- merkle root of all deposits in the mixer
|
2019-07-25 15:58:21 +02:00
|
|
|
- hash of unique deposit nullifier to prevent double spends
|
2019-07-10 14:35:46 +02:00
|
|
|
- the receiver of funds
|
|
|
|
- optional fee that goes to the transaction sender (usually a relay)
|
|
|
|
*/
|
2019-07-09 15:05:30 +02:00
|
|
|
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public {
|
|
|
|
uint256 root = input[0];
|
2019-07-25 15:58:21 +02:00
|
|
|
uint256 nullifierHash = input[1];
|
2019-07-09 15:05:30 +02:00
|
|
|
address payable receiver = address(input[2]);
|
|
|
|
uint256 fee = input[3];
|
|
|
|
|
|
|
|
require(fee < transferValue, "Fee exceeds transfer value");
|
2019-07-25 15:58:21 +02:00
|
|
|
require(!nullifierHashes[nullifierHash], "The note has been already spent");
|
2019-07-09 15:05:30 +02:00
|
|
|
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
|
2019-07-12 23:50:26 +02:00
|
|
|
require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof");
|
2019-07-09 15:05:30 +02:00
|
|
|
|
2019-07-25 15:58:21 +02:00
|
|
|
nullifierHashes[nullifierHash] = true;
|
2019-07-09 15:05:30 +02:00
|
|
|
receiver.transfer(transferValue - fee);
|
|
|
|
if (fee > 0) {
|
|
|
|
msg.sender.transfer(fee);
|
|
|
|
}
|
2019-07-25 15:58:21 +02:00
|
|
|
emit Withdraw(receiver, nullifierHash, fee);
|
2019-07-09 15:05:30 +02:00
|
|
|
}
|
2019-07-15 18:15:06 +02:00
|
|
|
|
2019-08-01 10:41:22 +02:00
|
|
|
function toggleDeposits() external {
|
|
|
|
require(msg.sender == pauseAccount, "unauthorized");
|
|
|
|
isDepositsEnabled = !isDepositsEnabled;
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:58:34 +02:00
|
|
|
function setPauseAccount(address _newAccount) external {
|
|
|
|
require(msg.sender == pauseAccount, "unauthorized");
|
|
|
|
pauseAccount = _newAccount;
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:15:06 +02:00
|
|
|
function isSpent(uint256 nullifier) public view returns(bool) {
|
2019-07-25 15:58:21 +02:00
|
|
|
return nullifierHashes[nullifier];
|
2019-07-15 18:15:06 +02:00
|
|
|
}
|
2019-07-10 18:58:21 +02:00
|
|
|
}
|