Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tx: Cleanup 7702 tests #3578

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
199 changes: 46 additions & 153 deletions packages/tx/test/eip7702.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
Expand All @@ -13,6 +15,26 @@ const addr = createAddressFromPrivateKey(pkey)

const ones32 = `0x${'01'.repeat(32)}` as PrefixedHexString

function getTxData(override: Partial<AuthorizationListItem> = {}): 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(
Expand All @@ -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<AuthorizationListItem>, 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 })
})
})
})
Loading