2019-07-13 13:57:49 +02:00
|
|
|
#!/usr/bin/env node
|
2019-07-17 13:12:57 +02:00
|
|
|
// Temporary demo client
|
2019-07-18 20:41:18 +02:00
|
|
|
// Works both in browser and node.js
|
2019-07-16 19:20:16 +02:00
|
|
|
const fs = require('fs')
|
2019-11-10 01:48:09 +01:00
|
|
|
const axios = require('axios')
|
2019-07-16 19:20:16 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const snarkjs = require('snarkjs')
|
2019-07-18 20:27:51 +02:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const circomlib = require('circomlib')
|
2019-07-16 19:20:16 +02:00
|
|
|
const bigInt = snarkjs.bigInt
|
|
|
|
const merkleTree = require('./lib/MerkleTree')
|
|
|
|
const Web3 = require('web3')
|
|
|
|
const buildGroth16 = require('websnark/src/groth16')
|
|
|
|
const websnarkUtils = require('websnark/src/utils')
|
2019-11-10 01:48:09 +01:00
|
|
|
const { toWei, fromWei } = require('web3-utils')
|
2019-07-16 19:20:16 +02:00
|
|
|
|
2019-12-13 14:49:19 +01:00
|
|
|
let web3, tornado, erc20tornado, circuit, proving_key, groth16, erc20, senderAccount
|
2019-11-10 01:48:09 +01:00
|
|
|
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, ERC20_TOKEN
|
|
|
|
|
|
|
|
/** Whether we are in a browser or node.js */
|
2019-07-16 19:20:16 +02:00
|
|
|
const inBrowser = (typeof window !== 'undefined')
|
2019-07-13 16:45:08 +02:00
|
|
|
|
2019-09-05 23:43:06 +02:00
|
|
|
/** Generate random number of specified byte length */
|
2019-11-10 01:48:09 +01:00
|
|
|
const rbigint = nbytes => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
2019-09-05 23:43:06 +02:00
|
|
|
|
|
|
|
/** Compute pedersen hash */
|
2019-11-10 01:48:09 +01:00
|
|
|
const pedersenHash = data => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
|
|
|
|
|
|
|
/** BigNumber to hex string of specified length */
|
|
|
|
function toHex(number, length = 32) {
|
|
|
|
let str = number instanceof Buffer ? number.toString('hex') : bigInt(number).toString(16)
|
|
|
|
return '0x' + str.padStart(length * 2, '0')
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Display account balance */
|
|
|
|
async function printBalance(account, name) {
|
|
|
|
console.log(`${name} ETH balance is`, web3.utils.fromWei(await web3.eth.getBalance(account)))
|
|
|
|
console.log(`${name} Token Balance is`, web3.utils.fromWei(await erc20.methods.balanceOf(account).call()))
|
|
|
|
}
|
2019-07-18 20:27:51 +02:00
|
|
|
|
2019-09-05 23:43:06 +02:00
|
|
|
/**
|
|
|
|
* Create deposit object from secret and nullifier
|
|
|
|
*/
|
2019-07-13 16:45:08 +02:00
|
|
|
function createDeposit(nullifier, secret) {
|
2019-07-16 19:20:16 +02:00
|
|
|
let deposit = { nullifier, secret }
|
2019-08-01 16:49:34 +02:00
|
|
|
deposit.preimage = Buffer.concat([deposit.nullifier.leInt2Buff(31), deposit.secret.leInt2Buff(31)])
|
2019-07-18 20:27:51 +02:00
|
|
|
deposit.commitment = pedersenHash(deposit.preimage)
|
2019-11-10 01:48:09 +01:00
|
|
|
deposit.nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(31))
|
2019-07-16 19:20:16 +02:00
|
|
|
return deposit
|
2019-07-13 16:45:08 +02:00
|
|
|
}
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-09-05 23:43:06 +02:00
|
|
|
/**
|
2019-11-10 01:48:09 +01:00
|
|
|
* Make an ETH deposit
|
2019-09-05 23:43:06 +02:00
|
|
|
*/
|
2019-07-13 13:57:49 +02:00
|
|
|
async function deposit() {
|
2019-07-18 20:27:51 +02:00
|
|
|
const deposit = createDeposit(rbigint(31), rbigint(31))
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Submitting deposit transaction')
|
2019-12-13 21:36:32 +01:00
|
|
|
await tornado.methods.deposit(toHex(deposit.commitment)).send({ value: ETH_AMOUNT, from: senderAccount, gas:2e6 })
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
const note = toHex(deposit.preimage, 62)
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Your note:', note)
|
|
|
|
return note
|
2019-07-13 13:57:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
/**
|
|
|
|
* Make an ERC20 deposit
|
|
|
|
*/
|
2019-09-14 03:05:08 +02:00
|
|
|
async function depositErc20() {
|
|
|
|
const deposit = createDeposit(rbigint(31), rbigint(31))
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
if(ERC20_TOKEN === '') {
|
|
|
|
console.log('Minting some test tokens to deposit')
|
2019-12-13 21:01:38 +01:00
|
|
|
await erc20.methods.mint(senderAccount, TOKEN_AMOUNT).send({ from: senderAccount, gas: 2e6 })
|
2019-09-14 03:05:08 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
console.log('Approving tokens for deposit')
|
2019-12-13 14:49:19 +01:00
|
|
|
await erc20.methods.approve(erc20tornado._address, TOKEN_AMOUNT).send({ from: senderAccount, gas:1e6 })
|
2019-09-14 20:21:53 +02:00
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
console.log('Submitting deposit transaction')
|
2019-12-13 21:36:32 +01:00
|
|
|
await erc20tornado.methods.deposit(toHex(deposit.commitment)).send({ from: senderAccount, gas:2e6 })
|
2019-09-14 20:21:53 +02:00
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
const note = toHex(deposit.preimage, 62)
|
|
|
|
console.log('Your note:', note)
|
|
|
|
return note
|
2019-11-04 20:42:41 +01:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
/**
|
|
|
|
* Generate merkle tree for a deposit.
|
|
|
|
* Download deposit events from the contract, reconstructs merkle tree, finds our deposit leaf
|
|
|
|
* in it and generates merkle proof
|
2019-12-13 14:49:19 +01:00
|
|
|
* @param contract Tornado contract address
|
2019-11-10 01:48:09 +01:00
|
|
|
* @param deposit Deposit object
|
|
|
|
*/
|
|
|
|
async function generateMerkleProof(contract, deposit) {
|
2019-09-05 23:43:06 +02:00
|
|
|
// Get all deposit events from smart contract and assemble merkle tree from them
|
2019-12-13 14:49:19 +01:00
|
|
|
console.log('Getting current state from tornado contract')
|
2019-11-10 01:48:09 +01:00
|
|
|
const events = await contract.getPastEvents('Deposit', { fromBlock: contract.deployedBlock, toBlock: 'latest' })
|
2019-07-24 18:57:51 +02:00
|
|
|
const leaves = events
|
2019-11-03 11:11:22 +01:00
|
|
|
.sort((a, b) => a.returnValues.leafIndex - b.returnValues.leafIndex) // Sort events in chronological order
|
2019-09-05 23:43:06 +02:00
|
|
|
.map(e => e.returnValues.commitment)
|
2019-11-02 13:35:22 +01:00
|
|
|
const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-09-05 23:43:06 +02:00
|
|
|
// Find current commitment in the tree
|
2019-11-10 01:48:09 +01:00
|
|
|
let depositEvent = events.find(e => e.returnValues.commitment === toHex(deposit.commitment))
|
2019-11-03 11:11:22 +01:00
|
|
|
let leafIndex = depositEvent ? depositEvent.returnValues.leafIndex : -1
|
2019-09-05 23:43:06 +02:00
|
|
|
|
|
|
|
// Validate that our data is correct
|
2019-11-10 01:48:09 +01:00
|
|
|
const isValidRoot = await contract.methods.isKnownRoot(toHex(await tree.root())).call()
|
|
|
|
const isSpent = await contract.methods.isSpent(toHex(deposit.nullifierHash)).call()
|
|
|
|
assert(isValidRoot === true, 'Merkle tree is corrupted')
|
|
|
|
assert(isSpent === false, 'The note is already spent')
|
|
|
|
assert(leafIndex >= 0, 'The deposit is not found in the tree')
|
|
|
|
|
|
|
|
// Compute merkle proof of our commitment
|
|
|
|
return await tree.path(leafIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate SNARK proof for withdrawal
|
2019-12-13 14:49:19 +01:00
|
|
|
* @param contract Tornado contract address
|
2019-11-10 01:48:09 +01:00
|
|
|
* @param note Note string
|
|
|
|
* @param recipient Funds recipient
|
|
|
|
* @param relayer Relayer address
|
|
|
|
* @param fee Relayer fee
|
|
|
|
* @param refund Receive ether for exchanged tokens
|
|
|
|
*/
|
|
|
|
async function generateProof(contract, note, recipient, relayer = 0, fee = 0, refund = 0) {
|
|
|
|
// Decode hex string and restore the deposit object
|
|
|
|
let buf = Buffer.from(note.slice(2), 'hex')
|
|
|
|
let deposit = createDeposit(bigInt.leBuff2int(buf.slice(0, 31)), bigInt.leBuff2int(buf.slice(31, 62)))
|
2019-09-05 23:43:06 +02:00
|
|
|
|
|
|
|
// Compute merkle proof of our commitment
|
2019-11-10 01:48:09 +01:00
|
|
|
const { root, path_elements, path_index } = await generateMerkleProof(contract, deposit)
|
2019-09-05 23:43:06 +02:00
|
|
|
|
|
|
|
// Prepare circuit input
|
2019-07-13 13:57:49 +02:00
|
|
|
const input = {
|
2019-09-05 23:43:06 +02:00
|
|
|
// Public snark inputs
|
2019-07-13 13:57:49 +02:00
|
|
|
root: root,
|
2019-11-10 01:48:09 +01:00
|
|
|
nullifierHash: deposit.nullifierHash,
|
2019-11-07 08:04:29 +01:00
|
|
|
recipient: bigInt(recipient),
|
2019-11-10 01:48:09 +01:00
|
|
|
relayer: bigInt(relayer),
|
|
|
|
fee: bigInt(fee),
|
|
|
|
refund: bigInt(refund),
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-09-05 23:43:06 +02:00
|
|
|
// Private snark inputs
|
2019-07-19 18:37:38 +02:00
|
|
|
nullifier: deposit.nullifier,
|
2019-07-13 13:57:49 +02:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-02 03:05:25 +01:00
|
|
|
pathIndices: path_index,
|
2019-07-16 19:20:16 +02:00
|
|
|
}
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Generating SNARK proof')
|
2019-07-17 13:12:57 +02:00
|
|
|
console.time('Proof time')
|
2019-10-04 17:20:20 +02:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 20:42:41 +01:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
2019-07-17 13:12:57 +02:00
|
|
|
console.timeEnd('Proof time')
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-11-04 20:42:41 +01:00
|
|
|
const args = [
|
|
|
|
toHex(input.root),
|
|
|
|
toHex(input.nullifierHash),
|
2019-11-07 08:04:29 +01:00
|
|
|
toHex(input.recipient, 20),
|
2019-11-04 20:42:41 +01:00
|
|
|
toHex(input.relayer, 20),
|
|
|
|
toHex(input.fee),
|
|
|
|
toHex(input.refund)
|
|
|
|
]
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
return { proof, args }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do an ETH withdrawal
|
|
|
|
* @param note Note to withdraw
|
|
|
|
* @param recipient Recipient address
|
|
|
|
*/
|
|
|
|
async function withdraw(note, recipient) {
|
2019-12-13 14:49:19 +01:00
|
|
|
const { proof, args } = await generateProof(tornado, note, recipient)
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
console.log('Submitting withdraw transaction')
|
2019-12-13 14:49:19 +01:00
|
|
|
await tornado.methods.withdraw(proof, ...args).send({ from: senderAccount, gas: 1e6 })
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Done')
|
2019-07-13 13:57:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
/**
|
|
|
|
* Do a ERC20 withdrawal
|
|
|
|
* @param note Note to withdraw
|
|
|
|
* @param recipient Recipient address
|
|
|
|
*/
|
|
|
|
async function withdrawErc20(note, recipient) {
|
2019-12-13 14:49:19 +01:00
|
|
|
const { proof, args } = await generateProof(erc20tornado, note, recipient)
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
console.log('Submitting withdraw transaction')
|
2019-12-13 14:49:19 +01:00
|
|
|
await erc20tornado.methods.withdraw(proof, ...args).send({ from: senderAccount, gas: 1e6 })
|
2019-11-10 01:48:09 +01:00
|
|
|
console.log('Done')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do an ETH withdrawal through relay
|
|
|
|
* @param note Note to withdraw
|
|
|
|
* @param recipient Recipient address
|
|
|
|
* @param relayUrl Relay url address
|
|
|
|
*/
|
|
|
|
async function withdrawRelay(note, recipient, relayUrl) {
|
|
|
|
const resp = await axios.get(relayUrl + '/status')
|
|
|
|
const { relayerAddress, netId, gasPrices } = resp.data
|
|
|
|
assert(netId === await web3.eth.net.getId() || netId === '*', 'This relay is for different network')
|
|
|
|
console.log('Relay address: ', relayerAddress)
|
|
|
|
|
|
|
|
const fee = bigInt(toWei(gasPrices.fast.toString(), 'gwei')).mul(bigInt(1e6))
|
2019-12-13 14:49:19 +01:00
|
|
|
const { proof, args } = await generateProof(tornado, note, recipient, relayerAddress, fee)
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
console.log('Sending withdraw transaction through relay')
|
2019-12-13 14:49:19 +01:00
|
|
|
const resp2 = await axios.post(relayUrl + '/relay', { contract: tornado._address, proof: { proof, publicSignals: args } })
|
2019-11-10 01:48:09 +01:00
|
|
|
console.log(`Transaction submitted through relay, tx hash: ${resp2.data.txHash}`)
|
|
|
|
|
|
|
|
let receipt = await waitForTxReceipt(resp2.data.txHash)
|
|
|
|
console.log('Transaction mined in block', receipt.blockNumber)
|
|
|
|
console.log('Done')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a ERC20 withdrawal through relay
|
|
|
|
* @param note Note to withdraw
|
|
|
|
* @param recipient Recipient address
|
|
|
|
* @param relayUrl Relay url address
|
|
|
|
*/
|
|
|
|
async function withdrawRelayErc20(note, recipient, relayUrl) {
|
|
|
|
const resp = await axios.get(relayUrl + '/status')
|
|
|
|
const { relayerAddress, netId, gasPrices, ethPriceInDai } = resp.data
|
|
|
|
assert(netId === await web3.eth.net.getId() || netId === '*', 'This relay is for different network')
|
|
|
|
console.log('Relay address: ', relayerAddress)
|
|
|
|
|
|
|
|
const refund = bigInt(toWei('0.001'))
|
|
|
|
const fee = bigInt(toWei(gasPrices.fast.toString(), 'gwei')).mul(bigInt(1e6)).add(refund).mul(bigInt(fromWei(ethPriceInDai.toString())))
|
2019-12-13 14:49:19 +01:00
|
|
|
const { proof, args } = await generateProof(erc20tornado, note, recipient, relayerAddress, fee, refund)
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
console.log('Sending withdraw transaction through relay')
|
2019-12-13 14:49:19 +01:00
|
|
|
const resp2 = await axios.post(relayUrl + '/relay', { contract: erc20tornado._address, proof: { proof, publicSignals: args } })
|
2019-11-10 01:48:09 +01:00
|
|
|
console.log(`Transaction submitted through relay, tx hash: ${resp2.data.txHash}`)
|
|
|
|
|
|
|
|
let receipt = await waitForTxReceipt(resp2.data.txHash)
|
|
|
|
console.log('Transaction mined in block', receipt.blockNumber)
|
|
|
|
console.log('Done')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for transaction to be mined
|
|
|
|
* @param txHash Hash of transaction
|
|
|
|
* @param attempts
|
|
|
|
* @param delay
|
|
|
|
*/
|
|
|
|
function waitForTxReceipt(txHash, attempts = 60, delay = 1000) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const checkForTx = async (txHash, retryAttempt = 0) => {
|
|
|
|
const result = await web3.eth.getTransactionReceipt(txHash)
|
|
|
|
if (!result || !result.blockNumber) {
|
|
|
|
if (retryAttempt <= attempts) {
|
|
|
|
setTimeout(() => checkForTx(txHash, retryAttempt + 1), delay)
|
|
|
|
} else {
|
|
|
|
reject(new Error('tx was not mined'))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkForTx(txHash)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-05 23:43:06 +02:00
|
|
|
/**
|
|
|
|
* Init web3, contracts, and snark
|
|
|
|
*/
|
2019-07-13 16:52:42 +02:00
|
|
|
async function init() {
|
2019-12-13 14:49:19 +01:00
|
|
|
let contractJson, erc20ContractJson, erc20tornadoJson
|
2019-07-15 18:23:03 +02:00
|
|
|
if (inBrowser) {
|
2019-09-05 23:43:06 +02:00
|
|
|
// Initialize using injected web3 (Metamask)
|
|
|
|
// To assemble web version run `npm run browserify`
|
2019-07-16 22:49:45 +02:00
|
|
|
web3 = new Web3(window.web3.currentProvider, null, { transactionConfirmationBlocks: 1 })
|
2019-12-13 14:49:19 +01:00
|
|
|
contractJson = await (await fetch('build/contracts/ETHTornado.json')).json()
|
2019-07-16 19:20:16 +02:00
|
|
|
circuit = await (await fetch('build/circuits/withdraw.json')).json()
|
|
|
|
proving_key = await (await fetch('build/circuits/withdraw_proving_key.bin')).arrayBuffer()
|
|
|
|
MERKLE_TREE_HEIGHT = 16
|
2019-08-27 22:42:24 +02:00
|
|
|
ETH_AMOUNT = 1e18
|
2019-11-10 01:48:09 +01:00
|
|
|
TOKEN_AMOUNT = 1e19
|
2019-07-15 18:23:03 +02:00
|
|
|
} else {
|
2019-09-05 23:43:06 +02:00
|
|
|
// Initialize from local node
|
2019-07-16 22:49:45 +02:00
|
|
|
web3 = new Web3('http://localhost:8545', null, { transactionConfirmationBlocks: 1 })
|
2019-12-13 14:49:19 +01:00
|
|
|
contractJson = require('./build/contracts/ETHTornado.json')
|
2019-07-16 19:20:16 +02:00
|
|
|
circuit = require('./build/circuits/withdraw.json')
|
|
|
|
proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer
|
|
|
|
require('dotenv').config()
|
|
|
|
MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT
|
2019-08-27 22:42:24 +02:00
|
|
|
ETH_AMOUNT = process.env.ETH_AMOUNT
|
2019-11-10 01:48:09 +01:00
|
|
|
TOKEN_AMOUNT = process.env.TOKEN_AMOUNT
|
2019-09-14 03:05:08 +02:00
|
|
|
ERC20_TOKEN = process.env.ERC20_TOKEN
|
|
|
|
erc20ContractJson = require('./build/contracts/ERC20Mock.json')
|
2019-12-13 14:49:19 +01:00
|
|
|
erc20tornadoJson = require('./build/contracts/ERC20Tornado.json')
|
2019-07-15 18:23:03 +02:00
|
|
|
}
|
2019-07-16 19:20:16 +02:00
|
|
|
groth16 = await buildGroth16()
|
|
|
|
let netId = await web3.eth.net.getId()
|
2019-09-14 20:21:53 +02:00
|
|
|
if (contractJson.networks[netId]) {
|
|
|
|
const tx = await web3.eth.getTransaction(contractJson.networks[netId].transactionHash)
|
2019-12-13 14:49:19 +01:00
|
|
|
tornado = new web3.eth.Contract(contractJson.abi, contractJson.networks[netId].address)
|
|
|
|
tornado.deployedBlock = tx.blockNumber
|
2019-09-14 20:21:53 +02:00
|
|
|
}
|
2019-09-14 03:05:08 +02:00
|
|
|
|
2019-12-13 14:49:19 +01:00
|
|
|
const tx3 = await web3.eth.getTransaction(erc20tornadoJson.networks[netId].transactionHash)
|
|
|
|
erc20tornado = new web3.eth.Contract(erc20tornadoJson.abi, erc20tornadoJson.networks[netId].address)
|
|
|
|
erc20tornado.deployedBlock = tx3.blockNumber
|
2019-09-14 03:05:08 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
senderAccount = (await web3.eth.getAccounts())[0]
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Loaded')
|
2019-07-13 16:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========== CLI related stuff below ==============
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
/** Print command line help */
|
2019-07-13 16:45:08 +02:00
|
|
|
function printHelp(code = 0) {
|
|
|
|
console.log(`Usage:
|
2019-07-15 21:19:34 +02:00
|
|
|
Submit a deposit from default eth account and return the resulting note
|
2019-07-13 13:57:49 +02:00
|
|
|
$ ./cli.js deposit
|
2019-11-10 01:48:09 +01:00
|
|
|
$ ./cli.js depositErc20
|
2019-07-15 21:19:34 +02:00
|
|
|
|
2019-11-07 08:04:29 +01:00
|
|
|
Withdraw a note to 'recipient' account
|
2019-11-10 01:48:09 +01:00
|
|
|
$ ./cli.js withdraw <note> <recipient> [relayUrl]
|
|
|
|
$ ./cli.js withdrawErc20 <note> <recipient> [relayUrl]
|
2019-07-19 19:08:47 +02:00
|
|
|
|
2019-07-17 23:59:04 +02:00
|
|
|
Check address balance
|
|
|
|
$ ./cli.js balance <address>
|
2019-12-13 21:01:38 +01:00
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
Perform an automated test
|
|
|
|
$ ./cli.js test
|
|
|
|
$ ./cli.js testRelay
|
2019-07-15 21:19:34 +02:00
|
|
|
|
2019-07-13 13:57:49 +02:00
|
|
|
Example:
|
|
|
|
$ ./cli.js deposit
|
|
|
|
...
|
|
|
|
Your note: 0x1941fa999e2b4bfeec3ce53c2440c3bc991b1b84c9bb650ea19f8331baf621001e696487e2a2ee54541fa12f49498d71e24d00b1731a8ccd4f5f5126f3d9f400
|
2019-07-15 21:19:34 +02:00
|
|
|
|
2019-07-13 13:57:49 +02:00
|
|
|
$ ./cli.js withdraw 0x1941fa999e2b4bfeec3ce53c2440c3bc991b1b84c9bb650ea19f8331baf621001e696487e2a2ee54541fa12f49498d71e24d00b1731a8ccd4f5f5126f3d9f400 0xee6249BA80596A4890D1BD84dbf5E4322eA4E7f0
|
2019-07-16 19:20:16 +02:00
|
|
|
`)
|
|
|
|
process.exit(code)
|
2019-07-13 13:57:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:48:09 +01:00
|
|
|
/** Process command line args and run */
|
|
|
|
async function runConsole(args) {
|
2019-07-15 18:25:54 +02:00
|
|
|
if (args.length === 0) {
|
2019-07-16 19:20:16 +02:00
|
|
|
printHelp()
|
2019-07-15 18:25:54 +02:00
|
|
|
} else {
|
|
|
|
switch (args[0]) {
|
2019-07-16 19:20:16 +02:00
|
|
|
case 'deposit':
|
|
|
|
if (args.length === 1) {
|
2019-11-10 01:48:09 +01:00
|
|
|
await init()
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(senderAccount, 'Sender account')
|
|
|
|
await deposit()
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(senderAccount, 'Sender account')
|
|
|
|
} else {
|
2019-09-14 03:05:08 +02:00
|
|
|
printHelp(1)
|
2019-11-10 01:48:09 +01:00
|
|
|
}
|
2019-09-14 03:05:08 +02:00
|
|
|
break
|
|
|
|
case 'depositErc20':
|
|
|
|
if (args.length === 1) {
|
2019-11-10 01:48:09 +01:00
|
|
|
await init()
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(erc20tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(senderAccount, 'Sender account')
|
|
|
|
await depositErc20()
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(erc20tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(senderAccount, 'Sender account')
|
|
|
|
} else {
|
2019-07-16 19:20:16 +02:00
|
|
|
printHelp(1)
|
2019-11-10 01:48:09 +01:00
|
|
|
}
|
2019-07-16 19:20:16 +02:00
|
|
|
break
|
|
|
|
case 'balance':
|
|
|
|
if (args.length === 2 && /^0x[0-9a-fA-F]{40}$/.test(args[1])) {
|
2019-11-10 01:48:09 +01:00
|
|
|
await init()
|
|
|
|
await printBalance(args[1])
|
|
|
|
} else {
|
2019-09-14 20:21:53 +02:00
|
|
|
printHelp(1)
|
2019-11-10 01:48:09 +01:00
|
|
|
}
|
2019-09-14 20:21:53 +02:00
|
|
|
break
|
2019-07-16 19:20:16 +02:00
|
|
|
case 'withdraw':
|
2019-11-10 01:48:09 +01:00
|
|
|
if (args.length >= 3 && args.length <= 4 && /^0x[0-9a-fA-F]{124}$/.test(args[1]) && /^0x[0-9a-fA-F]{40}$/.test(args[2])) {
|
|
|
|
await init()
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(args[2], 'Recipient account')
|
|
|
|
if (args[3]) {
|
|
|
|
await withdrawRelay(args[1], args[2], args[3])
|
|
|
|
} else {
|
|
|
|
await withdraw(args[1], args[2])
|
|
|
|
}
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(args[2], 'Recipient account')
|
|
|
|
} else {
|
2019-07-16 19:20:16 +02:00
|
|
|
printHelp(1)
|
2019-11-10 01:48:09 +01:00
|
|
|
}
|
2019-07-16 19:20:16 +02:00
|
|
|
break
|
2019-09-14 20:21:53 +02:00
|
|
|
case 'withdrawErc20':
|
2019-11-10 01:48:09 +01:00
|
|
|
if (args.length >= 3 && args.length <= 4 && /^0x[0-9a-fA-F]{124}$/.test(args[1]) && /^0x[0-9a-fA-F]{40}$/.test(args[2])) {
|
|
|
|
await init()
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(erc20tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(args[2], 'Recipient account')
|
|
|
|
if (args[3]) {
|
|
|
|
await withdrawRelayErc20(args[1], args[2], args[3])
|
|
|
|
} else {
|
|
|
|
await withdrawErc20(args[1], args[2])
|
|
|
|
}
|
2019-12-13 14:49:19 +01:00
|
|
|
await printBalance(erc20tornado._address, 'Tornado')
|
2019-11-10 01:48:09 +01:00
|
|
|
await printBalance(args[2], 'Recipient account')
|
|
|
|
} else {
|
2019-09-14 20:21:53 +02:00
|
|
|
printHelp(1)
|
2019-11-10 01:48:09 +01:00
|
|
|
}
|
2019-09-14 20:21:53 +02:00
|
|
|
break
|
2019-10-04 18:27:19 +02:00
|
|
|
case 'test':
|
2019-08-01 15:46:41 +02:00
|
|
|
if (args.length === 1) {
|
2019-11-10 01:48:09 +01:00
|
|
|
await init()
|
|
|
|
const note1 = await deposit()
|
|
|
|
await withdraw(note1, senderAccount)
|
|
|
|
|
|
|
|
const note2 = await depositErc20()
|
|
|
|
await withdrawErc20(note2, senderAccount)
|
|
|
|
} else {
|
|
|
|
printHelp(1)
|
2019-08-01 15:46:41 +02:00
|
|
|
}
|
2019-11-10 01:48:09 +01:00
|
|
|
break
|
|
|
|
case 'testRelay':
|
|
|
|
if (args.length === 1) {
|
|
|
|
await init()
|
|
|
|
const note1 = await deposit()
|
|
|
|
await withdrawRelay(note1, senderAccount, 'http://localhost:8000')
|
|
|
|
|
|
|
|
const note2 = await depositErc20()
|
|
|
|
await withdrawRelayErc20(note2, senderAccount, 'http://localhost:8000')
|
|
|
|
} else {
|
2019-08-01 15:46:41 +02:00
|
|
|
printHelp(1)
|
2019-11-10 01:48:09 +01:00
|
|
|
}
|
2019-08-01 15:46:41 +02:00
|
|
|
break
|
2019-07-15 18:25:54 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
default:
|
|
|
|
printHelp(1)
|
2019-07-15 18:25:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-10 01:48:09 +01:00
|
|
|
|
|
|
|
if (inBrowser) {
|
|
|
|
window.deposit = deposit
|
|
|
|
window.depositErc20 = depositErc20
|
|
|
|
window.withdraw = async () => {
|
|
|
|
const note = prompt('Enter the note to withdraw')
|
|
|
|
const recipient = (await web3.eth.getAccounts())[0]
|
|
|
|
await withdraw(note, recipient)
|
|
|
|
}
|
|
|
|
init()
|
|
|
|
} else {
|
|
|
|
runConsole(process.argv.slice(2))
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => { console.log(err); process.exit(1) })
|
|
|
|
}
|