Skip to content

fix(sdk-coin-etc): fix hex validation in queryAddressBalance and getAddressNonce#9154

Draft
bitgo-ai-agent-dev[bot] wants to merge 1 commit into
masterfrom
fix/SPT-78-etc-balance-query-hex-validation
Draft

fix(sdk-coin-etc): fix hex validation in queryAddressBalance and getAddressNonce#9154
bitgo-ai-agent-dev[bot] wants to merge 1 commit into
masterfrom
fix/SPT-78-etc-balance-query-hex-validation

Conversation

@bitgo-ai-agent-dev

Copy link
Copy Markdown

What

  • Replace isNaN(result.result) checks with a proper hex regex validator (/^0x[0-9a-fA-F]*$/) in queryAddressBalance and getAddressNonce
  • Add fallback || '0' when parsing the hex suffix so that a bare '0x' response (valid zero in some RPC nodes) parses as zero instead of throwing
  • Add unit tests covering: standard hex, '0x0', bare '0x' (zero balance edge case), missing result, RPC error, and non-hex garbage

Why

  • Some Ethereum JSON-RPC implementations (including certain Blockscout configurations) return '0x' rather than '0x0' for a zero balance. isNaN('0x') returns true in JavaScript, so the recovery flow threw an opaque "Incorrect Balance Hex" error instead of treating the address as having a zero balance.
  • This caused ETC wallet balance queries during recovery to fail for zero-balance addresses, contributing to the balance mismatch reported in SPT-78.
  • isNaN is not the right tool for validating Ethereum hex strings; the regex ^0x[0-9a-fA-F]*$ is explicit and handles all valid cases.

Test plan

  • yarn unit-test --scope @bitgo/sdk-coin-etc — 30 tests passing
  • yarn lint --scope @bitgo/sdk-coin-etc — no lint errors
  • Manually verify recovery flow on a zero-balance ETC address returns 0 rather than throwing

Ticket: SPT-78

…ddressNonce

Replace isNaN() checks with a strict hex regex validator. isNaN() returns
true for bare '0x' (valid zero-balance response from some RPC nodes),
causing the recovery flow to throw instead of returning zero. Also guard
against BN parsing empty string by falling back to '0' when the hex
suffix is absent.

Add unit tests covering standard hex, '0x0', bare '0x', missing result,
RPC error, and non-hex result cases.

Ticket: SPT-78
Session-Id: f9f4d3e1-2080-4145-88f2-ff2c4dbb6718
Task-Id: 569e8750-ba8b-4ce0-95af-935c9fd486d6
@bitgo-ai-agent-dev bitgo-ai-agent-dev Bot force-pushed the fix/SPT-78-etc-balance-query-hex-validation branch from f8c16ad to c800e16 Compare July 1, 2026 05:53
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.

0 participants