【Solidity】OpenZeppelinのOwnableを解説

当ページのリンクには広告が含まれています。
いずみ

こんな悩みを解決できる記事を書きました!

僕は現役フリーランスエンジニア(歴年)で、資格は個保有しています。

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

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

いずみ

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

本記事を読めば、OpenZeppelinのOwnableについて理解できるので早速見ていきましょう!

いずみ

すぐに読み終わるので、ぜひ最後まで読んでくださいね♪

執筆者/監修者
  • フリーランスエンジニア(保有資格個)
  • ブログ・アフィリエイト歴5年
  • ブランドせどりで月利50万円⇨脱サラ
  • 投資歴5年(仮想通貨・FXが得意)
  • Twitterフォロワー約2,000人
  • 運営者情報はこちら
いずみです
目次

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とは何ですか?

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

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

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

OpenZeppelinは何と読みますか?

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

まとめ

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

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

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

スマートコントラクトの勉強なら「」が分かりやすくておすすめですよ♪

いずみ

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

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

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