tornado-core/contracts/Mixer.sol

141 lines
5.6 KiB
Solidity
Raw Normal View History

2019-08-02 19:12:30 +02: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 15:05:30 +02:00
pragma solidity ^0.5.8;
import "./MerkleTreeWithHistory.sol";
contract IVerifier {
function verifyProof(uint256[8] memory proof, uint256[6] memory input) public returns(bool);
2019-07-09 15:05:30 +02:00
}
contract Mixer is MerkleTreeWithHistory {
2019-10-04 16:27:47 +02:00
uint256 public denomination;
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-08-04 19:57:01 +02:00
IVerifier public verifier;
2019-10-04 16:27:47 +02:00
// operator can
// - receive a relayer fee
// - disable new deposits in case of emergency
// - update snark verification key until this ability is permanently disabled
2019-11-01 01:56:24 +01:00
address public operator;
2019-10-04 16:27:47 +02:00
bool public isDepositsEnabled = true;
bool public isVerifierUpdateAllowed = true;
modifier onlyOperator {
require(msg.sender == operator, "Only operator can call this function.");
_;
}
2019-07-09 15:05:30 +02:00
2019-07-22 22:37:02 +02:00
event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp);
2019-09-10 15:31:34 +02:00
event Withdraw(address to, uint256 nullifierHash, address indexed relayer, 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
2019-08-27 22:42:24 +02: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 14:35:46 +02:00
*/
2019-07-12 17:04:45 +02:00
constructor(
address _verifier,
2019-10-04 16:27:47 +02:00
uint256 _denomination,
2019-07-12 17:04:45 +02:00
uint8 _merkleTreeHeight,
2019-08-01 10:41:22 +02:00
uint256 _emptyElement,
2019-11-01 01:56:24 +01:00
address _operator
2019-07-12 17:04:45 +02:00
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
2019-07-09 15:05:30 +02:00
verifier = IVerifier(_verifier);
2019-08-02 19:12:30 +02:00
operator = _operator;
2019-10-04 16:27:47 +02:00
denomination = _denomination;
2019-07-09 15:05:30 +02:00
}
2019-10-04 16:27:47 +02:00
2019-09-06 23:22:30 +02:00
/**
2019-10-04 16:27:47 +02:00
@dev Deposit funds into mixer. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this mixer.
2019-09-06 23:22:30 +02:00
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
2019-10-04 16:27:47 +02:00
require(isDepositsEnabled, "deposits are disabled");
2019-07-15 17:04:48 +02:00
require(!commitments[commitment], "The commitment has been submitted");
2019-11-01 02:07:11 +01:00
uint256 insertedIndex = _insert(commitment);
2019-07-15 17:04:48 +02:00
commitments[commitment] = true;
_processDeposit();
2019-07-09 15:05:30 +02:00
2019-11-01 02:07:11 +01:00
emit Deposit(commitment, insertedIndex, block.timestamp);
2019-09-06 23:22:30 +02:00
}
2019-10-04 16:27:47 +02:00
/** @dev this function is defined in a child contract */
function _processDeposit() internal {}
2019-09-06 23:22:30 +02:00
/**
@dev Withdraw deposit from the mixer. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs
2019-09-06 23:22:30 +02:00
`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)
*/
function withdraw(uint256[8] memory proof, uint256[6] memory input) public payable {
2019-07-09 15:05:30 +02:00
uint256 root = input[0];
2019-07-25 15:58:21 +02:00
uint256 nullifierHash = input[1];
2019-09-06 23:22:30 +02:00
address payable receiver = address(input[2]);
2019-09-06 22:54:37 +02:00
address payable relayer = address(input[3]);
uint256 fee = input[4];
uint256 refund = input[5];
2019-11-01 01:54:29 +01:00
require(fee <= denomination, "Fee exceeds transfer value");
2019-07-25 15:58:21 +02:00
require(!nullifierHashes[nullifierHash], "The note has been already spent");
2019-08-20 22:39:21 +02:00
2019-07-09 15:05:30 +02:00
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
2019-10-04 17:20:20 +02:00
require(verifier.verifyProof(proof, input), "Invalid withdraw proof");
2019-07-25 15:58:21 +02:00
nullifierHashes[nullifierHash] = true;
_processWithdraw(receiver, relayer, fee, refund);
2019-09-06 22:54:37 +02:00
emit Withdraw(receiver, nullifierHash, relayer, fee);
2019-07-09 15:05:30 +02:00
}
2019-07-15 18:15:06 +02:00
2019-10-04 16:27:47 +02:00
/** @dev this function is defined in a child contract */
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal {}
2019-10-04 16:27:47 +02:00
/** @dev whether a note is already spent */
function isSpent(uint256 nullifier) public view returns(bool) {
return nullifierHashes[nullifier];
}
/**
@dev Allow operator to temporarily disable new deposits. This is needed to protect users funds in case a vulnerability is discovered.
It does not affect existing deposits.
*/
function toggleDeposits() external onlyOperator {
2019-08-01 10:41:22 +02:00
isDepositsEnabled = !isDepositsEnabled;
}
2019-10-04 16:27:47 +02:00
/**
@dev allow operator to update SNARK verification keys. This is needed to update keys after the final trusted setup ceremony is held.
After that operator is supposed to permanently disable this ability.
*/
function updateVerifier(address newVerifier) external onlyOperator {
require(isVerifierUpdateAllowed, "Verifier updates have been disabled.");
verifier = IVerifier(newVerifier);
}
2019-10-04 16:27:47 +02:00
/**
@dev an option for operator to permanently disable verification keys update ability.
This is supposed to be called after the final trusted setup ceremony is held.
*/
function disableVerifierUpdate() external onlyOperator {
isVerifierUpdateAllowed = false;
}
2019-10-04 16:27:47 +02:00
/** @dev operator can change his address */
2019-11-01 01:56:24 +01:00
function changeOperator(address _newAccount) external onlyOperator {
2019-08-02 19:12:30 +02:00
operator = _newAccount;
2019-08-01 18:58:34 +02:00
}
2019-07-10 18:58:21 +02:00
}