Ethers.js Research

· 3 min read

Recently, an open source project needed to implement a web3.js library, so I researched the leading web3.js library - ethers.js.

https://static.1991421.cn/2025/2025-08-06-140751.jpeg

Code Size

The source code is written in TypeScript, with actual code size around 90k lines.

https://static.1991421.cn/2025/2025-08-05-145322.jpeg

Cross-Platform Support

ethers.js outputs both ES and CJS compliant code, therefore supporting both Browser and server-side Node.js.

- lib.commonjs
- lib.esm

Features

  • Wallet management (Wallet)
  • Ethereum node interaction (Provider)
  • Smart contract interaction (Contract)
  • Transaction processing
  • Utility and helper functions
  • Support for multiple Ethereum-compatible chains

Architecture

ethers.js is a modular library that includes the following modules:

1. ABI Module (src.ts/abi)

Responsibility: Handle Ethereum Application Binary Interface (ABI)

  • abi-coder.ts: AbiCoder class, responsible for ABI encoding/decoding
  • interface.ts: Interface class, parse contract ABI, handle function calls and events
  • fragments.ts: Define ABI fragment types (functions, events, errors, etc.)
  • coders/: Various data type encoders/decoders

2. Address Module (src.ts/address)

Responsibility: Handle Ethereum address-related operations

  • Address validation and formatting
  • Contract address calculation (CREATE, CREATE2)
  • ICAP address format support

4. Contract Module (src.ts/contract)

Responsibility: Smart contract interaction

  • contract.ts: Contract class, provides contract method call interface
  • factory.ts: Contract deployment factory
  • wrappers.ts: Event log and transaction receipt wrapper classes

5. Cryptography Module (src.ts/crypto)

Responsibility: Cryptographic primitives and key management

  • signing-key.ts: SigningKey class, elliptic curve key operations
  • signature.ts: Digital signature processing
  • keccak.ts, sha2.ts etc.: Hash functions
  • scrypt.ts, pbkdf2.ts: Key derivation functions

6. Hash Module (src.ts/hash)

Responsibility: Various hashing and encoding operations

  • id.ts: String to hash conversion
  • message.ts: Message signing and verification
  • typed-data.ts: EIP-712 typed data signing
  • namehash.ts: ENS domain name hashing

7. Provider Module (src.ts/providers)

Responsibility: Blockchain network connection and data retrieval

  • abstract-provider.ts: AbstractProvider base class
  • provider-jsonrpc.ts: JSON-RPC provider
  • Various third-party blockchain node service providers (Infura, Alchemy, etc.)

8. Transaction Module (src.ts/transaction)

Responsibility: Transaction creation, signing, and serialization

  • transaction.ts: Transaction class
  • address.ts: Address-related utility functions

9. Utilities Module (src.ts/utils)

Responsibility: Common utility functions

  • data.ts: Byte data processing
  • errors.ts: Error handling
  • fetch.ts: HTTP request wrapper
  • maths.ts: Mathematical operations
  • utf8.ts: UTF-8 encoding/decoding

10. Wallet Module (src.ts/wallet)

Responsibility: Wallet management and key storage

  • wallet.ts: Wallet class
  • hdwallet.ts: HD wallet implementation
  • json-keystore.ts: Keystore format support
  • mnemonic.ts: Mnemonic phrase processing

11. Wordlists Module (src.ts/wordlists)

Responsibility: Mnemonic wordlists

  • Support for multilingual mnemonics

This modular design gives ethers.js clear separation of concerns, with each module focusing on specific functional areas, making it easy to maintain and extend.

Relationship between ethers.js and MetaMask wallet

  • Standardized interfaces: Both follow Ethereum standards like EIP-1193
  • Separation of responsibilities:
    • MetaMask: Private key management, user authorization, transaction signing
    • ethers.js: Data formatting, ABI encoding/decoding, network communication
  • Interchangeability: ethers.js can also work with other wallets (like WalletConnect)

MetaMask provides low-level primitive interfaces without any abstraction. While ethers.js provides higher-level abstractions and wrappers, making it more convenient for developers to interact with the Ethereum network.

https://static.1991421.cn/2025/2025-08-06-171143.jpeg

Additional Concept Introduction

Smart Contracts

This name is indeed somewhat exaggerated, but it emphasizes several key characteristics:

  • Automated execution
  • Reduced human intervention
  • Transparent and verifiable
  • Reduced trust costs
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover