tornado-nova/test/full.test.js

610 lines
24 KiB
JavaScript
Raw Normal View History

2021-08-21 14:23:17 +02:00
const hre = require('hardhat')
2021-08-21 14:46:17 +02:00
const { ethers, waffle } = hre
const { loadFixture } = waffle
const { expect } = require('chai')
2021-09-29 21:39:30 +02:00
const { utils } = ethers
2021-06-16 02:31:31 +02:00
2021-06-15 22:02:59 +02:00
const Utxo = require('../src/utxo')
2021-11-01 18:56:24 +01:00
const { transaction, registerAndTransact, prepareTransaction, buildMerkleTree } = require('../src/index')
const { toFixedHex, poseidonHash } = require('../src/utils')
2021-07-22 16:01:22 +02:00
const { Keypair } = require('../src/keypair')
const { encodeDataForBridge } = require('./utils')
2022-02-11 21:53:36 +01:00
const config = require('../config')
const { generate } = require('../src/0_generateAddresses')
2021-06-15 14:48:26 +02:00
2021-09-26 18:14:05 +02:00
const MERKLE_TREE_HEIGHT = 5
const l1ChainId = 1
const MINIMUM_WITHDRAWAL_AMOUNT = utils.parseEther(process.env.MINIMUM_WITHDRAWAL_AMOUNT || '0.05')
const MAXIMUM_DEPOSIT_AMOUNT = utils.parseEther(process.env.MAXIMUM_DEPOSIT_AMOUNT || '1')
2021-09-26 18:14:05 +02:00
2021-08-21 14:23:17 +02:00
describe('TornadoPool', function () {
this.timeout(20000)
2021-06-09 12:56:33 +02:00
2021-08-21 14:46:17 +02:00
async function deploy(contractName, ...args) {
const Factory = await ethers.getContractFactory(contractName)
const instance = await Factory.deploy(...args)
2021-08-21 14:54:24 +02:00
return instance.deployed()
2021-08-21 14:46:17 +02:00
}
2021-08-21 14:46:17 +02:00
async function fixture() {
2021-09-26 18:14:05 +02:00
require('../scripts/compileHasher')
2022-01-22 01:21:34 +01:00
const [sender, gov, multisig] = await ethers.getSigners()
2021-08-21 14:46:17 +02:00
const verifier2 = await deploy('Verifier2')
const verifier16 = await deploy('Verifier16')
2021-09-26 18:14:05 +02:00
const hasher = await deploy('Hasher')
2021-09-29 21:39:30 +02:00
const token = await deploy('PermittableToken', 'Wrapped ETH', 'WETH', 18, l1ChainId)
2021-09-29 21:39:30 +02:00
await token.mint(sender.address, utils.parseEther('10000'))
2022-01-22 01:21:34 +01:00
const l1Token = await deploy('WETH', 'Wrapped ETH', 'WETH')
2022-01-22 01:23:33 +01:00
await l1Token.deposit({ value: utils.parseEther('3') })
2022-01-22 01:21:34 +01:00
const amb = await deploy('MockAMB', gov.address, l1ChainId)
2021-09-29 21:39:30 +02:00
const omniBridge = await deploy('MockOmniBridge', amb.address)
2022-02-11 21:53:36 +01:00
// deploy L1Unwrapper with CREATE2
const singletonFactory = await ethers.getContractAt('SingletonFactory', config.singletonFactory)
let customConfig = Object.assign({}, config)
2022-02-14 11:27:44 +01:00
customConfig.omniBridge = omniBridge.address
2022-02-11 21:53:36 +01:00
customConfig.weth = l1Token.address
2022-02-14 13:24:33 +01:00
customConfig.multisig = multisig.address
2022-02-11 21:53:36 +01:00
const contracts = await generate(customConfig)
await singletonFactory.deploy(contracts.unwrapperContract.bytecode, config.salt)
const l1Unwrapper = await ethers.getContractAt('L1Unwrapper', contracts.unwrapperContract.address)
2021-09-29 21:39:30 +02:00
2021-09-26 18:22:19 +02:00
/** @type {TornadoPool} */
2021-10-05 14:12:39 +02:00
const tornadoPoolImpl = await deploy(
2021-09-26 18:14:05 +02:00
'TornadoPool',
verifier2.address,
verifier16.address,
MERKLE_TREE_HEIGHT,
hasher.address,
2021-09-29 21:39:30 +02:00
token.address,
omniBridge.address,
2021-09-30 17:34:07 +02:00
l1Unwrapper.address,
gov.address,
2021-10-12 17:50:25 +02:00
l1ChainId,
2021-10-29 12:20:25 +02:00
multisig.address,
2021-09-26 18:14:05 +02:00
)
2021-08-05 09:29:49 +02:00
2021-10-19 10:09:04 +02:00
const { data } = await tornadoPoolImpl.populateTransaction.initialize(
MINIMUM_WITHDRAWAL_AMOUNT,
MAXIMUM_DEPOSIT_AMOUNT,
)
2021-09-26 18:22:19 +02:00
const proxy = await deploy(
'CrossChainUpgradeableProxy',
2021-10-05 14:12:39 +02:00
tornadoPoolImpl.address,
2021-09-07 11:46:43 +02:00
gov.address,
2021-10-19 10:09:04 +02:00
data,
2021-10-05 11:08:04 +02:00
amb.address,
l1ChainId,
2021-08-21 14:54:24 +02:00
)
2021-08-05 09:29:49 +02:00
2021-10-19 10:09:04 +02:00
const tornadoPool = tornadoPoolImpl.attach(proxy.address)
2021-08-05 09:29:49 +02:00
2021-10-05 14:12:39 +02:00
await token.approve(tornadoPool.address, utils.parseEther('10000'))
2022-01-22 01:21:34 +01:00
return { tornadoPool, token, proxy, omniBridge, amb, gov, multisig, l1Unwrapper, sender, l1Token }
2021-08-21 14:46:17 +02:00
}
2021-06-09 12:56:33 +02:00
2021-08-05 09:29:49 +02:00
describe('Upgradeability tests', () => {
it('admin should be gov', async () => {
2021-10-05 14:12:39 +02:00
const { proxy, amb, gov } = await loadFixture(fixture)
2021-08-05 09:29:49 +02:00
const { data } = await proxy.populateTransaction.admin()
2021-10-29 17:36:38 +02:00
const { result } = await amb.callStatic.execute([{ who: proxy.address, callData: data }])
2021-08-05 09:29:49 +02:00
expect('0x' + result.slice(26)).to.be.equal(gov.address.toLowerCase())
})
it('non admin cannot call', async () => {
2021-10-05 14:12:39 +02:00
const { proxy } = await loadFixture(fixture)
2021-09-07 11:46:43 +02:00
await expect(proxy.admin()).to.be.revertedWith(
"Transaction reverted: function selector was not recognized and there's no fallback function",
)
2021-08-05 09:29:49 +02:00
})
2021-10-12 17:50:25 +02:00
it('should configure', async () => {
2022-01-24 16:25:17 +01:00
const { tornadoPool, multisig } = await loadFixture(fixture)
2021-10-12 17:50:25 +02:00
const newWithdrawalLimit = utils.parseEther('0.01337')
const newDepositLimit = utils.parseEther('1337')
2022-01-24 16:40:45 +01:00
await tornadoPool.connect(multisig).configureLimits(newWithdrawalLimit, newDepositLimit)
2021-10-12 17:50:25 +02:00
expect(await tornadoPool.maximumDepositAmount()).to.be.equal(newDepositLimit)
expect(await tornadoPool.minimalWithdrawalAmount()).to.be.equal(newWithdrawalLimit)
})
2021-08-05 09:29:49 +02:00
})
2021-06-09 12:56:33 +02:00
it('encrypt -> decrypt should work', () => {
const data = Buffer.from([0xff, 0xaa, 0x00, 0x01])
2021-06-15 22:02:59 +02:00
const keypair = new Keypair()
const ciphertext = keypair.encrypt(data)
const result = keypair.decrypt(ciphertext)
expect(result).to.be.deep.equal(data)
2021-06-15 22:02:59 +02:00
})
2021-08-16 21:17:07 +02:00
it('constants check', async () => {
2021-08-21 14:46:17 +02:00
const { tornadoPool } = await loadFixture(fixture)
2021-08-16 21:17:07 +02:00
const maxFee = await tornadoPool.MAX_FEE()
const maxExtAmount = await tornadoPool.MAX_EXT_AMOUNT()
const fieldSize = await tornadoPool.FIELD_SIZE()
expect(maxExtAmount.add(maxFee)).to.be.lt(fieldSize)
})
2021-07-22 16:01:22 +02:00
it('should register and deposit', async function () {
2021-08-21 14:46:17 +02:00
let { tornadoPool } = await loadFixture(fixture)
2021-09-07 11:46:43 +02:00
const sender = (await ethers.getSigners())[0]
2021-08-21 14:46:17 +02:00
2021-07-22 16:01:22 +02:00
// Alice deposits into tornado pool
const aliceDepositAmount = 1e7
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount })
tornadoPool = tornadoPool.connect(sender)
await registerAndTransact({
tornadoPool,
outputs: [aliceDepositUtxo],
2021-10-04 18:28:08 +02:00
account: {
owner: sender.address,
publicKey: aliceDepositUtxo.keypair.address(),
},
2021-07-22 16:01:22 +02:00
})
const filter = tornadoPool.filters.NewCommitment()
const fromBlock = await ethers.provider.getBlock()
const events = await tornadoPool.queryFilter(filter, fromBlock.number)
let aliceReceiveUtxo
try {
aliceReceiveUtxo = Utxo.decrypt(
aliceDepositUtxo.keypair,
events[0].args.encryptedOutput,
events[0].args.index,
)
} catch (e) {
// we try to decrypt another output here because it shuffles outputs before sending to blockchain
aliceReceiveUtxo = Utxo.decrypt(
aliceDepositUtxo.keypair,
events[1].args.encryptedOutput,
events[1].args.index,
)
}
expect(aliceReceiveUtxo.amount).to.be.equal(aliceDepositAmount)
const filterRegister = tornadoPool.filters.PublicKey(sender.address)
const filterFromBlock = await ethers.provider.getBlock()
const registerEvents = await tornadoPool.queryFilter(filterRegister, filterFromBlock.number)
const [registerEvent] = registerEvents.sort((a, b) => a.blockNumber - b.blockNumber).slice(-1)
expect(registerEvent.args.key).to.be.equal(aliceDepositUtxo.keypair.address())
})
2021-06-15 22:02:59 +02:00
it('should deposit, transact and withdraw', async function () {
2021-09-29 21:39:30 +02:00
const { tornadoPool, token } = await loadFixture(fixture)
2021-08-21 14:46:17 +02:00
2021-06-15 22:02:59 +02:00
// Alice deposits into tornado pool
const aliceDepositAmount = utils.parseEther('0.1')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount })
await transaction({ tornadoPool, outputs: [aliceDepositUtxo] })
2021-06-15 22:02:59 +02:00
// Bob gives Alice address to send some eth inside the shielded pool
2021-06-16 02:31:31 +02:00
const bobKeypair = new Keypair() // contains private and public keys
const bobAddress = bobKeypair.address() // contains only public key
2021-06-15 22:02:59 +02:00
// Alice sends some funds to Bob
const bobSendAmount = utils.parseEther('0.06')
const bobSendUtxo = new Utxo({ amount: bobSendAmount, keypair: Keypair.fromString(bobAddress) })
2021-06-16 02:31:31 +02:00
const aliceChangeUtxo = new Utxo({
amount: aliceDepositAmount.sub(bobSendAmount),
2021-06-16 02:31:31 +02:00
keypair: aliceDepositUtxo.keypair,
})
await transaction({ tornadoPool, inputs: [aliceDepositUtxo], outputs: [bobSendUtxo, aliceChangeUtxo] })
2021-06-15 22:02:59 +02:00
// Bob parses chain to detect incoming funds
const filter = tornadoPool.filters.NewCommitment()
2021-06-15 22:02:59 +02:00
const fromBlock = await ethers.provider.getBlock()
const events = await tornadoPool.queryFilter(filter, fromBlock.number)
2021-06-21 19:05:10 +02:00
let bobReceiveUtxo
try {
bobReceiveUtxo = Utxo.decrypt(bobKeypair, events[0].args.encryptedOutput, events[0].args.index)
} catch (e) {
// we try to decrypt another output here because it shuffles outputs before sending to blockchain
bobReceiveUtxo = Utxo.decrypt(bobKeypair, events[1].args.encryptedOutput, events[1].args.index)
}
expect(bobReceiveUtxo.amount).to.be.equal(bobSendAmount)
2021-06-16 10:28:39 +02:00
// Bob withdraws a part of his funds from the shielded pool
const bobWithdrawAmount = utils.parseEther('0.05')
const bobEthAddress = '0xDeaD00000000000000000000000000000000BEEf'
const bobChangeUtxo = new Utxo({ amount: bobSendAmount.sub(bobWithdrawAmount), keypair: bobKeypair })
2021-06-16 02:31:31 +02:00
await transaction({
tornadoPool,
inputs: [bobReceiveUtxo],
outputs: [bobChangeUtxo],
recipient: bobEthAddress,
})
2021-09-29 21:39:30 +02:00
const bobBalance = await token.balanceOf(bobEthAddress)
expect(bobBalance).to.be.equal(bobWithdrawAmount)
2021-06-09 12:56:33 +02:00
})
2021-09-30 17:34:07 +02:00
it('should deposit from L1 and withdraw to L1', async function () {
const { tornadoPool, token, omniBridge } = await loadFixture(fixture)
2021-10-05 14:12:39 +02:00
const aliceKeypair = new Keypair() // contains private and public keys
2021-09-30 17:34:07 +02:00
// Alice deposits into tornado pool
const aliceDepositAmount = utils.parseEther('0.07')
2021-10-05 14:12:39 +02:00
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount, keypair: aliceKeypair })
2021-09-30 17:34:07 +02:00
const { args, extData } = await prepareTransaction({
tornadoPool,
outputs: [aliceDepositUtxo],
})
2021-10-05 14:12:39 +02:00
const onTokenBridgedData = encodeDataForBridge({
proof: args,
2021-10-04 18:28:08 +02:00
extData,
2021-10-05 14:12:39 +02:00
})
2021-09-30 17:34:07 +02:00
const onTokenBridgedTx = await tornadoPool.populateTransaction.onTokenBridged(
token.address,
aliceDepositUtxo.amount,
onTokenBridgedData,
)
2021-10-29 17:36:38 +02:00
// emulating bridge. first it sends tokens to omnibridge mock then it sends to the pool
await token.transfer(omniBridge.address, aliceDepositAmount)
const transferTx = await token.populateTransaction.transfer(tornadoPool.address, aliceDepositAmount)
await omniBridge.execute([
{ who: token.address, callData: transferTx.data }, // send tokens to pool
{ who: tornadoPool.address, callData: onTokenBridgedTx.data }, // call onTokenBridgedTx
])
2021-09-30 17:34:07 +02:00
// withdraws a part of his funds from the shielded pool
const aliceWithdrawAmount = utils.parseEther('0.06')
2021-09-30 17:34:07 +02:00
const recipient = '0xDeaD00000000000000000000000000000000BEEf'
const aliceChangeUtxo = new Utxo({
amount: aliceDepositAmount.sub(aliceWithdrawAmount),
2021-09-30 17:34:07 +02:00
keypair: aliceKeypair,
})
await transaction({
tornadoPool,
inputs: [aliceDepositUtxo],
outputs: [aliceChangeUtxo],
recipient: recipient,
isL1Withdrawal: true,
})
const recipientBalance = await token.balanceOf(recipient)
expect(recipientBalance).to.be.equal(0)
const omniBridgeBalance = await token.balanceOf(omniBridge.address)
expect(omniBridgeBalance).to.be.equal(aliceWithdrawAmount)
})
2022-01-22 01:21:34 +01:00
it('should withdraw with L1 fee', async function () {
2022-02-14 11:34:03 +01:00
const { tornadoPool, token, omniBridge, l1Unwrapper, sender, l1Token } = await loadFixture(fixture)
2022-01-22 01:21:34 +01:00
const aliceKeypair = new Keypair() // contains private and public keys
// regular L1 deposit -------------------------------------------
const aliceDepositAmount = utils.parseEther('0.07')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount, keypair: aliceKeypair })
const { args, extData } = await prepareTransaction({
tornadoPool,
outputs: [aliceDepositUtxo],
})
let onTokenBridgedData = encodeDataForBridge({
proof: args,
extData,
})
let onTokenBridgedTx = await tornadoPool.populateTransaction.onTokenBridged(
token.address,
aliceDepositUtxo.amount,
onTokenBridgedData,
)
// emulating bridge. first it sends tokens to omnibridge mock then it sends to the pool
await token.transfer(omniBridge.address, aliceDepositAmount)
let transferTx = await token.populateTransaction.transfer(tornadoPool.address, aliceDepositAmount)
await omniBridge.execute([
{ who: token.address, callData: transferTx.data }, // send tokens to pool
{ who: tornadoPool.address, callData: onTokenBridgedTx.data }, // call onTokenBridgedTx
])
// withdrawal with L1 fee ---------------------------------------
// withdraws a part of his funds from the shielded pool
const aliceWithdrawAmount = utils.parseEther('0.06')
const l1Fee = utils.parseEther('0.01')
// sum of desired withdraw amount and L1 fee are stored in extAmount
const extAmount = aliceWithdrawAmount.add(l1Fee)
const recipient = '0xDeaD00000000000000000000000000000000BEEf'
const aliceChangeUtxo = new Utxo({
amount: aliceDepositAmount.sub(extAmount),
keypair: aliceKeypair,
})
await transaction({
tornadoPool,
inputs: [aliceDepositUtxo],
outputs: [aliceChangeUtxo],
recipient: recipient,
isL1Withdrawal: true,
l1Fee: l1Fee,
})
const filter = omniBridge.filters.OnTokenTransfer()
const fromBlock = await ethers.provider.getBlock()
const events = await omniBridge.queryFilter(filter, fromBlock.number)
onTokenBridgedData = events[0].args.data
const hexL1Fee = '0x' + events[0].args.data.toString().slice(42)
expect(ethers.BigNumber.from(hexL1Fee)).to.be.equal(l1Fee)
const recipientBalance = await token.balanceOf(recipient)
expect(recipientBalance).to.be.equal(0)
const omniBridgeBalance = await token.balanceOf(omniBridge.address)
expect(omniBridgeBalance).to.be.equal(extAmount)
// L1 transactions:
onTokenBridgedTx = await l1Unwrapper.populateTransaction.onTokenBridged(
l1Token.address,
extAmount,
onTokenBridgedData,
)
2022-02-14 11:27:44 +01:00
// emulating bridge. first it sends tokens to omniBridge mock then it sends to the recipient
await l1Token.transfer(omniBridge.address, extAmount)
2022-01-22 01:21:34 +01:00
transferTx = await l1Token.populateTransaction.transfer(l1Unwrapper.address, extAmount)
const senderBalanceBefore = await ethers.provider.getBalance(sender.address)
2022-02-14 11:27:44 +01:00
let tx = await omniBridge.execute([
2022-01-22 01:21:34 +01:00
{ who: l1Token.address, callData: transferTx.data }, // send tokens to L1Unwrapper
{ who: l1Unwrapper.address, callData: onTokenBridgedTx.data }, // call onTokenBridged on L1Unwrapper
])
let receipt = await tx.wait()
let txFee = receipt.cumulativeGasUsed.mul(receipt.effectiveGasPrice)
const senderBalanceAfter = await ethers.provider.getBalance(sender.address)
expect(senderBalanceAfter).to.be.equal(senderBalanceBefore.sub(txFee).add(l1Fee))
expect(await ethers.provider.getBalance(recipient)).to.be.equal(aliceWithdrawAmount)
})
2022-02-14 13:24:33 +01:00
it('should set L1FeeReceiver on L1Unwrapper contract', async function () {
const { tornadoPool, token, omniBridge, l1Unwrapper, sender, l1Token, multisig } = await loadFixture(
fixture,
)
// check init l1FeeReceiver
expect(await l1Unwrapper.l1FeeReceiver()).to.be.equal(ethers.constants.AddressZero)
// should not set from not multisig
await expect(l1Unwrapper.connect(sender).setL1FeeReceiver(multisig.address)).to.be.reverted
expect(await l1Unwrapper.l1FeeReceiver()).to.be.equal(ethers.constants.AddressZero)
// should set from multisig
await l1Unwrapper.connect(multisig).setL1FeeReceiver(multisig.address)
expect(await l1Unwrapper.l1FeeReceiver()).to.be.equal(multisig.address)
// ------------------------------------------------------------------------
// check withdraw with L1 fee ---------------------------------------------
const aliceKeypair = new Keypair() // contains private and public keys
// regular L1 deposit -------------------------------------------
const aliceDepositAmount = utils.parseEther('0.07')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount, keypair: aliceKeypair })
const { args, extData } = await prepareTransaction({
tornadoPool,
outputs: [aliceDepositUtxo],
})
let onTokenBridgedData = encodeDataForBridge({
proof: args,
extData,
})
let onTokenBridgedTx = await tornadoPool.populateTransaction.onTokenBridged(
token.address,
aliceDepositUtxo.amount,
onTokenBridgedData,
)
// emulating bridge. first it sends tokens to omnibridge mock then it sends to the pool
await token.transfer(omniBridge.address, aliceDepositAmount)
let transferTx = await token.populateTransaction.transfer(tornadoPool.address, aliceDepositAmount)
await omniBridge.execute([
{ who: token.address, callData: transferTx.data }, // send tokens to pool
{ who: tornadoPool.address, callData: onTokenBridgedTx.data }, // call onTokenBridgedTx
])
// withdrawal with L1 fee ---------------------------------------
// withdraws a part of his funds from the shielded pool
const aliceWithdrawAmount = utils.parseEther('0.06')
const l1Fee = utils.parseEther('0.01')
// sum of desired withdraw amount and L1 fee are stored in extAmount
const extAmount = aliceWithdrawAmount.add(l1Fee)
const recipient = '0xDeaD00000000000000000000000000000000BEEf'
const aliceChangeUtxo = new Utxo({
amount: aliceDepositAmount.sub(extAmount),
keypair: aliceKeypair,
})
await transaction({
tornadoPool,
inputs: [aliceDepositUtxo],
outputs: [aliceChangeUtxo],
recipient: recipient,
isL1Withdrawal: true,
l1Fee: l1Fee,
})
const filter = omniBridge.filters.OnTokenTransfer()
const fromBlock = await ethers.provider.getBlock()
const events = await omniBridge.queryFilter(filter, fromBlock.number)
onTokenBridgedData = events[0].args.data
const hexL1Fee = '0x' + events[0].args.data.toString().slice(42)
expect(ethers.BigNumber.from(hexL1Fee)).to.be.equal(l1Fee)
const recipientBalance = await token.balanceOf(recipient)
expect(recipientBalance).to.be.equal(0)
const omniBridgeBalance = await token.balanceOf(omniBridge.address)
expect(omniBridgeBalance).to.be.equal(extAmount)
// L1 transactions:
onTokenBridgedTx = await l1Unwrapper.populateTransaction.onTokenBridged(
l1Token.address,
extAmount,
onTokenBridgedData,
)
// emulating bridge. first it sends tokens to omniBridge mock then it sends to the recipient
await l1Token.transfer(omniBridge.address, extAmount)
transferTx = await l1Token.populateTransaction.transfer(l1Unwrapper.address, extAmount)
const senderBalanceBefore = await ethers.provider.getBalance(sender.address)
const multisigBalanceBefore = await ethers.provider.getBalance(multisig.address)
let tx = await omniBridge.execute([
{ who: l1Token.address, callData: transferTx.data }, // send tokens to L1Unwrapper
{ who: l1Unwrapper.address, callData: onTokenBridgedTx.data }, // call onTokenBridged on L1Unwrapper
])
let receipt = await tx.wait()
let txFee = receipt.cumulativeGasUsed.mul(receipt.effectiveGasPrice)
expect(await ethers.provider.getBalance(sender.address)).to.be.equal(senderBalanceBefore.sub(txFee))
expect(await ethers.provider.getBalance(multisig.address)).to.be.equal(multisigBalanceBefore.add(l1Fee))
expect(await ethers.provider.getBalance(recipient)).to.be.equal(aliceWithdrawAmount)
})
2021-10-29 17:36:38 +02:00
it('should transfer funds to multisig in case of L1 deposit fail', async function () {
const { tornadoPool, token, omniBridge, multisig } = await loadFixture(fixture)
const aliceKeypair = new Keypair() // contains private and public keys
// Alice deposits into tornado pool
const aliceDepositAmount = utils.parseEther('0.07')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount, keypair: aliceKeypair })
const { args, extData } = await prepareTransaction({
tornadoPool,
outputs: [aliceDepositUtxo],
})
args.proof = args.proof.slice(0, -2)
const onTokenBridgedData = encodeDataForBridge({
proof: args,
extData,
})
const onTokenBridgedTx = await tornadoPool.populateTransaction.onTokenBridged(
token.address,
aliceDepositUtxo.amount,
onTokenBridgedData,
)
// emulating bridge. first it sends tokens to omnibridge mock then it sends to the pool
await token.transfer(omniBridge.address, aliceDepositAmount)
const transferTx = await token.populateTransaction.transfer(tornadoPool.address, aliceDepositAmount)
const lastRoot = await tornadoPool.getLastRoot()
await omniBridge.execute([
{ who: token.address, callData: transferTx.data }, // send tokens to pool
{ who: tornadoPool.address, callData: onTokenBridgedTx.data }, // call onTokenBridgedTx
])
const multisigBalance = await token.balanceOf(multisig.address)
expect(multisigBalance).to.be.equal(aliceDepositAmount)
expect(await tornadoPool.getLastRoot()).to.be.equal(lastRoot)
})
it('should revert if onTransact called directly', async () => {
const { tornadoPool } = await loadFixture(fixture)
const aliceKeypair = new Keypair() // contains private and public keys
// Alice deposits into tornado pool
const aliceDepositAmount = utils.parseEther('0.07')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount, keypair: aliceKeypair })
const { args, extData } = await prepareTransaction({
tornadoPool,
outputs: [aliceDepositUtxo],
})
await expect(tornadoPool.onTransact(args, extData)).to.be.revertedWith(
'can be called only from onTokenBridged',
)
})
2021-06-15 13:47:54 +02:00
it('should work with 16 inputs', async function () {
2021-08-21 14:46:17 +02:00
const { tornadoPool } = await loadFixture(fixture)
const aliceDepositAmount = utils.parseEther('0.07')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount })
await transaction({
tornadoPool,
inputs: [new Utxo(), new Utxo(), new Utxo()],
outputs: [aliceDepositUtxo],
})
2021-06-15 13:47:54 +02:00
})
2021-11-01 18:56:24 +01:00
it('should be compliant', async function () {
// basically verifier should check if a commitment and a nullifier hash are on chain
const { tornadoPool } = await loadFixture(fixture)
const aliceDepositAmount = utils.parseEther('0.07')
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount })
const [sender] = await ethers.getSigners()
const { args, extData } = await prepareTransaction({
tornadoPool,
outputs: [aliceDepositUtxo],
})
const receipt = await tornadoPool.transact(args, extData, {
gasLimit: 2e6,
})
await receipt.wait()
// withdrawal
await transaction({
tornadoPool,
inputs: [aliceDepositUtxo],
outputs: [],
recipient: sender.address,
})
const tree = await buildMerkleTree({ tornadoPool })
const commitment = aliceDepositUtxo.getCommitment()
const index = tree.indexOf(toFixedHex(commitment)) // it's the same as merklePath and merklePathIndexes and index in the tree
aliceDepositUtxo.index = index
const nullifier = aliceDepositUtxo.getNullifier()
// commitment = hash(amount, pubKey, blinding)
// nullifier = hash(commitment, merklePath, sign(merklePath, privKey))
const dataForVerifier = {
commitment: {
amount: aliceDepositUtxo.amount,
pubkey: aliceDepositUtxo.keypair.pubkey,
blinding: aliceDepositUtxo.blinding,
},
nullifier: {
2021-11-08 16:05:57 +01:00
commitment,
2021-11-01 18:56:24 +01:00
merklePath: index,
2021-11-08 16:05:57 +01:00
signature: aliceDepositUtxo.keypair.sign(commitment, index),
2021-11-01 18:56:24 +01:00
},
}
// generateReport(dataForVerifier) -> compliance report
// on the verifier side we compute commitment and nullifier and then check them onchain
const commitmentV = poseidonHash([...Object.values(dataForVerifier.commitment)])
const nullifierV = poseidonHash([
commitmentV,
dataForVerifier.nullifier.merklePath,
dataForVerifier.nullifier.signature,
])
expect(commitmentV).to.be.equal(commitment)
expect(nullifierV).to.be.equal(nullifier)
expect(await tornadoPool.nullifierHashes(nullifierV)).to.be.equal(true)
// expect commitmentV present onchain (it will be in NewCommitment events)
// in report we can see the tx with NewCommitment event (this is how alice got money)
// and the tx with NewNullifier event is where alice spent the UTXO
})
2021-06-09 12:56:33 +02:00
})