UNPKG

@ethereumjs/tx

Version:
638 lines (478 loc) 28.4 kB
# @ethereumjs/tx `v10` [![NPM Package][tx-npm-badge]][tx-npm-link] [![GitHub Issues][tx-issues-badge]][tx-issues-link] [![Actions Status][tx-actions-badge]][tx-actions-link] [![Code Coverage][tx-coverage-badge]][tx-coverage-link] [![Discord][discord-badge]][discord-link] | Implements schema and functions for the different Ethereum transaction types | | ---------------------------------------------------------------------------- | - 🦄 All tx types up to **Osaka** - 🌴 Tree-shakeable API - 👷🏼 Controlled dependency set (1 external + `@Noble` crypto) - 🎼 Unified tx type API - 📲 New type for **EIP-7702** account abstraction - 🔮 `EIP-7594` PeerDAS Blob Transactions - 🛵 190KB bundle size (all tx types) (47KB gzipped) - 🏄🏾‍♂️ WASM-free default + Fully browser ready ## Table of Contents - [Installation](#installation) - [Getting Started](#getting-started) - [Chain and Hardfork Support](#chain-and-hardfork-support) - [Transaction Types](#transaction-types) - [Gas Fee Market Transactions (EIP-1559)](#gas-fee-market-transactions-eip-1559) - [Access List Transactions (EIP-2930)](#access-list-transactions-eip-2930) - [Blob Transactions (EIP-4844 / EIP-7594)](#blob-transactions-eip-4844--eip-7594) - [EOA Code Transaction (EIP-7702)](#eoa-code-transaction-eip-7702) - [Legacy Transactions](#legacy-transactions) - [Amsterdam transaction validation (EIP-7976, EIP-7981)](#amsterdam-transaction-validation-eip-7976-eip-7981) - [Transaction Factory](#transaction-factory) - [KZG Setup](#kzg-setup) - [Sending a Transaction](#sending-a-transaction) - [Browser](#browser) - [Hardware Wallets](#hardware-wallets) - [API](#api) - [EthereumJS](#ethereumjs) - [License](#license) ## Installation To obtain the latest version, simply require the project using `npm`: ```shell npm install @ethereumjs/tx ``` ## Getting Started ### Static Constructor Methods 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 help instantiate depending on the input data format: - `public static fromTxData(txData: TxData, opts: TxOptions = {})`: instantiate from a data dictionary - `public static fromSerializedTx(serialized: Uint8Array, opts: TxOptions = {})`: instantiate from a serialized tx - `public static fromValuesArray(values: Uint8Array[], opts: TxOptions = {})`: instantiate from a values array See 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. ### WASM Crypto Support This library by default uses JavaScript implementations for the basic standard crypto primitives like hashing or signature verification. See `@ethereumjs/common` [README](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/common) for instructions on how to replace with e.g. a more performant WASM implementation by using a shared `common` instance. ## Chain and Hardfork Support To use a chain other than the default Mainnet chain, or a different hardfork than the default [`@ethereumjs/common`](https://github.com/ethereumjs/ethereumjs-monorepo/blob/master/packages/common) hardfork (`Hardfork.Prague`), provide a `common` object in the constructor of the tx. Base default HF (determined by `Common`): `Hardfork.Prague` 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](https://eips.ethereum.org/EIPS/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 | | `prague` | `v10.0.0` | `EIP-7702` Transactions | | `amsterdam` | `v10.x` | `EIP-7976` / `EIP-7981` floor pricing, bundled with other Amsterdam EIPs (experimental) | ## Transaction Types ### Table of Contents This library supports the following transaction types ([EIP-2718](https://eips.ethereum.org/EIPS/eip-2718)): - [Gas Fee Market Transactions (EIP-1559)](#gas-fee-market-transactions-eip-1559) - [Access List Transactions (EIP-2930)](#access-list-transactions-eip-2930) - [Blob Transactions (EIP-4844)](#blob-transactions-eip-4844) - [EOA Code Transaction (EIP-7702)](#eoa-code-transaction-eip-7702) - [Legacy Transactions](#legacy-transactions) (original Ethereum txs) - [Amsterdam transaction validation (EIP-7976, EIP-7981)](#amsterdam-transaction-validation-eip-7976-eip-7981) ### Gas Fee Market Transactions (EIP-1559) - Class: `FeeMarketEIP1559Tx` - EIP: [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) - Activation: `london` - Type: `2` This is the recommended tx type starting with the activation of the `london` HF, see the following code snippet for an example on how to instantiate: ```ts // ./examples/londonTx.ts import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import type { FeeMarketEIP1559TxData } from '@ethereumjs/tx' import { createFeeMarket1559Tx } from '@ethereumjs/tx' import { bytesToHex } from '@ethereumjs/util' const common = new Common({ chain: Mainnet, hardfork: Hardfork.London }) const txData: FeeMarketEIP1559TxData = { 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 ``` ### Access List Transactions (EIP-2930) - Class: `AccessListEIP2930Tx` - EIP: [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) - Activation: `berlin` - Type: `1` This transaction type has been introduced along the `berlin` HF. See the following code snippet for an example on how to instantiate: ```ts // ./examples/accessListTx.ts import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import type { AccessList2930TxData } from '@ethereumjs/tx' import { createAccessList2930Tx } from '@ethereumjs/tx' import { bytesToHex } from '@ethereumjs/util' const common = new Common({ chain: Mainnet, hardfork: Hardfork.Berlin }) const txData: AccessList2930TxData = { 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. ### Blob Transactions (EIP-4844 / EIP-7594) - Class: `BlobEIP4844Tx` - EIPs: [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844), [EIP-7594](https://eips.ethereum.org/EIPS/eip-7594) - Activation: `cancun` (EIP-4844), `osaka` (EIP-7594) - Type: `3` #### Introduction This library supports the blob transaction type introduced with [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844). Additionally it is able to process blobs in the "PeerDAS way" - introduced with [EIP-7594](https://eips.ethereum.org/EIPS/eip-7594) along the `osaka` hardfork and generate cell proofs instead of blob proofs. **Note:** This functionality needs a manual KZG library installation and global initialization, see [KZG Setup](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/tx/README.md#kzg-setup) for instructions. #### Example See the following code snippet for an example on how to create a blob transaction, one for EIP-4844 only and one taking EIP-7594 into the mix: ```ts // ./examples/blobTx.ts import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import type { BlobEIP4844TxData } from '@ethereumjs/tx' import { createBlob4844Tx } from '@ethereumjs/tx' import { bytesToHex, getBlobs, randomBytes } from '@ethereumjs/util' import { trustedSetup } from '@paulmillr/trusted-setups/fast-peerdas.js' import { KZG as microEthKZG } from 'micro-eth-signer/kzg.js' const main = async () => { const kzg = new microEthKZG(trustedSetup) // EIP-4844 only const common4844 = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, customCrypto: { kzg }, }) // EIP-4844 and EIP-7594 const common4844and7594 = new Common({ chain: Mainnet, hardfork: Hardfork.Osaka, customCrypto: { kzg }, }) const setups = [ { title: 'Blob transaction (EIP-4844 only)', common: common4844, proofAmountComment: 'one proof per blob', }, { title: 'Blob transaction (EIP-4844 + EIP-7594)', common: common4844and7594, proofAmountComment: '128 cells per blob + one proof per cell -> NUM_BLOBS * 128 proofs', }, ] for (const setup of setups) { console.log(`\n${setup.title}:`) console.log('---------------------------------------') const blobsData = ['blob 1', 'blob 2', 'blob 3'] console.log(`Blobs (Data) : "${blobsData.join('", "')}"`) // Final format, filled with a lot of 0s, added marker const blobs = getBlobs(blobsData) console.log('Generating tx...') const txData: BlobEIP4844TxData = { data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', gasLimit: 16_000_000n, maxPriorityFeePerGas: '0x01', maxFeePerGas: '0xff', maxFeePerBlobGas: '0xfff', nonce: '0x00', to: '0xcccccccccccccccccccccccccccccccccccccccc', value: '0x0186a0', v: '0x01', r: '0xafb6e247b1c490e284053c87ab5f6b59e219d51f743f7a4d83e400782bc7e4b9', s: '0x479a268e0e0acd4de3f1e28e4fac2a6b32a4195e8dfa9d19147abe8807aa6f64', chainId: '0x01', accessList: [], type: '0x05', blobs, } const tx = createBlob4844Tx(txData, { common: setup.common }) console.log(`Tx hash : ${bytesToHex(tx.hash())}`) console.log(`Num blobs : ${tx.numBlobs()}`) console.log(`Blob versioned hashes : ${tx.blobVersionedHashes.join(', ')}`) console.log(`KZG commitments : ${tx.kzgCommitments!.join(', ')}`) console.log(`First KZG (cell) proof: ${tx.kzgProofs![0]}`) console.log(`Num KZG (cell) proofs : ${tx.kzgProofs!.length} (${setup.proofAmountComment})`) } // To send a transaction via RPC, you can something like this: // const rawTx = tx.sign(privateKeyBytes).serializeNetworkWrapper() // myRPCClient.request('eth_sendRawTransaction', [rawTx]) // submits a transaction via RPC // // Also see ./sendRawSepoliaTx.ts example } void main() ``` **Note:** `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. You can either pass in blobs as the initial `blobsData` (the data you want to store in the blob) - and the final `blobs` format (filled with a lot of 0s, added marker) will be derived for you - or you can pass in the final `blobs` format directly as bytes. `versionedHashes`, `kzgCommitments` and `kzgProofs` are either derived or taken from the values passed in. The `kzgProofs` field is used for both blob proofs (EIP-4844) and cell proofs (EIP-7594). Note that the amount of proofs increases by a factor of 128 when EIP-7594 is activated, since proofs are then computed per cell instead of per blob (128 cells per blob). For manually deriving commitments, proofs and versioned hashes, there are dedicated helpers available in the [@ethereumjs/util](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/util) package. #### Serialization Blob transactions can be serialized in two ways. 1) `tx.serialize()` - the standard serialization returns an RLP-encoded `Uint8Array` that conforms to the transaction as represented after it is included in a block 2) `tx.serializeNetworkWrapper()` - this serialization format includes the `blobs` in the encoded data and is the format specified for transactions that are being submitted to/gossipped around the mempool. **If you are constructing a transaction to submit via JSON-RPC, use this format.** See the [Send Raw Sepolia Tx](./examples/sendRawSepoliaTx.ts) example for a detailed example on how to send a blob transaction via JSON-RPC. See the [Blob Transaction Tests](./test/eip4844.spec.ts) for additional examples of usage in instantiating, serializing, and deserializing these transactions. ### EOA Code Transaction (EIP-7702) - Class: `EOACodeEIP7702Tx` - EIP: [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) - Activation: `prague` - Type: `4` This tx type lets you run code in the context of an EOA, extending the functionality available to an otherwise limited account. The following is a simple example how to use an `EOACodeEIP7702Tx` with one authorization list item: ```ts // ./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).`, ) ``` ### Legacy Transactions - Class: `LegacyTx` - Activation: `chainstart` (with modifications along the road, see HF section below) - Type: `0` (internal) Legacy transactions are still valid transactions within Ethereum `mainnet` but will likely be deprecated at some point. See this [example script](./examples/transactions.ts) or the following code example on how to use. ```ts // ./examples/legacyTx.ts import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import type { LegacyTxData } from '@ethereumjs/tx' import { createLegacyTx } from '@ethereumjs/tx' import { bytesToHex, hexToBytes } from '@ethereumjs/util' const txData: LegacyTxData = { nonce: '0x0', gasPrice: '0x09184e72a000', gasLimit: '0x2710', to: '0x0000000000000000000000000000000000000000', value: '0x00', data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', } const common = new Common({ chain: Mainnet, hardfork: Hardfork.Istanbul }) const tx = createLegacyTx(txData, { common }) const privateKey = hexToBytes('0xe331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109') const signedTx = tx.sign(privateKey) const _serializedTx = signedTx.serialize() console.log(bytesToHex(signedTx.hash())) // 0x894b72d87f8333fccd29d1b3aca39af69d97a6bc281e7e7a3a60640690a3cd2b ``` ### Amsterdam transaction validation (EIP-7976, EIP-7981) See the [canonical Amsterdam overview](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/vm#amsterdam-hardfork-experimental) in `@ethereumjs/vm` for release ↔ spec tracking. On `Hardfork.Amsterdam` (experimental), two EIPs adjust the minimum gas a transaction must pay before execution: **[EIP-7976](https://eips.ethereum.org/EIPS/eip-7976) — calldata floor:** under EIP-7623, calldata is priced in **tokens** (1 per zero byte, 4 per non-zero byte pre-7976). EIP-7976 raises the floor to a uniform **4 tokens per byte** for all calldata bytes. The floor cost is `txGas + totalCostFloorPerToken × tokens` and is enforced in `getValidationErrors()` / intrinsic gas calculation for all tx types. **[EIP-7981](https://eips.ethereum.org/EIPS/eip-7981) — access-list floor:** for typed txs with an access list (types `1`, `2`, `3`, `4`), an additional floor charge applies to the raw access-list bytes: **20 bytes per address + 32 bytes per storage key**, each counted at 4 tokens per byte (`totalCostFloorPerToken × accessListBytes × 4`). Both floors feed into EIP-8037 block-level regular-gas accounting in the VM: `RunTxResult.txRegularGas = max(raw_regular_gas, calldata_floor)`. See [@ethereumjs/vm EIP-8037 docs](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/vm#eip-8037-state-creation-gas-cost-increase-amsterdam). **Example:** Amsterdam txs in tests typically need `baseFeePerGas: 1n` on the block header and a sufficiently high `gasPrice` / `maxFeePerGas` on legacy/1559 txs to satisfy the 1559 base-fee check alongside the new floors. ## Transaction Factory If you only know at 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: ```ts // ./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 at runtime based on the data provided as an input. `TransactionFactory` supports the following static constructor methods: - `public static fromTxData(txData: TxData | AccessListEIP2930TxData, txOptions: TxOptions = {})` - `public static fromSerializedData(data: Uint8Array, txOptions: TxOptions = {})` - `public static fromBlockBodyData(data: Uint8Array | Uint8Array[], txOptions: TxOptions = {})` - `public static async fromJsonRpcProvider(provider: string | EthersProvider, txHash: string, txOptions?: TxOptions)` ## KZG Setup This library fully supports `EIP-4844` blob transactions. 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. As a first step add the [micro-eth-signer](https://github.com/paulmillr/micro-eth-signer) package for KZG and [@paulmillr/trusted-setups](https://github.com/paulmillr/trusted-setups) for the trusted setup data as dependencies to your `package.json` file and install the libraries. Then initialization can be done like the following: ```ts // ./examples/initKzg.ts import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { trustedSetup } from '@paulmillr/trusted-setups/fast-peerdas.js' import { KZG as microEthKZG } from 'micro-eth-signer/kzg.js' 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: We did not want to directly bundle because bundle sizes are large due to the large trusted setup inclusion (especially for the mainnet trusted setup). ## Sending a Transaction ### L2 Support This library has been tested to work with various L2 networks. To set an associated chainID, use the `createCustomCommon()` constructor from our `Common` library. The following is a simple example to send a tx to the xDai chain: ```ts // ./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 ``` ## Browser We provide hybrid ESM/CJS builds for all our libraries. With the v10 breaking release round from Spring 2025, all libraries are "pure-JS" by default and we have eliminated all hard-wired WASM code. Additionally we have substantially lowered the bundle sizes, reduced the number of dependencies, and cut out all usages of Node.js-specific primitives (like the Node.js event emitter). It is 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](./examples/browser.html). ## Hardware Wallets ### Ledger To sign a tx with a hardware or external wallet use `tx.getMessageToSign()` to return an [EIP-155](https://eips.ethereum.org/EIPS/eip-155) compliant unsigned tx. A legacy transaction will return a Buffer list of the values, and a Typed Transaction ([EIP-2718](https://eips.ethereum.org/EIPS/eip-2718)) will return the serialized output. Here is an example of signing txs with `@ledgerhq/hw-app-eth` with `v6.45.4` and `@ledgerhq/hw-transport-node-hid` with `v6.29.5`: ```ts // examples/ledgerSigner.mts import { Common, Sepolia } from '@ethereumjs/common' import { RLP } from '@ethereumjs/rlp' import { type FeeMarketEIP1559TxData, type LegacyTxData, createFeeMarket1559Tx, createLegacyTx, } from '@ethereumjs/tx' import { bytesToHex } from '@ethereumjs/util' import Eth from '@ledgerhq/hw-app-eth' import TransportNodeHid from '@ledgerhq/hw-transport-node-hid' const transport = await TransportNodeHid.default.open() const eth = new Eth.default(transport) const common = new Common({ chain: Sepolia }) // Signing with the first key of the derivation path const bip32Path = "44'/60'/0'/0/0" const legacyTxData: LegacyTxData = { nonce: '0x0', gasPrice: '0x09184e72a000', gasLimit: '0x2710', to: '0x0000000000000000000000000000000000000000', value: '0x00', data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', } const eip1559TxData: FeeMarketEIP1559TxData = { data: '0x1a8451e600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', gasLimit: '0x02625a00', maxPriorityFeePerGas: '0x01', maxFeePerGas: '0xff', nonce: '0x00', to: '0xcccccccccccccccccccccccccccccccccccccccc', value: '0x0186a0', accessList: [], type: '0x02', } const run = async () => { // Signing a legacy tx const tx1 = createLegacyTx(legacyTxData, { common }) const unsignedTx1 = tx1.getMessageToSign() // Ledger signTransaction API expects it to be serialized // Ledger returns unprefixed hex strings without 0x for v, r, s values const { v, r, s } = await eth.signTransaction( bip32Path, bytesToHex(RLP.encode(unsignedTx1)).slice(2), null, ) const signedTx1 = tx1.addSignature(BigInt(`0x${v}`), BigInt(`0x${r}`), BigInt(`0x${s}`)) const from = signedTx1.getSenderAddress().toString() console.log(`signedTx: ${bytesToHex(tx1.serialize())}\nfrom: ${from}`) // Signing a 1559 tx const tx2 = createFeeMarket1559Tx(eip1559TxData, { common }) // Ledger returns unprefixed hex strings without 0x for v, r, s values const unsignedTx2 = tx2.getMessageToSign() const { v2, r2, s2 } = await eth.signTransaction( bip32Path, bytesToHex(unsignedTx2).slice(2), null, ) const signedTx2 = tx2.addSignature(BigInt(`0x${v2}`), BigInt(`0x${r2}`), BigInt(`0x${s2}`)) const from2 = signedTx2.getSenderAddress().toString() console.log(`signedTx: ${bytesToHex(tx2.serialize())}\nfrom: ${from2}`) } run() ``` ## API ### Docs Generated TypeDoc API [Documentation](./docs/README.md) ### Hybrid CJS/ESM Builds 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: ```ts import { EthereumJSClass } from '@ethereumjs/[PACKAGE_NAME]' ``` If you use Node.js specific `require`, the CJS build will be used: ```ts 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. ## EthereumJS The `EthereumJS` GitHub organization and its repositories are managed by members of the former Ethereum Foundation JavaScript team and the broader Ethereum community. If you want to join for work or carry out improvements on the libraries see the [developer docs](../../DEVELOPER.md) for an overview of current standards and tools and review our [code of conduct](../../CODE_OF_CONDUCT.md). ## License [MPL-2.0](<https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)>) [discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue [discord-link]: https://discord.gg/TNwARpR [tx-npm-badge]: https://img.shields.io/npm/v/@ethereumjs/tx.svg [tx-npm-link]: https://www.npmjs.com/package/@ethereumjs/tx [tx-issues-badge]: https://img.shields.io/github/issues/ethereumjs/ethereumjs-monorepo/package:%20tx?label=issues [tx-issues-link]: https://github.com/ethereumjs/ethereumjs-monorepo/issues?q=is%3Aopen+is%3Aissue+label%3A"package%3A+tx" [tx-actions-badge]: https://github.com/ethereumjs/ethereumjs-monorepo/workflows/Tx/badge.svg [tx-actions-link]: https://github.com/ethereumjs/ethereumjs-monorepo/actions?query=workflow%3A%22Tx%22 [tx-coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/branch/master/graph/badge.svg?flag=tx [tx-coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-monorepo/tree/master/packages/tx