65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import chai from 'chai'
|
|
|
|
import { TorHttpClient, TorProvider } from 'lib/web'
|
|
|
|
// Waffle matchers
|
|
import { solidity } from 'ethereum-waffle'
|
|
import { ErrorUtils } from 'lib/utils'
|
|
import { parseUnits } from 'ethers/lib/utils'
|
|
|
|
chai.use(solidity)
|
|
|
|
const expect = chai.expect
|
|
|
|
describe('web', () => {
|
|
if (!process.env.ETH_MAINNET_TEST_RPC || !process.env.TOR_PORT)
|
|
throw ErrorUtils.getError('need a tor port and mainnet rpc endpoint.')
|
|
|
|
const torProvider = new TorProvider(process.env.ETH_MAINNET_TEST_RPC, { port: +process.env.TOR_PORT })
|
|
const httpClient = new TorHttpClient({ port: +process.env.TOR_PORT })
|
|
|
|
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('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.only('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.skip('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)
|
|
})
|