viem
viem copied to clipboard
Tor integration
Adds tor support via tor-hazae41.
This can be used like so:
const client = createPublicClient({
chain: mainnet,
transport: http({
tor: {
snowflakeUrl: 'wss://snowflake.pse.dev/',
filter: ['eth_sendRawTransaction'],
},
}),
})
(Described further in docs additions.)
I added some unit tests, but to try out the actual tor routing I've also added src/manualTorTest.ts which can be run like this:
npx tsx src/manualTorTest.ts
Sample output:
[2025-11-06T03:43:33.603Z] Manual Tor Integration Test Starting...
[2025-11-06T03:43:33.946Z] ✓ RPC connection successful. Latest block: 0x16a3522
[2025-11-06T03:43:33.946Z] Starting Tor Integration Test...
[2025-11-06T03:43:33.946Z] [tor] [info] Initializing WASM modules
[2025-11-06T03:43:33.953Z] >>> Tor fetch called for: {"jsonrpc":"2.0","id":0,"method":"eth_getBalance","params":["0x0000000000000000000000000000000000000000","latest"]}
[2025-11-06T03:43:33.953Z] >>> Tor routing methods: [ 'eth_getBalance' ]
[2025-11-06T03:43:33.953Z] [tor] [info] Starting fetch request to https://eth.llamarpc.com/
[2025-11-06T03:43:33.953Z] [tor] [info] Target: eth.llamarpc.com:443 (HTTPS: true)
[2025-11-06T03:43:33.961Z] [tor] [success] WASM modules initialized successfully
[2025-11-06T03:43:33.961Z] [tor] [info] Connecting to Snowflake bridge at wss://snowflake.pse.dev/
[2025-11-06T03:43:34.893Z] [tor] [info] Creating Snowflake stream
[2025-11-06T03:43:34.895Z] [tor] [info] Connecting streams
[2025-11-06T03:43:34.895Z] [tor] [info] Waiting for Tor to be ready (timeout: 90000ms)
[2025-11-06T03:43:35.931Z] [tor] [success] Tor client ready!
[2025-11-06T03:43:35.932Z] [tor] [info] Creating circuit
[2025-11-06T03:43:36.242Z] [tor] [success] Circuit created successfully
[2025-11-06T03:43:36.242Z] [tor] [info] Fetching consensus
[2025-11-06T03:43:49.689Z] [tor] [success] Consensus fetched with 9206 microdescs
[2025-11-06T03:43:49.691Z] [tor] [info] Filtering relays
[2025-11-06T03:43:49.694Z] [tor] [info] Found 7015 middle relays and 2593 exit relays
[2025-11-06T03:43:49.694Z] [tor] [info] Extending circuit through middle relay
[2025-11-06T03:43:51.013Z] [tor] [success] Extended through middle relay
[2025-11-06T03:43:51.013Z] [tor] [info] Extending circuit through exit relay
[2025-11-06T03:43:52.346Z] [tor] [success] Extended through exit relay
[2025-11-06T03:43:52.346Z] [tor] [info] Scheduled next circuit update in 600000ms with 60000ms advance
[2025-11-06T03:43:52.346Z] [tor] [info] Circuit used for first time, scheduling automatic updates
[2025-11-06T03:43:52.346Z] [tor] [info] Opening connection to eth.llamarpc.com:443
[2025-11-06T03:43:52.347Z] [tor] [info] Setting up TLS connection
[2025-11-06T03:43:52.347Z] [tor] [info] Making HTTPS request through Tor
[2025-11-06T03:43:54.635Z] [tor] [success] Request completed successfully
[2025-11-06T03:43:54.637Z] ✓ Balance should be a bigint
[2025-11-06T03:43:54.637Z] ✓ Tor should have been used with array filter
[2025-11-06T03:43:54.637Z] ✓ Regular fetch should NOT have been used with array filter
[2025-11-06T03:43:54.637Z] Balance with array filter: 14132601958130278963839 (burned at 0x0)
[2025-11-06T03:43:54.637Z]
🎉 All tests passed! Tor integration is working correctly.
[2025-11-06T03:43:54.637Z]
✅ All tests completed successfully!
⚠️ No Changeset found
Latest commit: 7cfd49ee4d4db0d76d646dae9a016702da70610c
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
| Diff | Package | Supply Chain Security |
Vulnerability | Quality | Maintenance | License |
|---|---|---|---|---|---|---|
| tor-hazae41@0.2.5 |
Thanks for the PR! It looks great, however we are not planning to add Tor support to Viem just yet as this would introduce an extra dependency to our supply chain. Would you be able to publish this in an external package instead?
Ah I understand. Yes I will look into this. Would that be something that could be mentioned in the docs still?