sdk-monorepo/src/lib/web.ts

33 lines
1.2 KiB
TypeScript
Raw Normal View History

import { SocksProxyAgent } from 'socks-proxy-agent'
import { Web3Provider, Networkish } from '@ethersproject/providers'
// It seems that the default HttpProvider offered by the normal web3 package
// has some logic which either ignores the SocksProxyAgent or which falls back to
// using no proxy for some reason. In any case, the Tornado-modified one, originally
// modified by the Tornado Team or who else, seems to properly error out when Tor
// is not running.
const HttpProvider = require('web3-providers-http')
export interface TorOptions {
port?: number
headers?: { name: string; value: string }[]
}
export class TorProvider extends Web3Provider {
constructor(url: string, torOpts: TorOptions, network?: Networkish) {
const torPort = torOpts.port ?? 9050,
headers = torOpts.headers ?? [
{ name: 'User-Agent', value: 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0' }
]
super(
new HttpProvider(url, {
agent: { https: new SocksProxyAgent('socks5h://127.0.0.1:' + torPort) }
// Don't want to set for some reason, need to override somehow
// headers: headers
}),
network
)
}
}