tornado-core/test/ETHTornado.test.js

542 lines
20 KiB
JavaScript
Raw Normal View History

2019-07-16 12:23:12 -04:00
/* global artifacts, web3, contract */
2021-02-11 01:23:18 -05:00
require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
2019-07-15 13:21:58 -04:00
const fs = require('fs')
2019-07-12 11:04:45 -04:00
2019-11-28 03:04:09 -05:00
const { toBN, 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-12-13 08:49:19 -05:00
const Tornado = artifacts.require('./ETHTornado.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]
2021-02-11 01:23:18 -05:00
const toFixedHex = (number, length = 32) =>
'0x' +
bigInt(number)
.toString(16)
.padStart(length * 2, '0')
2019-11-27 23:52:17 -05:00
const getRandomRecipient = () => rbigint(20)
2019-07-18 14:27:51 -04:00
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 = []
2021-02-11 01:23:18 -05:00
array.forEach((item) => {
2019-07-15 11:04:48 -04:00
arrayToPrint.push(item.toString())
})
return arrayToPrint
}
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)
}
2021-02-11 01:23:18 -05:00
contract('ETHTornado', (accounts) => {
2019-12-13 08:49:19 -05:00
let tornado
2019-07-12 11:04:45 -04:00
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)
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 () => {
2021-02-11 01:23:18 -05:00
tree = new MerkleTree(levels, null, prefix)
2019-12-13 08:49:19 -05:00
tornado = await Tornado.deployed()
2019-07-12 11:04:45 -04:00
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-12-13 08:49:19 -05:00
const etherDenomination = await tornado.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-12-13 08:49:19 -05:00
let { logs } = await tornado.deposit(commitment, { value, from: sender })
2019-07-19 13:08:47 -04:00
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
2021-02-11 01:23:18 -05:00
commitment = toFixedHex(12)
;({ logs } = await tornado.deposit(commitment, { value, from: accounts[2] }))
2019-07-19 13:08:47 -04:00
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
it('should throw if there is a such commitment', async () => {
2019-11-04 16:04:22 -05:00
const commitment = toFixedHex(42)
2019-12-13 08:49:19 -05:00
await tornado.deposit(commitment, { value, from: sender }).should.be.fulfilled
const error = await tornado.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,
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
2021-02-11 01:23:18 -05:00
proofData.publicSignals[1] =
'133792158246920651341275668520530514036799294649489851421007411546007850802'
2019-10-04 11:20:20 -04:00
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
2019-12-13 08:49:19 -05:00
// let gas = await tornado.deposit.estimateGas(toBN(deposit.commitment.toString()), { value, from: user, gasPrice: '0' })
2019-08-01 05:44:20 -04:00
// console.log('deposit gas:', gas)
2019-12-13 08:49:19 -05:00
await tornado.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,
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-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-12-13 08:49:19 -05:00
const balanceTornadoBefore = await web3.eth.getBalance(tornado.address)
2019-07-15 06:42:59 -04:00
const balanceRelayerBefore = await web3.eth.getBalance(relayer)
2019-08-02 13:12:30 -04:00
const balanceOperatorBefore = await web3.eth.getBalance(operator)
2019-11-27 23:52:17 -05:00
const balanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20))
2019-12-13 08:49:19 -05:00
let isSpent = await tornado.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-12-13 08:49:19 -05:00
// gas = await tornado.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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
const { logs } = await tornado.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
2019-07-15 06:42:59 -04:00
2019-12-13 08:49:19 -05:00
const balanceTornadoAfter = await web3.eth.getBalance(tornado.address)
2019-07-15 06:42:59 -04:00
const balanceRelayerAfter = await web3.eth.getBalance(relayer)
2019-08-02 13:12:30 -04:00
const balanceOperatorAfter = await web3.eth.getBalance(operator)
2019-11-27 23:52:17 -05:00
const balanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20))
2019-07-15 11:04:48 -04:00
const feeBN = toBN(fee.toString())
2019-12-13 08:49:19 -05:00
balanceTornadoAfter.should.be.eq.BN(toBN(balanceTornadoBefore).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-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-12-13 08:49:19 -05:00
isSpent = await tornado.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-12-13 08:49:19 -05:00
await tornado.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,
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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
await tornado.withdraw(proof, ...args, { from: relayer }).should.be.fulfilled
const error = await tornado.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-12-13 08:49:19 -05:00
await tornado.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,
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),
2021-02-11 01:23:18 -05:00
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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2021-03-10 11:37:17 -05:00
error.reason.should.be.equal('verifier-gte-snark-scalar-field')
2019-08-01 03:33:12 -04:00
})
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-12-13 08:49:19 -05:00
await tornado.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-11-15 14:42:59 -05:00
const largeFee = bigInt(value).add(bigInt(1))
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-11-15 14:42:59 -05:00
fee: largeFee,
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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
const error = await tornado.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-12-13 08:49:19 -05:00
await tornado.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,
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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
const error = await tornado.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-12-13 08:49:19 -05:00
await tornado.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,
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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
let error = await tornado.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'),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
error = await tornado.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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
error = await tornado.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)
2019-12-13 08:49:19 -05:00
await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
2019-07-15 13:21:58 -04:00
// should work with original values
2019-12-13 08:49:19 -05:00
await tornado.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-12-13 08:49:19 -05:00
await tornado.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),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-04 14:42:41 -05:00
]
2019-12-13 08:49:19 -05:00
const error = await tornado.withdraw(proof, ...args, { from: relayer }).should.be.rejected
error.reason.should.be.equal('Refund value is supposed to be zero for ETH instance')
2019-10-17 11:54:21 -04:00
})
2019-07-12 15:11:37 -04:00
})
2019-11-27 22:45:52 -05:00
describe('#isSpent', () => {
it('should work', async () => {
const deposit1 = generateDeposit()
const deposit2 = generateDeposit()
await tree.insert(deposit1.commitment)
await tree.insert(deposit2.commitment)
2019-12-13 08:49:19 -05:00
await tornado.deposit(toFixedHex(deposit1.commitment), { value, gasPrice: '0' })
await tornado.deposit(toFixedHex(deposit2.commitment), { value, gasPrice: '0' })
2019-11-27 22:45:52 -05:00
const { root, path_elements, path_index } = await tree.path(1)
// Circuit input
const input = stringifyBigInts({
// public
root,
nullifierHash: pedersenHash(deposit2.nullifier.leInt2Buff(31)),
relayer: operator,
recipient,
fee,
refund,
// private
nullifier: deposit2.nullifier,
secret: deposit2.secret,
pathElements: path_elements,
pathIndices: path_index,
})
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
const { proof } = websnarkUtils.toSolidityInput(proofData)
const args = [
toFixedHex(input.root),
toFixedHex(input.nullifierHash),
toFixedHex(input.recipient, 20),
toFixedHex(input.relayer, 20),
toFixedHex(input.fee),
2021-02-11 01:23:18 -05:00
toFixedHex(input.refund),
2019-11-27 22:45:52 -05:00
]
2019-12-13 08:49:19 -05:00
await tornado.withdraw(proof, ...args, { from: relayer, gasPrice: '0' })
2019-11-27 22:45:52 -05:00
const nullifierHash1 = toFixedHex(pedersenHash(deposit1.nullifier.leInt2Buff(31)))
const nullifierHash2 = toFixedHex(pedersenHash(deposit2.nullifier.leInt2Buff(31)))
2019-12-13 08:49:19 -05:00
const spentArray = await tornado.isSpentArray([nullifierHash1, nullifierHash2])
2019-11-27 22:45:52 -05:00
spentArray.should.be.deep.equal([false, true])
})
})
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()
2021-02-11 01:23:18 -05:00
tree = new MerkleTree(levels, null, prefix)
2019-07-12 11:04:45 -04:00
})
})