From 13e01755a6c66132e1f8cc99487acc1c5e5e396c Mon Sep 17 00:00:00 2001 From: Roman Storm Date: Fri, 13 Sep 2019 18:05:08 -0700 Subject: [PATCH] WIP --- cli.js | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index bc88ceb..0f1740f 100755 --- a/cli.js +++ b/cli.js @@ -12,8 +12,8 @@ const Web3 = require('web3') const buildGroth16 = require('websnark/src/groth16') const websnarkUtils = require('websnark/src/utils') -let web3, mixer, circuit, proving_key, groth16 -let MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT +let web3, mixer, erc20mixer, circuit, proving_key, groth16, erc20 +let MERKLE_TREE_HEIGHT, ETH_AMOUNT, EMPTY_ELEMENT, ERC20_TOKEN const inBrowser = (typeof window !== 'undefined') const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) @@ -37,6 +37,78 @@ async function deposit() { return note } +async function depositErc20() { + const account = (await web3.eth.getAccounts())[0] + const tokenAmount = process.env.TOKEN_AMOUNT + await erc20.methods.mint(account, tokenAmount).send({ from: account, gas:1e6 }) + const allowance = await erc20.methods.allowance(account, erc20mixer.address).call() + console.log('erc20mixer allowance', allowance.toString(10)) + + await erc20.methods.approve(erc20mixer.address, tokenAmount).send({ from: account, gas:1e6 }) + + const deposit = createDeposit(rbigint(31), rbigint(31)) + await erc20mixer.methods.deposit('0x' + deposit.commitment.toString(16)).send({ value: ETH_AMOUNT, from: account, gas:1e6 }) + + const balance = await erc20.methods.balanceOf(erc20mixer.address).call() + console.log('erc20mixer balance', balance.toString(10)) + const note = '0x' + deposit.preimage.toString('hex') + console.log('Your note:', note) + return note +} + +async function withdrawErc20(note, receiver) { + let buf = Buffer.from(note.slice(2), 'hex') + let deposit = createDeposit(bigInt.leBuff2int(buf.slice(0, 31)), bigInt.leBuff2int(buf.slice(31, 62))) + + console.log('Getting current state from mixer contract') + const events = await erc20mixer.getPastEvents('Deposit', { fromBlock: erc20mixer.deployedBlock, toBlock: 'latest' }) + let leafIndex + + const commitment = deposit.commitment.toString(16).padStart('66', '0x000000') + const leaves = events + .sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) + .map(e => { + if (e.returnValues.commitment.eq(commitment)) { + leafIndex = e.returnValues.leafIndex.toNumber() + } + return e.returnValues.commitment + }) + const tree = new merkleTree(MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, leaves) + const validRoot = await erc20mixer.methods.isKnownRoot(await tree.root()).call() + const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(31)) + const nullifierHashToCheck = nullifierHash.toString(16).padStart('66', '0x000000') + const isSpent = await mixer.methods.isSpent(nullifierHashToCheck).call() + assert(validRoot === true) + assert(isSpent === false) + + assert(leafIndex >= 0) + const { root, path_elements, path_index } = await tree.path(leafIndex) + // Circuit input + const input = { + // public + root: root, + nullifierHash, + receiver: bigInt(receiver), + fee: bigInt(0), + + // private + nullifier: deposit.nullifier, + secret: deposit.secret, + pathElements: path_elements, + pathIndex: path_index, + } + + console.log('Generating SNARK proof') + console.time('Proof time') + const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key) + const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof) + console.timeEnd('Proof time') + + console.log('Submitting withdraw transaction') + await mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).send({ from: (await web3.eth.getAccounts())[0], gas: 1e6 }) + console.log('Done') +} + async function getBalance(receiver) { const balance = await web3.eth.getBalance(receiver) console.log('Balance is ', web3.utils.fromWei(balance)) @@ -96,7 +168,7 @@ async function withdraw(note, receiver) { } async function init() { - let contractJson + let contractJson, erc20ContractJson, erc20mixerJson if (inBrowser) { web3 = new Web3(window.web3.currentProvider, null, { transactionConfirmationBlocks: 1 }) contractJson = await (await fetch('build/contracts/ETHMixer.json')).json() @@ -114,12 +186,25 @@ async function init() { MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT ETH_AMOUNT = process.env.ETH_AMOUNT EMPTY_ELEMENT = process.env.EMPTY_ELEMENT + ERC20_TOKEN = process.env.ERC20_TOKEN + erc20ContractJson = require('./build/contracts/ERC20Mock.json') + erc20mixerJson = require('./build/contracts/ERC20Mixer.json') } groth16 = await buildGroth16() let netId = await web3.eth.net.getId() - const tx = await web3.eth.getTransaction(contractJson.networks[netId].transactionHash) - mixer = new web3.eth.Contract(contractJson.abi, contractJson.networks[netId].address) - mixer.deployedBlock = tx.blockNumber + // const tx = await web3.eth.getTransaction(contractJson.networks[netId].transactionHash) + // mixer = new web3.eth.Contract(contractJson.abi, contractJson.networks[netId].address) + // mixer.deployedBlock = tx.blockNumber + + const tx3 = await web3.eth.getTransaction(erc20mixerJson.networks[netId].transactionHash) + erc20mixer = new web3.eth.Contract(erc20mixerJson.abi, erc20mixerJson.networks[netId].address) + erc20mixer.deployedBlock = tx3.blockNumber + + if(ERC20_TOKEN === '') { + erc20 = new web3.eth.Contract(erc20ContractJson.abi, erc20ContractJson.networks[netId].address) + const tx2 = await web3.eth.getTransaction(erc20ContractJson.networks[netId].transactionHash) + erc20.deployedBlock = tx2.blockNumber + } console.log('Loaded') } @@ -167,6 +252,13 @@ if (inBrowser) { else printHelp(1) break + case 'depositErc20': + if (args.length === 1) { + init().then(() => depositErc20()).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)}) + } + else + printHelp(1) + break case 'balance': if (args.length === 2 && /^0x[0-9a-fA-F]{40}$/.test(args[1])) { init().then(() => getBalance(args[1])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})