tornado-core/contracts/Tornado.sol

131 lines
5.0 KiB
Solidity
Raw Normal View History

2019-08-02 17:12:30 +00:00
// https://tornado.cash
/*
2021-02-11 06:23:18 +00:00
* 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-08-02 17:12:30 +00:00
2021-02-11 06:03:43 +00:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
2019-07-09 13:05:30 +00:00
import "./MerkleTreeWithHistory.sol";
2019-11-11 16:12:17 +00:00
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
2019-07-09 13:05:30 +00:00
2021-02-11 06:03:43 +00:00
interface IVerifier {
2021-02-11 06:23:18 +00:00
function verifyProof(bytes memory _proof, uint256[6] memory _input) external returns (bool);
2019-07-09 13:05:30 +00:00
}
2021-02-11 06:03:43 +00:00
abstract contract Tornado is MerkleTreeWithHistory, ReentrancyGuard {
2019-10-04 14:27:47 +00:00
uint256 public denomination;
2019-11-04 21:04:22 +00:00
mapping(bytes32 => bool) public nullifierHashes;
2019-07-15 15:04:48 +00:00
// we store all commitments just to prevent accidental deposits with the same commitment
2019-11-04 21:04:22 +00:00
mapping(bytes32 => bool) public commitments;
2021-02-11 05:37:18 +00:00
IVerifier public immutable verifier;
2019-10-04 14:27:47 +00:00
2019-11-08 23:48:35 +00:00
// operator can update snark verification key
// after the final trusted setup ceremony operator rights are supposed to be transferred to zero address
2019-11-01 00:56:24 +00:00
address public operator;
2019-10-04 14:27:47 +00:00
modifier onlyOperator {
require(msg.sender == operator, "Only operator can call this function.");
_;
}
2019-07-09 13:05:30 +00:00
2019-11-04 21:04:22 +00:00
event Deposit(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp);
event Withdrawal(address to, bytes32 nullifierHash, address indexed relayer, uint256 fee);
2019-07-09 13:05:30 +00:00
2019-07-10 12:35:46 +00:00
/**
@dev The constructor
@param _verifier the address of SNARK verifier for this contract
2021-02-11 06:03:43 +00:00
@param _hasher the address of MiMC hash contract
2019-11-04 19:45:56 +00:00
@param _denomination transfer amount for each deposit
2019-08-27 20:42:24 +00:00
@param _merkleTreeHeight the height of deposits' Merkle Tree
2019-07-10 12:35:46 +00:00
*/
2019-07-12 15:04:45 +00:00
constructor(
2019-11-01 01:14:01 +00:00
IVerifier _verifier,
2021-02-11 05:37:18 +00:00
Hasher _hasher,
2019-10-04 14:27:47 +00:00
uint256 _denomination,
2021-02-11 05:37:18 +00:00
uint32 _merkleTreeHeight
2021-02-11 06:23:18 +00:00
) public MerkleTreeWithHistory(_merkleTreeHeight, _hasher) {
2019-11-01 01:15:55 +00:00
require(_denomination > 0, "denomination should be greater than 0");
2019-11-01 01:14:01 +00:00
verifier = _verifier;
2019-10-04 14:27:47 +00:00
denomination = _denomination;
2019-07-09 13:05:30 +00:00
}
2019-10-04 14:27:47 +00:00
2019-09-06 21:22:30 +00:00
/**
2019-12-13 13:49:19 +00:00
@dev Deposit funds into the contract. The caller must send (for ETH) or approve (for ERC20) value equal to or `denomination` of this instance.
@param _commitment the note commitment, which is PedersenHash(nullifier + secret)
2019-09-06 21:22:30 +00:00
*/
2019-11-14 17:49:34 +00:00
function deposit(bytes32 _commitment) external payable nonReentrant {
require(!commitments[_commitment], "The commitment has been submitted");
2019-11-04 19:42:41 +00:00
uint32 insertedIndex = _insert(_commitment);
commitments[_commitment] = true;
_processDeposit();
2019-07-09 13:05:30 +00:00
emit Deposit(_commitment, insertedIndex, block.timestamp);
2019-09-06 21:22:30 +00:00
}
2019-10-04 14:27:47 +00:00
/** @dev this function is defined in a child contract */
2021-02-11 05:37:18 +00:00
function _processDeposit() internal virtual;
2019-10-04 14:27:47 +00:00
2019-09-06 21:22:30 +00:00
/**
2019-12-13 13:49:19 +00:00
@dev Withdraw a deposit from the contract. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs
2019-09-06 21:22:30 +00:00
`input` array consists of:
2019-12-13 13:49:19 +00:00
- merkle root of all deposits in the contract
2019-09-06 21:22:30 +00:00
- hash of unique deposit nullifier to prevent double spends
2019-11-07 07:04:29 +00:00
- the recipient of funds
2019-09-06 21:22:30 +00:00
- optional fee that goes to the transaction sender (usually a relay)
*/
2021-02-11 06:23:18 +00:00
function withdraw(
bytes calldata _proof,
bytes32 _root,
bytes32 _nullifierHash,
address payable _recipient,
address payable _relayer,
uint256 _fee,
uint256 _refund
) external payable nonReentrant {
2019-11-04 19:42:41 +00:00
require(_fee <= denomination, "Fee exceeds transfer value");
require(!nullifierHashes[_nullifierHash], "The note has been already spent");
require(isKnownRoot(_root), "Cannot find your merkle root"); // Make sure to use a recent one
2021-02-11 06:23:18 +00:00
require(
verifier.verifyProof(
_proof,
[uint256(_root), uint256(_nullifierHash), uint256(_recipient), uint256(_relayer), _fee, _refund]
),
"Invalid withdraw proof"
);
2019-11-04 19:42:41 +00:00
nullifierHashes[_nullifierHash] = true;
2019-11-07 07:04:29 +00:00
_processWithdraw(_recipient, _relayer, _fee, _refund);
emit Withdrawal(_recipient, _nullifierHash, _relayer, _fee);
2019-07-09 13:05:30 +00:00
}
2019-07-15 16:15:06 +00:00
2019-10-04 14:27:47 +00:00
/** @dev this function is defined in a child contract */
2021-02-11 06:23:18 +00:00
function _processWithdraw(
address payable _recipient,
address payable _relayer,
uint256 _fee,
uint256 _refund
) internal virtual;
2019-10-04 14:27:47 +00:00
/** @dev whether a note is already spent */
2021-02-11 06:23:18 +00:00
function isSpent(bytes32 _nullifierHash) public view returns (bool) {
return nullifierHashes[_nullifierHash];
2019-10-04 14:27:47 +00:00
}
2019-11-28 03:45:52 +00:00
/** @dev whether an array of notes is already spent */
2021-02-11 06:23:18 +00:00
function isSpentArray(bytes32[] calldata _nullifierHashes) external view returns (bool[] memory spent) {
2019-11-28 03:45:52 +00:00
spent = new bool[](_nullifierHashes.length);
2021-02-11 06:23:18 +00:00
for (uint256 i = 0; i < _nullifierHashes.length; i++) {
2019-11-28 03:45:52 +00:00
if (isSpent(_nullifierHashes[i])) {
spent[i] = true;
}
}
}
2019-07-10 16:58:21 +00:00
}