@@ -15,29 +15,14 @@ import {
15
15
AccessList ,
16
16
AccessListBuffer ,
17
17
AccessListEIP2930TxData ,
18
+ AccessListEIP2930ValuesArray ,
18
19
AccessListItem ,
19
20
isAccessList ,
20
21
JsonTx ,
21
22
TxOptions ,
23
+ N_DIV_2 ,
22
24
} from './types'
23
25
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
-
41
26
/**
42
27
* Typed transaction with optional access lists
43
28
*
@@ -88,21 +73,22 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
88
73
}
89
74
90
75
const values = rlp . decode ( serialized . slice ( 1 ) )
76
+
91
77
if ( ! Array . isArray ( values ) ) {
92
78
throw new Error ( 'Invalid serialized tx input: must be array' )
93
79
}
94
80
95
- return AccessListEIP2930Transaction . fromValuesArray ( values , opts )
81
+ return AccessListEIP2930Transaction . fromValuesArray ( values as any , opts )
96
82
}
97
83
98
84
/**
99
85
* Instantiate a transaction from the serialized tx.
100
- * (alias of fromSerializedTx())
86
+ * (alias of ` fromSerializedTx()` )
101
87
*
102
88
* Note: This means that the Buffer should start with 0x01.
103
89
*
104
90
* @deprecated this constructor alias is deprecated and will be removed
105
- * in favor of the from SerializedTx() constructor
91
+ * in favor of the `fromSerializedTx()` constructor
106
92
*/
107
93
public static fromRlpSerializedTx ( serialized : Buffer , opts : TxOptions = { } ) {
108
94
return AccessListEIP2930Transaction . fromSerializedTx ( serialized , opts )
@@ -114,34 +100,33 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
114
100
* The format is:
115
101
* chainId, nonce, gasPrice, gasLimit, to, value, data, access_list, yParity (v), senderR (r), senderS (s)
116
102
*/
117
- 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
+ public static fromValuesArray ( values : AccessListEIP2930ValuesArray , opts : TxOptions = { } ) {
104
+ if ( values . length !== 8 && values . length !== 11 ) {
141
105
throw new Error (
142
106
'Invalid EIP-2930 transaction. Only expecting 8 values (for unsigned tx) or 11 values (for signed tx).'
143
107
)
144
108
}
109
+
110
+ const [ chainId , nonce , gasPrice , gasLimit , to , value , data , accessList , v , r , s ] = values
111
+
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
+ )
145
130
}
146
131
147
132
/**
@@ -158,17 +143,14 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
158
143
throw new Error ( 'EIP-2930 not enabled on Common' )
159
144
}
160
145
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.
163
147
let usedAccessList
164
148
if ( accessList && isAccessList ( accessList ) ) {
165
149
this . AccessListJSON = accessList
166
-
167
150
const newAccessList : AccessListBuffer = [ ]
168
151
169
152
for ( let i = 0 ; i < accessList . length ; i ++ ) {
170
153
const item : AccessListItem = accessList [ i ]
171
- //const addItem: AccessListBufferItem = []
172
154
const addressBuffer = toBuffer ( item . address )
173
155
const storageItems : Buffer [ ] = [ ]
174
156
for ( let index = 0 ; index < item . storageKeys . length ; index ++ ) {
@@ -208,7 +190,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
208
190
throw new Error ( 'The y-parity of the transaction should either be 0 or 1' )
209
191
}
210
192
211
- if ( this . common . gteHardfork ( 'homestead' ) && this . s && this . s . gt ( N_DIV_2 ) ) {
193
+ if ( this . common . gteHardfork ( 'homestead' ) && this . s ? .gt ( N_DIV_2 ) ) {
212
194
throw new Error (
213
195
'Invalid Signature: s-values greater than secp256k1n/2 are considered invalid'
214
196
)
@@ -263,20 +245,10 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
263
245
/**
264
246
* Returns a Buffer Array of the raw Buffers of this transaction, in order.
265
247
*
266
- * Note that if you want to use this function in a tx type independent way
267
- * to then use the raw data output for tx instantiation with
268
- * `Tx.fromValuesArray()` you should set the `asList` parameter to `true` -
269
- * which is ignored on a legacy tx but provides the correct format on
270
- * a typed tx.
271
- *
272
- * To prepare a tx to be added as block data with `Block.fromValuesArray()`
273
- * just use the plain `raw()` method.
274
- *
275
- * @param asList - By default, this method returns a concatenated Buffer
276
- * If this is not desired, then set this to `true`, to get a Buffer array.
248
+ * Use `serialize()` to add to block data for `Block.fromValuesArray()`.
277
249
*/
278
- raw ( asList = false ) : Buffer [ ] | Buffer {
279
- const base = < Buffer [ ] > [
250
+ raw ( ) : AccessListEIP2930ValuesArray {
251
+ return [
280
252
bnToRlp ( this . chainId ) ,
281
253
bnToRlp ( this . nonce ) ,
282
254
bnToRlp ( this . gasPrice ) ,
@@ -289,27 +261,22 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
289
261
this . r !== undefined ? bnToRlp ( this . r ) : Buffer . from ( [ ] ) ,
290
262
this . s !== undefined ? bnToRlp ( this . s ) : Buffer . from ( [ ] ) ,
291
263
]
292
- if ( ! asList ) {
293
- return Buffer . concat ( [ Buffer . from ( '01' , 'hex' ) , rlp . encode ( base ) ] )
294
- } else {
295
- return base
296
- }
297
264
}
298
265
299
266
/**
300
- * Returns the encoding of the transaction. For typed transaction, this is the raw Buffer.
301
- * In Transaction, this is a Buffer array.
267
+ * Returns the serialized encoding of the transaction.
302
268
*/
303
269
serialize ( ) : Buffer {
304
- return < Buffer > this . raw ( )
270
+ const base = this . raw ( )
271
+ return Buffer . concat ( [ Buffer . from ( '01' , 'hex' ) , rlp . encode ( base as any ) ] )
305
272
}
306
273
307
274
/**
308
275
* Computes a sha3-256 hash of the serialized unsigned tx, which is used to sign the transaction.
309
276
*/
310
277
getMessageToSign ( ) {
311
- const base = this . raw ( true ) . slice ( 0 , 8 )
312
- return keccak256 ( Buffer . concat ( [ Buffer . from ( '01' , 'hex' ) , rlp . encode ( base ) ] ) )
278
+ const base = this . raw ( ) . slice ( 0 , 8 )
279
+ return keccak256 ( Buffer . concat ( [ Buffer . from ( '01' , 'hex' ) , rlp . encode ( base as any ) ] ) )
313
280
}
314
281
315
282
/**
@@ -342,7 +309,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
342
309
343
310
// All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
344
311
// TODO: verify if this is the case for EIP-2930
345
- if ( this . common . gteHardfork ( 'homestead' ) && this . s && this . s . gt ( N_DIV_2 ) ) {
312
+ if ( this . common . gteHardfork ( 'homestead' ) && this . s ? .gt ( N_DIV_2 ) ) {
346
313
throw new Error (
347
314
'Invalid Signature: s-values greater than secp256k1n/2 are considered invalid'
348
315
)
0 commit comments