From 910643ab5ef5ff457661c7d4c83eceb7e2accee6 Mon Sep 17 00:00:00 2001 From: Theo Date: Wed, 10 May 2023 18:49:59 +0300 Subject: [PATCH] Add --private-key command argument so that users shouldn't have to save the private key in the clear in .env file --- README.md | 18 ++++++++---------- cli.js | 28 ++++++++++++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 451f793..bb7da04 100644 --- a/README.md +++ b/README.md @@ -38,17 +38,17 @@ If you want to use Tor connection to conceal ip address, install [Tor Browser](h Note that you should reset your tor connection by restarting the browser every time when you deposit & withdraw otherwise you will have the same exit node used for connection. ### Goerli, Mainnet, Binance Smart Chain, Gnosis Chain, Polygon Network, Arbitrum, Avalanche -1. Add `PRIVATE_KEY` to `.env` file -2. `node cli.js --help` -3. If you want to use secure, anonymous tor connection add `--tor ` behind the command. +1. `node cli.js --help` +2. If you want to use secure, anonymous tor connection add `--tor ` behind the command. +3. Add `PRIVATE_KEY` to `.env` file (optional, only if you want to use it for many operations) - open `.env.example` file, add private key after `PRIVATE_KEY=` and rename file to `.env`. #### To deposit: ```bash -$ node cli.js deposit --rpc --tor +$ node cli.js deposit --rpc --tor --private-key ``` -Note that `--tor ` is optional. +Note that `--tor ` is optional, and use `--private-key ` only if you didn't add it to `.env` file. For RPC nodes please refer to the list of public RPC nodes below. @@ -67,16 +67,14 @@ Sender account ETH balance is 1004873.361652048361352542 #### To withdraw: ```bash -$ node cli.js withdraw --rpc --relayer --tor +$ node cli.js withdraw --rpc --relayer --tor --private-key ``` -Note that `--relayer `, `--tor ` is optional. +Note that `--relayer `, `--tor ` is optional, and use `--private-key ` only if you withdraw without relayer. If you want to use Tornado Cash relayer for your first withdrawal to your new ethereum account, please refer to the list of relayers below. -If you don't need relayer while doing withdrawals, you must apply your withdrawal account's private key to `.env` file. - -Copy the `PRIVATE_KEY=` line of `.env.example` to `.env`, and add your private key behind the `=`. +If you don't need relayer while doing withdrawals, you must provide your withdrawal account's private key - either as parameter, or by adding it to `.env` file. ##### Example: diff --git a/cli.js b/cli.js index 0529193..d888151 100755 --- a/cli.js +++ b/cli.js @@ -1368,7 +1368,7 @@ async function promptConfirmation() { /** * Init web3, contracts, and snark */ -async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceCheck, localMode }) { +async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceCheck, localMode, privateKey }) { let contractJson, instanceJson, erc20ContractJson, erc20tornadoJson, tornadoAddress, tokenAddress; let ipOptions = {}; @@ -1428,13 +1428,13 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT || 20; ETH_AMOUNT = process.env.ETH_AMOUNT; TOKEN_AMOUNT = process.env.TOKEN_AMOUNT; - const privKey = process.env.PRIVATE_KEY; + const privKey = privateKey || process.env.PRIVATE_KEY; if (privKey) { - if (privKey.includes('0x')) { - PRIVATE_KEY = process.env.PRIVATE_KEY.substring(2); + if (privKey.startsWith('0x')) { + PRIVATE_KEY = privKey.substring(2); } else { - PRIVATE_KEY = process.env.PRIVATE_KEY; + PRIVATE_KEY = privKey; } } if (PRIVATE_KEY) { @@ -1502,6 +1502,7 @@ async function main() { .option('-r, --rpc ', 'The RPC that CLI should interact with', 'http://localhost:8545') .option('-R, --relayer ', 'Withdraw via relayer') .option('-T, --tor ', 'Optional tor port') + .option('-p, --private-key ', "Wallet private key - If you didn't add it to .env file and it is needed for operation") .option('-S --gas_speed ', 'Gas speed preference [ instant, fast, standard, low ]') .option('-N --noconfirmation', 'No confirmation mode - Does not query confirmation ') .option('-L, --local-rpc', 'Local node mode - Does not submit signed transaction to the node') @@ -1522,7 +1523,7 @@ async function main() { statePreferences(program); const { currency, amount, netId, commitmentNote } = parseInvoice(invoice); - await init({ rpc: program.rpc, currency, amount, localMode: program.local }); + await init({ rpc: program.rpc, currency, amount, localMode: program.local, privateKey: program.privateKey }); console.log('Creating', currency.toUpperCase(), amount, 'deposit for', netName, 'Tornado Cash Instance'); await deposit({ currency, amount, commitmentNote }); }); @@ -1536,7 +1537,7 @@ async function main() { statePreferences(program); - await init({ rpc: program.rpc, currency, amount, localMode: program.local }); + await init({ rpc: program.rpc, currency, amount, localMode: program.local, privateKey: program.privateKey }); await deposit({ currency, amount }); }); program @@ -1549,7 +1550,14 @@ async function main() { const { currency, amount, netId, deposit } = parseNote(noteString); - await init({ rpc: program.rpc, noteNetId: netId, currency, amount, localMode: program.local }); + await init({ + rpc: program.rpc, + noteNetId: netId, + currency, + amount, + localMode: program.local, + privateKey: program.privateKey + }); await withdraw({ deposit, currency, @@ -1581,7 +1589,7 @@ async function main() { .action(async (address, amount, tokenAddress) => { statePreferences(program); - await init({ rpc: program.rpc, balanceCheck: true, localMode: program.local }); + await init({ rpc: program.rpc, balanceCheck: true, localMode: program.local, privateKey: program.privateKey }); await send({ address, amount, tokenAddress }); }); program @@ -1693,7 +1701,7 @@ async function main() { console.log('Start performing ETH deposit-withdraw test'); let currency = 'eth'; let amount = '0.1'; - await init({ rpc: program.rpc, currency, amount }); + await init({ rpc: program.rpc, currency, amount, privateKey: program.privateKey }); let noteString = await deposit({ currency, amount }); let parsedNote = parseNote(noteString); await withdraw({