Skip to content

fix: throw RangeError for an unsupported network option - #13

Merged
Samarth208P merged 3 commits into
Samarth208P:mainfrom
Dhananjay-alt:fix/invalid-network-option
Nov 21, 2026
Merged

Samarth208P merged 3 commits into
Samarth208P:mainfrom
Dhananjay-alt:fix/invalid-network-option

Conversation

@Dhananjay-alt

Copy link
Copy Markdown
Contributor

Closes #6.

User-visible change

An unsupported network value now throws a RangeError. That's any value other than 'mainnet', 'testnet', 'auto', undefined or null. It throws from:

  • validateAddress
  • validateBatch
  • detectBlockchain
  • every validator's validate method

The check runs before any input is read, so the error doesn't depend on the data. An empty batch, and empty, blank or unsafe detection input, all throw too.

Previously, network: 'foo' made a valid mainnet Bitcoin address fail with NETWORK_MISMATCH, while a valid Ethereum address passed with the same option.

undefined and null still mean auto, as on main, so configurations that store an unset value as null keep working. A valid network paired with a real address on the other network still returns NETWORK_MISMATCH with the decoded network.

README, AGENTS.md and the CHANGELOG ("Unreleased") are updated.

Implementation

A shared assertValidationOptions in src/validators/base.ts runs at the start of three places:

  • AddressValidator.validate
  • validateBatch
  • detectBlockchain

It isn't exported from the package entry points.

Tests

tests/network-option.test.ts covers each case requested in the issue:

  • Invalid values tested: 'foo', 'Mainnet', 'main', '', 1, true and {}. Each one throws a RangeError from:
    • single-address validation on Bitcoin (mainnet and testnet), Ethereum and Base
    • direct BitcoinValidator, EthereumValidator and CardanoValidator instances
    • batches, including empty batches
    • detection on valid, empty, blank, unsafe and non-string input
    • the error message names the accepted values
  • Valid values: undefined, null, 'auto', 'mainnet' and 'testnet' return [] for an empty batch and null for empty detection input, without throwing.
  • Real mismatches:
    • network: 'mainnet' with the BIP-173 testnet vector returns NETWORK_MISMATCH with network: 'testnet' and format: 'segwit'.
    • The reverse case also returns NETWORK_MISMATCH.
    • Batches report it per entry.
  • Unaffected cases: matching networks, and chains whose addresses don't encode a network, still pass.

On main, 35 of the 42 new tests fail; the 7 control tests pass. All of them pass on this branch.

Results

  • 287 tests.
  • Coverage: statements 96.4%, branches 92.2%, functions 100%, lines 99.4%.
  • Locally on Linux, I ran a clean npm ci, npm run check and npm audit (0 vulnerabilities) on Node 22.23.5, 24.16.0 and 26.13.1, and npm run test:coverage on Node 24.16.0. All passed.
  • This repository's CI workflow ran on my fork for this exact commit (1f4eb4c), and all 7 jobs passed: package on Ubuntu and Windows with Node 22, 24 and 26 (Chrome stable), plus coverage. Run: https://github.com/Dhananjay-alt/validator0x/actions/runs/44905428027
  • Locally, the packed worker test ran in Playwright's Chromium because Chrome stable isn't installed on that machine. The fork CI run above used Chrome stable.

From JavaScript, network: 'foo' made a valid mainnet Bitcoin address
fail with NETWORK_MISMATCH while a valid Ethereum address passed with
the same option, pointing callers at the recipient instead of their
configuration.

validateAddress, validateBatch, detectBlockchain and validator validate
methods now check the option through a shared assertValidationOptions
before reading input, so empty batches and empty, blank or unsafe
detection input throw too. undefined and null still mean auto. A valid
network paired with a real address on the other network still returns
NETWORK_MISMATCH with the decoded network.

Closes Samarth208P#6
Copilot AI balanced review requested due to automatic review settings November 21, 2026 12:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Samarth208P
Samarth208P merged commit c260f93 into Samarth208P:main Nov 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: an invalid network option silently rejects every address

3 participants