2023-04-08 22:17:52 +03:00
import chai from 'chai'
2023-05-03 01:19:28 +03:00
import { TorHttpClient , RegularHttpClient , TorProvider , Relayer } from '@tornado/sdk-web'
2023-04-08 22:17:52 +03:00
// Waffle matchers
import { solidity } from 'ethereum-waffle'
2023-05-03 01:19:28 +03:00
import { ErrorUtils } from '@tornado/sdk-utils'
2023-04-11 22:36:32 +03:00
import { parseUnits } from 'ethers/lib/utils'
2023-04-27 20:34:22 +03:00
import { Web3Provider } from '@ethersproject/providers'
2023-04-08 22:17:52 +03:00
chai . use ( solidity )
const expect = chai . expect
2023-04-11 22:36:32 +03:00
describe ( 'web' , ( ) = > {
2023-04-27 20:34:22 +03:00
const torify = process . env . TORIFY === 'true'
2023-04-08 22:17:52 +03:00
if ( ! process . env . ETH_MAINNET_TEST_RPC || ! process . env . TOR_PORT )
throw ErrorUtils . getError ( 'need a tor port and mainnet rpc endpoint.' )
2023-04-27 20:34:22 +03:00
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 ( )
2023-04-08 22:17:52 +03:00
2023-04-27 20:34:22 +03:00
if ( torify )
2023-04-18 00:56:57 +03:00
console . log (
'\nSome Tor tips: Support non-profit exit node operators, host your own nodes, avoid spy nodes by configuring torrc.\n'
)
2023-04-08 22:17:52 +03:00
2023-04-11 22:36:32 +03:00
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
}
2023-04-27 20:34:22 +03:00
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 )
2023-04-11 22:36:32 +03:00
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 )
2023-04-18 00:56:57 +03:00
it ( 'TorProvider: Should be able to fetch some basic blockchain data over Tor' , async ( ) = > {
2023-04-11 22:36:32 +03:00
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 ) )
}
2023-04-08 22:17:52 +03:00
} ) . timeout ( 0 )
2023-04-18 00:56:57 +03:00
it ( 'DISCONNECTED: Should not be able to request over Tor' , async function ( ) {
2023-04-08 22:17:52 +03:00
try {
await torProvider . getBlockNumber ( )
throw ErrorUtils . getError ( 'should not have succeeded.' )
} catch ( err ) { }
} ) . timeout ( 0 )
} )