-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tx: improve tx capability handling (#3010)
* tx: TxCapability interfaces and rename generic capability to legacy * tx: txTypeBytes helper * tx: add missing methods and props to tx interfaces * tx: refactor and improve serialize helper * tx: implement update serialize helper and txTypeBytes helper * tx: refactor getDataFee for use in legacyTransaction * tx: remove redundant prop from EIP4844CompatibleTxInterface * tx: shorten compatibletxInterface name * tx: typedTransaction -> EIP2718CompatibleTx * tx: remove implements interface * tx: add legacytxinterface and move accesslists props to eip2930 interface
- Loading branch information
1 parent
04aa4da
commit 67a20de
Showing
11 changed files
with
189 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RLP } from '@ethereumjs/rlp' | ||
import { concatBytes } from '@ethereumjs/util' | ||
import { keccak256 } from 'ethereum-cryptography/keccak.js' | ||
|
||
import { txTypeBytes } from '../util.js' | ||
|
||
import { errorMsg } from './legacy.js' | ||
|
||
import type { EIP2718CompatibleTx } from '../types' | ||
import type { Input } from '@ethereumjs/rlp' | ||
|
||
export function getHashedMessageToSign(tx: EIP2718CompatibleTx): Uint8Array { | ||
return keccak256(tx.getMessageToSign()) | ||
} | ||
|
||
export function serialize(tx: EIP2718CompatibleTx, base?: Input): Uint8Array { | ||
return concatBytes(txTypeBytes(tx.type), RLP.encode(base ?? tx.raw())) | ||
} | ||
|
||
export function validateYParity(tx: EIP2718CompatibleTx) { | ||
const { v } = tx | ||
if (v !== undefined && v !== BigInt(0) && v !== BigInt(1)) { | ||
const msg = errorMsg(tx, 'The y-parity of the transaction should either be 0 or 1') | ||
throw new Error(msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,12 @@ | ||
import { RLP } from '@ethereumjs/rlp' | ||
import { concatBytes, hexToBytes } from '@ethereumjs/util' | ||
import { keccak256 } from 'ethereum-cryptography/keccak.js' | ||
|
||
import { BaseTransaction } from '../baseTransaction.js' | ||
import { type TypedTransaction } from '../types.js' | ||
import { AccessLists } from '../util.js' | ||
|
||
import type { Transaction, TransactionType } from '../types.js' | ||
import * as Legacy from './legacy.js' | ||
|
||
type TypedTransactionEIP2930 = Exclude<TypedTransaction, Transaction[TransactionType.Legacy]> | ||
import type { EIP2930CompatibleTx } from '../types.js' | ||
|
||
/** | ||
* The amount of gas paid for the data in this tx | ||
*/ | ||
export function getDataFee(tx: TypedTransactionEIP2930): bigint { | ||
if (tx['cache'].dataFee && tx['cache'].dataFee.hardfork === tx.common.hardfork()) { | ||
return tx['cache'].dataFee.value | ||
} | ||
|
||
let cost = BaseTransaction.prototype.getDataFee.bind(tx)() | ||
cost += BigInt(AccessLists.getDataFeeEIP2930(tx.accessList, tx.common)) | ||
|
||
if (Object.isFrozen(tx)) { | ||
tx['cache'].dataFee = { | ||
value: cost, | ||
hardfork: tx.common.hardfork(), | ||
} | ||
} | ||
|
||
return cost | ||
} | ||
|
||
export function getHashedMessageToSign(tx: TypedTransactionEIP2930): Uint8Array { | ||
return keccak256(tx.getMessageToSign()) | ||
} | ||
|
||
export function serialize(tx: TypedTransactionEIP2930): Uint8Array { | ||
const base = tx.raw() | ||
const txTypeBytes = hexToBytes('0x' + tx.type.toString(16).padStart(2, '0')) | ||
return concatBytes(txTypeBytes, RLP.encode(base)) | ||
export function getDataFee(tx: EIP2930CompatibleTx): bigint { | ||
return Legacy.getDataFee(tx, BigInt(AccessLists.getDataFeeEIP2930(tx.accessList, tx.common))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.