CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ethersproject--transactions

Utilities for decoding and encoding Ethereum transaction for ethers.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

@ethersproject/transactions

@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.

Package Information

  • Package Name: @ethersproject/transactions
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @ethersproject/transactions

Core Imports

import { 
  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");

Basic Usage

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);

Architecture

The package is organized around several key components:

  • Transaction Types: Support for legacy (pre-EIP-155), EIP-155, EIP-2930 (access lists), and EIP-1559 (fee market) transactions
  • Serialization: RLP encoding of transactions for network transmission
  • Parsing: Decoding of raw transaction data back into structured objects
  • Address Utilities: Computing and recovering Ethereum addresses from keys and signatures
  • Access Lists: EIP-2930/EIP-1559 access list normalization and formatting

Capabilities

Address Computation

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;

Address Recovery

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;

Transaction Serialization

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;

Transaction Parsing

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;

Access List Normalization

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;

Types

Transaction Types Enum

enum TransactionTypes {
  legacy = 0,   // Legacy transactions (pre-EIP-155)
  eip2930 = 1,  // EIP-2930 access list transactions
  eip1559 = 2   // EIP-1559 fee market transactions
}

Access List Types

// 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>>;

Unsigned Transaction

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)
};

Complete Transaction

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)
}

External Dependency Types

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
}

Error Handling

The package throws descriptive errors for invalid inputs:

  • Invalid transaction fields: When required fields are missing or malformed
  • Invalid signatures: When signature components are invalid
  • Invalid access lists: When access list format is incorrect
  • Unsupported transaction types: When transaction type is not 0, 1, or 2
  • Invalid addresses: When addresses don't follow Ethereum format
  • Invalid chain IDs: When chain ID doesn't match signature

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);
}

docs

index.md

tile.json