2019-07-16 12:23:12 -04:00
|
|
|
/* global artifacts, web3, contract */
|
|
|
|
require('chai')
|
2019-07-12 11:04:45 -04:00
|
|
|
.use(require('bn-chai')(web3.utils.BN))
|
|
|
|
.use(require('chai-as-promised'))
|
2019-07-16 12:23:12 -04:00
|
|
|
.should()
|
2019-07-15 13:21:58 -04:00
|
|
|
const fs = require('fs')
|
2019-07-12 11:04:45 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
const { toBN, toHex, randomHex } = require('web3-utils')
|
2019-07-18 14:38:21 -04:00
|
|
|
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
|
2019-07-12 11:04:45 -04:00
|
|
|
|
2019-08-20 16:39:21 -04:00
|
|
|
const Mixer = artifacts.require('./ETHMixer.sol')
|
2019-11-02 08:35:22 -04:00
|
|
|
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env
|
2019-07-12 11:04:45 -04:00
|
|
|
|
2019-07-15 13:21:58 -04:00
|
|
|
const websnarkUtils = require('websnark/src/utils')
|
2019-07-16 12:23:12 -04:00
|
|
|
const buildGroth16 = require('websnark/src/groth16')
|
2019-07-15 11:04:48 -04:00
|
|
|
const stringifyBigInts = require('websnark/tools/stringifybigint').stringifyBigInts
|
2019-07-19 08:06:06 -04:00
|
|
|
const unstringifyBigInts2 = require('snarkjs/src/stringifybigint').unstringifyBigInts
|
2019-07-16 12:23:12 -04:00
|
|
|
const snarkjs = require('snarkjs')
|
|
|
|
const bigInt = snarkjs.bigInt
|
2019-07-18 14:27:51 -04:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const circomlib = require('circomlib')
|
2019-07-12 15:11:37 -04:00
|
|
|
const MerkleTree = require('../lib/MerkleTree')
|
|
|
|
|
2019-07-18 14:27:51 -04:00
|
|
|
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
|
|
|
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
|
|
|
|
2019-07-12 15:11:37 -04:00
|
|
|
function generateDeposit() {
|
|
|
|
let deposit = {
|
2019-07-18 14:27:51 -04:00
|
|
|
secret: rbigint(31),
|
|
|
|
nullifier: rbigint(31),
|
2019-07-16 12:23:12 -04:00
|
|
|
}
|
2019-08-01 10:49:34 -04:00
|
|
|
const preimage = Buffer.concat([deposit.nullifier.leInt2Buff(31), deposit.secret.leInt2Buff(31)])
|
2019-07-18 14:27:51 -04:00
|
|
|
deposit.commitment = pedersenHash(preimage)
|
2019-07-16 12:23:12 -04:00
|
|
|
return deposit
|
2019-07-12 15:11:37 -04:00
|
|
|
}
|
2019-07-12 11:04:45 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2019-07-15 11:04:48 -04:00
|
|
|
function BNArrayToStringArray(array) {
|
|
|
|
const arrayToPrint = []
|
|
|
|
array.forEach(item => {
|
|
|
|
arrayToPrint.push(item.toString())
|
|
|
|
})
|
|
|
|
return arrayToPrint
|
|
|
|
}
|
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
function getRandomRecipient() {
|
|
|
|
let recipient = rbigint(20)
|
|
|
|
while (toHex(recipient.toString()).length !== 42) {
|
|
|
|
recipient = rbigint(20)
|
2019-07-15 13:21:58 -04:00
|
|
|
}
|
2019-11-07 02:04:29 -05:00
|
|
|
return recipient
|
2019-07-15 13:21:58 -04:00
|
|
|
}
|
|
|
|
|
2019-07-18 14:27:51 -04:00
|
|
|
function snarkVerify(proof) {
|
2019-10-04 11:20:20 -04:00
|
|
|
proof = unstringifyBigInts2(proof)
|
2019-07-19 08:06:06 -04:00
|
|
|
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'))
|
2019-07-18 14:27:51 -04:00
|
|
|
return snarkjs['groth'].isValid(verification_key, proof, proof.publicSignals)
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:42:41 -05:00
|
|
|
function toFixedHex(number, length = 32) {
|
|
|
|
let str = bigInt(number).toString(16)
|
|
|
|
while (str.length < length * 2) str = '0' + str
|
|
|
|
str = '0x' + str
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-08-27 16:42:24 -04:00
|
|
|
contract('ETHMixer', accounts => {
|
2019-07-12 11:04:45 -04:00
|
|
|
let mixer
|
|
|
|
const sender = accounts[0]
|
2019-08-02 13:12:30 -04:00
|
|
|
const operator = accounts[0]
|
2019-07-16 05:50:52 -04:00
|
|
|
const levels = MERKLE_TREE_HEIGHT || 16
|
2019-08-27 16:42:24 -04:00
|
|
|
const value = ETH_AMOUNT || '1000000000000000000' // 1 ether
|
2019-07-12 11:04:45 -04:00
|
|
|
let snapshotId
|
|
|
|
let prefix = 'test'
|
|
|
|
let tree
|
2019-08-27 16:42:24 -04:00
|
|
|
const fee = bigInt(ETH_AMOUNT).shr(1) || bigInt(1e17)
|
2019-10-06 01:36:01 -04:00
|
|
|
const refund = bigInt(0)
|
2019-11-07 02:04:29 -05:00
|
|
|
const recipient = getRandomRecipient()
|
2019-07-15 11:04:48 -04:00
|
|
|
const relayer = accounts[1]
|
2019-07-15 13:21:58 -04:00
|
|
|
let groth16
|
|
|
|
let circuit
|
|
|
|
let proving_key
|
2019-07-12 11:04:45 -04:00
|
|
|
|
|
|
|
before(async () => {
|
2019-07-12 15:11:37 -04:00
|
|
|
tree = new MerkleTree(
|
|
|
|
levels,
|
2019-07-13 05:14:27 -04:00
|
|
|
null,
|
|
|
|
prefix,
|
2019-07-12 15:11:37 -04:00
|
|
|
)
|
2019-07-12 11:04:45 -04:00
|
|
|
mixer = await Mixer.deployed()
|
|
|
|
snapshotId = await takeSnapshot()
|
2019-07-15 13:21:58 -04:00
|
|
|
groth16 = await buildGroth16()
|
2019-07-16 12:23:12 -04:00
|
|
|
circuit = require('../build/circuits/withdraw.json')
|
|
|
|
proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer
|
2019-07-12 11:04:45 -04:00
|
|
|
})
|
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
describe('#constructor', () => {
|
2019-07-12 11:04:45 -04:00
|
|
|
it('should initialize', async () => {
|
2019-10-04 10:27:47 -04:00
|
|
|
const etherDenomination = await mixer.denomination()
|
2019-08-27 16:42:24 -04:00
|
|
|
etherDenomination.should.be.eq.BN(toBN(value))
|
2019-07-12 11:04:45 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
describe('#deposit', () => {
|
2019-07-12 15:11:37 -04:00
|
|
|
it('should emit event', async () => {
|
2019-11-04 16:04:22 -05:00
|
|
|
let commitment = toFixedHex(42)
|
2019-07-19 13:08:47 -04:00
|
|
|
let { logs } = await mixer.deposit(commitment, { value, from: sender })
|
|
|
|
|
|
|
|
logs[0].event.should.be.equal('Deposit')
|
2019-11-04 16:04:22 -05:00
|
|
|
logs[0].args.commitment.should.be.equal(commitment)
|
|
|
|
logs[0].args.leafIndex.should.be.eq.BN(0)
|
2019-07-19 13:08:47 -04:00
|
|
|
|
2019-11-04 16:04:22 -05:00
|
|
|
commitment = toFixedHex(12);
|
2019-07-19 13:08:47 -04:00
|
|
|
({ logs } = await mixer.deposit(commitment, { value, from: accounts[2] }))
|
|
|
|
|
|
|
|
logs[0].event.should.be.equal('Deposit')
|
2019-11-04 16:04:22 -05:00
|
|
|
logs[0].args.commitment.should.be.equal(commitment)
|
|
|
|
logs[0].args.leafIndex.should.be.eq.BN(1)
|
2019-07-12 15:11:37 -04:00
|
|
|
})
|
2019-07-15 11:04:48 -04:00
|
|
|
|
2019-08-01 04:41:22 -04:00
|
|
|
it('should not deposit if disabled', async () => {
|
2019-11-04 16:04:22 -05:00
|
|
|
let commitment = toFixedHex(42);
|
2019-10-31 21:11:08 -04:00
|
|
|
(await mixer.isDepositsDisabled()).should.be.equal(false)
|
|
|
|
const err = await mixer.toggleDeposits(true, { from: accounts[1] }).should.be.rejected
|
2019-10-04 10:27:47 -04:00
|
|
|
err.reason.should.be.equal('Only operator can call this function.')
|
2019-10-31 21:11:08 -04:00
|
|
|
await mixer.toggleDeposits(false, { from: sender });
|
|
|
|
(await mixer.isDepositsDisabled()).should.be.equal(false)
|
|
|
|
await mixer.toggleDeposits(true, { from: sender });
|
|
|
|
(await mixer.isDepositsDisabled()).should.be.equal(true)
|
|
|
|
await mixer.toggleDeposits(true, { from: sender });
|
|
|
|
(await mixer.isDepositsDisabled()).should.be.equal(true)
|
2019-08-01 04:41:22 -04:00
|
|
|
let error = await mixer.deposit(commitment, { value, from: sender }).should.be.rejected
|
2019-10-04 10:27:47 -04:00
|
|
|
error.reason.should.be.equal('deposits are disabled')
|
2019-08-01 04:41:22 -04:00
|
|
|
})
|
|
|
|
|
2019-07-15 11:04:48 -04:00
|
|
|
it('should throw if there is a such commitment', async () => {
|
2019-11-04 16:04:22 -05:00
|
|
|
const commitment = toFixedHex(42)
|
2019-07-16 05:50:52 -04:00
|
|
|
await mixer.deposit(commitment, { value, from: sender }).should.be.fulfilled
|
|
|
|
const error = await mixer.deposit(commitment, { value, from: sender }).should.be.rejected
|
2019-07-15 11:04:48 -04:00
|
|
|
error.reason.should.be.equal('The commitment has been submitted')
|
|
|
|
})
|
2019-07-12 15:11:37 -04:00
|
|
|
})
|
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
describe('snark proof verification on js side', () => {
|
2019-07-15 13:21:58 -04:00
|
|
|
it('should detect tampering', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-16 12:23:12 -04:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 13:21:58 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-07-15 13:21:58 -04:00
|
|
|
fee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-07-15 13:21:58 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-15 13:21:58 -04:00
|
|
|
})
|
|
|
|
|
2019-10-04 11:20:20 -04:00
|
|
|
let proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const originalProof = JSON.parse(JSON.stringify(proofData))
|
|
|
|
let result = snarkVerify(proofData)
|
2019-07-15 13:21:58 -04:00
|
|
|
result.should.be.equal(true)
|
|
|
|
|
|
|
|
// nullifier
|
2019-10-04 11:20:20 -04:00
|
|
|
proofData.publicSignals[1] = '133792158246920651341275668520530514036799294649489851421007411546007850802'
|
|
|
|
result = snarkVerify(proofData)
|
2019-07-15 13:21:58 -04:00
|
|
|
result.should.be.equal(false)
|
2019-10-04 11:20:20 -04:00
|
|
|
proofData = originalProof
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
// try to cheat with recipient
|
2019-10-04 11:20:20 -04:00
|
|
|
proofData.publicSignals[2] = '133738360804642228759657445999390850076318544422'
|
|
|
|
result = snarkVerify(proofData)
|
2019-07-15 13:21:58 -04:00
|
|
|
result.should.be.equal(false)
|
2019-10-04 11:20:20 -04:00
|
|
|
proofData = originalProof
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
// fee
|
2019-10-04 11:20:20 -04:00
|
|
|
proofData.publicSignals[3] = '1337100000000000000000'
|
|
|
|
result = snarkVerify(proofData)
|
2019-07-15 13:21:58 -04:00
|
|
|
result.should.be.equal(false)
|
2019-10-04 11:20:20 -04:00
|
|
|
proofData = originalProof
|
2019-07-15 13:21:58 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
describe('#withdraw', () => {
|
2019-07-12 17:50:26 -04:00
|
|
|
it('should work', async () => {
|
2019-07-12 15:11:37 -04:00
|
|
|
const deposit = generateDeposit()
|
2019-07-15 06:42:59 -04:00
|
|
|
const user = accounts[4]
|
2019-07-12 15:11:37 -04:00
|
|
|
await tree.insert(deposit.commitment)
|
2019-07-15 06:42:59 -04:00
|
|
|
|
|
|
|
const balanceUserBefore = await web3.eth.getBalance(user)
|
|
|
|
|
2019-08-01 05:44:20 -04:00
|
|
|
// 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)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: user, gasPrice: '0' })
|
2019-07-15 06:42:59 -04:00
|
|
|
|
|
|
|
const balanceUserAfter = await web3.eth.getBalance(user)
|
2019-07-16 05:50:52 -04:00
|
|
|
balanceUserAfter.should.be.eq.BN(toBN(balanceUserBefore).sub(toBN(value)))
|
2019-07-12 15:11:37 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-12 15:11:37 -04:00
|
|
|
|
|
|
|
// Circuit input
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
// public
|
2019-07-15 06:42:59 -04:00
|
|
|
root,
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-07-15 06:42:59 -04:00
|
|
|
fee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-07-12 15:11:37 -04:00
|
|
|
|
|
|
|
// private
|
2019-07-23 16:00:45 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-07-12 15:11:37 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-12 15:11:37 -04:00
|
|
|
})
|
|
|
|
|
2019-07-23 16:00:45 -04:00
|
|
|
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
2019-07-12 17:50:26 -04:00
|
|
|
|
2019-07-15 06:42:59 -04:00
|
|
|
const balanceMixerBefore = await web3.eth.getBalance(mixer.address)
|
|
|
|
const balanceRelayerBefore = await web3.eth.getBalance(relayer)
|
2019-08-02 13:12:30 -04:00
|
|
|
const balanceOperatorBefore = await web3.eth.getBalance(operator)
|
2019-11-07 02:04:29 -05:00
|
|
|
const balanceRecieverBefore = await web3.eth.getBalance(toHex(recipient.toString()))
|
2019-11-04 16:04:22 -05:00
|
|
|
let isSpent = await mixer.isSpent(toFixedHex(input.nullifierHash))
|
2019-07-25 09:58:21 -04:00
|
|
|
isSpent.should.be.equal(false)
|
2019-07-15 06:42:59 -04:00
|
|
|
|
2019-08-01 05:44:20 -04:00
|
|
|
// Uncomment to measure gas usage
|
2019-10-04 11:20:20 -04:00
|
|
|
// gas = await mixer.withdraw.estimateGas(proof, publicSignals, { from: relayer, gasPrice: '0' })
|
2019-08-01 05:44:20 -04:00
|
|
|
// console.log('withdraw gas:', gas)
|
2019-11-04 14:42:41 -05:00
|
|
|
const args = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
const { logs } = await mixer.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
|
2019-07-15 06:42:59 -04:00
|
|
|
|
|
|
|
const balanceMixerAfter = await web3.eth.getBalance(mixer.address)
|
|
|
|
const balanceRelayerAfter = await web3.eth.getBalance(relayer)
|
2019-08-02 13:12:30 -04:00
|
|
|
const balanceOperatorAfter = await web3.eth.getBalance(operator)
|
2019-11-07 02:04:29 -05:00
|
|
|
const balanceRecieverAfter = await web3.eth.getBalance(toHex(recipient.toString()))
|
2019-07-15 11:04:48 -04:00
|
|
|
const feeBN = toBN(fee.toString())
|
2019-07-16 05:50:52 -04:00
|
|
|
balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value)))
|
2019-08-02 13:12:30 -04:00
|
|
|
balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore))
|
|
|
|
balanceOperatorAfter.should.be.eq.BN(toBN(balanceOperatorBefore).add(feeBN))
|
2019-07-16 05:50:52 -04:00
|
|
|
balanceRecieverAfter.should.be.eq.BN(toBN(balanceRecieverBefore).add(toBN(value)).sub(feeBN))
|
2019-07-15 06:42:59 -04:00
|
|
|
|
2019-07-25 09:58:21 -04:00
|
|
|
|
2019-10-31 21:12:32 -04:00
|
|
|
logs[0].event.should.be.equal('Withdrawal')
|
2019-11-04 16:04:22 -05:00
|
|
|
logs[0].args.nullifierHash.should.be.equal(toFixedHex(input.nullifierHash))
|
2019-09-06 16:54:37 -04:00
|
|
|
logs[0].args.relayer.should.be.eq.BN(operator)
|
2019-07-15 11:04:48 -04:00
|
|
|
logs[0].args.fee.should.be.eq.BN(feeBN)
|
2019-11-04 16:04:22 -05:00
|
|
|
isSpent = await mixer.isSpent(toFixedHex(input.nullifierHash))
|
2019-07-25 09:58:21 -04:00
|
|
|
isSpent.should.be.equal(true)
|
2019-07-15 11:04:48 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should prevent double spend', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
2019-07-15 11:04:48 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 11:04:48 -04:00
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 11:04:48 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-07-15 11:04:48 -04:00
|
|
|
fee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-07-15 11:04:48 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-15 11:04:48 -04:00
|
|
|
})
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
|
|
|
const args = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
await mixer.withdraw(proof, ...args, { from: relayer }).should.be.fulfilled
|
|
|
|
const error = await mixer.withdraw(proof, ...args, { from: relayer }).should.be.rejected
|
2019-07-15 11:04:48 -04:00
|
|
|
error.reason.should.be.equal('The note has been already spent')
|
|
|
|
})
|
|
|
|
|
2019-08-01 03:33:12 -04:00
|
|
|
it('should prevent double spend with overflow', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
2019-08-01 03:33:12 -04:00
|
|
|
|
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-08-01 03:33:12 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-08-01 03:33:12 -04:00
|
|
|
fee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-08-01 03:33:12 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-08-01 03:33:12 -04:00
|
|
|
})
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
|
|
|
const args = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(toBN(input.nullifierHash).add(toBN('21888242871839275222246405745257275088548364400416034343698204186575808495617'))),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
const error = await mixer.withdraw(proof, ...args, { from: relayer }).should.be.rejected
|
2019-08-01 03:33:12 -04:00
|
|
|
error.reason.should.be.equal('verifier-gte-snark-scalar-field')
|
|
|
|
})
|
|
|
|
|
2019-07-15 11:04:48 -04:00
|
|
|
it('fee should be less or equal transfer value', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
2019-07-15 11:04:48 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
|
|
|
const oneEtherFee = bigInt(1e18) // 1 ether
|
2019-07-15 11:04:48 -04:00
|
|
|
const input = stringifyBigInts({
|
|
|
|
root,
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 11:04:48 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-07-15 11:04:48 -04:00
|
|
|
fee: oneEtherFee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-07-15 11:04:48 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-15 11:04:48 -04:00
|
|
|
})
|
|
|
|
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
|
|
|
const args = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
const error = await mixer.withdraw(proof, ...args, { from: relayer }).should.be.rejected
|
2019-07-15 11:04:48 -04:00
|
|
|
error.reason.should.be.equal('Fee exceeds transfer value')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should throw for corrupted merkle tree root', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
2019-07-15 11:04:48 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 11:04:48 -04:00
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 11:04:48 -04:00
|
|
|
root,
|
|
|
|
nullifier: deposit.nullifier,
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-07-15 11:04:48 -04:00
|
|
|
fee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-07-15 11:04:48 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-15 11:04:48 -04:00
|
|
|
})
|
|
|
|
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
|
|
|
|
|
|
|
const args = [
|
|
|
|
toFixedHex(randomHex(32)),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
const error = await mixer.withdraw(proof, ...args, { from: relayer }).should.be.rejected
|
2019-07-15 11:04:48 -04:00
|
|
|
error.reason.should.be.equal('Cannot find your merkle root')
|
2019-07-12 15:11:37 -04:00
|
|
|
})
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
it('should reject with tampered public inputs', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
2019-07-15 13:21:58 -04:00
|
|
|
|
2019-07-16 12:23:12 -04:00
|
|
|
let { root, path_elements, path_index } = await tree.path(0)
|
2019-07-15 13:21:58 -04:00
|
|
|
|
2019-07-15 14:57:06 -04:00
|
|
|
const input = stringifyBigInts({
|
2019-07-15 13:21:58 -04:00
|
|
|
root,
|
2019-08-01 10:49:34 -04:00
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
2019-07-15 13:21:58 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-07-15 13:21:58 -04:00
|
|
|
fee,
|
2019-10-06 01:36:01 -04:00
|
|
|
refund,
|
2019-07-15 13:21:58 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-15 13:21:58 -04:00
|
|
|
})
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
let { proof } = websnarkUtils.toSolidityInput(proofData)
|
|
|
|
const args = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
let incorrectArgs
|
2019-10-04 11:20:20 -04:00
|
|
|
const originalProof = proof.slice()
|
2019-07-15 13:21:58 -04:00
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
// recipient
|
2019-11-04 14:42:41 -05:00
|
|
|
incorrectArgs = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
|
|
|
toFixedHex('0x0000000000000000000000007a1f9131357404ef86d7c38dbffed2da70321337', 20),
|
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
let error = await mixer.withdraw(proof, ...incorrectArgs, { from: relayer }).should.be.rejected
|
2019-07-16 12:23:12 -04:00
|
|
|
error.reason.should.be.equal('Invalid withdraw proof')
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
// fee
|
2019-11-04 14:42:41 -05:00
|
|
|
incorrectArgs = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex('0x000000000000000000000000000000000000000000000000015345785d8a0000'),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
error = await mixer.withdraw(proof, ...incorrectArgs, { from: relayer }).should.be.rejected
|
2019-07-16 12:23:12 -04:00
|
|
|
error.reason.should.be.equal('Invalid withdraw proof')
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
// nullifier
|
2019-11-04 14:42:41 -05:00
|
|
|
incorrectArgs = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex('0x00abdfc78211f8807b9c6504a6e537e71b8788b2f529a95f1399ce124a8642ad'),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
error = await mixer.withdraw(proof, ...incorrectArgs, { from: relayer }).should.be.rejected
|
2019-07-16 12:23:12 -04:00
|
|
|
error.reason.should.be.equal('Invalid withdraw proof')
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
// proof itself
|
2019-11-04 14:42:41 -05:00
|
|
|
proof = '0xbeef' + proof.substr(6)
|
|
|
|
await mixer.withdraw(proof, ...args, { from: relayer }).should.be.rejected
|
2019-07-15 13:21:58 -04:00
|
|
|
|
|
|
|
// should work with original values
|
2019-11-04 14:42:41 -05:00
|
|
|
await mixer.withdraw(originalProof, ...args, { from: relayer }).should.be.fulfilled
|
2019-07-15 13:21:58 -04:00
|
|
|
})
|
2019-10-17 11:54:21 -04:00
|
|
|
|
|
|
|
it('should reject with non zero refund', async () => {
|
|
|
|
const deposit = generateDeposit()
|
|
|
|
await tree.insert(deposit.commitment)
|
2019-11-04 16:04:22 -05:00
|
|
|
await mixer.deposit(toFixedHex(deposit.commitment), { value, from: sender })
|
2019-10-17 11:54:21 -04:00
|
|
|
|
|
|
|
const { root, path_elements, path_index } = await tree.path(0)
|
|
|
|
|
|
|
|
const input = stringifyBigInts({
|
|
|
|
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(31)),
|
|
|
|
root,
|
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
relayer: operator,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient,
|
2019-10-17 11:54:21 -04:00
|
|
|
fee,
|
|
|
|
refund: bigInt(1),
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-10-17 11:54:21 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
|
|
|
|
|
|
|
const args = [
|
|
|
|
toFixedHex(input.root),
|
|
|
|
toFixedHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toFixedHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toFixedHex(input.relayer, 20),
|
|
|
|
toFixedHex(input.fee),
|
|
|
|
toFixedHex(input.refund)
|
|
|
|
]
|
|
|
|
const error = await mixer.withdraw(proof, ...args, { from: relayer }).should.be.rejected
|
2019-10-17 11:54:21 -04:00
|
|
|
error.reason.should.be.equal('Refund value is supposed to be zero for ETH mixer')
|
|
|
|
})
|
2019-07-12 15:11:37 -04:00
|
|
|
})
|
|
|
|
|
2019-08-02 13:12:30 -04:00
|
|
|
describe('#changeOperator', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
let operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(sender)
|
|
|
|
|
|
|
|
const newOperator = accounts[7]
|
|
|
|
await mixer.changeOperator(newOperator).should.be.fulfilled
|
|
|
|
|
|
|
|
operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(newOperator)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('cannot change from different address', async () => {
|
|
|
|
let operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(sender)
|
|
|
|
|
|
|
|
const newOperator = accounts[7]
|
|
|
|
const error = await mixer.changeOperator(newOperator, { from: accounts[7] }).should.be.rejected
|
2019-10-04 10:27:47 -04:00
|
|
|
error.reason.should.be.equal('Only operator can call this function.')
|
2019-08-02 13:12:30 -04:00
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-10-04 10:27:47 -04:00
|
|
|
describe('#updateVerifier', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
let operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(sender)
|
|
|
|
|
|
|
|
const newVerifier = accounts[7]
|
|
|
|
await mixer.updateVerifier(newVerifier).should.be.fulfilled
|
|
|
|
|
2019-10-04 12:17:28 -04:00
|
|
|
const verifier = await mixer.verifier()
|
2019-10-04 10:27:47 -04:00
|
|
|
verifier.should.be.equal(newVerifier)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('cannot change from different address', async () => {
|
|
|
|
let operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(sender)
|
|
|
|
|
|
|
|
const newVerifier = accounts[7]
|
|
|
|
const error = await mixer.updateVerifier(newVerifier, { from: accounts[7] }).should.be.rejected
|
|
|
|
error.reason.should.be.equal('Only operator can call this function.')
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#disableVerifierUpdate', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
let operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(sender)
|
|
|
|
|
2019-10-31 21:11:08 -04:00
|
|
|
let isVerifierUpdateDisabled = await mixer.isVerifierUpdateDisabled()
|
|
|
|
isVerifierUpdateDisabled.should.be.equal(false)
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
await mixer.disableVerifierUpdate().should.be.fulfilled
|
|
|
|
|
2019-10-31 21:11:08 -04:00
|
|
|
const newValue = await mixer.isVerifierUpdateDisabled()
|
|
|
|
newValue.should.be.equal(true)
|
2019-10-04 10:27:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('cannot update verifier after this function is called', async () => {
|
|
|
|
let operator = await mixer.operator()
|
|
|
|
operator.should.be.equal(sender)
|
|
|
|
|
2019-10-31 21:11:08 -04:00
|
|
|
let isVerifierUpdateDisabled = await mixer.isVerifierUpdateDisabled()
|
|
|
|
isVerifierUpdateDisabled.should.be.equal(false)
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
await mixer.disableVerifierUpdate().should.be.fulfilled
|
|
|
|
|
2019-10-31 21:11:08 -04:00
|
|
|
const newValue = await mixer.isVerifierUpdateDisabled()
|
|
|
|
newValue.should.be.equal(true)
|
2019-10-04 10:27:47 -04:00
|
|
|
|
|
|
|
const newVerifier = accounts[7]
|
|
|
|
const error = await mixer.updateVerifier(newVerifier).should.be.rejected
|
|
|
|
error.reason.should.be.equal('Verifier updates have been disabled.')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-07-12 11:04:45 -04:00
|
|
|
afterEach(async () => {
|
|
|
|
await revertSnapshot(snapshotId.result)
|
2019-07-16 12:23:12 -04:00
|
|
|
// eslint-disable-next-line require-atomic-updates
|
2019-07-12 11:04:45 -04:00
|
|
|
snapshotId = await takeSnapshot()
|
2019-07-15 11:04:48 -04:00
|
|
|
tree = new MerkleTree(
|
|
|
|
levels,
|
|
|
|
null,
|
|
|
|
prefix,
|
|
|
|
)
|
2019-07-12 11:04:45 -04:00
|
|
|
})
|
|
|
|
})
|