or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdl1-messaging-bridging.mdl1-rollup-management.mdl2-messaging-bridging.mdl2-system-contracts.mdstandards-interfaces.mdtypescript-utilities.mdutility-libraries.md
tile.json

utility-libraries.mddocs/

Utility Libraries

Comprehensive library contracts providing shared functionality for encoding, cryptography, data structures, address resolution, and Ethereum standards used throughout the Optimism ecosystem.

Bridge Libraries

Lib_CrossDomainUtils

Contract Path: libraries/bridge/Lib_CrossDomainUtils.sol

Utilities for cross-domain message encoding and validation.

library Lib_CrossDomainUtils {
  function encodeXDomainCalldata(
    address _target,
    address _sender,
    bytes memory _message,
    uint256 _messageNonce
  ) internal pure returns (bytes memory);
  
  function hashCrossDomainMessage(
    address _target,
    address _sender,
    bytes memory _message,
    uint256 _messageNonce
  ) internal pure returns (bytes32);
}

encodeXDomainCalldata

Encodes cross-domain message data for transmission.

Parameters:

  • _target (address): Target contract address
  • _sender (address): Sender address
  • _message (bytes): Message data
  • _messageNonce (uint256): Message nonce

Returns: bytes - Encoded calldata

hashCrossDomainMessage

Creates a hash of a cross-domain message for verification.

Parameters:

  • _target (address): Target contract address
  • _sender (address): Sender address
  • _message (bytes): Message data
  • _messageNonce (uint256): Message nonce

Returns: bytes32 - Message hash

CrossDomainEnabled

Contract Path: libraries/bridge/CrossDomainEnabled.sol

Base contract for cross-domain functionality with authentication modifiers.

abstract contract CrossDomainEnabled {
  modifier onlyFromCrossDomainAccount(address _sourceDomainAccount);
  modifier onlyCrossDomainMessage();
  
  function getCrossDomainMessenger() internal view returns (address);
}

onlyFromCrossDomainAccount

Modifier that restricts function calls to messages from a specific cross-domain account.

Parameters:

  • _sourceDomainAccount (address): Expected sender address from the other domain

onlyCrossDomainMessage

Modifier that restricts function calls to cross-domain messages only.

ICrossDomainMessenger

Contract Path: libraries/bridge/ICrossDomainMessenger.sol

Interface defining the cross-domain messenger contract API.

interface ICrossDomainMessenger {
  function sendMessage(
    address _target,
    bytes calldata _message,
    uint32 _gasLimit
  ) external;
  
  function xDomainMessageSender() external view returns (address);
}

Codec Libraries

Lib_OVMCodec

Contract Path: libraries/codec/Lib_OVMCodec.sol

Encoding and decoding utilities for OVM data structures and transaction formats.

library Lib_OVMCodec {
  struct QueueElement {
    bytes32 transactionHash;
    uint256 timestamp;
    uint256 blockNumber;
  }
  
  struct ChainBatchHeader {
    uint256 batchIndex;
    bytes32 batchRoot;
    uint256 batchSize;
    uint256 prevTotalElements;
    bytes extraData;
  }
  
  struct ChainInclusionProof {
    uint256 index;
    bytes32[] siblings;
  }
  
  function encodeTransaction(
    uint256 _nonce,
    uint256 _gasPrice,
    uint256 _gasLimit,
    address _to,
    uint256 _value,
    bytes memory _data,
    uint256 _chainId
  ) internal pure returns (bytes memory);
  
  function hashTransaction(
    uint256 _nonce,
    uint256 _gasPrice,
    uint256 _gasLimit,
    address _to,
    uint256 _value,
    bytes memory _data,
    uint256 _chainId
  ) internal pure returns (bytes32);
  
  function decodeEIP155Transaction(bytes memory _transaction)
    internal
    pure
    returns (
      uint256 _nonce,
      uint256 _gasPrice,
      uint256 _gasLimit,
      address _to,
      uint256 _value,
      bytes memory _data,
      uint256 _chainId,
      uint8 _v,
      bytes32 _r,
      bytes32 _s
    );
}

encodeTransaction

Encodes transaction parameters into bytes.

Parameters:

  • _nonce (uint256): Transaction nonce
  • _gasPrice (uint256): Gas price
  • _gasLimit (uint256): Gas limit
  • _to (address): Recipient address
  • _value (uint256): ETH value
  • _data (bytes): Transaction data
  • _chainId (uint256): Chain ID

Returns: bytes - Encoded transaction

decodeEIP155Transaction

Decodes an EIP-155 transaction from bytes.

Parameters:

  • _transaction (bytes): Encoded transaction data

Returns: All transaction components including signature values

Utility Libraries

Lib_MerkleTree

Contract Path: libraries/utils/Lib_MerkleTree.sol

Merkle tree operations for proof verification.

library Lib_MerkleTree {
  function verify(
    bytes32 _root,
    bytes32 _leaf,
    uint256 _index,
    bytes32[] memory _siblings,
    uint256 _totalLeaves
  ) internal pure returns (bool);
  
  function getMerkleRoot(bytes32[] memory _elements)
    internal
    pure
    returns (bytes32);
}

verify

Verifies a Merkle proof against a root.

Parameters:

  • _root (bytes32): Merkle root to verify against
  • _leaf (bytes32): Leaf value to verify
  • _index (uint256): Index of the leaf
  • _siblings (bytes32[]): Sibling hashes for the proof path
  • _totalLeaves (uint256): Total number of leaves in the tree

Returns: bool - True if proof is valid

getMerkleRoot

Calculates the Merkle root for a set of elements.

Parameters:

  • _elements (bytes32[]): Elements to calculate root for

Returns: bytes32 - Calculated Merkle root

Lib_BytesUtils

Contract Path: libraries/utils/Lib_BytesUtils.sol

Byte array manipulation utilities.

library Lib_BytesUtils {
  function concat(bytes memory _preBytes, bytes memory _postBytes)
    internal
    pure
    returns (bytes memory);
    
  function slice(
    bytes memory _bytes,
    uint256 _start,
    uint256 _length
  ) internal pure returns (bytes memory);
  
  function toBytes32(bytes memory _bytes) internal pure returns (bytes32);
  
  function toAddress(bytes memory _bytes) internal pure returns (address);
  
  function toUint256(bytes memory _bytes) internal pure returns (uint256);
}

concat

Concatenates two byte arrays.

Parameters:

  • _preBytes (bytes): First byte array
  • _postBytes (bytes): Second byte array

Returns: bytes - Concatenated result

slice

Extracts a slice from a byte array.

Parameters:

  • _bytes (bytes): Source byte array
  • _start (uint256): Starting index
  • _length (uint256): Length of slice

Returns: bytes - Extracted slice

toBytes32

Converts bytes to bytes32.

Parameters:

  • _bytes (bytes): Bytes to convert

Returns: bytes32 - Converted value

Lib_Bytes32Utils

Contract Path: libraries/utils/Lib_Bytes32Utils.sol

Utilities for bytes32 manipulation.

library Lib_Bytes32Utils {
  function toBytes(bytes32 _bytes32) internal pure returns (bytes memory);
  
  function toString(bytes32 _bytes32) internal pure returns (string memory);
  
  function removeLeadingZeros(bytes32 _bytes32) internal pure returns (bytes memory);
}

Lib_Buffer

Contract Path: libraries/utils/Lib_Buffer.sol

Buffer operations for building byte arrays.

library Lib_Buffer {
  struct Buffer {
    bytes buf;
    uint256 capacity;
  }
  
  function init(Buffer memory _buf, uint256 _capacity) internal pure;
  
  function append(Buffer memory _buf, bytes memory _data) internal pure;
  
  function getBytes(Buffer memory _buf) internal pure returns (bytes memory);
}

RLP Libraries

Lib_RLPReader

Contract Path: libraries/rlp/Lib_RLPReader.sol

RLP (Recursive Length Prefix) decoding utilities.

library Lib_RLPReader {
  struct RLPItem {
    uint256 length;
    uint256 ptr;
  }
  
  function readList(bytes memory _in) internal pure returns (RLPItem[] memory);
  
  function readBytes(bytes memory _in) internal pure returns (bytes memory);
  
  function readString(bytes memory _in) internal pure returns (string memory);
  
  function readUint256(bytes memory _in) internal pure returns (uint256);
  
  function readAddress(bytes memory _in) internal pure returns (address);
}

readList

Decodes an RLP-encoded list.

Parameters:

  • _in (bytes): RLP-encoded data

Returns: RLPItem[] - Array of decoded items

readBytes

Decodes RLP-encoded bytes.

Parameters:

  • _in (bytes): RLP-encoded data

Returns: bytes - Decoded bytes

Lib_RLPWriter

Contract Path: libraries/rlp/Lib_RLPWriter.sol

RLP encoding utilities.

library Lib_RLPWriter {
  function writeBytes(bytes memory _in) internal pure returns (bytes memory);
  
  function writeList(bytes[] memory _in) internal pure returns (bytes memory);
  
  function writeString(string memory _in) internal pure returns (bytes memory);
  
  function writeAddress(address _in) internal pure returns (bytes memory);
  
  function writeUint256(uint256 _in) internal pure returns (bytes memory);
}

writeBytes

Encodes bytes as RLP.

Parameters:

  • _in (bytes): Bytes to encode

Returns: bytes - RLP-encoded data

writeList

Encodes a list as RLP.

Parameters:

  • _in (bytes[]): Array of elements to encode

Returns: bytes - RLP-encoded list

Trie Libraries

Lib_MerkleTrie

Contract Path: libraries/trie/Lib_MerkleTrie.sol

Merkle Patricia Trie operations for Ethereum state verification.

library Lib_MerkleTrie {
  function verifyInclusionProof(
    bytes memory _key,
    bytes memory _value,
    bytes memory _proof,
    bytes32 _root
  ) internal pure returns (bool);
  
  function get(
    bytes memory _key,
    bytes memory _proof,
    bytes32 _root
  ) internal pure returns (bool, bytes memory);
  
  function proveAccountInState(
    address _account,
    bytes memory _accountStateProof,
    bytes32 _stateRoot
  ) internal pure returns (bool, bytes memory);
}

verifyInclusionProof

Verifies that a key-value pair is included in the trie.

Parameters:

  • _key (bytes): Key to verify
  • _value (bytes): Expected value
  • _proof (bytes): Merkle proof
  • _root (bytes32): Trie root

Returns: bool - True if inclusion is proven

get

Gets a value from the trie with proof verification.

Parameters:

  • _key (bytes): Key to look up
  • _proof (bytes): Merkle proof
  • _root (bytes32): Trie root

Returns: (bool, bytes) - Success flag and value

Lib_SecureMerkleTrie

Contract Path: libraries/trie/Lib_SecureMerkleTrie.sol

Secure Merkle trie with keccak256 key hashing.

library Lib_SecureMerkleTrie {
  function verifyInclusionProof(
    bytes memory _key,
    bytes memory _value,
    bytes memory _proof,
    bytes32 _root
  ) internal pure returns (bool);
  
  function get(
    bytes memory _key,
    bytes memory _proof,
    bytes32 _root
  ) internal pure returns (bool, bytes memory);
}

Functions are similar to Lib_MerkleTrie but use keccak256 hashing of keys for security.

Resolver Libraries

Lib_AddressManager

Contract Path: libraries/resolver/Lib_AddressManager.sol

Central registry for contract addresses used throughout the system.

contract Lib_AddressManager {
  function setAddress(string memory _name, address _address) external;
  
  function getAddress(string memory _name) external view returns (address);
  
  function owner() external view returns (address);
  function setOwner(address _newOwner) external;
}

setAddress

Sets the address for a named contract (owner only).

Parameters:

  • _name (string): Name of the contract
  • _address (address): Address of the contract

getAddress

Gets the address for a named contract.

Parameters:

  • _name (string): Name of the contract

Returns: address - Contract address

Usage:

// Set contract address
addressManager.setAddress("OVM_L1CrossDomainMessenger", messengerAddress);

// Get contract address
address messenger = addressManager.getAddress("OVM_L1CrossDomainMessenger");

Events

event AddressSet(
  string indexed _name,
  address _newAddress,
  address _oldAddress
);

event OwnershipTransferred(
  address indexed _previousOwner,
  address indexed _newOwner
);

Lib_AddressResolver

Contract Path: libraries/resolver/Lib_AddressResolver.sol

Base contract for address resolution functionality.

abstract contract Lib_AddressResolver {
  function resolve(string memory _name) internal view returns (address);
  
  function requireResolve(string memory _name) internal view returns (address);
}

resolve

Resolves a contract name to its address.

Parameters:

  • _name (string): Contract name to resolve

Returns: address - Resolved address (zero if not found)

requireResolve

Resolves a contract name to its address with requirement that it exists.

Parameters:

  • _name (string): Contract name to resolve

Returns: address - Resolved address

Reverts: If address is not found

Lib_ResolvedDelegateProxy

Contract Path: libraries/resolver/Lib_ResolvedDelegateProxy.sol

Proxy contract that resolves its implementation address dynamically.

contract Lib_ResolvedDelegateProxy {
  constructor(
    address _libAddressManager,
    string memory _implementationName
  );
  
  function setImplementationName(string memory _implementationName) external;
  
  function getImplementationName() external view returns (string memory);
}

Constants Libraries

Lib_DefaultValues

Contract Path: libraries/constants/Lib_DefaultValues.sol

System default values and constants.

library Lib_DefaultValues {
  address internal constant DEFAULT_XDOMAIN_SENDER = 0x000000000000000000000000000000000000dEaD;
  uint256 internal constant DEFAULT_CHAINID = 1;
  uint256 internal constant L2_GAS_DISCOUNT_DIVISOR = 32;
  uint256 internal constant ENQUEUE_GAS_COST = 60000;
  uint256 internal constant MAX_ROLLUP_TX_SIZE = 50000;
  uint256 internal constant MAX_GAS_PER_QUEUE_PER_EPOCH = 250000000;
  uint256 internal constant SECONDS_PER_EPOCH = 0;
}

Lib_PredeployAddresses

Contract Path: libraries/constants/Lib_PredeployAddresses.sol

Predeploy contract addresses and utilities.

library Lib_PredeployAddresses {
  address internal constant L2_TO_L1_MESSAGE_PASSER = 0x4200000000000000000000000000000000000000;
  address internal constant L2_CROSS_DOMAIN_MESSENGER = 0x4200000000000000000000000000000000000007;
  address internal constant L2_STANDARD_BRIDGE = 0x4200000000000000000000000000000000000010;
  address internal constant OVM_ETH = 0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000;
  address internal constant L2_STANDARD_TOKEN_FACTORY = 0x4200000000000000000000000000000000000012;
  // ... additional predeploy addresses
}

Usage Examples

Cross-Domain Message Encoding

contract MessageEncoder {
  using Lib_CrossDomainUtils for bytes;
  
  function encodeMessage(
    address target,
    address sender,
    bytes memory data,
    uint256 nonce
  ) external pure returns (bytes memory) {
    return Lib_CrossDomainUtils.encodeXDomainCalldata(
      target,
      sender,
      data,
      nonce
    );
  }
}

Merkle Proof Verification

contract ProofVerifier {
  using Lib_MerkleTree for bytes32[];
  
  function verifyInclusion(
    bytes32 root,
    bytes32 leaf,
    uint256 index,
    bytes32[] memory proof,
    uint256 totalLeaves
  ) external pure returns (bool) {
    return Lib_MerkleTree.verify(
      root,
      leaf,
      index,
      proof,
      totalLeaves
    );
  }
}

Address Resolution

contract AddressUser is Lib_AddressResolver {
  function getMessenger() external view returns (address) {
    return resolve("OVM_L1CrossDomainMessenger");
  }
  
  function sendMessage(bytes memory data) external {
    address messenger = requireResolve("OVM_L1CrossDomainMessenger");
    IL1CrossDomainMessenger(messenger).sendMessage(
      target,
      data,
      gasLimit
    );
  }
}

RLP Encoding/Decoding

contract RLPExample {
  using Lib_RLPWriter for bytes[];
  using Lib_RLPReader for bytes;
  
  function encodeData(
    address addr,
    uint256 value,
    bytes memory data
  ) external pure returns (bytes memory) {
    bytes[] memory items = new bytes[](3);
    items[0] = Lib_RLPWriter.writeAddress(addr);
    items[1] = Lib_RLPWriter.writeUint256(value);
    items[2] = Lib_RLPWriter.writeBytes(data);
    
    return Lib_RLPWriter.writeList(items);
  }
}