depreciate browser support

This commit is contained in:
gozzy 2023-03-23 03:47:37 +00:00
parent f3ff636840
commit 4b4364e2fe

47
cli.js
View File

@ -1,5 +1,4 @@
#!/usr/bin/env node #!/usr/bin/env node
// Works both in browser and node.js
require('dotenv').config(); require('dotenv').config();
const fs = require('fs'); const fs = require('fs');
@ -25,8 +24,6 @@ const is_ip_private = require('private-ip');
let web3, torPort, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol, doNotSubmitTx, multiCall, privateRpc, subgraph; let web3, torPort, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol, doNotSubmitTx, multiCall, privateRpc, subgraph;
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY; let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY;
/** Whether we are in a browser or node.js */
const inBrowser = typeof window !== 'undefined';
let isTestRPC = false; let isTestRPC = false;
/** Generate random number of specified byte length */ /** Generate random number of specified byte length */
@ -1186,23 +1183,8 @@ async function loadWithdrawalData({ amount, currency, deposit }) {
*/ */
async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceCheck, localMode }) { async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceCheck, localMode }) {
let contractJson, instanceJson, erc20ContractJson, erc20tornadoJson, tornadoAddress, tokenAddress; let contractJson, instanceJson, erc20ContractJson, erc20tornadoJson, tornadoAddress, tokenAddress;
// TODO do we need this? should it work in browser really?
if (inBrowser) {
// Initialize using injected web3 (Metamask)
// To assemble web version run `npm run browserify`
web3 = new Web3(window.web3.currentProvider, null, {
transactionConfirmationBlocks: 1
});
contractJson = await (await fetch('build/contracts/TornadoProxy.abi.json')).json();
instanceJson = await (await fetch('build/contracts/Instance.abi.json')).json();
circuit = await (await fetch('build/circuits/tornado.json')).json();
proving_key = await (await fetch('build/circuits/tornadoProvingKey.bin')).arrayBuffer();
MERKLE_TREE_HEIGHT = 20;
ETH_AMOUNT = 1e18;
TOKEN_AMOUNT = 1e19;
senderAccount = (await web3.eth.getAccounts())[0];
} else {
let ipOptions = {}; let ipOptions = {};
if (torPort && rpc.includes("https")) { if (torPort && rpc.includes("https")) {
console.log("Using tor network"); console.log("Using tor network");
web3Options = { agent: { https: new SocksProxyAgent('socks5h://127.0.0.1:' + torPort) }, timeout: 60000 }; web3Options = { agent: { https: new SocksProxyAgent('socks5h://127.0.0.1:' + torPort) }, timeout: 60000 };
@ -1226,8 +1208,10 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
console.log("Connecting to remote node"); console.log("Connecting to remote node");
web3 = new Web3(rpc, null, { transactionConfirmationBlocks: 1 }); web3 = new Web3(rpc, null, { transactionConfirmationBlocks: 1 });
} }
const rpcHost = new URL(rpc).hostname; const rpcHost = new URL(rpc).hostname;
const isIpPrivate = is_ip_private(rpcHost); const isIpPrivate = is_ip_private(rpcHost);
if (!isIpPrivate && !rpc.includes("localhost") && !privateRpc) { if (!isIpPrivate && !rpc.includes("localhost") && !privateRpc) {
try { try {
const fetchRemoteIP = await axios.get('https://ip.tornado.cash', ipOptions); const fetchRemoteIP = await axios.get('https://ip.tornado.cash', ipOptions);
@ -1240,6 +1224,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
console.log('Local RPC detected'); console.log('Local RPC detected');
privateRpc = true; privateRpc = true;
} }
contractJson = require('./build/contracts/TornadoProxy.abi.json'); contractJson = require('./build/contracts/TornadoProxy.abi.json');
instanceJson = require('./build/contracts/Instance.abi.json'); instanceJson = require('./build/contracts/Instance.abi.json');
circuit = require('./build/circuits/tornado.json'); circuit = require('./build/circuits/tornado.json');
@ -1248,27 +1233,28 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
ETH_AMOUNT = process.env.ETH_AMOUNT; ETH_AMOUNT = process.env.ETH_AMOUNT;
TOKEN_AMOUNT = process.env.TOKEN_AMOUNT; TOKEN_AMOUNT = process.env.TOKEN_AMOUNT;
const privKey = process.env.PRIVATE_KEY; const privKey = process.env.PRIVATE_KEY;
if (privKey) { if (privKey) {
if (privKey.includes("0x")) { if (privKey.includes("0x")) {
PRIVATE_KEY = process.env.PRIVATE_KEY.substring(2); PRIVATE_KEY = process.env.PRIVATE_KEY.substring(2);
} else { } else {
PRIVATE_KEY = process.env.PRIVATE_KEY; PRIVATE_KEY = process.env.PRIVATE_KEY;
} }
} } if (PRIVATE_KEY) {
if (PRIVATE_KEY) {
const account = web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY); const account = web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY);
web3.eth.accounts.wallet.add('0x' + PRIVATE_KEY); web3.eth.accounts.wallet.add('0x' + PRIVATE_KEY);
web3.eth.defaultAccount = account.address; web3.eth.defaultAccount = account.address;
senderAccount = account.address; senderAccount = account.address;
} }
erc20ContractJson = require('./build/contracts/ERC20Mock.json'); erc20ContractJson = require('./build/contracts/ERC20Mock.json');
erc20tornadoJson = require('./build/contracts/ERC20Tornado.json'); erc20tornadoJson = require('./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 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(); groth16 = await buildGroth16();
netId = await web3.eth.net.getId(); netId = await web3.eth.net.getId();
netName = getCurrentNetworkName(); netName = getCurrentNetworkName();
netSymbol = getCurrentNetworkSymbol(); netSymbol = getCurrentNetworkSymbol();
if (noteNetId && Number(noteNetId) !== netId) { if (noteNetId && Number(noteNetId) !== netId) {
throw new Error('This note is for a different network. Specify the --rpc option explicitly'); throw new Error('This note is for a different network. Specify the --rpc option explicitly');
} }
@ -1314,21 +1300,6 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', balanceC
} }
async function main() { async function main() {
if (inBrowser) {
const instance = { currency: 'eth', amount: '0.1' };
await init(instance);
window.deposit = async () => {
await deposit(instance);
}
window.withdraw = async () => {
const noteString = prompt('Enter the note to withdraw');
const recipient = (await web3.eth.getAccounts())[0];
const { currency, amount, netId, deposit } = parseNote(noteString);
await init({ noteNetId: netId, currency, amount });
await withdraw({ deposit, currency, amount, recipient });
}
} else {
program program
.option('-r, --rpc <URL>', 'The RPC that CLI should interact with', 'http://localhost:8545') .option('-r, --rpc <URL>', 'The RPC that CLI should interact with', 'http://localhost:8545')
.option('-R, --relayer <URL>', 'Withdraw via relayer') .option('-R, --relayer <URL>', 'Withdraw via relayer')
@ -1460,7 +1431,6 @@ async function main() {
return; return;
} }
console.log('=====================================', '\n'); console.log('=====================================', '\n');
const withdrawInfo = await loadWithdrawalData({ amount, currency, deposit }); const withdrawInfo = await loadWithdrawalData({ amount, currency, deposit });
const withdrawalDate = new Date(withdrawInfo.timestamp * 1000); const withdrawalDate = new Date(withdrawInfo.timestamp * 1000);
console.log('\n=============Withdrawal=============='); console.log('\n=============Withdrawal==============');
@ -1545,6 +1515,5 @@ async function main() {
process.exit(1); process.exit(1);
} }
} }
}
main(); main();