import chai from 'chai' import { TorHttpClient, RegularHttpClient, TorProvider, Relayer } from '@tornado/sdk-web' // Waffle matchers import { solidity } from 'ethereum-waffle' import { ErrorUtils } from '@tornado/sdk-utils' import { parseUnits } from 'ethers/lib/utils' import { Web3Provider } from '@ethersproject/providers' chai.use(solidity) const expect = chai.expect describe('web', () => { const torify = process.env.TORIFY === 'true' if (!process.env.ETH_MAINNET_TEST_RPC || !process.env.TOR_PORT) throw ErrorUtils.getError('need a tor port and mainnet rpc endpoint.') let torProvider: Web3Provider if (torify) torProvider = new TorProvider(process.env.ETH_MAINNET_TEST_RPC, { port: +process.env.TOR_PORT }) const httpClient = torify ? new TorHttpClient({ port: +process.env.TOR_PORT }) : new RegularHttpClient() if (torify) console.log( '\nSome Tor tips: Support non-profit exit node operators, host your own nodes, avoid spy nodes by configuring torrc.\n' ) function torErrorThrow(err: Error) { err.message = "\n\nThis test most likely failed because you (Tor) didn't open a SOCKS5 tunnel at either 9050 or the Tor port you specified in .env. As such, the provider couldn't send a request. Please start Tor or Tor Browser. šŸ§…\n\n" throw err } it('Relayer: should be able to initialize a Relayer and fetch properties', async () => { if (!process.env.TEST_RELAYER_DOMAIN) throw ErrorUtils.getError('web.test.ts: this test requires a relayer domain name.') const relayer = new Relayer({ url: 'https://' + process.env.TEST_RELAYER_DOMAIN, httpClient: httpClient }) console.log(await relayer.fetchProperties()) }).timeout(0) it('httpClient: Should be able to send requests over Tor', async function () { try { const check = (await httpClient.get('https://check.torproject.org/api/ip')).data expect(check.IsTor).to.be.true console.log( `\nšŸ§… check.torproject.org/api/ip says...\n\nWe are using Tor: ${check.IsTor ? 'āœ…' : 'āŒ'}` ) console.log(`Our IP is: ${check.IP}\n`) } catch (err) { torErrorThrow(ErrorUtils.ensureError(err)) } }).timeout(0) it('TorProvider: Should be able to fetch some basic blockchain data over Tor', async () => { try { console.log('\nBlock Number: ' + (await torProvider.getBlockNumber())) console.log('Gas Price: ' + (await torProvider.getGasPrice()).div(1000000000) + ' gwei') console.log( 'Zero address ETH burned: ' + (await torProvider.getBalance('0x0000000000000000000000000000000000000000')).div(parseUnits('1')) + '\n' ) } catch (err) { torErrorThrow(ErrorUtils.ensureError(err)) } }).timeout(0) it('DISCONNECTED: Should not be able to request over Tor', async function () { try { await torProvider.getBlockNumber() throw ErrorUtils.getError('should not have succeeded.') } catch (err) {} }).timeout(0) })