2019-07-13 07:57:49 -04:00
|
|
|
#!/usr/bin/env node
|
2019-07-17 07:12:57 -04:00
|
|
|
// Temporary demo client
|
2019-07-18 14:41:18 -04:00
|
|
|
// Works both in browser and node.js
|
2019-07-16 13:20:16 -04:00
|
|
|
const fs = require('fs')
|
|
|
|
const assert = require('assert')
|
|
|
|
const snarkjs = require('snarkjs')
|
2019-07-18 14:27:51 -04:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const circomlib = require('circomlib')
|
2019-07-16 13:20:16 -04: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-09-13 21:05:08 -04:00
|
|
|
let web3, mixer, erc20mixer, circuit, proving_key, groth16, erc20
|
2019-11-02 08:35:22 -04:00
|
|
|
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, ERC20_TOKEN
|
2019-07-16 13:20:16 -04:00
|
|
|
const inBrowser = (typeof window !== 'undefined')
|
2019-07-13 10:45:08 -04:00
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
/** Generate random number of specified byte length */
|
2019-07-18 14:27:51 -04:00
|
|
|
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
2019-09-05 17:43:06 -04:00
|
|
|
|
|
|
|
/** Compute pedersen hash */
|
2019-07-18 14:27:51 -04:00
|
|
|
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
/**
|
|
|
|
* Create deposit object from secret and nullifier
|
|
|
|
*/
|
2019-07-13 10:45:08 -04:00
|
|
|
function createDeposit(nullifier, secret) {
|
2019-07-16 13:20:16 -04:00
|
|
|
let deposit = { nullifier, secret }
|
2019-08-01 10:49:34 -04:00
|
|
|
deposit.preimage = Buffer.concat([deposit.nullifier.leInt2Buff(31), deposit.secret.leInt2Buff(31)])
|
2019-07-18 14:27:51 -04:00
|
|
|
deposit.commitment = pedersenHash(deposit.preimage)
|
2019-07-16 13:20:16 -04:00
|
|
|
return deposit
|
2019-07-13 10:45:08 -04:00
|
|
|
}
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
/**
|
|
|
|
* Make a deposit
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
2019-07-13 07:57:49 -04:00
|
|
|
async function deposit() {
|
2019-07-18 14:27:51 -04:00
|
|
|
const deposit = createDeposit(rbigint(31), rbigint(31))
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-07-16 13:20:16 -04:00
|
|
|
console.log('Submitting deposit transaction')
|
2019-08-27 16:42:24 -04:00
|
|
|
await mixer.methods.deposit('0x' + deposit.commitment.toString(16)).send({ value: ETH_AMOUNT, from: (await web3.eth.getAccounts())[0], gas:1e6 })
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-07-16 13:20:16 -04:00
|
|
|
const note = '0x' + deposit.preimage.toString('hex')
|
|
|
|
console.log('Your note:', note)
|
|
|
|
return note
|
2019-07-13 07:57:49 -04:00
|
|
|
}
|
|
|
|
|
2019-09-13 21:05:08 -04:00
|
|
|
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 })
|
|
|
|
|
|
|
|
await erc20.methods.approve(erc20mixer.address, tokenAmount).send({ from: account, gas:1e6 })
|
2019-09-14 14:21:53 -04:00
|
|
|
const allowance = await erc20.methods.allowance(account, erc20mixer.address).call()
|
|
|
|
console.log('erc20mixer allowance', allowance.toString(10))
|
2019-09-13 21:05:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
async function withdrawErc20(note, recipient, relayer) {
|
2019-09-13 21:05:08 -04:00
|
|
|
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
|
|
|
|
})
|
2019-11-02 08:35:22 -04:00
|
|
|
const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
|
2019-09-13 21:05:08 -04:00
|
|
|
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')
|
2019-09-14 14:21:53 -04:00
|
|
|
const isSpent = await erc20mixer.methods.isSpent(nullifierHashToCheck).call()
|
2019-09-13 21:05:08 -04:00
|
|
|
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,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient: bigInt(recipient),
|
2019-09-14 14:21:53 -04:00
|
|
|
relayer: bigInt(relayer),
|
|
|
|
fee: bigInt(web3.utils.toWei('0.01')),
|
2019-10-06 01:09:26 -04:00
|
|
|
refund: bigInt(0),
|
2019-09-13 21:05:08 -04:00
|
|
|
|
|
|
|
// private
|
|
|
|
nullifier: deposit.nullifier,
|
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-09-13 21:05:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Generating SNARK proof')
|
|
|
|
console.time('Proof time')
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
2019-09-13 21:05:08 -04:00
|
|
|
console.timeEnd('Proof time')
|
|
|
|
|
|
|
|
console.log('Submitting withdraw transaction')
|
2019-11-04 14:42:41 -05:00
|
|
|
const args = [
|
|
|
|
toHex(input.root),
|
|
|
|
toHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toHex(input.relayer, 20),
|
|
|
|
toHex(input.fee),
|
|
|
|
toHex(input.refund)
|
|
|
|
]
|
|
|
|
await erc20mixer.methods.withdraw(proof, ...args).send({ from: (await web3.eth.getAccounts())[0], gas: 1e6 })
|
2019-09-13 21:05:08 -04:00
|
|
|
console.log('Done')
|
|
|
|
}
|
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
async function getBalance(recipient) {
|
|
|
|
const balance = await web3.eth.getBalance(recipient)
|
2019-07-17 17:59:04 -04:00
|
|
|
console.log('Balance is ', web3.utils.fromWei(balance))
|
2019-07-15 15:19:34 -04:00
|
|
|
}
|
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
async function getBalanceErc20(recipient, relayer) {
|
|
|
|
const balanceRecipient = await web3.eth.getBalance(recipient)
|
2019-09-14 14:21:53 -04:00
|
|
|
const balanceRelayer = await web3.eth.getBalance(relayer)
|
2019-11-07 02:04:29 -05:00
|
|
|
const tokenBalanceRecipient = await erc20.methods.balanceOf(recipient).call()
|
2019-09-14 14:21:53 -04:00
|
|
|
const tokenBalanceRelayer = await erc20.methods.balanceOf(relayer).call()
|
2019-11-07 02:04:29 -05:00
|
|
|
console.log('Recipient eth Balance is ', web3.utils.fromWei(balanceRecipient))
|
2019-09-14 14:21:53 -04:00
|
|
|
console.log('Relayer eth Balance is ', web3.utils.fromWei(balanceRelayer))
|
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
console.log('Recipient token Balance is ', web3.utils.fromWei(tokenBalanceRecipient.toString()))
|
2019-09-14 14:21:53 -04:00
|
|
|
console.log('Relayer token Balance is ', web3.utils.fromWei(tokenBalanceRelayer.toString()))
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:42:41 -05:00
|
|
|
function toHex(number, length = 32) {
|
|
|
|
let str = bigInt(number).toString(16)
|
|
|
|
while (str.length < length * 2) str = '0' + str
|
|
|
|
str = '0x' + str
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
async function withdraw(note, recipient) {
|
2019-09-05 17:43:06 -04:00
|
|
|
// Decode hex string and restore the deposit object
|
2019-07-16 13:20:16 -04:00
|
|
|
let buf = Buffer.from(note.slice(2), 'hex')
|
2019-08-06 01:20:45 -04:00
|
|
|
let deposit = createDeposit(bigInt.leBuff2int(buf.slice(0, 31)), bigInt.leBuff2int(buf.slice(31, 62)))
|
2019-09-05 17:43:06 -04:00
|
|
|
const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(31))
|
|
|
|
const paddedNullifierHash = nullifierHash.toString(16).padStart('66', '0x000000')
|
|
|
|
const paddedCommitment = deposit.commitment.toString(16).padStart('66', '0x000000')
|
2019-07-16 13:20:16 -04:00
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
// Get all deposit events from smart contract and assemble merkle tree from them
|
2019-07-16 13:20:16 -04:00
|
|
|
console.log('Getting current state from mixer contract')
|
2019-07-19 13:08:47 -04:00
|
|
|
const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' })
|
2019-07-24 12:57:51 -04:00
|
|
|
const leaves = events
|
2019-11-03 05:11:22 -05:00
|
|
|
.sort((a, b) => a.returnValues.leafIndex - b.returnValues.leafIndex) // Sort events in chronological order
|
2019-09-05 17:43:06 -04:00
|
|
|
.map(e => e.returnValues.commitment)
|
2019-11-02 08:35:22 -04:00
|
|
|
const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves)
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
// Find current commitment in the tree
|
2019-11-04 16:04:22 -05:00
|
|
|
let depositEvent = events.find(e => e.returnValues.commitment === paddedCommitment)
|
2019-11-03 05:11:22 -05:00
|
|
|
let leafIndex = depositEvent ? depositEvent.returnValues.leafIndex : -1
|
2019-09-05 17:43:06 -04:00
|
|
|
|
|
|
|
// Validate that our data is correct
|
2019-11-04 16:04:22 -05:00
|
|
|
const isValidRoot = await mixer.methods.isKnownRoot(toHex(await tree.root())).call()
|
2019-09-05 17:43:06 -04:00
|
|
|
const isSpent = await mixer.methods.isSpent(paddedNullifierHash).call()
|
|
|
|
assert(isValidRoot === true) // Merkle tree assembled correctly
|
|
|
|
assert(isSpent === false) // The note is not spent
|
|
|
|
assert(leafIndex >= 0) // Our deposit is present in the tree
|
|
|
|
|
|
|
|
// Compute merkle proof of our commitment
|
2019-07-16 13:20:16 -04:00
|
|
|
const { root, path_elements, path_index } = await tree.path(leafIndex)
|
2019-09-05 17:43:06 -04:00
|
|
|
|
|
|
|
// Prepare circuit input
|
2019-07-13 07:57:49 -04:00
|
|
|
const input = {
|
2019-09-05 17:43:06 -04:00
|
|
|
// Public snark inputs
|
2019-07-13 07:57:49 -04:00
|
|
|
root: root,
|
2019-07-25 08:16:09 -04:00
|
|
|
nullifierHash,
|
2019-11-07 02:04:29 -05:00
|
|
|
recipient: bigInt(recipient),
|
2019-09-06 16:54:37 -04:00
|
|
|
relayer: bigInt(0),
|
2019-07-13 07:57:49 -04:00
|
|
|
fee: bigInt(0),
|
2019-10-06 01:09:26 -04:00
|
|
|
refund: bigInt(0),
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
// Private snark inputs
|
2019-07-19 12:37:38 -04:00
|
|
|
nullifier: deposit.nullifier,
|
2019-07-13 07:57:49 -04:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
2019-11-01 22:05:25 -04:00
|
|
|
pathIndices: path_index,
|
2019-07-16 13:20:16 -04:00
|
|
|
}
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-07-16 13:20:16 -04:00
|
|
|
console.log('Generating SNARK proof')
|
2019-07-17 07:12:57 -04:00
|
|
|
console.time('Proof time')
|
2019-10-04 11:20:20 -04:00
|
|
|
const proofData = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
2019-11-04 14:42:41 -05:00
|
|
|
const { proof } = websnarkUtils.toSolidityInput(proofData)
|
2019-07-17 07:12:57 -04:00
|
|
|
console.timeEnd('Proof time')
|
2019-07-13 07:57:49 -04:00
|
|
|
|
2019-07-16 13:20:16 -04:00
|
|
|
console.log('Submitting withdraw transaction')
|
2019-11-04 14:42:41 -05:00
|
|
|
const args = [
|
|
|
|
toHex(input.root),
|
|
|
|
toHex(input.nullifierHash),
|
2019-11-07 02:04:29 -05:00
|
|
|
toHex(input.recipient, 20),
|
2019-11-04 14:42:41 -05:00
|
|
|
toHex(input.relayer, 20),
|
|
|
|
toHex(input.fee),
|
|
|
|
toHex(input.refund)
|
|
|
|
]
|
|
|
|
await mixer.methods.withdraw(proof, ...args).send({ from: (await web3.eth.getAccounts())[0], gas: 1e6 })
|
2019-07-16 13:20:16 -04:00
|
|
|
console.log('Done')
|
2019-07-13 07:57:49 -04:00
|
|
|
}
|
|
|
|
|
2019-09-05 17:43:06 -04:00
|
|
|
/**
|
|
|
|
* Init web3, contracts, and snark
|
|
|
|
*/
|
2019-07-13 10:52:42 -04:00
|
|
|
async function init() {
|
2019-09-13 21:05:08 -04:00
|
|
|
let contractJson, erc20ContractJson, erc20mixerJson
|
2019-07-15 12:23:03 -04:00
|
|
|
if (inBrowser) {
|
2019-09-05 17:43:06 -04:00
|
|
|
// Initialize using injected web3 (Metamask)
|
|
|
|
// To assemble web version run `npm run browserify`
|
2019-07-16 16:49:45 -04:00
|
|
|
web3 = new Web3(window.web3.currentProvider, null, { transactionConfirmationBlocks: 1 })
|
2019-09-02 10:56:10 -04:00
|
|
|
contractJson = await (await fetch('build/contracts/ETHMixer.json')).json()
|
2019-07-16 13:20:16 -04: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 16:42:24 -04:00
|
|
|
ETH_AMOUNT = 1e18
|
2019-07-15 12:23:03 -04:00
|
|
|
} else {
|
2019-09-05 17:43:06 -04:00
|
|
|
// Initialize from local node
|
2019-07-16 16:49:45 -04:00
|
|
|
web3 = new Web3('http://localhost:8545', null, { transactionConfirmationBlocks: 1 })
|
2019-09-02 10:56:10 -04:00
|
|
|
contractJson = require('./build/contracts/ETHMixer.json')
|
2019-07-16 13:20:16 -04: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 16:42:24 -04:00
|
|
|
ETH_AMOUNT = process.env.ETH_AMOUNT
|
2019-09-13 21:05:08 -04:00
|
|
|
ERC20_TOKEN = process.env.ERC20_TOKEN
|
|
|
|
erc20ContractJson = require('./build/contracts/ERC20Mock.json')
|
|
|
|
erc20mixerJson = require('./build/contracts/ERC20Mixer.json')
|
2019-07-15 12:23:03 -04:00
|
|
|
}
|
2019-07-16 13:20:16 -04:00
|
|
|
groth16 = await buildGroth16()
|
|
|
|
let netId = await web3.eth.net.getId()
|
2019-09-14 14:21:53 -04:00
|
|
|
if (contractJson.networks[netId]) {
|
|
|
|
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
|
|
|
|
}
|
2019-09-13 21:05:08 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-07-16 13:20:16 -04:00
|
|
|
console.log('Loaded')
|
2019-07-13 10:52:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========== CLI related stuff below ==============
|
|
|
|
|
2019-07-13 10:45:08 -04:00
|
|
|
function printHelp(code = 0) {
|
|
|
|
console.log(`Usage:
|
2019-07-15 15:19:34 -04:00
|
|
|
Submit a deposit from default eth account and return the resulting note
|
2019-07-13 07:57:49 -04:00
|
|
|
$ ./cli.js deposit
|
2019-07-15 15:19:34 -04:00
|
|
|
|
2019-11-07 02:04:29 -05:00
|
|
|
Withdraw a note to 'recipient' account
|
|
|
|
$ ./cli.js withdraw <note> <recipient>
|
2019-07-19 13:08:47 -04:00
|
|
|
|
2019-07-17 17:59:04 -04:00
|
|
|
Check address balance
|
|
|
|
$ ./cli.js balance <address>
|
2019-07-15 15:19:34 -04:00
|
|
|
|
2019-07-13 07:57:49 -04:00
|
|
|
Example:
|
|
|
|
$ ./cli.js deposit
|
|
|
|
...
|
|
|
|
Your note: 0x1941fa999e2b4bfeec3ce53c2440c3bc991b1b84c9bb650ea19f8331baf621001e696487e2a2ee54541fa12f49498d71e24d00b1731a8ccd4f5f5126f3d9f400
|
2019-07-15 15:19:34 -04:00
|
|
|
|
2019-07-13 07:57:49 -04:00
|
|
|
$ ./cli.js withdraw 0x1941fa999e2b4bfeec3ce53c2440c3bc991b1b84c9bb650ea19f8331baf621001e696487e2a2ee54541fa12f49498d71e24d00b1731a8ccd4f5f5126f3d9f400 0xee6249BA80596A4890D1BD84dbf5E4322eA4E7f0
|
2019-07-16 13:20:16 -04:00
|
|
|
`)
|
|
|
|
process.exit(code)
|
2019-07-13 07:57:49 -04:00
|
|
|
}
|
|
|
|
|
2019-07-15 12:25:54 -04:00
|
|
|
if (inBrowser) {
|
2019-07-16 13:20:16 -04:00
|
|
|
window.deposit = deposit
|
2019-07-15 12:25:54 -04:00
|
|
|
window.withdraw = async () => {
|
2019-07-16 13:20:16 -04:00
|
|
|
const note = prompt('Enter the note to withdraw')
|
2019-11-07 02:04:29 -05:00
|
|
|
const recipient = (await web3.eth.getAccounts())[0]
|
|
|
|
await withdraw(note, recipient)
|
2019-07-16 13:20:16 -04:00
|
|
|
}
|
|
|
|
init()
|
2019-07-15 12:25:54 -04:00
|
|
|
} else {
|
2019-07-16 13:20:16 -04:00
|
|
|
const args = process.argv.slice(2)
|
2019-07-15 12:25:54 -04:00
|
|
|
if (args.length === 0) {
|
2019-07-16 13:20:16 -04:00
|
|
|
printHelp()
|
2019-07-15 12:25:54 -04:00
|
|
|
} else {
|
|
|
|
switch (args[0]) {
|
2019-07-16 13:20:16 -04:00
|
|
|
case 'deposit':
|
|
|
|
if (args.length === 1) {
|
2019-07-16 16:49:45 -04:00
|
|
|
init().then(() => deposit()).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
2019-09-13 21:05:08 -04:00
|
|
|
}
|
|
|
|
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)})
|
2019-07-16 13:20:16 -04:00
|
|
|
}
|
2019-07-15 12:25:54 -04:00
|
|
|
else
|
2019-07-16 13:20:16 -04:00
|
|
|
printHelp(1)
|
|
|
|
break
|
|
|
|
case 'balance':
|
|
|
|
if (args.length === 2 && /^0x[0-9a-fA-F]{40}$/.test(args[1])) {
|
2019-07-16 16:49:45 -04:00
|
|
|
init().then(() => getBalance(args[1])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
2019-07-17 17:59:04 -04:00
|
|
|
} else
|
|
|
|
printHelp(1)
|
2019-07-16 13:20:16 -04:00
|
|
|
break
|
2019-09-14 14:21:53 -04:00
|
|
|
case 'balanceErc20':
|
|
|
|
if (args.length === 3 && /^0x[0-9a-fA-F]{40}$/.test(args[1]) && /^0x[0-9a-fA-F]{40}$/.test(args[2])) {
|
|
|
|
init().then(() => getBalanceErc20(args[1], args[2])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
|
|
|
} else
|
|
|
|
printHelp(1)
|
|
|
|
break
|
2019-07-16 13:20:16 -04:00
|
|
|
case 'withdraw':
|
2019-08-06 01:20:45 -04:00
|
|
|
if (args.length === 3 && /^0x[0-9a-fA-F]{124}$/.test(args[1]) && /^0x[0-9a-fA-F]{40}$/.test(args[2])) {
|
2019-07-16 16:49:45 -04:00
|
|
|
init().then(() => withdraw(args[1], args[2])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
2019-07-16 13:20:16 -04:00
|
|
|
}
|
2019-07-15 12:25:54 -04:00
|
|
|
else
|
2019-07-16 13:20:16 -04:00
|
|
|
printHelp(1)
|
|
|
|
break
|
2019-09-14 14:21:53 -04:00
|
|
|
case 'withdrawErc20':
|
|
|
|
if (args.length === 4 && /^0x[0-9a-fA-F]{124}$/.test(args[1]) && /^0x[0-9a-fA-F]{40}$/.test(args[2]) && /^0x[0-9a-fA-F]{40}$/.test(args[3])) {
|
|
|
|
init().then(() => withdrawErc20(args[1], args[2], args[3])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printHelp(1)
|
|
|
|
break
|
2019-10-04 12:27:19 -04:00
|
|
|
case 'test':
|
2019-08-01 09:46:41 -04:00
|
|
|
if (args.length === 1) {
|
|
|
|
(async () => {
|
|
|
|
await init()
|
2019-10-04 12:27:19 -04:00
|
|
|
const account = (await web3.eth.getAccounts())[0]
|
2019-08-01 09:46:41 -04:00
|
|
|
const note = await deposit()
|
2019-10-04 12:27:19 -04:00
|
|
|
await withdraw(note, account)
|
|
|
|
|
|
|
|
const note2 = await deposit()
|
|
|
|
await withdraw(note2, account, account)
|
2019-08-01 09:46:41 -04:00
|
|
|
process.exit(0)
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printHelp(1)
|
|
|
|
break
|
2019-07-15 12:25:54 -04:00
|
|
|
|
2019-07-16 13:20:16 -04:00
|
|
|
default:
|
|
|
|
printHelp(1)
|
2019-07-15 12:25:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|