Skip to content

Commit d1c6208

Browse files
committed
move types to types file, simplify error and lint checks
1 parent 53150c1 commit d1c6208

File tree

5 files changed

+82
-93
lines changed

5 files changed

+82
-93
lines changed

packages/tx/.eslintrc.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module.exports = {
2-
extends: "@ethereumjs/eslint-config-defaults",
3-
ignorePatterns: ["examples", "karma.conf.js", "test-build"],
2+
extends: '@ethereumjs/eslint-config-defaults',
3+
ignorePatterns: ['examples', 'karma.conf.js', 'test-build'],
44
rules: {
5-
"@typescript-eslint/no-unnecessary-condition": "off"
6-
}
5+
'@typescript-eslint/no-unnecessary-condition': 'off',
6+
'no-dupe-class-members': 'off',
7+
},
78
}

packages/tx/src/baseTransaction.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ export abstract class BaseTransaction<TransactionObject> {
6161
* (DataFee + TxFee + Creation Fee).
6262
*/
6363
validate(): boolean
64-
/* eslint-disable-next-line no-dupe-class-members */
6564
validate(stringError: false): boolean
66-
/* eslint-disable-next-line no-dupe-class-members */
6765
validate(stringError: true): string[]
68-
/* eslint-disable-next-line no-dupe-class-members */
6966
validate(stringError: boolean = false): boolean | string[] {
7067
const errors = []
7168

@@ -185,13 +182,8 @@ export abstract class BaseTransaction<TransactionObject> {
185182
if (privateKey.length !== 32) {
186183
throw new Error('Private key must be 32 bytes in length.')
187184
}
188-
189185
const msgHash = this.getMessageToSign()
190-
191-
// Only `v` is reassigned.
192-
/* eslint-disable-next-line prefer-const */
193-
let { v, r, s } = ecsign(msgHash, privateKey)
194-
186+
const { v, r, s } = ecsign(msgHash, privateKey)
195187
return this._processSignature(v, r, s)
196188
}
197189

packages/tx/src/eip2930Transaction.ts

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,14 @@ import {
1515
AccessList,
1616
AccessListBuffer,
1717
AccessListEIP2930TxData,
18+
AccessListEIP2930ValuesArray,
1819
AccessListItem,
1920
isAccessList,
2021
JsonTx,
2122
TxOptions,
23+
N_DIV_2,
2224
} from './types'
2325

24-
// secp256k1n/2
25-
const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
26-
27-
type EIP2930ValuesArray = [
28-
Buffer,
29-
Buffer,
30-
Buffer,
31-
Buffer,
32-
Buffer,
33-
Buffer,
34-
Buffer,
35-
AccessListBuffer,
36-
Buffer?,
37-
Buffer?,
38-
Buffer?
39-
]
40-
4126
/**
4227
* Typed transaction with optional access lists
4328
*
@@ -115,33 +100,33 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
115100
* chainId, nonce, gasPrice, gasLimit, to, value, data, access_list, yParity (v), senderR (r), senderS (s)
116101
*/
117102
public static fromValuesArray(values: (Buffer | AccessListBuffer)[], opts: TxOptions = {}) {
118-
if (values.length == 8 || values.length == 11) {
119-
const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = <
120-
EIP2930ValuesArray
121-
>values
122-
const emptyBuffer = Buffer.from([])
123-
124-
return new AccessListEIP2930Transaction(
125-
{
126-
chainId: new BN(chainId),
127-
nonce: new BN(nonce),
128-
gasPrice: new BN(gasPrice),
129-
gasLimit: new BN(gasLimit),
130-
to: to && to.length > 0 ? new Address(to) : undefined,
131-
value: new BN(value),
132-
data: data ?? emptyBuffer,
133-
accessList: accessList ?? emptyBuffer,
134-
v: v !== undefined ? new BN(v) : undefined, // EIP2930 supports v's with value 0 (empty Buffer)
135-
r: r !== undefined && !r.equals(emptyBuffer) ? new BN(r) : undefined,
136-
s: s !== undefined && !s.equals(emptyBuffer) ? new BN(s) : undefined,
137-
},
138-
opts ?? {}
139-
)
140-
} else {
103+
if (values.length !== 8 && values.length !== 11) {
141104
throw new Error(
142105
'Invalid EIP-2930 transaction. Only expecting 8 values (for unsigned tx) or 11 values (for signed tx).'
143106
)
144107
}
108+
109+
const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = <
110+
AccessListEIP2930ValuesArray
111+
>values
112+
const emptyBuffer = Buffer.from([])
113+
114+
return new AccessListEIP2930Transaction(
115+
{
116+
chainId: new BN(chainId),
117+
nonce: new BN(nonce),
118+
gasPrice: new BN(gasPrice),
119+
gasLimit: new BN(gasLimit),
120+
to: to && to.length > 0 ? new Address(to) : undefined,
121+
value: new BN(value),
122+
data: data ?? emptyBuffer,
123+
accessList: accessList ?? emptyBuffer,
124+
v: v !== undefined ? new BN(v) : undefined, // EIP2930 supports v's with value 0 (empty Buffer)
125+
r: r !== undefined && !r.equals(emptyBuffer) ? new BN(r) : undefined,
126+
s: s !== undefined && !s.equals(emptyBuffer) ? new BN(s) : undefined,
127+
},
128+
opts
129+
)
145130
}
146131

147132
/**
@@ -158,8 +143,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
158143
throw new Error('EIP-2930 not enabled on Common')
159144
}
160145

161-
// check the type of AccessList. If it's a JSON-type, we have to convert it to a buffer.
162-
146+
// check the type of AccessList. If it's a JSON-type, we have to convert it to a buffer.
163147
let usedAccessList
164148
if (accessList && isAccessList(accessList)) {
165149
this.AccessListJSON = accessList
@@ -168,7 +152,6 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
168152

169153
for (let i = 0; i < accessList.length; i++) {
170154
const item: AccessListItem = accessList[i]
171-
//const addItem: AccessListBufferItem = []
172155
const addressBuffer = toBuffer(item.address)
173156
const storageItems: Buffer[] = []
174157
for (let index = 0; index < item.storageKeys.length; index++) {
@@ -208,7 +191,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
208191
throw new Error('The y-parity of the transaction should either be 0 or 1')
209192
}
210193

211-
if (this.common.gteHardfork('homestead') && this.s && this.s.gt(N_DIV_2)) {
194+
if (this.common.gteHardfork('homestead') && this.s?.gt(N_DIV_2)) {
212195
throw new Error(
213196
'Invalid Signature: s-values greater than secp256k1n/2 are considered invalid'
214197
)
@@ -342,7 +325,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
342325

343326
// All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
344327
// TODO: verify if this is the case for EIP-2930
345-
if (this.common.gteHardfork('homestead') && this.s && this.s.gt(N_DIV_2)) {
328+
if (this.common.gteHardfork('homestead') && this.s?.gt(N_DIV_2)) {
346329
throw new Error(
347330
'Invalid Signature: s-values greater than secp256k1n/2 are considered invalid'
348331
)

packages/tx/src/legacyTransaction.ts

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* eslint-disable no-dupe-class-members */
2-
3-
import { Buffer } from 'buffer'
41
import {
52
Address,
63
BN,
@@ -12,12 +9,9 @@ import {
129
toBuffer,
1310
unpadBuffer,
1411
} from 'ethereumjs-util'
15-
import { TxOptions, TxData, JsonTx } from './types'
12+
import { TxOptions, TxData, JsonTx, N_DIV_2 } from './types'
1613
import { BaseTransaction } from './baseTransaction'
1714

18-
// secp256k1n/2
19-
const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
20-
2115
/**
2216
* An Ethereum non-typed (legacy) transaction
2317
*/
@@ -64,38 +58,32 @@ export default class Transaction extends BaseTransaction<Transaction> {
6458
* nonce, gasPrice, gasLimit, to, value, data, v, r, s
6559
*/
6660
public static fromValuesArray(values: Buffer[], opts: TxOptions = {}) {
67-
if (values.length !== 6 && values.length !== 9) {
68-
throw new Error(
69-
'Invalid transaction. Only expecting 6 values (for unsigned tx) or 9 values (for signed tx).'
70-
)
71-
}
72-
7361
// If length is not 6, it has length 9. If v/r/s are empty Buffers, it is still an unsigned transaction
7462
// This happens if you get the RLP data from `raw()`
75-
if (values.length === 6 || values.length == 9) {
76-
const [nonce, gasPrice, gasLimit, to, value, data, v, r, s] = values
77-
78-
const emptyBuffer = Buffer.from([])
79-
80-
return new Transaction(
81-
{
82-
nonce: new BN(nonce),
83-
gasPrice: new BN(gasPrice),
84-
gasLimit: new BN(gasLimit),
85-
to: to && to.length > 0 ? new Address(to) : undefined,
86-
value: new BN(value),
87-
data: data ?? emptyBuffer,
88-
v: v !== undefined && !v.equals(emptyBuffer) ? new BN(v) : undefined,
89-
r: r !== undefined && !r.equals(emptyBuffer) ? new BN(r) : undefined,
90-
s: s !== undefined && !s.equals(emptyBuffer) ? new BN(s) : undefined,
91-
},
92-
opts
93-
)
94-
} else {
63+
if (values.length !== 6 && values.length !== 9) {
9564
throw new Error(
9665
'Invalid transaction. Only expecting 6 values (for unsigned tx) or 9 values (for signed tx).'
9766
)
9867
}
68+
69+
const [nonce, gasPrice, gasLimit, to, value, data, v, r, s] = values
70+
71+
const emptyBuffer = Buffer.from([])
72+
73+
return new Transaction(
74+
{
75+
nonce: new BN(nonce),
76+
gasPrice: new BN(gasPrice),
77+
gasLimit: new BN(gasLimit),
78+
to: to && to.length > 0 ? new Address(to) : undefined,
79+
value: new BN(value),
80+
data: data ?? emptyBuffer,
81+
v: v !== undefined && !v.equals(emptyBuffer) ? new BN(v) : undefined,
82+
r: r !== undefined && !r.equals(emptyBuffer) ? new BN(r) : undefined,
83+
s: s !== undefined && !s.equals(emptyBuffer) ? new BN(s) : undefined,
84+
},
85+
opts
86+
)
9987
}
10088

10189
/**
@@ -212,7 +200,7 @@ export default class Transaction extends BaseTransaction<Transaction> {
212200
const msgHash = this.getMessageToVerifySignature()
213201

214202
// All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
215-
if (this.common.gteHardfork('homestead') && this.s && this.s.gt(N_DIV_2)) {
203+
if (this.common.gteHardfork('homestead') && this.s?.gt(N_DIV_2)) {
216204
throw new Error(
217205
'Invalid Signature: s-values greater than secp256k1n/2 are considered invalid'
218206
)

packages/tx/src/types.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AddressLike, BNLike, BufferLike } from 'ethereumjs-util'
1+
import { BN, AddressLike, BNLike, BufferLike } from 'ethereumjs-util'
22
import Common from '@ethereumjs/common'
33
import { default as Transaction } from './legacyTransaction'
44
import { default as AccessListEIP2930Transaction } from './eip2930Transaction'
@@ -128,6 +128,23 @@ export interface AccessListEIP2930TxData extends TxData {
128128
type?: BNLike
129129
}
130130

131+
/**
132+
* Buffer values array for EIP2930 transaction
133+
*/
134+
export type AccessListEIP2930ValuesArray = [
135+
Buffer,
136+
Buffer,
137+
Buffer,
138+
Buffer,
139+
Buffer,
140+
Buffer,
141+
Buffer,
142+
AccessListBuffer,
143+
Buffer?,
144+
Buffer?,
145+
Buffer?
146+
]
147+
131148
type JsonAccessListItem = { address: string; storageKeys: string[] }
132149

133150
/**
@@ -147,3 +164,11 @@ export interface JsonTx {
147164
accessList?: JsonAccessListItem[]
148165
type?: string
149166
}
167+
168+
/**
169+
* A const defining secp256k1n/2
170+
*/
171+
export const N_DIV_2 = new BN(
172+
'7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0',
173+
16
174+
)

0 commit comments

Comments
 (0)