diff --git a/packages/tx/test/eip7702.spec.ts b/packages/tx/test/eip7702.spec.ts index 8299172f83..ef8e980d51 100644 --- a/packages/tx/test/eip7702.spec.ts +++ b/packages/tx/test/eip7702.spec.ts @@ -4,6 +4,8 @@ import { assert, describe, it } from 'vitest' import { create7702EOACodeTx } from '../src/index.js' +import type { TxData } from '../src/7702/tx.js' +import type { AuthorizationListItem } from '../src/index.js' import type { PrefixedHexString } from '@ethereumjs/util' const common = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, eips: [7702] }) @@ -13,6 +15,26 @@ const addr = createAddressFromPrivateKey(pkey) const ones32 = `0x${'01'.repeat(32)}` as PrefixedHexString +function getTxData(override: Partial = {}): TxData { + const validAuthorizationList: AuthorizationListItem = { + chainId: '0x', + address: `0x${'20'.repeat(20)}`, + nonce: ['0x1'], + yParity: '0x1', + r: ones32, + s: ones32, + } + + return { + authorizationList: [ + { + ...validAuthorizationList, + ...override, + }, + ], + } +} + describe('[EOACodeEIP7702Transaction]', () => { it('sign()', () => { const txn = create7702EOACodeTx( @@ -36,166 +58,37 @@ describe('[EOACodeEIP7702Transaction]', () => { }) it('valid and invalid authorizationList values', () => { - assert.throws(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(21)}`, - nonce: [], - yParity: '0x1', - r: ones32, - s: ones32, - }, - ], - }, - { common }, - ) - }, 'address length should be 20 bytes') - - assert.throws(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(20)}`, - nonce: ['0x1', '0x2'], - yParity: '0x1', - r: ones32, - s: ones32, - }, - ], - }, - { common }, - ) - }, 'nonce list should consist of at most 1 item') - - assert.throws(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(20)}`, - nonce: ['0x1'], - yParity: '0x1', - r: ones32, - s: undefined as never, - }, - ], - }, - { common }, - ) - }, 's is not defined') - - assert.throws(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(20)}`, - nonce: ['0x1'], - yParity: '0x1', - r: undefined as never, - s: ones32, - }, - ], - }, - { common }, - ) - }, 'r is not defined') - - assert.throws(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(20)}`, - nonce: ['0x1'], - yParity: undefined as never, - r: ones32, - s: ones32, - }, - ], - }, - { common }, - ) - }, 'yParity is not defined') - - assert.throws(() => { - create7702EOACodeTx( + const tests: [Partial, string][] = [ + [ { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(20)}`, - nonce: undefined as never, - yParity: '0x1', - r: ones32, - s: ones32, - }, - ], + address: `0x${'20'.repeat(21)}`, }, - { common }, - ) - }, 'nonce is not defined') - - assert.throws(() => { - create7702EOACodeTx( + 'address length should be 20 bytes', + ], + [ { - authorizationList: [ - { - chainId: '0x', - address: undefined as never, - nonce: ['0x1'], - yParity: '0x1', - r: ones32, - s: ones32, - }, - ], + nonce: ['0x1', '0x2'], }, - { common }, - ) - }, 'address is not defined') + 'nonce list should consist of at most 1 item', + ], + [{ s: undefined as never }, 's is not defined'], + [{ r: undefined as never }, 'r is not defined'], + [{ yParity: undefined as never }, 'yParity is not defined'], + [{ nonce: undefined as never }, 'nonce is not defined'], + [{ address: undefined as never }, 'address is not defined'], + [{ chainId: undefined as never }, 'chainId is not defined'], + ] - assert.throws(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: undefined as never, - address: `0x${'20'.repeat(20)}`, - nonce: ['0x1'], - yParity: '0x1', - r: ones32, - s: ones32, - }, - ], - }, - { common }, - ) - }, 'chainId is not defined') + for (const test of tests) { + const txData = getTxData(test[0]) + const testName = test[1] + assert.throws(() => { + create7702EOACodeTx(txData, { common }), testName + }) + } assert.doesNotThrow(() => { - create7702EOACodeTx( - { - authorizationList: [ - { - chainId: '0x', - address: `0x${'20'.repeat(20)}`, - nonce: ['0x1'], - yParity: '0x1', - r: ones32, - s: ones32, - }, - ], - }, - { common }, - ) + create7702EOACodeTx(getTxData(), { common }) }) }) })