diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index 81a8c11..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,28 +0,0 @@ -const MiMC = require('./MiMC') -const snarkjs = require('snarkjs') - -const hasher = new MiMC() -const bigInt = snarkjs.bigInt - -const toHex = (number, length = 32) => - '0x' + - (number instanceof Buffer ? number.toString('hex') : bigInt(number).toString(16)).padStart(length * 2, '0') - -function zeros( - levels, - defaultZero = '21663839004416932945382355908790599225266501822907911457504978515578255421292', -) { - const zeros = [] - - let currentZero = defaultZero - for (let i = 0; i < levels; i++) { - zeros.push(toHex(currentZero)) - currentZero = hasher.hash(levels, currentZero, currentZero) - } - - return zeros -} - -// console.log(zeros(32)) - -module.exports = { zeros, toHex } diff --git a/package.json b/package.json index bf370eb..b11fc10 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build:circuit:contract": "npx snarkjs generateverifier -v build/circuits/Verifier.sol --vk build/circuits/withdraw_verification_key.json", "build:circuit": "mkdir -p build/circuits && npm run build:circuit:compile && npm run build:circuit:setup && npm run build:circuit:bin && npm run build:circuit:contract", "build:contract": "npx truffle compile", - "build:browserify": "npx browserify cli.js -o index.js --exclude worker_threads", + "build:browserify": "npx browserify src/cli.js -o index.js --exclude worker_threads", "build": "npm run build:circuit && npm run build:contract && npm run build:browserify", "browserify": "npm run build:browserify", "test": "npx truffle test", @@ -24,7 +24,7 @@ "prettier:fix": "prettier --write . --config .prettierrc", "lint": "yarn eslint && yarn prettier:check", "flat": "npx truffle-flattener contracts/ETHTornado.sol > ETHTornado_flat.sol && npx truffle-flattener contracts/ERC20Tornado.sol > ERC20Tornado_flat.sol", - "download": "node downloadKeys.js" + "download": "node scripts/downloadKeys.js" }, "keywords": [], "author": "", diff --git a/compileHasher.js b/scripts/compileHasher.js similarity index 88% rename from compileHasher.js rename to scripts/compileHasher.js index be4af01..84767dd 100644 --- a/compileHasher.js +++ b/scripts/compileHasher.js @@ -6,7 +6,7 @@ const genContract = require('circomlib/src/mimcsponge_gencontract.js') // where Truffle will expect to find the results of the external compiler // command -const outputPath = path.join(__dirname, 'build', 'Hasher.json') +const outputPath = path.join(__dirname, '..', 'build', 'Hasher.json') function main() { const contract = { diff --git a/downloadKeys.js b/scripts/downloadKeys.js similarity index 91% rename from downloadKeys.js rename to scripts/downloadKeys.js index 3767948..15bda81 100644 --- a/downloadKeys.js +++ b/scripts/downloadKeys.js @@ -2,8 +2,8 @@ const axios = require('axios') const path = require('path') const fs = require('fs') const files = ['withdraw.json', 'withdraw_proving_key.bin', 'Verifier.sol', 'withdraw_verification_key.json'] -const circuitsPath = 'build/circuits' -const contractsPath = 'build/contracts' +const circuitsPath = __dirname + '/../build/circuits' +const contractsPath = __dirname + '/../build/contracts' async function downloadFile({ url, path }) { const writer = fs.createWriteStream(path) diff --git a/lib/ganacheHelper.js b/scripts/ganacheHelper.js similarity index 100% rename from lib/ganacheHelper.js rename to scripts/ganacheHelper.js diff --git a/cli.js b/src/cli.js similarity index 97% rename from cli.js rename to src/cli.js index bab65a2..2a67709 100755 --- a/cli.js +++ b/src/cli.js @@ -134,7 +134,8 @@ async function generateMerkleProof(deposit) { assert(leafIndex >= 0, 'The deposit is not found in the tree') // Compute merkle proof of our commitment - return tree.path(leafIndex) + const { pathElements, pathIndices } = tree.path(leafIndex) + return { pathElements, pathIndices, root: tree.root() } } /** @@ -439,7 +440,7 @@ async function loadDepositData({ deposit }) { } async function loadWithdrawalData({ amount, currency, deposit }) { try { - const events = await await tornado.getPastEvents('Withdrawal', { + const events = await tornado.getPastEvents('Withdrawal', { fromBlock: 0, toBlock: 'latest', }) @@ -487,9 +488,9 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100' }) { } else { // Initialize from local node web3 = new Web3(rpc, null, { transactionConfirmationBlocks: 1 }) - contractJson = require('./build/contracts/ETHTornado.json') - circuit = require('./build/circuits/withdraw.json') - proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer + contractJson = require(__dirname + '/../build/contracts/ETHTornado.json') + circuit = require(__dirname + '/../build/circuits/withdraw.json') + proving_key = fs.readFileSync(__dirname + '/../build/circuits/withdraw_proving_key.bin').buffer MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT || 20 ETH_AMOUNT = process.env.ETH_AMOUNT TOKEN_AMOUNT = process.env.TOKEN_AMOUNT @@ -502,8 +503,8 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100' }) { } else { console.log('Warning! PRIVATE_KEY not found. Please provide PRIVATE_KEY in .env file if you deposit') } - erc20ContractJson = require('./build/contracts/ERC20Mock.json') - erc20tornadoJson = require('./build/contracts/ERC20Tornado.json') + erc20ContractJson = require(__dirname + '/../build/contracts/ERC20Mock.json') + erc20tornadoJson = require(__dirname + '/../build/contracts/ERC20Tornado.json') } // groth16 initialises a lot of Promises that will never be resolved, that's why we need to use process.exit to terminate the CLI groth16 = await buildGroth16() diff --git a/config.js b/src/config.js similarity index 100% rename from config.js rename to src/config.js diff --git a/minimal-demo.js b/src/minimal-demo.js similarity index 92% rename from minimal-demo.js rename to src/minimal-demo.js index 47eb31b..e8f54bb 100644 --- a/minimal-demo.js +++ b/src/minimal-demo.js @@ -106,7 +106,8 @@ async function generateMerkleProof(deposit) { assert(leafIndex >= 0, 'The deposit is not found in the tree') // Compute merkle proof of our commitment - return tree.path(leafIndex) + const { pathElements, pathIndices } = tree.path(leafIndex) + return { pathElements, pathIndices, root: tree.root() } } /** @@ -116,7 +117,7 @@ async function generateMerkleProof(deposit) { */ async function generateSnarkProof(deposit, recipient) { // Compute merkle proof of our commitment - const { root, pathElements, pathIndices } = generateMerkleProof(deposit) + const { root, pathElements, pathIndices } = await generateMerkleProof(deposit) // Prepare circuit input const input = { @@ -155,11 +156,11 @@ async function main() { web3 = new Web3(new Web3.providers.HttpProvider(RPC_URL, { timeout: 5 * 60 * 1000 }), null, { transactionConfirmationBlocks: 1, }) - circuit = require('./build/circuits/withdraw.json') - proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer + circuit = require(__dirname + '/../build/circuits/withdraw.json') + proving_key = fs.readFileSync(__dirname + '/../build/circuits/withdraw_proving_key.bin').buffer groth16 = await buildGroth16() netId = await web3.eth.net.getId() - contract = new web3.eth.Contract(require('./build/contracts/ETHTornado.json').abi, CONTRACT_ADDRESS) + contract = new web3.eth.Contract(require('../build/contracts/ETHTornado.json').abi, CONTRACT_ADDRESS) const account = web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY) web3.eth.accounts.wallet.add('0x' + PRIVATE_KEY) // eslint-disable-next-line require-atomic-updates diff --git a/test/ERC20Tornado.test.js b/test/ERC20Tornado.test.js index a4b4680..febe36b 100644 --- a/test/ERC20Tornado.test.js +++ b/test/ERC20Tornado.test.js @@ -3,7 +3,7 @@ require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-prom const fs = require('fs') const { toBN } = require('web3-utils') -const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') +const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper') const Tornado = artifacts.require('./ERC20Tornado.sol') const BadRecipient = artifacts.require('./BadRecipient.sol') diff --git a/test/ETHTornado.test.js b/test/ETHTornado.test.js index b8a5656..d1e7991 100644 --- a/test/ETHTornado.test.js +++ b/test/ETHTornado.test.js @@ -3,7 +3,7 @@ require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-prom const fs = require('fs') const { toBN, randomHex } = require('web3-utils') -const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') +const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper') const Tornado = artifacts.require('./ETHTornado.sol') const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env diff --git a/test/MerkleTreeWithHistory.test.js b/test/MerkleTreeWithHistory.test.js index 90aab40..13afd80 100644 --- a/test/MerkleTreeWithHistory.test.js +++ b/test/MerkleTreeWithHistory.test.js @@ -1,7 +1,7 @@ /* global artifacts, web3, contract */ require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should() -const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') +const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper') const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol') const hasherContract = artifacts.require('./Hasher.sol') diff --git a/truffle-config.js b/truffle-config.js index be52484..ecee528 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -1,10 +1,6 @@ require('dotenv').config() const HDWalletProvider = require('@truffle/hdwallet-provider') const utils = require('web3-utils') -// const infuraKey = "fj4jll3k....."; -// -// const fs = require('fs'); -// const mnemonic = fs.readFileSync(".secret").toString().trim(); module.exports = { /** @@ -18,30 +14,11 @@ module.exports = { */ networks: { - // Useful for testing. The `development` name is special - truffle uses it by default - // if it's defined here and no other network is specified at the command line. - // You should run a client (like ganache-cli, geth or parity) in a separate terminal - // tab if you use this network and you must also set the `host`, `port` and `network_id` - // options below to some value. - development: { host: '127.0.0.1', // Localhost (default: none) port: 8545, // Standard Ethereum port (default: none) network_id: '*', // Any network (default: none) }, - - // Another network with more advanced options... - // advanced: { - // port: 8777, // Custom port - // network_id: 1342, // Custom network - // gas: 8500000, // Gas sent with each transaction (default: ~6700000) - // gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei) - // from:
, // Account to send txs from (default: accounts[0]) - // websockets: true // Enable EventEmitter interface for web3 (default: false) - // }, - - // Useful for deploying to a public network. - // NB: It's important to wrap the provider as a function. kovan: { provider: () => new HDWalletProvider( @@ -90,16 +67,8 @@ module.exports = { // timeoutBlocks: 200, skipDryRun: true, }, - - // Useful for private networks - // private: { - // provider: () => new HDWalletProvider(mnemonic, `https://network.io`), - // network_id: 2111, // This network is yours, in the cloud. - // production: true // Treats this network as if it was a public net. (default: false) - // } }, - // Set default mocha options here, use special reporters etc. mocha: { // timeout: 100000 }, @@ -107,19 +76,16 @@ module.exports = { // Configure your compilers compilers: { solc: { - version: '0.7.6', // Fetch exact version from solc-bin (default: truffle's version) - // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) + version: '0.7.6', settings: { - // See the solidity docs for advice about optimization and evmVersion optimizer: { enabled: true, runs: 200, }, - // evmVersion: "byzantium" }, }, external: { - command: 'node ./compileHasher.js', + command: 'node ./scripts/compileHasher.js', targets: [ { path: './build/Hasher.json',