Ownable

Sécurité

Définition

Contrat de base OpenZeppelin pour gérer un propriétaire unique (owner). Fournit le modifier onlyOwner pour restreindre l'accès aux fonctions critiques. Le propriétaire peut être transféré avec transferOwnership() ou renoncé avec renounceOwnership(). Pattern de sécurité fondamental pour les contrats administrés.

Version anglaise

OpenZeppelin base contract for managing single owner. Provides onlyOwner modifier to restrict access to critical functions. Owner can be transferred with transferOwnership() or renounced with renounceOwnership(). Fundamental security pattern for administered contracts.

Exemple de Code

import "@openzeppelin/contracts/access/Ownable.sol";

contract MyContract is Ownable {
  uint256 public value;

  constructor() Ownable(msg.sender) {}

  function setValue(uint256 _value) public onlyOwner {
    value = _value;
  }

  function transferOwnership(address newOwner) public override onlyOwner {
    super.transferOwnership(newOwner);
  }

  function renounceOwnership() public override onlyOwner {
    super.renounceOwnership(); // ⚠️ Contrat non administrable après
  }
}

Termes Liés

Pratique ce concept sur Solingo

Maîtrise Ownable avec des exercices interactifs et un IDE intégré.

Commencer gratuitement