@ethersproject/transactions provides comprehensive utilities for encoding, decoding, and manipulating Ethereum transactions. It supports legacy transactions as well as modern EIP-155, EIP-2930 (access lists), and EIP-1559 (fee market) transaction types with full RLP encoding/decoding capabilities.
npm install @ethersproject/transactionsimport {
computeAddress,
recoverAddress,
serialize,
parse,
accessListify,
TransactionTypes,
type Transaction,
type UnsignedTransaction,
type AccessList,
type AccessListish
} from "@ethersproject/transactions";
// External dependencies for type definitions
import type { BytesLike, SignatureLike } from "@ethersproject/bytes";
import type { BigNumber, BigNumberish } from "@ethersproject/bignumber";For CommonJS:
const {
computeAddress,
recoverAddress,
serialize,
parse,
accessListify,
TransactionTypes
} = require("@ethersproject/transactions");import { serialize, parse, computeAddress, UnsignedTransaction } from "@ethersproject/transactions";
// Create an unsigned transaction
const unsignedTx: UnsignedTransaction = {
to: "0x742d35Cc6634C0532925a3b8D87DF16B5e0f037e",
value: "1000000000000000000", // 1 ETH in wei
gasLimit: "21000",
gasPrice: "20000000000", // 20 gwei
nonce: 0,
chainId: 1, // Ethereum mainnet
data: "0x"
};
// Serialize the transaction
const serializedTx = serialize(unsignedTx);
console.log("Serialized:", serializedTx);
// Parse the transaction back
const parsedTx = parse(serializedTx);
console.log("Parsed:", parsedTx);
// Compute address from private key
const privateKey = "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
const address = computeAddress(privateKey);
console.log("Address:", address);The package is organized around several key components:
Computes Ethereum addresses from public or private keys using the standard Ethereum address derivation algorithm.
/**
* Computes Ethereum address from public key or private key
* @param key - Public key or private key as bytes or hex string
* @returns Ethereum address as hex string
*/
function computeAddress(key: BytesLike | string): string;Recovers Ethereum addresses from message digests and signatures, commonly used for signature verification.
/**
* Recovers Ethereum address from message digest and signature
* @param digest - Message digest/hash as bytes
* @param signature - Signature object or bytes
* @returns Recovered Ethereum address as hex string
*/
function recoverAddress(digest: BytesLike, signature: SignatureLike): string;Serializes unsigned or signed transactions into RLP-encoded hex strings for network transmission.
/**
* Serializes transaction to RLP-encoded hex string
* @param transaction - Unsigned transaction object
* @param signature - Optional signature for signed transaction
* @returns RLP-encoded transaction as hex string
*/
function serialize(transaction: UnsignedTransaction, signature?: SignatureLike): string;Parses RLP-encoded transaction data back into structured Transaction objects with full type information.
/**
* Parses RLP-encoded transaction data into Transaction object
* @param rawTransaction - Raw transaction bytes or hex string
* @returns Parsed Transaction object with all fields
*/
function parse(rawTransaction: BytesLike): Transaction;Converts various access list formats into the standard AccessList format required by EIP-2930 and EIP-1559 transactions.
/**
* Converts various access list formats to standard AccessList format
* @param value - Access list in any supported format
* @returns Normalized AccessList array
*/
function accessListify(value: AccessListish): AccessList;enum TransactionTypes {
legacy = 0, // Legacy transactions (pre-EIP-155)
eip2930 = 1, // EIP-2930 access list transactions
eip1559 = 2 // EIP-1559 fee market transactions
}// Standard access list format
type AccessList = Array<{
address: string;
storageKeys: Array<string>;
}>;
// Flexible input format for access lists
type AccessListish = AccessList |
Array<[string, Array<string>]> |
Record<string, Array<string>>;type UnsignedTransaction = {
to?: string; // Recipient address
nonce?: number; // Transaction nonce
gasLimit?: BigNumberish; // Gas limit
gasPrice?: BigNumberish; // Gas price (legacy transactions)
data?: BytesLike; // Transaction data
value?: BigNumberish; // Ether value to send
chainId?: number; // Chain ID for EIP-155
type?: number | null; // Transaction type (0, 1, or 2)
accessList?: AccessListish; // Access list (EIP-2930/1559)
maxPriorityFeePerGas?: BigNumberish; // Priority fee (EIP-1559)
maxFeePerGas?: BigNumberish; // Max fee (EIP-1559)
};interface Transaction {
hash?: string; // Transaction hash
to?: string; // Recipient address
from?: string; // Sender address
nonce: number; // Transaction nonce
gasLimit: BigNumber; // Gas limit
gasPrice?: BigNumber; // Gas price
data: string; // Transaction data
value: BigNumber; // Ether value
chainId: number; // Chain ID
r?: string; // Signature r component
s?: string; // Signature s component
v?: number; // Signature v component
type?: number | null; // Transaction type
accessList?: AccessList; // Access list
maxPriorityFeePerGas?: BigNumber; // Priority fee (EIP-1559)
maxFeePerGas?: BigNumber; // Max fee (EIP-1559)
}These types are imported from external @ethersproject packages and used throughout the API:
// From @ethersproject/bytes
type BytesLike = Bytes | string;
type Bytes = ArrayLike<number>;
type SignatureLike = {
r: string;
s?: string;
_vs?: string;
recoveryParam?: number;
v?: number;
} | BytesLike;
// From @ethersproject/bignumber
type BigNumberish = BigNumber | Bytes | bigint | string | number;
class BigNumber {
// BigNumber class implementation (from @ethersproject/bignumber)
// Used for precise arithmetic operations
}The package throws descriptive errors for invalid inputs:
Common error patterns:
import { serialize } from "@ethersproject/transactions";
try {
const result = serialize({
type: 999 // Invalid transaction type
});
} catch (error) {
console.error("Transaction serialization failed:", error.message);
}