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