Merge pull request #5 from peppersec/add_pause

add pauseDeposits
This commit is contained in:
Roman Storm 2019-08-01 10:00:07 -07:00 committed by GitHub
commit 42c65b54d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1657 additions and 12 deletions

View File

@ -3,4 +3,3 @@ MERKLE_TREE_HEIGHT=16
AMOUNT=1000000000000000000
EMPTY_ELEMENT=1337
PRIVATE_KEY=

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
build
.vscode
/index.js
Mixer_flat.sol
# Created by .ignore support plugin (hsz.mobi)
### Node template

6
.solhint.json Normal file
View File

@ -0,0 +1,6 @@
{
"extends": "solhint:recommended",
"rules": {
"indent": ["error", 2]
}
}

View File

@ -8,6 +8,8 @@ contract IVerifier {
contract Mixer is MerkleTreeWithHistory {
uint256 public transferValue;
bool public isDepositsEnabled = true;
address public pauseAccount;
mapping(uint256 => bool) public nullifierHashes;
// we store all commitments just to prevent accidental deposits with the same commitment
mapping(uint256 => bool) public commitments;
@ -25,10 +27,12 @@ contract Mixer is MerkleTreeWithHistory {
address _verifier,
uint256 _transferValue,
uint8 _merkleTreeHeight,
uint256 _emptyElement
uint256 _emptyElement,
address _pauseAccount
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
verifier = IVerifier(_verifier);
transferValue = _transferValue;
pauseAccount = _pauseAccount;
}
/**
@ -36,6 +40,7 @@ contract Mixer is MerkleTreeWithHistory {
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
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);
@ -70,6 +75,16 @@ contract Mixer is MerkleTreeWithHistory {
emit Withdraw(receiver, nullifierHash, fee);
}
function toggleDeposits() external {
require(msg.sender == pauseAccount, "unauthorized");
isDepositsEnabled = !isDepositsEnabled;
}
function setPauseAccount(address _newAccount) external {
require(msg.sender == pauseAccount, "unauthorized");
pauseAccount = _newAccount;
}
function isSpent(uint256 nullifier) public view returns(bool) {
return nullifierHashes[nullifier];
}

View File

@ -5,13 +5,13 @@ const Verifier = artifacts.require('Verifier')
const MiMC = artifacts.require('MiMC')
module.exports = function(deployer) {
module.exports = function(deployer, network, accounts) {
return deployer.then(async () => {
const { MERKLE_TREE_HEIGHT, AMOUNT, EMPTY_ELEMENT } = process.env
const verifier = await Verifier.deployed()
const miMC = await MiMC.deployed()
await Mixer.link(MiMC, miMC.address)
const mixer = await deployer.deploy(Mixer, verifier.address, AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT)
const mixer = await deployer.deploy(Mixer, verifier.address, AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, accounts[0])
console.log('Mixer\'s address ', mixer.address)
})
}

1608
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,8 @@
"migrate": "npx truffle migrate --network kovan --reset",
"migrate:dev": "npx truffle migrate --network development --reset",
"browserify": "npx browserify cli.js -o index.js --exclude worker_threads",
"eslint": "npx eslint --ignore-path .gitignore ."
"eslint": "npx eslint --ignore-path .gitignore .",
"flat": "truffle-flattener contracts/Mixer.sol > Mixer_flat.sol"
},
"keywords": [],
"author": "",
@ -28,7 +29,6 @@
"circomlib": "^0.0.10",
"dotenv": "^8.0.0",
"eslint": "^6.0.1",
"express": "^4.17.1",
"ganache-cli": "^6.4.5",
"snarkjs": "git+https://github.com/iden3/snarkjs.git#c428706ef69930e378c31199ff8d66ee13fada85",
"truffle": "^5.0.27",
@ -37,6 +37,9 @@
"truffle-hdwallet-provider": "^1.0.14",
"web3": "^1.0.0-beta.55",
"web3-utils": "^1.0.0-beta.55",
"websnark": "git+https://github.com/poma/websnark.git#271c74a76f4d9277c049283c45929fb3a5bb46fc"
"websnark": "git+https://github.com/peppersec/websnark.git#ed6a4d8a6fb081a62af26820980046bbb602d559"
},
"devDependencies": {
"truffle-flattener": "^1.4.0"
}
}

View File

@ -62,11 +62,11 @@ contract('Mixer', accounts => {
const sender = accounts[0]
const levels = MERKLE_TREE_HEIGHT || 16
const zeroValue = EMPTY_ELEMENT || 1337
const value = AMOUNT || '1000000000000000000'
const value = AMOUNT || '1000000000000000000' // 1 ether
let snapshotId
let prefix = 'test'
let tree
const fee = bigInt(1e17)
const fee = bigInt(AMOUNT).shr(1) || bigInt(1e17)
const receiver = getRandomReceiver()
const relayer = accounts[1]
let groth16
@ -111,6 +111,17 @@ contract('Mixer', accounts => {
logs[0].args.leafIndex.should.be.eq.BN(toBN(1))
})
it('should not deposit if disabled', async () => {
let commitment = 42;
(await mixer.isDepositsEnabled()).should.be.equal(true)
const err = await mixer.toggleDeposits({ from: accounts[1] }).should.be.rejected
err.reason.should.be.equal('unauthorized')
await mixer.toggleDeposits({ from: sender });
(await mixer.isDepositsEnabled()).should.be.equal(false)
let error = await mixer.deposit(commitment, { value, from: sender }).should.be.rejected
error.reason.should.be.equal('deposits disabled')
})
it('should throw if there is a such commitment', async () => {
const commitment = 42
await mixer.deposit(commitment, { value, from: sender }).should.be.fulfilled
@ -169,6 +180,9 @@ contract('Mixer', accounts => {
const balanceUserBefore = await web3.eth.getBalance(user)
// Uncomment to measure gas usage
// let gas = await mixer.deposit.estimateGas(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
// console.log('deposit gas:', gas)
await mixer.deposit(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
const balanceUserAfter = await web3.eth.getBalance(user)
@ -201,6 +215,9 @@ contract('Mixer', accounts => {
let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000'))
isSpent.should.be.equal(false)
// Uncomment to measure gas usage
// gas = await mixer.withdraw.estimateGas(pi_a, pi_b, pi_c, publicSignals, { from: relayer, gasPrice: '0' })
// console.log('withdraw gas:', gas)
const { logs } = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: relayer, gasPrice: '0' })
const balanceMixerAfter = await web3.eth.getBalance(mixer.address)

View File

@ -72,7 +72,7 @@ module.exports = {
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: false,
enabled: true,
runs: 200
},
// evmVersion: "byzantium"