2021-06-06 19:31:32 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-08 11:41:12 +02:00
|
|
|
// https://tornado.cash
|
|
|
|
/*
|
2021-06-16 02:31:31 +02: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
|
|
|
|
*/
|
2020-04-08 11:41:12 +02:00
|
|
|
|
2021-07-06 23:10:18 +02:00
|
|
|
pragma solidity ^0.7.0;
|
2021-06-07 12:12:15 +02:00
|
|
|
pragma experimental ABIEncoderV2;
|
2021-09-29 21:39:30 +02:00
|
|
|
|
2021-10-06 16:51:46 +02:00
|
|
|
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
2021-10-12 17:50:25 +02:00
|
|
|
import { IERC20Receiver, IERC6777, IOmniBridge } from "./interfaces/IBridge.sol";
|
|
|
|
import { CrossChainGuard } from "./bridge/CrossChainGuard.sol";
|
2021-10-06 16:51:46 +02:00
|
|
|
import { IVerifier } from "./interfaces/IVerifier.sol";
|
2021-09-26 18:14:05 +02:00
|
|
|
import "./MerkleTreeWithHistory.sol";
|
2020-04-08 11:41:12 +02:00
|
|
|
|
2021-10-26 12:10:30 +02:00
|
|
|
/** @dev This contract(pool) allows deposit of an arbitrary amount to it, shielded transfer to another registered user inside the pool
|
|
|
|
* and withdrawal from the pool. Project utilizes UTXO model to handle users' funds.
|
|
|
|
*/
|
2021-10-12 17:50:25 +02:00
|
|
|
contract TornadoPool is MerkleTreeWithHistory, IERC20Receiver, ReentrancyGuard, CrossChainGuard {
|
2021-08-16 21:17:07 +02:00
|
|
|
int256 public constant MAX_EXT_AMOUNT = 2**248;
|
2021-08-16 18:53:18 +02:00
|
|
|
uint256 public constant MAX_FEE = 2**248;
|
2022-01-24 18:16:55 +01:00
|
|
|
uint256 public constant MIN_EXT_AMOUNT_LIMIT = 0.5 ether;
|
2020-04-08 11:41:12 +02:00
|
|
|
|
2021-06-16 13:01:29 +02:00
|
|
|
IVerifier public immutable verifier2;
|
|
|
|
IVerifier public immutable verifier16;
|
2021-09-29 21:39:30 +02:00
|
|
|
IERC6777 public immutable token;
|
|
|
|
address public immutable omniBridge;
|
2021-09-30 17:34:07 +02:00
|
|
|
address public immutable l1Unwrapper;
|
2021-10-29 12:20:25 +02:00
|
|
|
address public immutable multisig;
|
2020-04-08 11:41:12 +02:00
|
|
|
|
2021-10-06 16:51:46 +02:00
|
|
|
uint256 public lastBalance;
|
|
|
|
uint256 public minimalWithdrawalAmount;
|
|
|
|
uint256 public maximumDepositAmount;
|
2021-10-04 12:06:58 +02:00
|
|
|
mapping(bytes32 => bool) public nullifierHashes;
|
|
|
|
|
2021-06-07 12:12:15 +02:00
|
|
|
struct ExtData {
|
2021-09-29 21:39:30 +02:00
|
|
|
address recipient;
|
2021-08-16 18:53:18 +02:00
|
|
|
int256 extAmount;
|
2021-09-29 21:39:30 +02:00
|
|
|
address relayer;
|
2021-08-13 19:07:53 +02:00
|
|
|
uint256 fee;
|
2021-06-07 12:12:15 +02:00
|
|
|
bytes encryptedOutput1;
|
|
|
|
bytes encryptedOutput2;
|
2021-09-30 17:34:07 +02:00
|
|
|
bool isL1Withdrawal;
|
2022-01-22 01:21:34 +01:00
|
|
|
uint256 l1Fee;
|
2021-06-07 12:12:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 16:01:22 +02:00
|
|
|
struct Proof {
|
|
|
|
bytes proof;
|
|
|
|
bytes32 root;
|
|
|
|
bytes32[] inputNullifiers;
|
|
|
|
bytes32[2] outputCommitments;
|
2021-08-13 19:07:53 +02:00
|
|
|
uint256 publicAmount;
|
2021-07-22 16:01:22 +02:00
|
|
|
bytes32 extDataHash;
|
|
|
|
}
|
|
|
|
|
2021-10-04 18:28:08 +02:00
|
|
|
struct Account {
|
|
|
|
address owner;
|
|
|
|
bytes publicKey;
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:31:31 +02:00
|
|
|
event NewCommitment(bytes32 commitment, uint256 index, bytes encryptedOutput);
|
2020-04-08 11:41:12 +02:00
|
|
|
event NewNullifier(bytes32 nullifier);
|
2021-06-21 19:05:36 +02:00
|
|
|
event PublicKey(address indexed owner, bytes key);
|
2020-04-08 11:41:12 +02:00
|
|
|
|
2021-10-29 12:20:25 +02:00
|
|
|
modifier onlyMultisig() {
|
|
|
|
require(msg.sender == multisig, "only governance");
|
|
|
|
_;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:41:12 +02:00
|
|
|
/**
|
|
|
|
@dev The constructor
|
2021-06-16 10:28:39 +02:00
|
|
|
@param _verifier2 the address of SNARK verifier for 2 inputs
|
|
|
|
@param _verifier16 the address of SNARK verifier for 16 inputs
|
2021-10-26 12:10:30 +02:00
|
|
|
@param _levels hight of the commitments merkle tree
|
|
|
|
@param _hasher hasher address for the merkle tree
|
|
|
|
@param _token token address for the pool
|
|
|
|
@param _omniBridge omniBridge address for specified token
|
|
|
|
@param _l1Unwrapper address of the L1Helper
|
|
|
|
@param _governance owner address
|
|
|
|
@param _l1ChainId chain id of L1
|
2021-10-29 12:20:25 +02:00
|
|
|
@param _multisig multisig on L2
|
2020-04-08 11:41:12 +02:00
|
|
|
*/
|
2021-09-26 18:14:05 +02:00
|
|
|
constructor(
|
|
|
|
IVerifier _verifier2,
|
|
|
|
IVerifier _verifier16,
|
|
|
|
uint32 _levels,
|
2021-09-29 18:41:56 +02:00
|
|
|
address _hasher,
|
2021-09-29 21:39:30 +02:00
|
|
|
IERC6777 _token,
|
2021-09-30 17:34:07 +02:00
|
|
|
address _omniBridge,
|
2021-10-05 14:12:39 +02:00
|
|
|
address _l1Unwrapper,
|
2021-10-12 17:50:25 +02:00
|
|
|
address _governance,
|
2021-10-29 12:20:25 +02:00
|
|
|
uint256 _l1ChainId,
|
|
|
|
address _multisig
|
2021-10-12 17:50:25 +02:00
|
|
|
)
|
|
|
|
MerkleTreeWithHistory(_levels, _hasher)
|
|
|
|
CrossChainGuard(address(IOmniBridge(_omniBridge).bridgeContract()), _l1ChainId, _governance)
|
|
|
|
{
|
2021-06-15 13:25:06 +02:00
|
|
|
verifier2 = _verifier2;
|
|
|
|
verifier16 = _verifier16;
|
2021-09-27 10:55:14 +02:00
|
|
|
token = _token;
|
2021-09-29 21:39:30 +02:00
|
|
|
omniBridge = _omniBridge;
|
2021-09-30 17:34:07 +02:00
|
|
|
l1Unwrapper = _l1Unwrapper;
|
2021-10-29 12:20:25 +02:00
|
|
|
multisig = _multisig;
|
2021-10-06 16:51:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function initialize(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) external initializer {
|
|
|
|
_configureLimits(_minimalWithdrawalAmount, _maximumDepositAmount);
|
|
|
|
super._initialize();
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:10:30 +02:00
|
|
|
/** @dev Main function that allows deposits, transfers and withdrawal.
|
|
|
|
*/
|
2021-09-29 21:39:30 +02:00
|
|
|
function transact(Proof memory _args, ExtData memory _extData) public {
|
|
|
|
if (_extData.extAmount > 0) {
|
|
|
|
// for deposits from L2
|
|
|
|
token.transferFrom(msg.sender, address(this), uint256(_extData.extAmount));
|
2021-10-06 16:51:46 +02:00
|
|
|
require(uint256(_extData.extAmount) <= maximumDepositAmount, "amount is larger than maximumDepositAmount");
|
2021-09-29 21:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_transact(_args, _extData);
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:46:29 +02:00
|
|
|
function register(Account memory _account) public {
|
|
|
|
require(_account.owner == msg.sender, "only owner can be registered");
|
|
|
|
_register(_account);
|
|
|
|
}
|
2021-07-22 16:01:22 +02:00
|
|
|
|
2021-10-21 18:46:29 +02:00
|
|
|
function registerAndTransact(
|
|
|
|
Account memory _account,
|
|
|
|
Proof memory _proofArgs,
|
|
|
|
ExtData memory _extData
|
|
|
|
) public {
|
|
|
|
register(_account);
|
|
|
|
transact(_proofArgs, _extData);
|
|
|
|
}
|
2021-06-16 10:28:39 +02:00
|
|
|
|
2021-10-21 18:46:29 +02:00
|
|
|
function onTokenBridged(
|
|
|
|
IERC6777 _token,
|
|
|
|
uint256 _amount,
|
|
|
|
bytes calldata _data
|
|
|
|
) external override {
|
|
|
|
(Proof memory _args, ExtData memory _extData) = abi.decode(_data, (Proof, ExtData));
|
|
|
|
require(_token == token, "provided token is not supported");
|
|
|
|
require(msg.sender == omniBridge, "only omni bridge");
|
|
|
|
require(_amount >= uint256(_extData.extAmount), "amount from bridge is incorrect");
|
|
|
|
require(token.balanceOf(address(this)) >= uint256(_extData.extAmount) + lastBalance, "bridge did not send enough tokens");
|
|
|
|
require(uint256(_extData.extAmount) <= maximumDepositAmount, "amount is larger than maximumDepositAmount");
|
2021-10-29 12:20:25 +02:00
|
|
|
uint256 sentAmount = token.balanceOf(address(this)) - lastBalance;
|
2021-10-29 17:36:38 +02:00
|
|
|
try TornadoPool(address(this)).onTransact(_args, _extData) {} catch (bytes memory) {
|
2021-10-29 12:20:25 +02:00
|
|
|
token.transfer(multisig, sentAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 17:36:38 +02:00
|
|
|
/**
|
|
|
|
* @dev Wrapper for the internal func _transact to call it using try-catch from onTokenBridged
|
|
|
|
*/
|
|
|
|
function onTransact(Proof memory _args, ExtData memory _extData) external {
|
2021-10-29 12:20:25 +02:00
|
|
|
require(msg.sender == address(this), "can be called only from onTokenBridged");
|
2021-10-21 18:46:29 +02:00
|
|
|
_transact(_args, _extData);
|
|
|
|
}
|
2021-06-16 10:28:39 +02:00
|
|
|
|
2021-10-29 12:20:25 +02:00
|
|
|
/// @dev Method to claim junk and accidentally sent tokens
|
|
|
|
function rescueTokens(
|
|
|
|
IERC6777 _token,
|
|
|
|
address payable _to,
|
|
|
|
uint256 _balance
|
|
|
|
) external onlyMultisig {
|
|
|
|
require(_to != address(0), "TORN: can not send to zero address");
|
|
|
|
require(_token != token, "can not rescue pool asset");
|
|
|
|
|
|
|
|
if (_token == IERC6777(0)) {
|
|
|
|
// for Ether
|
|
|
|
uint256 totalBalance = address(this).balance;
|
|
|
|
uint256 balance = _balance == 0 ? totalBalance : _balance;
|
|
|
|
_to.transfer(balance);
|
|
|
|
} else {
|
|
|
|
// any other erc20
|
|
|
|
uint256 totalBalance = _token.balanceOf(address(this));
|
|
|
|
uint256 balance = _balance == 0 ? totalBalance : _balance;
|
|
|
|
require(balance > 0, "TORN: trying to send 0 balance");
|
|
|
|
_token.transfer(_to, balance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:56:28 +01:00
|
|
|
function configureLimits(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) public onlyMultisig {
|
2021-10-21 18:46:29 +02:00
|
|
|
_configureLimits(_minimalWithdrawalAmount, _maximumDepositAmount);
|
2021-06-16 10:28:39 +02:00
|
|
|
}
|
|
|
|
|
2021-08-19 12:31:58 +02:00
|
|
|
function calculatePublicAmount(int256 _extAmount, uint256 _fee) public pure returns (uint256) {
|
2021-08-16 21:17:07 +02:00
|
|
|
require(_fee < MAX_FEE, "Invalid fee");
|
|
|
|
require(_extAmount > -MAX_EXT_AMOUNT && _extAmount < MAX_EXT_AMOUNT, "Invalid ext amount");
|
|
|
|
int256 publicAmount = _extAmount - int256(_fee);
|
|
|
|
return (publicAmount >= 0) ? uint256(publicAmount) : FIELD_SIZE - uint256(-publicAmount);
|
2021-06-16 10:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @dev whether a note is already spent */
|
|
|
|
function isSpent(bytes32 _nullifierHash) public view returns (bool) {
|
|
|
|
return nullifierHashes[_nullifierHash];
|
|
|
|
}
|
|
|
|
|
2021-09-27 10:55:14 +02:00
|
|
|
function verifyProof(Proof memory _args) public view returns (bool) {
|
2021-07-22 16:01:22 +02:00
|
|
|
if (_args.inputNullifiers.length == 2) {
|
2021-06-16 10:28:39 +02:00
|
|
|
return
|
2021-06-16 02:31:31 +02:00
|
|
|
verifier2.verifyProof(
|
2021-07-22 16:01:22 +02:00
|
|
|
_args.proof,
|
2021-06-16 02:31:31 +02:00
|
|
|
[
|
2021-07-22 16:01:22 +02:00
|
|
|
uint256(_args.root),
|
2021-08-13 19:07:53 +02:00
|
|
|
_args.publicAmount,
|
2021-07-22 16:01:22 +02:00
|
|
|
uint256(_args.extDataHash),
|
|
|
|
uint256(_args.inputNullifiers[0]),
|
|
|
|
uint256(_args.inputNullifiers[1]),
|
|
|
|
uint256(_args.outputCommitments[0]),
|
2021-09-26 18:14:05 +02:00
|
|
|
uint256(_args.outputCommitments[1])
|
2021-06-16 02:31:31 +02:00
|
|
|
]
|
2021-06-16 10:28:39 +02:00
|
|
|
);
|
2021-07-22 16:01:22 +02:00
|
|
|
} else if (_args.inputNullifiers.length == 16) {
|
2021-06-16 10:28:39 +02:00
|
|
|
return
|
2021-06-16 02:31:31 +02:00
|
|
|
verifier16.verifyProof(
|
2021-07-22 16:01:22 +02:00
|
|
|
_args.proof,
|
2021-06-16 02:31:31 +02:00
|
|
|
[
|
2021-07-22 16:01:22 +02:00
|
|
|
uint256(_args.root),
|
2021-08-13 19:07:53 +02:00
|
|
|
_args.publicAmount,
|
2021-07-22 16:01:22 +02:00
|
|
|
uint256(_args.extDataHash),
|
|
|
|
uint256(_args.inputNullifiers[0]),
|
|
|
|
uint256(_args.inputNullifiers[1]),
|
|
|
|
uint256(_args.inputNullifiers[2]),
|
|
|
|
uint256(_args.inputNullifiers[3]),
|
|
|
|
uint256(_args.inputNullifiers[4]),
|
|
|
|
uint256(_args.inputNullifiers[5]),
|
|
|
|
uint256(_args.inputNullifiers[6]),
|
|
|
|
uint256(_args.inputNullifiers[7]),
|
|
|
|
uint256(_args.inputNullifiers[8]),
|
|
|
|
uint256(_args.inputNullifiers[9]),
|
|
|
|
uint256(_args.inputNullifiers[10]),
|
|
|
|
uint256(_args.inputNullifiers[11]),
|
|
|
|
uint256(_args.inputNullifiers[12]),
|
|
|
|
uint256(_args.inputNullifiers[13]),
|
|
|
|
uint256(_args.inputNullifiers[14]),
|
|
|
|
uint256(_args.inputNullifiers[15]),
|
|
|
|
uint256(_args.outputCommitments[0]),
|
2021-09-26 18:14:05 +02:00
|
|
|
uint256(_args.outputCommitments[1])
|
2021-06-16 02:31:31 +02:00
|
|
|
]
|
2021-06-16 10:28:39 +02:00
|
|
|
);
|
2021-06-15 13:47:54 +02:00
|
|
|
} else {
|
|
|
|
revert("unsupported input count");
|
|
|
|
}
|
2020-04-08 11:41:12 +02:00
|
|
|
}
|
2021-06-21 19:05:36 +02:00
|
|
|
|
2021-10-04 18:28:08 +02:00
|
|
|
function _register(Account memory _account) internal {
|
|
|
|
emit PublicKey(_account.owner, _account.publicKey);
|
2021-07-22 16:01:22 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:46:29 +02:00
|
|
|
function _transact(Proof memory _args, ExtData memory _extData) internal nonReentrant {
|
|
|
|
require(isKnownRoot(_args.root), "Invalid merkle root");
|
|
|
|
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
|
|
|
|
require(!isSpent(_args.inputNullifiers[i]), "Input is already spent");
|
|
|
|
}
|
|
|
|
require(uint256(_args.extDataHash) == uint256(keccak256(abi.encode(_extData))) % FIELD_SIZE, "Incorrect external data hash");
|
|
|
|
require(_args.publicAmount == calculatePublicAmount(_extData.extAmount, _extData.fee), "Invalid public amount");
|
|
|
|
require(verifyProof(_args), "Invalid transaction proof");
|
2021-09-27 10:55:14 +02:00
|
|
|
|
2021-10-21 18:46:29 +02:00
|
|
|
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
|
|
|
|
nullifierHashes[_args.inputNullifiers[i]] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_extData.extAmount < 0) {
|
|
|
|
require(_extData.recipient != address(0), "Can't withdraw to zero address");
|
|
|
|
if (_extData.isL1Withdrawal) {
|
2022-01-22 01:23:33 +01:00
|
|
|
token.transferAndCall(
|
|
|
|
omniBridge,
|
|
|
|
uint256(-_extData.extAmount),
|
|
|
|
abi.encodePacked(l1Unwrapper, _extData.recipient, _extData.l1Fee)
|
|
|
|
);
|
2021-10-21 18:46:29 +02:00
|
|
|
} else {
|
|
|
|
token.transfer(_extData.recipient, uint256(-_extData.extAmount));
|
|
|
|
}
|
|
|
|
require(uint256(-_extData.extAmount) >= minimalWithdrawalAmount, "amount is less than minimalWithdrawalAmount"); // prevents ddos attack to Bridge
|
|
|
|
}
|
|
|
|
if (_extData.fee > 0) {
|
|
|
|
token.transfer(_extData.relayer, _extData.fee);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBalance = token.balanceOf(address(this));
|
|
|
|
_insert(_args.outputCommitments[0], _args.outputCommitments[1]);
|
|
|
|
emit NewCommitment(_args.outputCommitments[0], nextIndex - 2, _extData.encryptedOutput1);
|
|
|
|
emit NewCommitment(_args.outputCommitments[1], nextIndex - 1, _extData.encryptedOutput2);
|
|
|
|
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
|
|
|
|
emit NewNullifier(_args.inputNullifiers[i]);
|
|
|
|
}
|
2021-09-27 10:55:14 +02:00
|
|
|
}
|
2021-10-05 14:12:39 +02:00
|
|
|
|
2021-10-06 16:51:46 +02:00
|
|
|
function _configureLimits(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) internal {
|
2022-01-24 18:16:55 +01:00
|
|
|
require(_minimalWithdrawalAmount < MIN_EXT_AMOUNT_LIMIT, "minimalWithdrawal over limit");
|
2021-10-06 16:51:46 +02:00
|
|
|
minimalWithdrawalAmount = _minimalWithdrawalAmount;
|
|
|
|
maximumDepositAmount = _maximumDepositAmount;
|
2021-10-05 14:12:39 +02:00
|
|
|
}
|
2020-04-08 11:41:12 +02:00
|
|
|
}
|