Skip to content

de4ps/l2-inbox-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

L2 Inbox Generator

Generate batch inbox addresses for L2 chains using chainId.

Overview

This tool implements the batch inbox address generation algorithm used in Optimism-based L2 chains:

bytes32 hash = keccak256(abi.encodePacked(bytes32(uint256(chainId))));
address = prefix + hash[:19]

The batch inbox address is derived by:

  1. Converting the chainId to bytes32
  2. Computing the keccak256 hash
  3. Taking the first 19 bytes of the hash
  4. Prepending a version byte (default: 0x00)

Installation

Prerequisites

  • Node.js v14+ and npm

Setup

# Clone the repository
git clone <your-repo-url>
cd l2-inbox-generator

# Install dependencies
npm install

Usage

Basic Usage

# Generate address for chainId with default prefix (0x00)
node src/batch-inbox-address.js <chainId>

# Example
node src/batch-inbox-address.js 11235

Custom Prefix

# Generate address with custom prefix
node src/batch-inbox-address.js <chainId> <prefix>

# Example: use 0xff prefix for demo/testing
node src/batch-inbox-address.js 64322 0xff

Run Verification Tests

# Run without arguments to see test cases
node src/batch-inbox-address.js

Examples

Example 1: ChainId 11235

node src/batch-inbox-address.js 11235

Output:

ChainId: 11235
ChainId as bytes32: 0x0000000000000000000000000000000000000000000000000000000000002be3
Hash: 0x67d742c6fbc30dcb0c4479ba88615dd7d5a2d9dc884d9178f506a055b4a9ccd9
Hash[:19]: 0x67d742c6fbc30dcb0c4479ba88615dd7d5a2d9dc88
Batch Inbox Address: 0x0067d742c6fbc30dcb0c4479ba88615dd7d5a2d9dc88

Example 2: ChainId 64322 with 0xff prefix

node src/batch-inbox-address.js 64322 0xff

Output:

ChainId: 64322
ChainId as bytes32: 0x000000000000000000000000000000000000000000000000000000000000fb42
Hash: 0x3108624ddfd2f18f41fb62ccbbcdc3b1d6e39ffbcbcce942cf184d0c807b2b02
Hash[:19]: 0x3108624ddfd2f18f41fb62ccbbcdc3b1d6e39ffbcb
Batch Inbox Address: 0xff3108624ddfd2f18f41fb62ccbbcdc3b1d6e39ffbcb

Environment Variables

You can export the generated address as an environment variable:

# Generate and export (requires parsing the output)
export GS_BATCH_INBOX_ADDRESS=0xff3108624ddfd2f18f41fb62ccbbcdc3b1d6e39ffbcb

# Or use in a script
BATCH_INBOX=$(node src/batch-inbox-address.js 64322 0xff | grep "Batch Inbox Address:" | awk '{print $4}')
export GS_BATCH_INBOX_ADDRESS=$BATCH_INBOX

Verification

The script includes built-in verification for known test cases:

ChainId Expected Hash Expected Address (0x00 prefix)
11235 67d742c6... 0x0067d742c6fbc30dcb0c4479ba88615dd7d5a2d9dc88
64322 3108624d... 0x003108624ddfd2f18f41fb62ccbbcdc3b1d6e39ffbcb

Project Structure

l2-inbox-generator/
├── src/
│   └── batch-inbox-address.js    # Main script
├── node_modules/                  # Dependencies (gitignored)
├── .gitignore
├── package.json
├── package-lock.json
└── README.md

Troubleshooting

TypeError: Cannot read properties of undefined

The script supports both ethers.js v5 and v6. Your current installation uses ethers v6.15.0, which should work correctly.

If you encounter issues:

# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install

Module not found

Make sure you're in the project root and dependencies are installed:

npm install

Technical Details

Algorithm

  1. Input: ChainId (uint256)
  2. Step 1: Convert chainId to bytes32 format (left-padded with zeros)
  3. Step 2: Compute keccak256 hash of the bytes32 value
  4. Step 3: Extract first 19 bytes from the hash
  5. Step 4: Prepend version byte (typically 0x00 for production, 0xff for demo)
  6. Output: 20-byte Ethereum address

Prefix Meanings

  • 0x00 - Production/mainnet (default)
  • 0xff - Demo/testing environment

Ethers.js Compatibility

The script automatically detects and supports both ethers.js v5 and v6:

  • Current installation: ethers v6.15.0
  • Uses ethers.zeroPadValue, ethers.toBeHex, and ethers.keccak256 for v6
  • Falls back to ethers.utils.* methods for v5

Dependencies

  • ethers: ^6.15.0 - Ethereum library for cryptographic functions

License

ISC

About

Generate batch inbox addresses for Optimism-based L2 chains from chainId using keccak256

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published