2019-08-02 13:12:30 -04:00
|
|
|
// https://tornado.cash
|
|
|
|
/*
|
2021-02-11 01:23:18 -05: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 13:12:30 -04:00
|
|
|
|
2021-02-11 01:03:43 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-03-11 15:05:59 -05:00
|
|
|
pragma solidity ^0.7.0;
|
2019-07-09 09:05:30 -04:00
|
|
|
|
|
|
|
import "./MerkleTreeWithHistory.sol";
|
2019-11-11 11:12:17 -05:00
|
|
|
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
2019-07-09 09:05:30 -04:00
|
|
|
|
2021-02-11 01:03:43 -05:00
|
|
|
interface IVerifier {
|
2021-02-11 01:23:18 -05:00
|
|
|
function verifyProof(bytes memory _proof, uint256[6] memory _input) external returns (bool);
|
2019-07-09 09:05:30 -04:00
|
|
|
}
|
|
|
|
|
2021-02-11 01:03:43 -05:00
|
|
|
abstract contract Tornado is MerkleTreeWithHistory, ReentrancyGuard {
|
2021-03-11 14:51:09 -05:00
|
|
|
IVerifier public immutable verifier;
|
|
|
|
uint256 public immutable denomination;
|
|
|
|
|
2019-11-04 16:04:22 -05:00
|
|
|
mapping(bytes32 => bool) public nullifierHashes;
|
2019-07-15 11:04:48 -04:00
|
|
|
// we store all commitments just to prevent accidental deposits with the same commitment
|
2019-11-04 16:04:22 -05:00
|
|
|
mapping(bytes32 => bool) public commitments;
|
2019-07-09 09:05:30 -04:00
|
|
|
|
2019-11-04 16:04:22 -05:00
|
|
|
event Deposit(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp);
|
|
|
|
event Withdrawal(address to, bytes32 nullifierHash, address indexed relayer, uint256 fee);
|
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
|
2021-02-11 01:03:43 -05:00
|
|
|
@param _hasher the address of MiMC hash contract
|
2019-11-04 14:45:56 -05:00
|
|
|
@param _denomination transfer amount for each deposit
|
2019-08-27 16:42:24 -04:00
|
|
|
@param _merkleTreeHeight the height of deposits' Merkle Tree
|
2019-07-10 08:35:46 -04:00
|
|
|
*/
|
2019-07-12 11:04:45 -04:00
|
|
|
constructor(
|
2019-10-31 21:14:01 -04:00
|
|
|
IVerifier _verifier,
|
2021-02-11 02:00:53 -05:00
|
|
|
IHasher _hasher,
|
2019-10-04 10:27:47 -04:00
|
|
|
uint256 _denomination,
|
2021-02-11 00:37:18 -05:00
|
|
|
uint32 _merkleTreeHeight
|
2021-03-11 15:05:59 -05:00
|
|
|
) MerkleTreeWithHistory(_merkleTreeHeight, _hasher) {
|
2019-10-31 21:15:55 -04:00
|
|
|
require(_denomination > 0, "denomination should be greater than 0");
|
2019-10-31 21:14:01 -04:00
|
|
|
verifier = _verifier;
|
2019-10-04 10:27:47 -04:00
|
|
|
denomination = _denomination;
|
2019-07-09 09:05:30 -04:00
|
|
|
}
|
2019-10-04 10:27:47 -04:00
|
|
|
|
2019-09-06 17:22:30 -04:00
|
|
|
/**
|
2019-12-13 08:49:19 -05: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.
|
2019-11-02 08:48:22 -04:00
|
|
|
@param _commitment the note commitment, which is PedersenHash(nullifier + secret)
|
2019-09-06 17:22:30 -04:00
|
|
|
*/
|
2019-11-14 12:49:34 -05:00
|
|
|
function deposit(bytes32 _commitment) external payable nonReentrant {
|
2019-11-02 08:48:22 -04:00
|
|
|
require(!commitments[_commitment], "The commitment has been submitted");
|
2019-11-04 14:42:41 -05:00
|
|
|
|
2019-11-03 05:11:22 -05:00
|
|
|
uint32 insertedIndex = _insert(_commitment);
|
2019-11-02 08:48:22 -04:00
|
|
|
commitments[_commitment] = true;
|
2019-10-31 21:00:32 -04:00
|
|
|
_processDeposit();
|
2019-07-09 09:05:30 -04:00
|
|
|
|
2019-11-02 08:48:22 -04:00
|
|
|
emit Deposit(_commitment, insertedIndex, block.timestamp);
|
2019-09-06 17:22:30 -04:00
|
|
|
}
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
/** @dev this function is defined in a child contract */
|
2021-02-11 00:37:18 -05:00
|
|
|
function _processDeposit() internal virtual;
|
2019-10-04 10:27:47 -04:00
|
|
|
|
2019-09-06 17:22:30 -04:00
|
|
|
/**
|
2019-12-13 08:49:19 -05: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 17:22:30 -04:00
|
|
|
`input` array consists of:
|
2019-12-13 08:49:19 -05:00
|
|
|
- merkle root of all deposits in the contract
|
2019-09-06 17:22:30 -04:00
|
|
|
- hash of unique deposit nullifier to prevent double spends
|
2019-11-07 02:04:29 -05:00
|
|
|
- the recipient of funds
|
2019-09-06 17:22:30 -04:00
|
|
|
- optional fee that goes to the transaction sender (usually a relay)
|
|
|
|
*/
|
2021-02-11 01:23:18 -05: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 14:42:41 -05: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 01:23:18 -05:00
|
|
|
require(
|
|
|
|
verifier.verifyProof(
|
|
|
|
_proof,
|
|
|
|
[uint256(_root), uint256(_nullifierHash), uint256(_recipient), uint256(_relayer), _fee, _refund]
|
|
|
|
),
|
|
|
|
"Invalid withdraw proof"
|
|
|
|
);
|
2019-11-04 14:42:41 -05:00
|
|
|
|
|
|
|
nullifierHashes[_nullifierHash] = true;
|
2019-11-07 02:04:29 -05:00
|
|
|
_processWithdraw(_recipient, _relayer, _fee, _refund);
|
|
|
|
emit Withdrawal(_recipient, _nullifierHash, _relayer, _fee);
|
2019-07-09 09:05:30 -04:00
|
|
|
}
|
2019-07-15 12:15:06 -04:00
|
|
|
|
2019-10-04 10:27:47 -04:00
|
|
|
/** @dev this function is defined in a child contract */
|
2021-02-11 01:23:18 -05:00
|
|
|
function _processWithdraw(
|
|
|
|
address payable _recipient,
|
|
|
|
address payable _relayer,
|
|
|
|
uint256 _fee,
|
|
|
|
uint256 _refund
|
|
|
|
) internal virtual;
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
/** @dev whether a note is already spent */
|
2021-02-11 01:23:18 -05:00
|
|
|
function isSpent(bytes32 _nullifierHash) public view returns (bool) {
|
2019-11-02 08:48:22 -04:00
|
|
|
return nullifierHashes[_nullifierHash];
|
2019-10-04 10:27:47 -04:00
|
|
|
}
|
|
|
|
|
2019-11-27 22:45:52 -05:00
|
|
|
/** @dev whether an array of notes is already spent */
|
2021-02-11 01:23:18 -05:00
|
|
|
function isSpentArray(bytes32[] calldata _nullifierHashes) external view returns (bool[] memory spent) {
|
2019-11-27 22:45:52 -05:00
|
|
|
spent = new bool[](_nullifierHashes.length);
|
2021-02-11 01:23:18 -05:00
|
|
|
for (uint256 i = 0; i < _nullifierHashes.length; i++) {
|
2019-11-27 22:45:52 -05:00
|
|
|
if (isSpent(_nullifierHashes[i])) {
|
|
|
|
spent[i] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-10 12:58:21 -04:00
|
|
|
}
|