【Solidity】OpenZeppelinのOwnableを解説!

【Solidity】OpenZeppelinのOwnableを解説!
悩んでいる人

OpenZeppelinのOwnableがいまいち理解できないから教えてほしいんだよね。。

こんな疑問にお答えします。

  • 本記事の内容
  • 本記事の信頼性
プロフィール
  • フリーランスエンジニア(保有資格約20個)
  • ブログ・アフィリエイト歴5年
  • ブランドせどりで月利50万円⇨脱サラ
  • Twitterフォロワー約2,000人
いずみん

本記事を書いている僕はエンジニア歴7年のフリーランスエンジニアで、IT系資格は個保有しています。
SolidityやOpenZeppelinは普段から使用しているので、かなり有益な情報を発信できるかと思います。

OpenZeppelinはEthereum上で安全なスマートコントラクトを実装するためのフレームワークです。

OpenZeppelinのOwnableはコントラクトの実行権限を制御できるコントラクトになります。

OpenZeppelinのOwnableを正しく理解することで、コントラクトの本質が分かるようになります。

本記事を読んでいただければ、OpenZeppelinのOwnableについてしっかりと理解できるので早速見ていきましょう!

いずみん

すぐに読み終わるので、是非最後まで読んでみてください!

目次

OpenZeppelinとは

OpenZeppelinは、Ethereum上で安全なスマートコントラクトを実装するためのフレームワークです。

OpenZeppelinにはERC20・721・827トークンICOに関する機能が実装されています。

Ownableコントラクトとは

Ownableコントラクトは、コントラクトの実行権限を制御できるコントラクトです。

Ownableコントラクトを継承することで、コントラクト作成者しかコントラクトを実行できないようにできます。

いずみん

コントラクトの実行権限を制御することで悪用のリスクを減らすことができます。

Ownableコントラクトのソースコード

Ownableコントラクトのソースコードは以下になります。

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

Ownableコンストラクタ

Ownableコンストラクタは、コントラクト作成時に一度だけ実行されます。

コントラクト作成者のアドレスをowner変数に格納します。

modifier onlyOwner

modifier onlyOwnerは関数修飾子として使用します。

onlyOwnerが定義された関数は、コントラクト作成者以外は実行不可となります。

いずみん

コントラクト実行者のアドレスとコントラクト作成者のアドレスが一致しない場合、エラーとなります。

pragma solidity >=0.4.22 <0.8.1;

import "openzeppelin-solidity/contracts/access/Ownable.sol";

contract Greeter is Ownable {
    string private _greeting = "Hello, World!";

    function greet() external view returns(string memory) {
        return _greeting;
    }

    function setGreeting(string calldata greeting) external onlyOwner {
        _greeting = greeting;
    }
}

↑のように使用します。

transferOwnership

transferOwnershipはコントラクトの権限を委譲する際に使用します。

いずみん

owner変数に別アドレスを代入しているだけです。

Ownableコントラクトの使い方

Ownableコントラクトは継承して使用します。

contract MyContract is Ownable {
	...
}

↑のように継承すればOKです。

よくある質問 | OpenZeppelinのOwnableについて

OpenZeppelinとは何ですか?

Ethereum上で安全なスマートコントラクトを実装するためのフレームワークです。

Ownableコントラクトとは何ですか?

コントラクトの実行権限を制御できるコントラクトです。

OpenZeppelinは何と読みますか?

オープンツェッペリン」です。

まとめ | OpenZeppelinのOwnableについて

今回は、OpenZeppelinのOwnableについて解説しました。

以下が本記事のまとめになります。

まとめ
  • OpenZeppelinとは、Ethereum上で安全なスマートコントラクトを実装するためのフレームワーク。
  • Ownableコントラクトとは、コントラクトの実行権限を制御できるコントラクト。

この記事があなたのお役に立てば幸いです。

サイト内には、他にも初心者向けの記事を多く投稿していますので良ければ参考にしてみてください。

最後までお読みいただきありがとうございました!

【Solidity】OpenZeppelinのOwnableを解説!

この記事が気に入ったら
フォローしてね!

シェアしてね!
  • URLをコピーしました!
目次