github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/src/libraries/Hashing.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  import { Types } from "src/libraries/Types.sol";
     5  import { Encoding } from "src/libraries/Encoding.sol";
     6  
     7  /// @title Hashing
     8  /// @notice Hashing handles Optimism's various different hashing schemes.
     9  library Hashing {
    10      /// @notice Computes the hash of the RLP encoded L2 transaction that would be generated when a
    11      ///         given deposit is sent to the L2 system. Useful for searching for a deposit in the L2
    12      ///         system.
    13      /// @param _tx User deposit transaction to hash.
    14      /// @return Hash of the RLP encoded L2 deposit transaction.
    15      function hashDepositTransaction(Types.UserDepositTransaction memory _tx) internal pure returns (bytes32) {
    16          return keccak256(Encoding.encodeDepositTransaction(_tx));
    17      }
    18  
    19      /// @notice Computes the deposit transaction's "source hash", a value that guarantees the hash
    20      ///         of the L2 transaction that corresponds to a deposit is unique and is
    21      ///         deterministically generated from L1 transaction data.
    22      /// @param _l1BlockHash Hash of the L1 block where the deposit was included.
    23      /// @param _logIndex    The index of the log that created the deposit transaction.
    24      /// @return Hash of the deposit transaction's "source hash".
    25      function hashDepositSource(bytes32 _l1BlockHash, uint256 _logIndex) internal pure returns (bytes32) {
    26          bytes32 depositId = keccak256(abi.encode(_l1BlockHash, _logIndex));
    27          return keccak256(abi.encode(bytes32(0), depositId));
    28      }
    29  
    30      /// @notice Hashes the cross domain message based on the version that is encoded into the
    31      ///         message nonce.
    32      /// @param _nonce    Message nonce with version encoded into the first two bytes.
    33      /// @param _sender   Address of the sender of the message.
    34      /// @param _target   Address of the target of the message.
    35      /// @param _value    ETH value to send to the target.
    36      /// @param _gasLimit Gas limit to use for the message.
    37      /// @param _data     Data to send with the message.
    38      /// @return Hashed cross domain message.
    39      function hashCrossDomainMessage(
    40          uint256 _nonce,
    41          address _sender,
    42          address _target,
    43          uint256 _value,
    44          uint256 _gasLimit,
    45          bytes memory _data
    46      )
    47          internal
    48          pure
    49          returns (bytes32)
    50      {
    51          (, uint16 version) = Encoding.decodeVersionedNonce(_nonce);
    52          if (version == 0) {
    53              return hashCrossDomainMessageV0(_target, _sender, _data, _nonce);
    54          } else if (version == 1) {
    55              return hashCrossDomainMessageV1(_nonce, _sender, _target, _value, _gasLimit, _data);
    56          } else {
    57              revert("Hashing: unknown cross domain message version");
    58          }
    59      }
    60  
    61      /// @notice Hashes a cross domain message based on the V0 (legacy) encoding.
    62      /// @param _target Address of the target of the message.
    63      /// @param _sender Address of the sender of the message.
    64      /// @param _data   Data to send with the message.
    65      /// @param _nonce  Message nonce.
    66      /// @return Hashed cross domain message.
    67      function hashCrossDomainMessageV0(
    68          address _target,
    69          address _sender,
    70          bytes memory _data,
    71          uint256 _nonce
    72      )
    73          internal
    74          pure
    75          returns (bytes32)
    76      {
    77          return keccak256(Encoding.encodeCrossDomainMessageV0(_target, _sender, _data, _nonce));
    78      }
    79  
    80      /// @notice Hashes a cross domain message based on the V1 (current) encoding.
    81      /// @param _nonce    Message nonce.
    82      /// @param _sender   Address of the sender of the message.
    83      /// @param _target   Address of the target of the message.
    84      /// @param _value    ETH value to send to the target.
    85      /// @param _gasLimit Gas limit to use for the message.
    86      /// @param _data     Data to send with the message.
    87      /// @return Hashed cross domain message.
    88      function hashCrossDomainMessageV1(
    89          uint256 _nonce,
    90          address _sender,
    91          address _target,
    92          uint256 _value,
    93          uint256 _gasLimit,
    94          bytes memory _data
    95      )
    96          internal
    97          pure
    98          returns (bytes32)
    99      {
   100          return keccak256(Encoding.encodeCrossDomainMessageV1(_nonce, _sender, _target, _value, _gasLimit, _data));
   101      }
   102  
   103      /// @notice Derives the withdrawal hash according to the encoding in the L2 Withdrawer contract
   104      /// @param _tx Withdrawal transaction to hash.
   105      /// @return Hashed withdrawal transaction.
   106      function hashWithdrawal(Types.WithdrawalTransaction memory _tx) internal pure returns (bytes32) {
   107          return keccak256(abi.encode(_tx.nonce, _tx.sender, _tx.target, _tx.value, _tx.gasLimit, _tx.data));
   108      }
   109  
   110      /// @notice Hashes the various elements of an output root proof into an output root hash which
   111      ///         can be used to check if the proof is valid.
   112      /// @param _outputRootProof Output root proof which should hash to an output root.
   113      /// @return Hashed output root proof.
   114      function hashOutputRootProof(Types.OutputRootProof memory _outputRootProof) internal pure returns (bytes32) {
   115          return keccak256(
   116              abi.encode(
   117                  _outputRootProof.version,
   118                  _outputRootProof.stateRoot,
   119                  _outputRootProof.messagePasserStorageRoot,
   120                  _outputRootProof.latestBlockhash
   121              )
   122          );
   123      }
   124  }