| Implements schema and functions related to Ethereum’s transaction. | | —————————————————————— |
Note: this README
reflects the state of the library from v3.0.0
onwards. See README
from the standalone repository for an introduction on the last preceding release.
To obtain the latest version, simply require the project using npm
:
npm install @ethereumjs/tx
This library now fully supports EIP-4844
blob transactions (see usage instructions below) starting with v5.2.0
.
For blob transactions and other KZG related proof functionality (e.g. for EVM precompiles) KZG has to be manually installed and initialized in the common
instance to be used in instantiating blob transactions.
Note: starting with the v5.3
release of this library and associated releases of upstream EthereumJS libraries the old c-kzg
centered recommended setup has been replaced by using our own WASM build of the c-kzg
library which has been released as a separate package kzg-wasm on npm.
This new setup is now both browser compatible 🎉 and the official KZG setup file has been integrated by default and the new setup is now the default recommended setup to be used.
As a first step add the kzg-wasm
package as a dependency to your package.json
file and install the library.
Initialization can then be done like the following:
// ./examples/initKzg.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
const main = async () => {
const kzg = new microEthKZG(trustedSetup)
// Instantiate `common`
const common = new Common({
chain: Mainnet,
hardfork: Hardfork.Cancun,
customCrypto: { kzg },
})
console.log(common.customCrypto.kzg) // should output the KZG API as an object
}
void main()
Note: Manual addition is necessary because we did not want to bundle our libraries with WASM code by default, since some projects are then prevented from using our libraries.
To instantiate a tx it is not recommended to use the constructor directly. Instead each tx type comes with the following set of static constructor methods which helps on instantiation depending on the input data format:
public static fromTxData(txData: TxData, opts: TxOptions = {})
: instantiate from a data dictionarypublic static fromSerializedTx(serialized: Uint8Array, opts: TxOptions = {})
: instantiate from a serialized txpublic static fromValuesArray(values: Uint8Array[], opts: TxOptions = {})
: instantiate from a values arraySee one of the code examples on the tx types below on how to use.
All types of transaction objects are frozen with Object.freeze()
which gives you enhanced security and consistency properties when working with the instantiated object. This behavior can be modified using the freeze
option in the constructor if needed.
The LegacyTransaction
constructor receives a parameter of an @ethereumjs/common
object that lets you specify the chain and hardfork to be used. If there is no Common
provided the chain ID provided as a parameter on typed tx or the chain ID derived from the v
value on signed EIP-155 conforming legacy txs will be taken (introduced in v3.2.1
). In other cases the chain defaults to mainnet
.
Base default HF (determined by Common
): Hardfork.Shanghai
Hardforks adding features and/or tx types:
Hardfork | Introduced | Description |
---|---|---|
spuriousDragon |
v2.0.0 |
EIP-155 replay protection (disable by setting HF pre-spuriousDragon ) |
istanbul |
v2.1.1 |
Support for reduced non-zero call blob gas prices (EIP-2028) |
muirGlacier |
v2.1.2 |
- |
berlin |
v3.1.0 |
EIP-2718 Typed Transactions, Optional Access Lists Tx Type EIP-2930 |
london |
v3.2.0 |
EIP-1559 Transactions |
cancun |
v5.0.0 |
EIP-4844 Transactions |
This library by default uses JavaScript implementations for the basic standard crypto primitives like hashing or signature verification. See @ethereumjs/common
README for instructions on how to replace with e.g. a more performant WASM implementation by using a shared common
instance.
This library supports the following transaction types (EIP-2718):
BlobEIP4844Transaction
(EIP-4844, proto-danksharding)FeeMarketEIP1559Transaction
(EIP-1559, gas fee market)EOACodeEIP7702Transaction
(experimental) (EIP-7702, EOA code delegation)AccessListEIP2930Transaction
(EIP-2930, optional access lists)BlobEIP4844Transaction
(EIP-4844, blob transactions)LegacyTransaction
, the Ethereum standard tx up to berlin
, now referred to as legacy txs with the introduction of tx typesBlobEIP4844Transaction
cancun
3
This library supports the blob transaction type introduced with EIP-4844.
Note: This functionality needs a manual KZG library installation and global initialization, see KZG Setup for instructions.
See the following code snipped for an example on how to instantiate (using the c-kzg
module for our KZG dependency).
// ./examples/blobTx.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createBlob4844Tx } from '@ethereumjs/tx'
import { bytesToHex } from '@ethereumjs/util'
import { trustedSetup } from '@paulmillr/trusted-setups/fast.js'
import { KZG as microEthKZG } from 'micro-eth-signer/kzg'
const main = async () => {
const kzg = new microEthKZG(trustedSetup)
const common = new Common({
chain: Mainnet,
hardfork: Hardfork.Shanghai,
eips: [4844],
customCrypto: { kzg },
})
const txData = {
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
gasLimit: '0x02625a00',
maxPriorityFeePerGas: '0x01',
maxFeePerGas: '0xff',
maxFeePerDataGas: '0xfff',
nonce: '0x00',
to: '0xcccccccccccccccccccccccccccccccccccccccc',
value: '0x0186a0',
v: '0x01',
r: '0xafb6e247b1c490e284053c87ab5f6b59e219d51f743f7a4d83e400782bc7e4b9',
s: '0x479a268e0e0acd4de3f1e28e4fac2a6b32a4195e8dfa9d19147abe8807aa6f64',
chainId: '0x01',
accessList: [],
type: '0x05',
blobsData: ['abcd'],
}
const tx = createBlob4844Tx(txData, { common })
console.log(bytesToHex(tx.hash())) //0x3c3e7c5e09c250d2200bcc3530f4a9088d7e3fb4ea3f4fccfd09f535a3539e84
}
void main()
Note that versionedHashes
and kzgCommitments
have a real length of 32 bytes, blobs
have a real length of 4096
bytes and values are trimmed here for brevity.
Alternatively, you can pass a blobsData
property with an array of strings corresponding to a set of blobs and the fromTxData
constructor will derive the corresponding blobs
, versionedHashes
, kzgCommitments
, and kzgProofs
for you.
See the Blob Transaction Tests for examples of usage in instantiating, serializing, and deserializing these transactions.
FeeMarketEIP1559Transaction
london
2
This is the recommended tx type starting with the activation of the london
HF, see the following code snipped for an example on how to instantiate:
// ./examples/londonTx.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createFeeMarket1559Tx } from '@ethereumjs/tx'
import { bytesToHex } from '@ethereumjs/util'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const txData = {
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
gasLimit: '0x02625a00',
maxPriorityFeePerGas: '0x01',
maxFeePerGas: '0xff',
nonce: '0x00',
to: '0xcccccccccccccccccccccccccccccccccccccccc',
value: '0x0186a0',
v: '0x01',
r: '0xafb6e247b1c490e284053c87ab5f6b59e219d51f743f7a4d83e400782bc7e4b9',
s: '0x479a268e0e0acd4de3f1e28e4fac2a6b32a4195e8dfa9d19147abe8807aa6f64',
chainId: '0x01',
accessList: [],
type: '0x02',
}
const tx = createFeeMarket1559Tx(txData, { common })
console.log(bytesToHex(tx.hash())) // 0x6f9ef69ccb1de1aea64e511efd6542541008ced321887937c95b03779358ec8a
EOACodeEIP7702Transaction
prague
(or per EIP setting)4
This library supports a non-final version of EIP-7702 starting with v5.4.0
. This tx type allows to run code in the context of an EOA and therefore extend the functionality which can be “reached” from respectively integrated into the scope of an otherwise limited EOA account.
The following is a simple example how to use an EOACodeEIP7702Transaction
with one authorization list item:
// ./examples/EOACodeTx.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createEOACode7702Tx } from '@ethereumjs/tx'
import { type PrefixedHexString, createAddressFromPrivateKey, randomBytes } from '@ethereumjs/util'
const ones32 = `0x${'01'.repeat(32)}` as PrefixedHexString
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, eips: [7702] })
const tx = createEOACode7702Tx(
{
authorizationList: [
{
chainId: '0x2',
address: `0x${'20'.repeat(20)}`,
nonce: '0x1',
yParity: '0x1',
r: ones32,
s: ones32,
},
],
to: createAddressFromPrivateKey(randomBytes(32)),
},
{ common },
)
console.log(
`EIP-7702 EOA code tx created with ${tx.authorizationList.length} authorization list item(s).`,
)
Note: Things move fast with EIP-7702
and the currently released implementation is based on this commit and therefore already outdated. An up-to-date version will be released along our breaking release round planned for early September 2024.
AccessListEIP2930Transaction
berlin
1
This transaction type has been introduced along the berlin
HF. See the following code snipped for an example on how to instantiate:
// ./examples/accessListTx.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createAccessList2930Tx } from '@ethereumjs/tx'
import { bytesToHex } from '@ethereumjs/util'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin })
const txData = {
data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
gasLimit: '0x02625a00',
gasPrice: '0x01',
nonce: '0x00',
to: '0xcccccccccccccccccccccccccccccccccccccccc',
value: '0x0186a0',
v: '0x01',
r: '0xafb6e247b1c490e284053c87ab5f6b59e219d51f743f7a4d83e400782bc7e4b9',
s: '0x479a268e0e0acd4de3f1e28e4fac2a6b32a4195e8dfa9d19147abe8807aa6f64',
chainId: '0x01',
accessList: [
{
address: '0x0000000000000000000000000000000000000101',
storageKeys: [
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x00000000000000000000000000000000000000000000000000000000000060a7',
],
},
],
type: '0x01',
}
const tx = createAccessList2930Tx(txData, { common })
console.log(bytesToHex(tx.hash())) // 0x9150cdebad74e88b038e6c6b964d99af705f9c0883d7f0bbc0f3e072358f5b1d
For generating access lists from tx data based on a certain network state there is a reportAccessList
option
on the Vm.runTx()
method of the @ethereumjs/vm
TypeScript
VM implementation.
LegacyTransaction
chainstart
(with modifications along the road, see HF section below)0
(internal)Legacy transaction are still valid transaction within Ethereum mainnet
but will likely be deprecated at some point.
See this example script or the following code example on how to use.
// ./examples/legacyTx.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createLegacyTx } from '@ethereumjs/tx'
import { bytesToHex } from '@ethereumjs/util'
import { hexToBytes } from 'ethereum-cryptography/utils'
const txParams = {
nonce: '0x0',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
}
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul })
const tx = createLegacyTx(txParams, { common })
const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109')
const signedTx = tx.sign(privateKey)
const _serializedTx = signedTx.serialize()
console.log(bytesToHex(signedTx.hash())) // 0x894b72d87f8333fccd29d1b3aca39af69d97a6bc281e7e7a3a60640690a3cd2b
If you only know on runtime which tx type will be used within your code or if you want to keep your code transparent to tx types, this library comes with a TransactionFactory
for your convenience which can be used as follows:
// ./examples/txFactory.ts
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { Capability, createTx } from '@ethereumjs/tx'
import type { EIP1559CompatibleTx } from '@ethereumjs/tx'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const txData = { type: 2, maxFeePerGas: BigInt(20) } // Creates an EIP-1559 compatible transaction
const tx = createTx(txData, { common })
if (tx.supports(Capability.EIP1559FeeMarket)) {
console.log(
`The max fee per gas for this transaction is ${(tx as EIP1559CompatibleTx).maxFeePerGas}`,
)
}
The correct tx type class for instantiation will then be chosen on runtime based on the data provided as an input.
TransactionFactory
supports the following static constructor methods:
public static fromTxData(txData: TxData | AccessListEIP2930TxData, txOptions: TxOptions = {}): TypedTransaction
public static fromSerializedData(data: Uint8Array, txOptions: TxOptions = {}): TypedTransaction
public static fromBlockBodyData(data: Uint8Array | Uint8Array[], txOptions: TxOptions = {})
public static async fromJsonRpcProvider(provider: string | EthersProvider, txHash: string, txOptions?: TxOptions)
This library has been tested to work with various L2 networks (v3.3.0
+). All predefined supported custom chains introduced with Common
v2.4.0
or higher are supported, the following is a simple example to send a tx to the xDai chain:
// ./examples/l2tx.ts
import { Mainnet, createCustomCommon } from '@ethereumjs/common'
import { createLegacyTx } from '@ethereumjs/tx'
import { bytesToHex, createAddressFromString, hexToBytes } from '@ethereumjs/util'
const pk = hexToBytes('0x076247989df60a82f6e86e58104368676096f84e60972282ee00d4673a2bc9b9')
// xDai chain ID
const common = createCustomCommon({ chainId: 100 }, Mainnet)
const to = createAddressFromString('0x256e8f0ba532ad83a0debde7501669511a41a1f3')
const txData = {
nonce: 0,
gasPrice: 1000000000,
gasLimit: 21000,
to,
value: 1,
}
const tx = createLegacyTx(txData, { common })
const signedTx = tx.sign(pk)
console.log(bytesToHex(signedTx.hash())) // 0xbf98f6f8700812ed6f2314275070256e11945fa48afd80fb301265f6a41a2dc2
The following L2 networks have been tested to work with @ethereumjs/tx
, see usage examples as well as some notes on peculiarities in the issues linked below:
L2 Network | Common name | Issue |
---|---|---|
Arbitrum Rinkeby Testnet | CustomChain.ArbitrumRinkebyTestnet |
#1290 |
Polygon Mainnet | CustomChain.PolygonMainnet |
#1289 |
Polygon Mumbai Testnet | CustomChain.PolygonMumbai |
#1289 |
xDai Chain | Common.xDaiChain |
#1323 |
Optimistic Kovan | Common.OptimisticKovan |
#1554 |
Optimistic Ethereum | Common.OptimisticEthereum |
#1554 |
Note: For Optimistic Kovan and Optimistic Ethereum, the London hardfork has not been implemented so transactions submitted with a baseFee
will revert.
The London hardfork is targeted to implement on Optimism in Q1.22.
For a non-predefined custom chain it is also possible to just provide a chain ID as well as other parameters to Common
:
const common = Common.custom({ chainId: 1234 })
With the breaking release round in Summer 2023 we have added hybrid ESM/CJS builds for all our libraries (see section below) and have eliminated many of the caveats which had previously prevented a frictionless browser usage.
It is now easily possible to run a browser build of one of the EthereumJS libraries within a modern browser using the provided ESM build. For a setup example see ./examples/browser.html.
To sign a tx with a hardware or external wallet use tx.getMessageToSign()
to return an EIP-155 compliant unsigned tx.
A legacy transaction will return a Buffer list of the values, and a Typed Transaction (EIP-2718) will return the serialized output.
Here is an example of signing txs with @ledgerhq/hw-app-eth
as of v6.5.0
:
import { Chain, Common } from '@ethereumjs/common'
import { LegacyTransaction, FeeMarketEIP1559Transaction } from '@ethereumjs/tx'
import { bytesToHex } from '@ethereumjs/util'
import { RLP } from '@ethereumjs/rlp'
import Eth from '@ledgerhq/hw-app-eth'
const eth = new Eth(transport)
const common = new Common({ chain: Chain.Sepolia })
let txData: any = { value: 1 }
let tx: LegacyTransaction | FeeMarketEIP1559Transaction
let unsignedTx: Uint8Array[] | Uint8Array
let signedTx: typeof tx
const bip32Path = "44'/60'/0'/0/0"
const run = async () => {
// Signing a legacy tx
tx = LegacyTransaction.fromTxData(txData, { common })
tx = tx.getMessageToSign()
// ledger signTransaction API expects it to be serialized
let { v, r, s } = await eth.signTransaction(bip32Path, RLP.encode(tx))
tx.addSignature(v, r, s, true)
let from = tx.getSenderAddress().toString()
console.log(`signedTx: ${bytesToHex(tx.serialize())}\nfrom: ${from}`)
// Signing a 1559 tx
txData = { value: 1 }
tx = FeeMarketEIP1559Transaction.fromTxData(txData, { common })
tx = tx.getMessageToSign()
;({ v, r, s } = await eth.signTransaction(bip32Path, unsignedTx)) // this syntax is: object destructuring - assignment without declaration
tx.addSignature(v, r, s)
from = tx.getSenderAddress().toString()
console.log(`signedTx: ${bytesToHex(tx.serialize())}\nfrom: ${from}`)
}
run()
Generated TypeDoc API Documentation
With the breaking releases from Summer 2023 we have started to ship our libraries with both CommonJS (cjs
folder) and ESM builds (esm
folder), see package.json
for the detailed setup.
If you use an ES6-style import
in your code files from the ESM build will be used:
import { EthereumJSClass } from '@ethereumjs/[PACKAGE_NAME]'
If you use Node.js specific require
, the CJS build will be used:
const { EthereumJSClass } = require('@ethereumjs/[PACKAGE_NAME]')
Using ESM will give you additional advantages over CJS beyond browser usage like static code analysis / Tree Shaking which CJS can not provide.
With the breaking releases from Summer 2023 we have removed all Node.js specific Buffer
usages from our libraries and replace these with Uint8Array representations, which are available both in Node.js and the browser (Buffer
is a subclass of Uint8Array
).
We have converted existing Buffer conversion methods to Uint8Array conversion methods in the @ethereumjs/util bytes
module, see the respective README section for guidance.
Starting with v4 the usage of BN.js for big numbers has been removed from the library and replaced with the usage of the native JS BigInt data type (introduced in ES2020
).
Please note that number-related API signatures have changed along with this version update and the minimal build target has been updated to ES2020
.
See our organizational documentation for an introduction to EthereumJS
as well as information on current standards and best practices. If you want to join for work or carry out improvements on the libraries, please review our contribution guidelines first.