Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tx-envelope-chain-id-assert.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ox": patch
---

Fixed `TxEnvelopeLegacy.assert`, `TxEnvelopeEip2930.assert`, `TxEnvelopeEip1559.assert` and `TxEnvelopeTempo.assert` to throw `InvalidChainIdError` for a `chainId` that is not a positive safe integer.
5 changes: 4 additions & 1 deletion src/core/TxEnvelopeEip1559.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export type Type = typeof type
*/
export function assert(envelope: PartialBy<TxEnvelopeEip1559, 'type'>) {
const { chainId, maxPriorityFeePerGas, maxFeePerGas, to } = envelope
if (chainId <= 0)
if (
typeof chainId !== 'undefined' &&
(!Number.isSafeInteger(chainId) || chainId <= 0)
)
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
if (to) Address.assert(to, { strict: false })
if (maxFeePerGas && BigInt(maxFeePerGas) > 2n ** 256n - 1n)
Expand Down
5 changes: 4 additions & 1 deletion src/core/TxEnvelopeEip2930.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export type Type = typeof type
*/
export function assert(envelope: PartialBy<TxEnvelopeEip2930, 'type'>) {
const { chainId, gasPrice, to } = envelope
if (chainId <= 0)
if (
typeof chainId !== 'undefined' &&
(!Number.isSafeInteger(chainId) || chainId <= 0)
)
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
if (to) Address.assert(to, { strict: false })
if (gasPrice && BigInt(gasPrice) > 2n ** 256n - 1n)
Expand Down
5 changes: 4 additions & 1 deletion src/core/TxEnvelopeLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export type Type = typeof type
export function assert(envelope: PartialBy<TxEnvelopeLegacy, 'type'>) {
const { chainId, gasPrice, to } = envelope
if (to) Address.assert(to, { strict: false })
if (typeof chainId !== 'undefined' && chainId <= 0)
if (
typeof chainId !== 'undefined' &&
(!Number.isSafeInteger(chainId) || chainId <= 0)
)
throw new TransactionEnvelope.InvalidChainIdError({ chainId })
if (gasPrice && BigInt(gasPrice) > 2n ** 256n - 1n)
throw new TransactionEnvelope.GasPriceTooHighError({ gasPrice })
Expand Down
28 changes: 28 additions & 0 deletions src/core/_test/TransactionEnvelopeEip1559.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ describe('assert', () => {
)
})

test('invalid chainId (not a positive safe integer)', () => {
expect(() =>
TxEnvelopeEip1559.assert({ chainId: Number.NaN }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "NaN" is invalid.]`,
)
expect(() =>
TxEnvelopeEip1559.assert({ chainId: 1.5 }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1.5" is invalid.]`,
)
expect(() =>
TxEnvelopeEip1559.assert({ chainId: Number.MAX_SAFE_INTEGER + 1 }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "9007199254740992" is invalid.]`,
)
expect(() =>
TxEnvelopeEip1559.assert({ chainId: '1' as never }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
expect(() =>
TxEnvelopeEip1559.assert({ chainId: 1n as never }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
})

test('invalid address', () => {
expect(() => TxEnvelopeEip1559.assert({ to: '0x123', chainId: 1 }))
.toThrowErrorMatchingInlineSnapshot(`
Expand Down
28 changes: 28 additions & 0 deletions src/core/_test/TransactionEnvelopeEip2930.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ describe('assert', () => {
)
})

test('invalid chainId (not a positive safe integer)', () => {
expect(() =>
TxEnvelopeEip2930.assert({ chainId: Number.NaN }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "NaN" is invalid.]`,
)
expect(() =>
TxEnvelopeEip2930.assert({ chainId: 1.5 }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1.5" is invalid.]`,
)
expect(() =>
TxEnvelopeEip2930.assert({ chainId: Number.MAX_SAFE_INTEGER + 1 }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "9007199254740992" is invalid.]`,
)
expect(() =>
TxEnvelopeEip2930.assert({ chainId: '1' as never }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
expect(() =>
TxEnvelopeEip2930.assert({ chainId: 1n as never }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
})

test('invalid address', () => {
expect(() => TxEnvelopeEip2930.assert({ to: '0x123', chainId: 1 }))
.toThrowErrorMatchingInlineSnapshot(`
Expand Down
28 changes: 28 additions & 0 deletions src/core/_test/TransactionEnvelopeLegacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ describe('assert', () => {
)
})

test('invalid chainId (not a positive safe integer)', () => {
expect(() =>
TxEnvelopeLegacy.assert({ chainId: Number.NaN }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "NaN" is invalid.]`,
)
expect(() =>
TxEnvelopeLegacy.assert({ chainId: 1.5 }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1.5" is invalid.]`,
)
expect(() =>
TxEnvelopeLegacy.assert({ chainId: Number.MAX_SAFE_INTEGER + 1 }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "9007199254740992" is invalid.]`,
)
expect(() =>
TxEnvelopeLegacy.assert({ chainId: '1' as never }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
expect(() =>
TxEnvelopeLegacy.assert({ chainId: 1n as never }),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
})

test('invalid address', () => {
expect(() => TxEnvelopeLegacy.assert({ to: '0x123', chainId: 1 }))
.toThrowErrorMatchingInlineSnapshot(`
Expand Down
43 changes: 43 additions & 0 deletions src/tempo/TxEnvelopeTempo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,49 @@ describe('assert', () => {
)
})

test('invalid chain id (not a positive safe integer)', () => {
expect(() =>
TxEnvelopeTempo.assert({
calls: [{ to: '0x0000000000000000000000000000000000000000' }],
chainId: Number.NaN,
}),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "NaN" is invalid.]`,
)
expect(() =>
TxEnvelopeTempo.assert({
calls: [{ to: '0x0000000000000000000000000000000000000000' }],
chainId: 1.5,
}),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1.5" is invalid.]`,
)
expect(() =>
TxEnvelopeTempo.assert({
calls: [{ to: '0x0000000000000000000000000000000000000000' }],
chainId: Number.MAX_SAFE_INTEGER + 1,
}),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "9007199254740992" is invalid.]`,
)
expect(() =>
TxEnvelopeTempo.assert({
calls: [{ to: '0x0000000000000000000000000000000000000000' }],
chainId: '1' as never,
}),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
expect(() =>
TxEnvelopeTempo.assert({
calls: [{ to: '0x0000000000000000000000000000000000000000' }],
chainId: 1n as never,
}),
).toThrowErrorMatchingInlineSnapshot(
`[TransactionEnvelope.InvalidChainIdError: Chain ID "1" is invalid.]`,
)
})

test('invalid fee payer signature', () => {
const envelope = {
calls: [{ to: '0x0000000000000000000000000000000000000000' }],
Expand Down
5 changes: 4 additions & 1 deletion src/tempo/TxEnvelopeTempo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ export function assert(envelope: PartialBy<TxEnvelopeTempo, 'type'>) {
if (feeToken) Address.assert(feeToken, { strict: false })

// Validate chain ID
if (chainId <= 0)
if (
typeof chainId !== 'undefined' &&
(!Number.isSafeInteger(chainId) || chainId <= 0)
)
throw new TransactionEnvelope.InvalidChainIdError({ chainId })

// Validate max fee per gas
Expand Down
Loading