Skip to content

Commit d3d1a17

Browse files
committed
tx: moved v,r,s to BaseTransaction, added type inheritance for TxData objects, separate AccessListEIP2930TxData type
1 parent 928db03 commit d3d1a17

File tree

5 files changed

+38
-86
lines changed

5 files changed

+38
-86
lines changed

packages/tx/src/baseTransaction.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ecsign,
99
publicToAddress,
1010
} from 'ethereumjs-util'
11-
import { BaseTransactionData, BaseTxOptions, JsonTx } from './types'
11+
import { TxData, TxOptions, JsonTx } from './types'
1212

1313
export abstract class BaseTransaction<TransactionObject> {
1414
public readonly nonce: BN
@@ -23,8 +23,8 @@ export abstract class BaseTransaction<TransactionObject> {
2323
public readonly r?: BN
2424
public readonly s?: BN
2525

26-
constructor(txData: BaseTransactionData, txOptions: BaseTxOptions = {}) {
27-
const { nonce, gasLimit, gasPrice, to, value, data } = txData
26+
constructor(txData: TxData, txOptions: TxOptions = {}) {
27+
const { nonce, gasLimit, gasPrice, to, value, data, v, r, s } = txData
2828

2929
this.nonce = new BN(toBuffer(nonce))
3030
this.gasPrice = new BN(toBuffer(gasPrice))
@@ -33,6 +33,10 @@ export abstract class BaseTransaction<TransactionObject> {
3333
this.value = new BN(toBuffer(value))
3434
this.data = toBuffer(data)
3535

36+
this.v = v ? new BN(toBuffer(v)) : undefined
37+
this.r = r ? new BN(toBuffer(r)) : undefined
38+
this.s = s ? new BN(toBuffer(s)) : undefined
39+
3640
const validateCannotExceedMaxInteger = {
3741
nonce: this.nonce,
3842
gasPrice: this.gasPrice,

packages/tx/src/eip2930Transaction.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import { BaseTransaction } from './baseTransaction'
1414
import {
1515
AccessList,
1616
AccessListBuffer,
17+
AccessListEIP2930TxData,
1718
AccessListItem,
1819
isAccessList,
1920
JsonTx,
20-
TxData,
2121
TxOptions,
2222
} from './types'
2323

@@ -41,9 +41,6 @@ type EIP2930ValuesArray = [
4141
export default class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2930Transaction> {
4242
public readonly chainId: BN
4343
public readonly accessList: AccessListBuffer
44-
public readonly v?: BN
45-
public readonly r?: BN
46-
public readonly s?: BN
4744

4845
get transactionType(): number {
4946
return 1
@@ -67,7 +64,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
6764
return this.v
6865
}
6966

70-
public static fromTxData(txData: TxData, opts: TxOptions = {}) {
67+
public static fromTxData(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
7168
return new AccessListEIP2930Transaction(txData, opts)
7269
}
7370

@@ -125,10 +122,10 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
125122
}
126123
}
127124

128-
public constructor(txData: TxData, opts: TxOptions = {}) {
129-
const { chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s } = txData
125+
public constructor(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
126+
const { chainId, accessList } = txData
130127

131-
super({ nonce, gasPrice, gasLimit, to, value, data }, opts)
128+
super(txData, opts)
132129

133130
// EIP-2718 check is done in Common
134131
if (!this.common.isActivatedEIP(2930)) {
@@ -176,9 +173,6 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
176173

177174
this.chainId = chainId ? new BN(toBuffer(chainId)) : new BN(this.common.chainId())
178175
this.accessList = usedAccessList
179-
this.v = v ? new BN(toBuffer(v)) : undefined
180-
this.r = r ? new BN(toBuffer(r)) : undefined
181-
this.s = s ? new BN(toBuffer(s)) : undefined
182176

183177
if (!this.chainId.eq(new BN(this.common.chainId().toString()))) {
184178
throw new Error('The chain ID does not match the chain ID of Common')

packages/tx/src/legacyTransaction.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
2222
* An Ethereum transaction.
2323
*/
2424
export default class Transaction extends BaseTransaction<Transaction> {
25-
public readonly v?: BN
26-
public readonly r?: BN
27-
public readonly s?: BN
2825

2926
get transactionType(): number {
3027
return 0
@@ -90,13 +87,7 @@ export default class Transaction extends BaseTransaction<Transaction> {
9087
* @note Transaction objects implement EIP155 by default. To disable it, pass in an `@ethereumjs/common` object set before EIP155 activation (i.e. before Spurious Dragon).
9188
*/
9289
public constructor(txData: TxData, opts: TxOptions = {}) {
93-
const { nonce, gasPrice, gasLimit, to, value, data, v, r, s } = txData
94-
95-
super({ nonce, gasPrice, gasLimit, to, value, data }, opts)
96-
97-
this.v = v ? new BN(toBuffer(v)) : undefined
98-
this.r = r ? new BN(toBuffer(r)) : undefined
99-
this.s = s ? new BN(toBuffer(s)) : undefined
90+
super(txData, opts)
10091

10192
const validateCannotExceedMaxInteger = {
10293
r: this.r ?? new BN(0),

packages/tx/src/transactionFactory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Common from '@ethereumjs/common'
22
import { default as Transaction } from './legacyTransaction'
33
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'
4-
import { TxOptions, TypedTransaction, TxData } from './types'
4+
import { TxOptions, TypedTransaction, TxData, AccessListEIP2930TxData } from './types'
55
import { BN } from 'ethereumjs-util'
66

77
const DEFAULT_COMMON = new Common({ chain: 'mainnet' })
@@ -15,14 +15,14 @@ export default class TransactionFactory {
1515
* @param txData - The transaction data. The `type` field will determine which transaction type is returned (if undefined, create a Transaction)
1616
* @param txOptions - Options to pass on to the constructor of the transaction
1717
*/
18-
public static fromTxData(txData: TxData, txOptions: TxOptions = {}): TypedTransaction {
18+
public static fromTxData(txData: TxData | AccessListEIP2930TxData, txOptions: TxOptions = {}): TypedTransaction {
1919
const common = txOptions.common ?? DEFAULT_COMMON
20-
if (txData.type === undefined) {
20+
if (!('type' in txData) || txData.type === undefined) {
2121
// Assume Transaction
22-
return Transaction.fromTxData(txData, txOptions)
22+
return Transaction.fromTxData(<TxData>txData, txOptions)
2323
} else {
2424
const txType = new BN(txData.type).toNumber()
25-
return TransactionFactory.getTransactionClass(txType, common).fromTxData(txData, txOptions)
25+
return TransactionFactory.getTransactionClass(txType, common).fromTxData(<AccessListEIP2930TxData>txData, txOptions)
2626
}
2727
}
2828

packages/tx/src/types.ts

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ export interface TxOptions {
3131
freeze?: boolean
3232
}
3333

34-
/**
35-
* The options for initializing a Transaction.
36-
*/
37-
export interface BaseTxOptions {
38-
common?: Common
39-
}
40-
4134
export type AccessListItem = {
4235
address: string
4336
storageKeys: string[]
@@ -65,15 +58,9 @@ export function isAccessList(input: AccessListBuffer | AccessList): input is Acc
6558
return !isAccessListBuffer(input) // This is exactly the same method, except the output is negated.
6659
}
6760

68-
/**
69-
* An object with an optional field with each of the transaction's values.
70-
*/
71-
export interface TxData {
72-
/**
73-
* The transaction's chain ID
74-
*/
75-
chainId?: BNLike
61+
export type TypedTransaction = Transaction | AccessListEIP2930Transaction
7662

63+
export type TxData = {
7764
/**
7865
* The transaction's nonce.
7966
*/
@@ -107,17 +94,27 @@ export interface TxData {
10794
/**
10895
* EC recovery ID.
10996
*/
110-
v?: BNLike
111-
112-
/**
113-
* EC signature parameter.
114-
*/
115-
r?: BNLike
97+
v?: BNLike
98+
99+
/**
100+
* EC signature parameter.
101+
*/
102+
r?: BNLike
103+
104+
/**
105+
* EC signature parameter.
106+
*/
107+
s?: BNLike
108+
}
116109

110+
/**
111+
* An object with an optional field with each of the transaction's values.
112+
*/
113+
export interface AccessListEIP2930TxData extends TxData {
117114
/**
118-
* EC signature parameter.
115+
* The transaction's chain ID
119116
*/
120-
s?: BNLike
117+
chainId?: BNLike
121118

122119
/**
123120
* The access list which contains the addresses/storage slots which the transaction wishes to access
@@ -131,40 +128,6 @@ export interface TxData {
131128
type?: BNLike
132129
}
133130

134-
export type TypedTransaction = Transaction | AccessListEIP2930Transaction
135-
136-
export type BaseTransactionData = {
137-
/**
138-
* The transaction's nonce.
139-
*/
140-
nonce?: BNLike
141-
142-
/**
143-
* The transaction's gas price.
144-
*/
145-
gasPrice?: BNLike
146-
147-
/**
148-
* The transaction's gas limit.
149-
*/
150-
gasLimit?: BNLike
151-
152-
/**
153-
* The transaction's the address is sent to.
154-
*/
155-
to?: AddressLike
156-
157-
/**
158-
* The amount of Ether sent.
159-
*/
160-
value?: BNLike
161-
162-
/**
163-
* This will contain the data of the message or the init of a contract.
164-
*/
165-
data?: BufferLike
166-
}
167-
168131
type JsonAccessListItem = { address: string; storageKeys: string[] }
169132

170133
/**

0 commit comments

Comments
 (0)