tornado-core/contracts/Mixer.sol

108 lines
4.5 KiB
Solidity
Raw Normal View History

2019-08-02 13:12:30 -04:00
// https://tornado.cash
/*
* d888888P dP a88888b. dP
* 88 88 d8' `88 88
* 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b.
* 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88
* 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88
* dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP
* ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
*/
2019-07-09 09:05:30 -04:00
pragma solidity ^0.5.8;
import "./MerkleTreeWithHistory.sol";
contract IVerifier {
2019-09-23 14:45:20 -04:00
function verifyProof(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[3] memory input) public returns(bool);
2019-07-09 09:05:30 -04:00
}
contract Mixer is MerkleTreeWithHistory {
2019-08-01 04:41:22 -04:00
bool public isDepositsEnabled = true;
2019-08-02 13:12:30 -04:00
// operator can disable new deposits in case of emergency
// it also receives a relayer fee
address payable public operator;
2019-07-25 09:58:21 -04:00
mapping(uint256 => bool) public nullifierHashes;
2019-07-15 11:04:48 -04:00
// we store all commitments just to prevent accidental deposits with the same commitment
mapping(uint256 => bool) public commitments;
2019-08-04 13:57:01 -04:00
IVerifier public verifier;
2019-09-06 17:22:30 -04:00
uint256 public mixDenomination;
2019-07-09 09:05:30 -04:00
2019-07-22 16:37:02 -04:00
event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp);
2019-09-23 14:45:20 -04:00
event Withdraw(address to, uint256 nullifierHash, address indexed relayer);
2019-07-09 09:05:30 -04:00
2019-07-10 08:35:46 -04:00
/**
@dev The constructor
@param _verifier the address of SNARK verifier for this contract
2019-08-27 16:42:24 -04:00
@param _merkleTreeHeight the height of deposits' Merkle Tree
@param _emptyElement default element of the deposits' Merkle Tree
@param _operator operator address (see operator above)
2019-07-10 08:35:46 -04:00
*/
2019-07-12 11:04:45 -04:00
constructor(
address _verifier,
2019-09-06 17:22:30 -04:00
uint256 _mixDenomination,
2019-07-12 11:04:45 -04:00
uint8 _merkleTreeHeight,
2019-08-01 04:41:22 -04:00
uint256 _emptyElement,
2019-08-02 13:12:30 -04:00
address payable _operator
2019-07-12 11:04:45 -04:00
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
2019-07-09 09:05:30 -04:00
verifier = IVerifier(_verifier);
2019-08-02 13:12:30 -04:00
operator = _operator;
2019-09-06 17:22:30 -04:00
mixDenomination = _mixDenomination;
2019-07-09 09:05:30 -04:00
}
2019-09-06 17:22:30 -04:00
/**
@dev Deposit funds into mixer. The caller must send value equal to `mixDenomination` of this mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
/**
@dev Deposit funds into the mixer. The caller must send ETH value equal to `userEther` of this mixer.
The caller also has to have at least `mixDenomination` amount approved for the mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
2019-08-01 04:41:22 -04:00
require(isDepositsEnabled, "deposits disabled");
2019-07-15 11:04:48 -04:00
require(!commitments[commitment], "The commitment has been submitted");
2019-09-06 17:22:30 -04:00
_processDeposit();
2019-07-10 12:58:21 -04:00
_insert(commitment);
2019-07-15 11:04:48 -04:00
commitments[commitment] = true;
2019-07-09 09:05:30 -04:00
2019-09-06 17:22:30 -04:00
emit Deposit(commitment, next_index - 1, block.timestamp);
}
/**
@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
- hash of unique deposit nullifier to prevent double spends
- the receiver of funds
- optional fee that goes to the transaction sender (usually a relay)
*/
2019-09-23 14:45:20 -04:00
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[3] memory input) public {
2019-07-09 09:05:30 -04:00
uint256 root = input[0];
2019-07-25 09:58:21 -04:00
uint256 nullifierHash = input[1];
2019-09-06 17:22:30 -04:00
address payable receiver = address(input[2]);
2019-07-25 09:58:21 -04:00
require(!nullifierHashes[nullifierHash], "The note has been already spent");
2019-08-20 16:39:21 -04:00
2019-07-09 09:05:30 -04:00
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
2019-07-12 17:50:26 -04:00
require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof");
2019-07-25 09:58:21 -04:00
nullifierHashes[nullifierHash] = true;
2019-09-23 14:45:20 -04:00
_processWithdraw(receiver);
emit Withdraw(receiver, nullifierHash, receiver);
2019-07-09 09:05:30 -04:00
}
2019-07-15 12:15:06 -04:00
2019-08-01 04:41:22 -04:00
function toggleDeposits() external {
2019-08-02 13:12:30 -04:00
require(msg.sender == operator, "unauthorized");
2019-08-01 04:41:22 -04:00
isDepositsEnabled = !isDepositsEnabled;
}
2019-08-02 13:12:30 -04:00
function changeOperator(address payable _newAccount) external {
require(msg.sender == operator, "unauthorized");
operator = _newAccount;
2019-08-01 12:58:34 -04:00
}
2019-07-15 12:15:06 -04:00
function isSpent(uint256 nullifier) public view returns(bool) {
2019-07-25 09:58:21 -04:00
return nullifierHashes[nullifier];
2019-07-15 12:15:06 -04:00
}
2019-09-06 17:22:30 -04:00
function _processDeposit() internal {}
2019-09-23 14:45:20 -04:00
function _processWithdraw(address payable _receiver) internal {}
2019-07-10 12:58:21 -04:00
}