Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/connect/src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the missing Vitest dependency

In packages/connect, this new test is included by the package tsconfig's ./src/**/*.ts pattern, so pnpm --filter @0xsequence/connect typecheck/build:esm will try to resolve this import. I checked packages/connect/package.json and the lockfile importer for packages/connect, and vitest is not declared there (nor at the workspace root), so a fresh pnpm install cannot resolve vitest from this package and the connect build/typecheck will fail with TS2307 before publishing.

Useful? React with 👍 / 👎.


import { normalizeChainId } from './helpers'

describe('normalizeChainId', () => {
it('normalizes supported chain id values', () => {
expect(normalizeChainId(1)).toBe(1)
expect(normalizeChainId(1n)).toBe(1)
expect(normalizeChainId('1')).toBe(1)
expect(normalizeChainId('0x1')).toBe(1)
expect(normalizeChainId({ chainId: '0x2105' })).toBe(8453)
})

it('rejects invalid chain id values', () => {
expect(() => normalizeChainId('1abc')).toThrow('Invalid chain id')
expect(() => normalizeChainId('abc')).toThrow('Invalid chain id')
expect(() => normalizeChainId('0x')).toThrow('Invalid chain id')
expect(() => normalizeChainId(Number.NaN)).toThrow('Invalid chain id')
expect(() => normalizeChainId(BigInt(Number.MAX_SAFE_INTEGER) + 1n)).toThrow('Invalid chain id')
})
})
25 changes: 21 additions & 4 deletions packages/connect/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,30 @@ export const normalizeChainId = (chainId: string | number | bigint | { chainId:
if (typeof chainId === 'object') {
return normalizeChainId(chainId.chainId)
}

let normalizedChainId: number

if (typeof chainId === 'string') {
return Number.parseInt(chainId, chainId.trim().substring(0, 2) === '0x' ? 16 : 10)
const trimmed = chainId.trim()
const isHex = trimmed.startsWith('0x')
if (isHex ? !/^0x[\da-f]+$/iu.test(trimmed) : !/^\d+$/u.test(trimmed)) {
throw new Error(`Invalid chain id: ${chainId}`)
}
normalizedChainId = Number.parseInt(trimmed, isHex ? 16 : 10)
} else if (typeof chainId === 'bigint') {
if (chainId < 0n || chainId > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new Error(`Invalid chain id: ${chainId.toString()}`)
}
normalizedChainId = Number(chainId)
} else {
normalizedChainId = chainId
}
if (typeof chainId === 'bigint') {
return Number(chainId)

if (!Number.isSafeInteger(normalizedChainId) || normalizedChainId < 0) {
throw new Error(`Invalid chain id: ${chainId.toString()}`)
}
return chainId

return normalizedChainId
}

export const isSequenceUrl = (url?: string): url is string => {
Expand Down