AccessControlCrossChain.sol 1.6 KB

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