AccessControlCrossChain.sol 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (access/AccessControlCrossChain.sol)
  3. pragma solidity ^0.8.4;
  4. import "./AccessControl.sol";
  5. import "../crosschain/CrossChainEnabled.sol";
  6. /**
  7. * @dev An extension to {AccessControl} with support for cross-chain access management.
  8. * For each role, is extension implements an equivalent "aliased" role that is used for
  9. * restricting calls originating from other chains.
  10. *
  11. * For example, if a function `myFunction` is protected by `onlyRole(SOME_ROLE)`, and
  12. * if an address `x` has role `SOME_ROLE`, it would be able to call `myFunction` directly.
  13. * A wallet or contract at the same address on another chain would however not be able
  14. * to call this function. In order to do so, it would require to have the role
  15. * `_crossChainRoleAlias(SOME_ROLE)`.
  16. *
  17. * This aliasing is required to protect against multiple contracts living at the same
  18. * address on different chains but controlled by conflicting entities.
  19. *
  20. * _Available since v4.6._
  21. */
  22. abstract contract AccessControlCrossChain is AccessControl, CrossChainEnabled {
  23. bytes32 public constant CROSSCHAIN_ALIAS = keccak256("CROSSCHAIN_ALIAS");
  24. /**
  25. * @dev See {AccessControl-_checkRole}.
  26. */
  27. function _checkRole(bytes32 role) internal view virtual override {
  28. if (_isCrossChain()) {
  29. _checkRole(_crossChainRoleAlias(role), _crossChainSender());
  30. } else {
  31. super._checkRole(role);
  32. }
  33. }
  34. /**
  35. * @dev Returns the aliased role corresponding to `role`.
  36. */
  37. function _crossChainRoleAlias(bytes32 role) internal pure virtual returns (bytes32) {
  38. return role ^ CROSSCHAIN_ALIAS;
  39. }
  40. }