erc20 mixer support WIP

This commit is contained in:
Alexey 2019-08-20 23:39:21 +03:00
parent 51b06ed661
commit b8142d03bb
11 changed files with 282 additions and 17 deletions

53
contracts/ERC20Mixer.sol Normal file
View file

@ -0,0 +1,53 @@
// 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
*/
pragma solidity ^0.5.8;
import "./Mixer.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract ERC20Mixer is Mixer {
IERC20 public token;
constructor(
address _verifier,
uint256 _transferValue,
uint8 _merkleTreeHeight,
uint256 _emptyElement,
address payable _operator,
IERC20 _token
) Mixer(_verifier, _transferValue, _merkleTreeHeight, _emptyElement, _operator) public {
token = _token;
}
function deposit(uint256 commitment) public {
require(token.transferFrom(msg.sender, address(this), transferValue), "Approve before using");
_deposit(commitment);
emit Deposit(commitment, next_index - 1, block.timestamp);
}
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public {
_withdraw(a, b, c, input);
address receiver = address(input[2]);
uint256 fee = input[3];
uint256 nullifierHash = input[1];
require(fee < transferValue, "Fee exceeds transfer value");
token.transfer(receiver, transferValue - fee);
if (fee > 0) {
token.transfer(operator, fee);
}
emit Withdraw(receiver, nullifierHash, fee);
}
}

47
contracts/ETHMixer.sol Normal file
View file

@ -0,0 +1,47 @@
// 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
*/
pragma solidity ^0.5.8;
import "./Mixer.sol";
contract ETHMixer is Mixer {
constructor(
address _verifier,
uint256 _transferValue,
uint8 _merkleTreeHeight,
uint256 _emptyElement,
address payable _operator
) Mixer(_verifier, _transferValue, _merkleTreeHeight, _emptyElement, _operator) public {}
function deposit(uint256 commitment) public payable {
require(msg.value == transferValue, "Please send `transferValue` ETH along with transaction");
_deposit(commitment);
emit Deposit(commitment, next_index - 1, block.timestamp);
}
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public {
_withdraw(a, b, c, input);
address payable receiver = address(input[2]);
uint256 fee = input[3];
uint256 nullifierHash = input[1];
require(fee < transferValue, "Fee exceeds transfer value");
receiver.transfer(transferValue - fee);
if (fee > 0) {
operator.transfer(fee);
}
emit Withdraw(receiver, nullifierHash, fee);
}
}

View file

@ -52,13 +52,11 @@ contract Mixer is MerkleTreeWithHistory {
@dev Deposit funds into mixer. The caller must send value equal to `transferValue` of this mixer.
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
function _deposit(uint256 commitment) internal {
require(isDepositsEnabled, "deposits disabled");
require(msg.value == transferValue, "Please send `transferValue` ETH along with transaction");
require(!commitments[commitment], "The commitment has been submitted");
_insert(commitment);
commitments[commitment] = true;
emit Deposit(commitment, next_index - 1, block.timestamp);
}
/**
@ -69,23 +67,16 @@ contract Mixer is MerkleTreeWithHistory {
- the receiver of funds
- optional fee that goes to the transaction sender (usually a relay)
*/
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) public {
function _withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[4] memory input) internal {
uint256 root = input[0];
uint256 nullifierHash = input[1];
address payable receiver = address(input[2]);
uint256 fee = input[3];
require(!nullifierHashes[nullifierHash], "The note has been already spent");
require(fee < transferValue, "Fee exceeds transfer value");
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof");
nullifierHashes[nullifierHash] = true;
receiver.transfer(transferValue - fee);
if (fee > 0) {
operator.transfer(fee);
}
emit Withdraw(receiver, nullifierHash, fee);
}
function toggleDeposits() external {

View file

@ -0,0 +1,10 @@
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
contract ERC20Mock is ERC20Detailed, ERC20Mintable {
constructor() ERC20Detailed("DAIMock", "DAIM", 18) public {
}
}