L2ContractHelper

Git Source

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

NameTypeDescription
_messagebytesData to be sent to L1.

Returns

NameTypeDescription
<none>bytes32keccak256 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

NameTypeDescription
_bytecodebytesThe bytecode to hash.

Returns

NameTypeDescription
hashedBytecodebytes32The 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

NameTypeDescription
_bytecodebytesThe bytecode to hash.

Returns

NameTypeDescription
hashedBytecodebytes32The 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

NameTypeDescription
_bytecodeHashbytes32The 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

NameTypeDescription
_bytecodeHashbytes32The hash of the bytecode.

Returns

NameTypeDescription
codeLengthInWordsuint256The 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

NameTypeDescription
_senderaddressThe address of the sender.
_saltbytes32The salt value to use in the create2 address computation.
_bytecodeHashbytes32The contract bytecode hash.
_constructorInputHashbytes32The hash of the constructor input data.

Returns

NameTypeDescription
<none>addressThe 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

NameTypeDescription
_senderaddressThe account that deploys the contract.
_senderNonceuint256The 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);