/
eip2930Transaction.ts
399 lines (351 loc) · 12.2 KB
/
eip2930Transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { RLP } from '@ethereumjs/rlp'
import {
MAX_INTEGER,
arrToBufArr,
bigIntToHex,
bigIntToUnpaddedBuffer,
bufArrToArr,
bufferToBigInt,
ecrecover,
toBuffer,
validateNoLeadingZeroes,
} from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { BaseTransaction } from './baseTransaction'
import { AccessLists, checkMaxInitCodeSize } from './util'
import type {
AccessList,
AccessListBuffer,
AccessListEIP2930TxData,
AccessListEIP2930ValuesArray,
JsonTx,
TxOptions,
} from './types'
import type { Common } from '@ethereumjs/common'
const TRANSACTION_TYPE = 1
const TRANSACTION_TYPE_BUFFER = Buffer.from(TRANSACTION_TYPE.toString(16).padStart(2, '0'), 'hex')
/**
* Typed transaction with optional access lists
*
* - TransactionType: 1
* - EIP: [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)
*/
export class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2930Transaction> {
public readonly chainId: bigint
public readonly accessList: AccessListBuffer
public readonly AccessListJSON: AccessList
public readonly gasPrice: bigint
public readonly common: Common
/**
* The default HF if the tx type is active on that HF
* or the first greater HF where the tx is active.
*
* @hidden
*/
protected DEFAULT_HARDFORK = 'berlin'
/**
* Instantiate a transaction from a data dictionary.
*
* Format: { chainId, nonce, gasPrice, gasLimit, to, value, data, accessList,
* v, r, s }
*
* Notes:
* - `chainId` will be set automatically if not provided
* - All parameters are optional and have some basic default values
*/
public static fromTxData(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
return new AccessListEIP2930Transaction(txData, opts)
}
/**
* Instantiate a transaction from the serialized tx.
*
* Format: `0x01 || rlp([chainId, nonce, gasPrice, gasLimit, to, value, data, accessList,
* signatureYParity (v), signatureR (r), signatureS (s)])`
*/
public static fromSerializedTx(serialized: Buffer, opts: TxOptions = {}) {
if (!serialized.slice(0, 1).equals(TRANSACTION_TYPE_BUFFER)) {
throw new Error(
`Invalid serialized tx input: not an EIP-2930 transaction (wrong tx type, expected: ${TRANSACTION_TYPE}, received: ${serialized
.slice(0, 1)
.toString('hex')}`
)
}
const values = arrToBufArr(RLP.decode(Uint8Array.from(serialized.slice(1))))
if (!Array.isArray(values)) {
throw new Error('Invalid serialized tx input: must be array')
}
return AccessListEIP2930Transaction.fromValuesArray(values as any, opts)
}
/**
* Create a transaction from a values array.
*
* Format: `[chainId, nonce, gasPrice, gasLimit, to, value, data, accessList,
* signatureYParity (v), signatureR (r), signatureS (s)]`
*/
public static fromValuesArray(values: AccessListEIP2930ValuesArray, opts: TxOptions = {}) {
if (values.length !== 8 && values.length !== 11) {
throw new Error(
'Invalid EIP-2930 transaction. Only expecting 8 values (for unsigned tx) or 11 values (for signed tx).'
)
}
const [chainId, nonce, gasPrice, gasLimit, to, value, data, accessList, v, r, s] = values
this._validateNotArray({ chainId, v })
validateNoLeadingZeroes({ nonce, gasPrice, gasLimit, value, v, r, s })
const emptyAccessList: AccessList = []
return new AccessListEIP2930Transaction(
{
chainId: bufferToBigInt(chainId),
nonce,
gasPrice,
gasLimit,
to,
value,
data,
accessList: accessList ?? emptyAccessList,
v: v !== undefined ? bufferToBigInt(v) : undefined, // EIP2930 supports v's with value 0 (empty Buffer)
r,
s,
},
opts
)
}
/**
* This constructor takes the values, validates them, assigns them and freezes the object.
*
* It is not recommended to use this constructor directly. Instead use
* the static factory methods to assist in creating a Transaction object from
* varying data types.
*/
public constructor(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
super({ ...txData, type: TRANSACTION_TYPE }, opts)
const { chainId, accessList, gasPrice } = txData
this.common = this._getCommon(opts.common, chainId)
this.chainId = this.common.chainId()
// EIP-2718 check is done in Common
if (!this.common.isActivatedEIP(2930)) {
throw new Error('EIP-2930 not enabled on Common')
}
this.activeCapabilities = this.activeCapabilities.concat([2718, 2930])
// Populate the access list fields
const accessListData = AccessLists.getAccessListData(accessList ?? [])
this.accessList = accessListData.accessList
this.AccessListJSON = accessListData.AccessListJSON
// Verify the access list format.
AccessLists.verifyAccessList(this.accessList)
this.gasPrice = bufferToBigInt(toBuffer(gasPrice === '' ? '0x' : gasPrice))
this._validateCannotExceedMaxInteger({
gasPrice: this.gasPrice,
})
BaseTransaction._validateNotArray(txData)
if (this.gasPrice * this.gasLimit > MAX_INTEGER) {
const msg = this._errorMsg('gasLimit * gasPrice cannot exceed MAX_INTEGER')
throw new Error(msg)
}
this._validateYParity()
this._validateHighS()
if (this.common.isActivatedEIP(3860)) {
checkMaxInitCodeSize(this.common, this.data.length)
}
const freeze = opts?.freeze ?? true
if (freeze) {
Object.freeze(this)
}
}
/**
* The amount of gas paid for the data in this tx
*/
getDataFee(): bigint {
if (this.cache.dataFee && this.cache.dataFee.hardfork === this.common.hardfork()) {
return this.cache.dataFee.value
}
let cost = super.getDataFee()
cost += BigInt(AccessLists.getDataFeeEIP2930(this.accessList, this.common))
if (Object.isFrozen(this)) {
this.cache.dataFee = {
value: cost,
hardfork: this.common.hardfork(),
}
}
return cost
}
/**
* The up front amount that an account must have for this transaction to be valid
*/
getUpfrontCost(): bigint {
return this.gasLimit * this.gasPrice + this.value
}
/**
* Returns a Buffer Array of the raw Buffers of the EIP-2930 transaction, in order.
*
* Format: `[chainId, nonce, gasPrice, gasLimit, to, value, data, accessList,
* signatureYParity (v), signatureR (r), signatureS (s)]`
*
* Use {@link AccessListEIP2930Transaction.serialize} to add a transaction to a block
* with {@link Block.fromValuesArray}.
*
* For an unsigned tx this method uses the empty Buffer values for the
* signature parameters `v`, `r` and `s` for encoding. For an EIP-155 compliant
* representation for external signing use {@link AccessListEIP2930Transaction.getMessageToSign}.
*/
raw(): AccessListEIP2930ValuesArray {
return [
bigIntToUnpaddedBuffer(this.chainId),
bigIntToUnpaddedBuffer(this.nonce),
bigIntToUnpaddedBuffer(this.gasPrice),
bigIntToUnpaddedBuffer(this.gasLimit),
this.to !== undefined ? this.to.buf : Buffer.from([]),
bigIntToUnpaddedBuffer(this.value),
this.data,
this.accessList,
this.v !== undefined ? bigIntToUnpaddedBuffer(this.v) : Buffer.from([]),
this.r !== undefined ? bigIntToUnpaddedBuffer(this.r) : Buffer.from([]),
this.s !== undefined ? bigIntToUnpaddedBuffer(this.s) : Buffer.from([]),
]
}
/**
* Returns the serialized encoding of the EIP-2930 transaction.
*
* Format: `0x01 || rlp([chainId, nonce, gasPrice, gasLimit, to, value, data, accessList,
* signatureYParity (v), signatureR (r), signatureS (s)])`
*
* Note that in contrast to the legacy tx serialization format this is not
* valid RLP any more due to the raw tx type preceding and concatenated to
* the RLP encoding of the values.
*/
serialize(): Buffer {
const base = this.raw()
return Buffer.concat([
TRANSACTION_TYPE_BUFFER,
Buffer.from(RLP.encode(bufArrToArr(base as Buffer[]))),
])
}
/**
* Returns the serialized unsigned tx (hashed or raw), which can be used
* to sign the transaction (e.g. for sending to a hardware wallet).
*
* Note: in contrast to the legacy tx the raw message format is already
* serialized and doesn't need to be RLP encoded any more.
*
* ```javascript
* const serializedMessage = tx.getMessageToSign(false) // use this for the HW wallet input
* ```
*
* @param hashMessage - Return hashed message if set to true (default: true)
*/
getMessageToSign(hashMessage = true): Buffer {
const base = this.raw().slice(0, 8)
const message = Buffer.concat([
TRANSACTION_TYPE_BUFFER,
Buffer.from(RLP.encode(bufArrToArr(base as Buffer[]))),
])
if (hashMessage) {
return Buffer.from(keccak256(message))
} else {
return message
}
}
/**
* Computes a sha3-256 hash of the serialized tx.
*
* This method can only be used for signed txs (it throws otherwise).
* Use {@link AccessListEIP2930Transaction.getMessageToSign} to get a tx hash for the purpose of signing.
*/
public hash(): Buffer {
if (!this.isSigned()) {
const msg = this._errorMsg('Cannot call hash method if transaction is not signed')
throw new Error(msg)
}
if (Object.isFrozen(this)) {
if (!this.cache.hash) {
this.cache.hash = Buffer.from(keccak256(this.serialize()))
}
return this.cache.hash
}
return Buffer.from(keccak256(this.serialize()))
}
/**
* Computes a sha3-256 hash which can be used to verify the signature
*/
public getMessageToVerifySignature(): Buffer {
return this.getMessageToSign()
}
/**
* Returns the public key of the sender
*/
public getSenderPublicKey(): Buffer {
if (!this.isSigned()) {
const msg = this._errorMsg('Cannot call this method if transaction is not signed')
throw new Error(msg)
}
const msgHash = this.getMessageToVerifySignature()
const { v, r, s } = this
this._validateHighS()
try {
return ecrecover(
msgHash,
v! + BigInt(27), // Recover the 27 which was stripped from ecsign
bigIntToUnpaddedBuffer(r!),
bigIntToUnpaddedBuffer(s!)
)
} catch (e: any) {
const msg = this._errorMsg('Invalid Signature')
throw new Error(msg)
}
}
_processSignature(v: bigint, r: Buffer, s: Buffer) {
const opts = { ...this.txOptions, common: this.common }
return AccessListEIP2930Transaction.fromTxData(
{
chainId: this.chainId,
nonce: this.nonce,
gasPrice: this.gasPrice,
gasLimit: this.gasLimit,
to: this.to,
value: this.value,
data: this.data,
accessList: this.accessList,
v: v - BigInt(27), // This looks extremely hacky: @ethereumjs/util actually adds 27 to the value, the recovery bit is either 0 or 1.
r: bufferToBigInt(r),
s: bufferToBigInt(s),
},
opts
)
}
/**
* Returns an object with the JSON representation of the transaction
*/
toJSON(): JsonTx {
const accessListJSON = AccessLists.getAccessListJSON(this.accessList)
return {
chainId: bigIntToHex(this.chainId),
nonce: bigIntToHex(this.nonce),
gasPrice: bigIntToHex(this.gasPrice),
gasLimit: bigIntToHex(this.gasLimit),
to: this.to !== undefined ? this.to.toString() : undefined,
value: bigIntToHex(this.value),
data: '0x' + this.data.toString('hex'),
accessList: accessListJSON,
v: this.v !== undefined ? bigIntToHex(this.v) : undefined,
r: this.r !== undefined ? bigIntToHex(this.r) : undefined,
s: this.s !== undefined ? bigIntToHex(this.s) : undefined,
}
}
/**
* Return a compact error string representation of the object
*/
public errorStr() {
let errorStr = this._getSharedErrorPostfix()
// Keep ? for this.accessList since this otherwise causes Hardhat E2E tests to fail
errorStr += ` gasPrice=${this.gasPrice} accessListCount=${this.accessList?.length ?? 0}`
return errorStr
}
/**
* Internal helper function to create an annotated error message
*
* @param msg Base error message
* @hidden
*/
protected _errorMsg(msg: string) {
return `${msg} (${this.errorStr()})`
}
}