diff --git a/test/ERC20Mixer.test.js b/test/ERC20Mixer.test.js index 30a178f..ad7e07b 100644 --- a/test/ERC20Mixer.test.js +++ b/test/ERC20Mixer.test.js @@ -25,6 +25,8 @@ const MerkleTree = require('../lib/MerkleTree') const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0] +const toFixedHex = (number, length = 32) => '0x' + bigInt(number).toString(16).padStart(length * 2, '0') +const getRandomRecipient = () => rbigint(20) function generateDeposit() { let deposit = { @@ -36,21 +38,6 @@ function generateDeposit() { return deposit } -function getRandomRecipient() { - let recipient = rbigint(20) - while (toHex(recipient.toString()).length !== 42) { - recipient = rbigint(20) - } - return recipient -} - -function toFixedHex(number, length = 32) { - let str = bigInt(number).toString(16) - while (str.length < length * 2) str = '0' + str - str = '0x' + str - return str -} - contract('ERC20Mixer', accounts => { let mixer let token @@ -161,10 +148,10 @@ contract('ERC20Mixer', accounts => { const balanceMixerBefore = await token.balanceOf(mixer.address) const balanceRelayerBefore = await token.balanceOf(relayer) - const balanceRecieverBefore = await token.balanceOf(toHex(recipient.toString())) + const balanceRecieverBefore = await token.balanceOf(toFixedHex(recipient, 20)) const ethBalanceOperatorBefore = await web3.eth.getBalance(operator) - const ethBalanceRecieverBefore = await web3.eth.getBalance(toHex(recipient.toString())) + const ethBalanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20)) const ethBalanceRelayerBefore = await web3.eth.getBalance(relayer) let isSpent = await mixer.isSpent(toFixedHex(input.nullifierHash)) isSpent.should.be.equal(false) @@ -184,8 +171,8 @@ contract('ERC20Mixer', accounts => { const balanceMixerAfter = await token.balanceOf(mixer.address) const balanceRelayerAfter = await token.balanceOf(relayer) const ethBalanceOperatorAfter = await web3.eth.getBalance(operator) - const balanceRecieverAfter = await token.balanceOf(toHex(recipient.toString())) - const ethBalanceRecieverAfter = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverAfter = await token.balanceOf(toFixedHex(recipient, 20)) + const ethBalanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20)) const ethBalanceRelayerAfter = await web3.eth.getBalance(relayer) const feeBN = toBN(fee.toString()) balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(tokenDenomination))) @@ -242,10 +229,10 @@ contract('ERC20Mixer', accounts => { const balanceMixerBefore = await token.balanceOf(mixer.address) const balanceRelayerBefore = await token.balanceOf(relayer) - const balanceRecieverBefore = await token.balanceOf(toHex(recipient.toString())) + const balanceRecieverBefore = await token.balanceOf(toFixedHex(recipient, 20)) const ethBalanceOperatorBefore = await web3.eth.getBalance(operator) - const ethBalanceRecieverBefore = await web3.eth.getBalance(toHex(recipient.toString())) + const ethBalanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20)) const ethBalanceRelayerBefore = await web3.eth.getBalance(relayer) let isSpent = await mixer.isSpent(toFixedHex(input.nullifierHash)) isSpent.should.be.equal(false) @@ -263,8 +250,8 @@ contract('ERC20Mixer', accounts => { const balanceMixerAfter = await token.balanceOf(mixer.address) const balanceRelayerAfter = await token.balanceOf(relayer) const ethBalanceOperatorAfter = await web3.eth.getBalance(operator) - const balanceRecieverAfter = await token.balanceOf(toHex(recipient.toString())) - const ethBalanceRecieverAfter = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverAfter = await token.balanceOf(toFixedHex(recipient, 20)) + const ethBalanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20)) const ethBalanceRelayerAfter = await web3.eth.getBalance(relayer) const feeBN = toBN(fee.toString()) balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(tokenDenomination))) @@ -384,8 +371,8 @@ contract('ERC20Mixer', accounts => { const balanceMixerBefore = await usdtToken.balanceOf(mixer.address) const balanceRelayerBefore = await usdtToken.balanceOf(relayer) const ethBalanceOperatorBefore = await web3.eth.getBalance(operator) - const balanceRecieverBefore = await usdtToken.balanceOf(toHex(recipient.toString())) - const ethBalanceRecieverBefore = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverBefore = await usdtToken.balanceOf(toFixedHex(recipient, 20)) + const ethBalanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20)) let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000')) isSpent.should.be.equal(false) @@ -405,8 +392,8 @@ contract('ERC20Mixer', accounts => { const balanceMixerAfter = await usdtToken.balanceOf(mixer.address) const balanceRelayerAfter = await usdtToken.balanceOf(relayer) const ethBalanceOperatorAfter = await web3.eth.getBalance(operator) - const balanceRecieverAfter = await usdtToken.balanceOf(toHex(recipient.toString())) - const ethBalanceRecieverAfter = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverAfter = await usdtToken.balanceOf(toFixedHex(recipient, 20)) + const ethBalanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20)) const feeBN = toBN(fee.toString()) balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(tokenDenomination))) balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore)) @@ -473,8 +460,8 @@ contract('ERC20Mixer', accounts => { const balanceMixerBefore = await token.balanceOf(mixer.address) const balanceRelayerBefore = await token.balanceOf(relayer) const ethBalanceOperatorBefore = await web3.eth.getBalance(operator) - const balanceRecieverBefore = await token.balanceOf(toHex(recipient.toString())) - const ethBalanceRecieverBefore = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverBefore = await token.balanceOf(toFixedHex(recipient, 20)) + const ethBalanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20)) let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000')) isSpent.should.be.equal(false) @@ -495,8 +482,8 @@ contract('ERC20Mixer', accounts => { const balanceMixerAfter = await token.balanceOf(mixer.address) const balanceRelayerAfter = await token.balanceOf(relayer) const ethBalanceOperatorAfter = await web3.eth.getBalance(operator) - const balanceRecieverAfter = await token.balanceOf(toHex(recipient.toString())) - const ethBalanceRecieverAfter = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverAfter = await token.balanceOf(toFixedHex(recipient, 20)) + const ethBalanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20)) const feeBN = toBN(fee.toString()) balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(tokenDenomination))) balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore)) diff --git a/test/ETHMixer.test.js b/test/ETHMixer.test.js index a2622af..9d01f22 100644 --- a/test/ETHMixer.test.js +++ b/test/ETHMixer.test.js @@ -23,6 +23,8 @@ const MerkleTree = require('../lib/MerkleTree') const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0] +const toFixedHex = (number, length = 32) => '0x' + bigInt(number).toString(16).padStart(length * 2, '0') +const getRandomRecipient = () => rbigint(20) function generateDeposit() { let deposit = { @@ -43,27 +45,12 @@ function BNArrayToStringArray(array) { return arrayToPrint } -function getRandomRecipient() { - let recipient = rbigint(20) - while (toHex(recipient.toString()).length !== 42) { - recipient = rbigint(20) - } - return recipient -} - function snarkVerify(proof) { proof = unstringifyBigInts2(proof) const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json')) return snarkjs['groth'].isValid(verification_key, proof, proof.publicSignals) } -function toFixedHex(number, length = 32) { - let str = bigInt(number).toString(16) - while (str.length < length * 2) str = '0' + str - str = '0x' + str - return str -} - contract('ETHMixer', accounts => { let mixer const sender = accounts[0] @@ -212,7 +199,7 @@ contract('ETHMixer', accounts => { const balanceMixerBefore = await web3.eth.getBalance(mixer.address) const balanceRelayerBefore = await web3.eth.getBalance(relayer) const balanceOperatorBefore = await web3.eth.getBalance(operator) - const balanceRecieverBefore = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverBefore = await web3.eth.getBalance(toFixedHex(recipient, 20)) let isSpent = await mixer.isSpent(toFixedHex(input.nullifierHash)) isSpent.should.be.equal(false) @@ -232,7 +219,7 @@ contract('ETHMixer', accounts => { const balanceMixerAfter = await web3.eth.getBalance(mixer.address) const balanceRelayerAfter = await web3.eth.getBalance(relayer) const balanceOperatorAfter = await web3.eth.getBalance(operator) - const balanceRecieverAfter = await web3.eth.getBalance(toHex(recipient.toString())) + const balanceRecieverAfter = await web3.eth.getBalance(toFixedHex(recipient, 20)) const feeBN = toBN(fee.toString()) balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value))) balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore))