Ownable2Step.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.0) (access/Ownable2Step.sol)
  3. pragma solidity ^0.8.20;
  4. import {Ownable} from "./Ownable.sol";
  5. /**
  6. * @dev Contract module which provides access control mechanism, where
  7. * there is an account (an owner) that can be granted exclusive access to
  8. * specific functions.
  9. *
  10. * This extension of the {Ownable} contract includes a two-step mechanism to transfer
  11. * ownership, where the new owner must call {acceptOwnership} in order to replace the
  12. * old one. This can help prevent common mistakes, such as transfers of ownership to
  13. * incorrect accounts, or to contracts that are unable to interact with the
  14. * permission system.
  15. *
  16. * The initial owner is specified at deployment time in the constructor for `Ownable`. This
  17. * can later be changed with {transferOwnership} and {acceptOwnership}.
  18. *
  19. * This module is used through inheritance. It will make available all functions
  20. * from parent (Ownable).
  21. */
  22. abstract contract Ownable2Step is Ownable {
  23. address private _pendingOwner;
  24. event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
  25. /**
  26. * @dev Returns the address of the pending owner.
  27. */
  28. function pendingOwner() public view virtual returns (address) {
  29. return _pendingOwner;
  30. }
  31. /**
  32. * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
  33. * Can only be called by the current owner.
  34. *
  35. * Setting `newOwner` to the zero address is allowed; this can be used to cancel an initiated ownership transfer.
  36. */
  37. function transferOwnership(address newOwner) public virtual override onlyOwner {
  38. _pendingOwner = newOwner;
  39. emit OwnershipTransferStarted(owner(), newOwner);
  40. }
  41. /**
  42. * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
  43. * Internal function without access restriction.
  44. */
  45. function _transferOwnership(address newOwner) internal virtual override {
  46. delete _pendingOwner;
  47. super._transferOwnership(newOwner);
  48. }
  49. /**
  50. * @dev The new owner accepts the ownership transfer.
  51. */
  52. function acceptOwnership() public virtual {
  53. address sender = _msgSender();
  54. if (pendingOwner() != sender) {
  55. revert OwnableUnauthorizedAccount(sender);
  56. }
  57. _transferOwnership(sender);
  58. }
  59. }