Smart contracts for Optimism Layer 2 scaling solution including L1/L2 messaging, bridging, rollup management, and predeploy contracts
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive library contracts providing shared functionality for encoding, cryptography, data structures, address resolution, and Ethereum standards used throughout the Optimism ecosystem.
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);
}Encodes cross-domain message data for transmission.
Parameters:
_target (address): Target contract address_sender (address): Sender address_message (bytes): Message data_messageNonce (uint256): Message nonceReturns: bytes - Encoded calldata
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 nonceReturns: bytes32 - Message hash
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);
}Modifier that restricts function calls to messages from a specific cross-domain account.
Parameters:
_sourceDomainAccount (address): Expected sender address from the other domainModifier that restricts function calls to cross-domain messages only.
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);
}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
);
}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 IDReturns: bytes - Encoded transaction
Decodes an EIP-155 transaction from bytes.
Parameters:
_transaction (bytes): Encoded transaction dataReturns: All transaction components including signature values
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);
}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 treeReturns: bool - True if proof is valid
Calculates the Merkle root for a set of elements.
Parameters:
_elements (bytes32[]): Elements to calculate root forReturns: bytes32 - Calculated Merkle root
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);
}Concatenates two byte arrays.
Parameters:
_preBytes (bytes): First byte array_postBytes (bytes): Second byte arrayReturns: bytes - Concatenated result
Extracts a slice from a byte array.
Parameters:
_bytes (bytes): Source byte array_start (uint256): Starting index_length (uint256): Length of sliceReturns: bytes - Extracted slice
Converts bytes to bytes32.
Parameters:
_bytes (bytes): Bytes to convertReturns: bytes32 - Converted value
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);
}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);
}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);
}Decodes an RLP-encoded list.
Parameters:
_in (bytes): RLP-encoded dataReturns: RLPItem[] - Array of decoded items
Decodes RLP-encoded bytes.
Parameters:
_in (bytes): RLP-encoded dataReturns: bytes - Decoded bytes
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);
}Encodes bytes as RLP.
Parameters:
_in (bytes): Bytes to encodeReturns: bytes - RLP-encoded data
Encodes a list as RLP.
Parameters:
_in (bytes[]): Array of elements to encodeReturns: bytes - RLP-encoded list
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);
}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 rootReturns: bool - True if inclusion is proven
Gets a value from the trie with proof verification.
Parameters:
_key (bytes): Key to look up_proof (bytes): Merkle proof_root (bytes32): Trie rootReturns: (bool, bytes) - Success flag and value
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.
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;
}Sets the address for a named contract (owner only).
Parameters:
_name (string): Name of the contract_address (address): Address of the contractGets the address for a named contract.
Parameters:
_name (string): Name of the contractReturns: address - Contract address
Usage:
// Set contract address
addressManager.setAddress("OVM_L1CrossDomainMessenger", messengerAddress);
// Get contract address
address messenger = addressManager.getAddress("OVM_L1CrossDomainMessenger");event AddressSet(
string indexed _name,
address _newAddress,
address _oldAddress
);
event OwnershipTransferred(
address indexed _previousOwner,
address indexed _newOwner
);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);
}Resolves a contract name to its address.
Parameters:
_name (string): Contract name to resolveReturns: address - Resolved address (zero if not found)
Resolves a contract name to its address with requirement that it exists.
Parameters:
_name (string): Contract name to resolveReturns: address - Resolved address
Reverts: If address is not found
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);
}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;
}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
}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
);
}
}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
);
}
}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
);
}
}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);
}
}