L2ContractHelper
Author: Matter Labs
Helper library for working with L2 contracts on L1.
Note: security-contact: security@matterlabs.dev
State Variables
CREATE2_PREFIX
The prefix used to create CREATE2 addresses.
bytes32 private constant CREATE2_PREFIX = keccak256("zksyncCreate2");
CREATE_PREFIX
Prefix used during derivation of account addresses using CREATE
keccak256("zksyncCreate")
bytes32 private constant CREATE_PREFIX =
0x63bae3a9951d38e8a3fbb7b70909afc1200610fc5bc55ade242f815974674f23;
Functions
sendMessageToL1
Sends L2 -> L1 arbitrary-long message through the system contract messenger.
function sendMessageToL1(bytes memory _message) internal returns (bytes32);
Parameters
Name | Type | Description |
---|---|---|
_message | bytes | Data to be sent to L1. |
Returns
Name | Type | Description |
---|---|---|
<none> | bytes32 | keccak256 hash of the sent message. |
hashL2Bytecode
Validate the bytecode format and calculate its hash.
function hashL2Bytecode(bytes memory _bytecode)
internal
pure
returns (bytes32 hashedBytecode);
Parameters
Name | Type | Description |
---|---|---|
_bytecode | bytes | The bytecode to hash. |
Returns
Name | Type | Description |
---|---|---|
hashedBytecode | bytes32 | The 32-byte hash of the bytecode. Note: The function reverts the execution if the bytecode has non expected format: - Bytecode bytes length is not a multiple of 32 - Bytecode bytes length is not less than 2^21 bytes (2^16 words) - Bytecode words length is not odd |
hashL2BytecodeCalldata
Validate the bytecode format and calculate its hash.
function hashL2BytecodeCalldata(bytes calldata _bytecode)
internal
pure
returns (bytes32 hashedBytecode);
Parameters
Name | Type | Description |
---|---|---|
_bytecode | bytes | The bytecode to hash. |
Returns
Name | Type | Description |
---|---|---|
hashedBytecode | bytes32 | The 32-byte hash of the bytecode. Note: The function reverts the execution if the bytecode has non expected format: - Bytecode bytes length is not a multiple of 32 - Bytecode bytes length is not less than 2^21 bytes (2^16 words) - Bytecode words length is not odd |
validateBytecodeHash
Validates the format of the given bytecode hash.
Due to the specification of the L2 bytecode hash, not every 32 bytes could be a legit bytecode hash.
The function reverts on invalid bytecode hash format.
function validateBytecodeHash(bytes32 _bytecodeHash) internal pure;
Parameters
Name | Type | Description |
---|---|---|
_bytecodeHash | bytes32 | The hash of the bytecode to validate. |
bytecodeLen
Returns the length of the bytecode associated with the given hash.
function bytecodeLen(bytes32 _bytecodeHash)
internal
pure
returns (uint256 codeLengthInWords);
Parameters
Name | Type | Description |
---|---|---|
_bytecodeHash | bytes32 | The hash of the bytecode. |
Returns
Name | Type | Description |
---|---|---|
codeLengthInWords | uint256 | The length of the bytecode in words. |
computeCreate2Address
Computes the create2 address for a Layer 2 contract.
function computeCreate2Address(
address _sender,
bytes32 _salt,
bytes32 _bytecodeHash,
bytes32 _constructorInputHash
) internal pure returns (address);
Parameters
Name | Type | Description |
---|---|---|
_sender | address | The address of the sender. |
_salt | bytes32 | The salt value to use in the create2 address computation. |
_bytecodeHash | bytes32 | The contract bytecode hash. |
_constructorInputHash | bytes32 | The hash of the constructor input data. |
Returns
Name | Type | Description |
---|---|---|
<none> | address | The create2 address of the contract. NOTE: L2 create2 derivation is different from L1 derivation! |
computeCreateAddress
Calculates the address of a deployed contract via create
function computeCreateAddress(address _sender, uint256 _senderNonce)
internal
pure
returns (address);
Parameters
Name | Type | Description |
---|---|---|
_sender | address | The account that deploys the contract. |
_senderNonce | uint256 | The deploy nonce of the sender's account. NOTE: L2 create derivation is different from L1 derivation! |
hashFactoryDeps
Hashes the L2 bytecodes and returns them in the format in which they are processed by the bootloader
function hashFactoryDeps(bytes[] memory _factoryDeps)
internal
pure
returns (uint256[] memory hashedFactoryDeps);