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

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  import { Predeploys } from "src/libraries/Predeploys.sol";
     5  import { L2CrossDomainMessenger } from "src/L2/L2CrossDomainMessenger.sol";
     6  import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
     7  
     8  /// @title CrossDomainOwnable3
     9  /// @notice This contract extends the OpenZeppelin `Ownable` contract for L2 contracts to be owned
    10  ///         by contracts on either L1 or L2. Note that this contract is meant to be used with
    11  ///         systems that use the CrossDomainMessenger system. It will not work if the
    12  ///         OptimismPortal is used directly.
    13  abstract contract CrossDomainOwnable3 is Ownable {
    14      /// @notice If true, the contract uses the cross domain _checkOwner function override.
    15      ///         If false it uses the standard Ownable _checkOwner function.
    16      bool public isLocal = true;
    17  
    18      /// @notice Emits when ownership of the contract is transferred. Includes the
    19      ///         isLocal field in addition to the standard `Ownable` OwnershipTransferred event.
    20      /// @param previousOwner The previous owner of the contract.
    21      /// @param newOwner      The new owner of the contract.
    22      /// @param isLocal       Configures the `isLocal` contract variable.
    23      event OwnershipTransferred(address indexed previousOwner, address indexed newOwner, bool isLocal);
    24  
    25      /// @notice Allows for ownership to be transferred with specifying the locality.
    26      /// @param _owner   The new owner of the contract.
    27      /// @param _isLocal Configures the locality of the ownership.
    28      function transferOwnership(address _owner, bool _isLocal) external onlyOwner {
    29          require(_owner != address(0), "CrossDomainOwnable3: new owner is the zero address");
    30  
    31          address oldOwner = owner();
    32          _transferOwnership(_owner);
    33          isLocal = _isLocal;
    34  
    35          emit OwnershipTransferred(oldOwner, _owner, _isLocal);
    36      }
    37  
    38      /// @notice Overrides the implementation of the `onlyOwner` modifier to check that the unaliased
    39      ///         `xDomainMessageSender` is the owner of the contract. This value is set to the caller
    40      ///         of the L1CrossDomainMessenger.
    41      function _checkOwner() internal view override {
    42          if (isLocal) {
    43              require(owner() == msg.sender, "CrossDomainOwnable3: caller is not the owner");
    44          } else {
    45              L2CrossDomainMessenger messenger = L2CrossDomainMessenger(Predeploys.L2_CROSS_DOMAIN_MESSENGER);
    46  
    47              require(msg.sender == address(messenger), "CrossDomainOwnable3: caller is not the messenger");
    48  
    49              require(owner() == messenger.xDomainMessageSender(), "CrossDomainOwnable3: caller is not the owner");
    50          }
    51      }
    52  }