Skip to content

Commit

Permalink
tx: Cleanup 7702 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Aug 12, 2024
1 parent e82ffec commit 7ebf5f8
Showing 1 changed file with 46 additions and 153 deletions.
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 })
})
})
})

0 comments on commit 7ebf5f8

Please sign in to comment.