0Pricing
Blockchain Smart Contracts with Solidity · レッスン

アクセス制御パターン

`Ownable`、`Pausable`、ロールベースアクセス制御(RBAC)パターンを使って、堅牢なアクセス制御機構を実装します。

「アクセス制御パターン」はCoddyKit上の無料Blockchain Smart Contracts with Solidityレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBlockchain Smart Contracts with Solidity学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What is Access Control?

In smart contracts, access control defines who can perform specific actions. It's like setting permissions on a file or folder.

Without proper access control, anyone could call sensitive functions, leading to vulnerabilities or unintended behavior.

Why It's Crucial

Imagine a contract that manages funds or critical system settings. You wouldn't want just anyone to be able to:

  • Withdraw all funds.
  • Change the contract's owner.
  • Pause essential operations.

Access control is a fundamental security measure.

The `onlyOwner` Modifier

A common pattern is to restrict certain functions to the contract's owner (the address that deployed it).

This is often achieved using a modifier, a special keyword in Solidity that can alter the behavior of a function.

Custom `onlyOwner` Example

Here's how you might manually implement an onlyOwner modifier and use it:

pragma solidity ^0.8.0;

contract MyBasicOwnable {
  address public owner;

  constructor() {
    owner = msg.sender;
  }

  modifier onlyOwner() {
    require(msg.sender == owner, "Not owner");
    _;
  }

  function setGreeting(string memory _text) public onlyOwner {
    // Only the owner can call this
    // ... (e.g., update a greeting message)
  }
}

OpenZeppelin's `Ownable`

While you can write your own, it's best practice to use battle-tested libraries. OpenZeppelin provides a secure and standardized Ownable contract.

By inheriting from Ownable, your contract gets the owner state variable and the onlyOwner modifier automatically.

Using OpenZeppelin `Ownable`

Simply import and inherit Ownable. The contract deployer automatically becomes the owner.

pragma solidity ^0.8.0;

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

contract MyOzOwnable is Ownable {
  uint256 public value;

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

  function getValue() public view returns (uint256) {
    return value;
  }
}

The `Pausable` Pattern

The Pausable pattern allows a contract to be put into a 'paused' state, preventing certain functions from being called.

This is crucial for emergency situations, like discovering a critical bug or reacting to a hack, giving developers time to mitigate issues.

Using OpenZeppelin `Pausable`

OpenZeppelin's Pausable provides paused state, whenNotPaused and whenPaused modifiers, and _pause()/_unpause() functions.

pragma solidity ^0.8.0;

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

contract MyPausableContract is Pausable, Ownable {
  uint256 public counter;

  function increment() public whenNotPaused {
    counter++;
  }

  function pauseContract() public onlyOwner {
    _pause(); // Only owner can pause
  }

  function unpauseContract() public onlyOwner {
    _unpause(); // Only owner can unpause
  }
}

Role-Based Access Control (RBAC)

For more complex contracts, a single 'owner' might not be enough. Role-Based Access Control (RBAC) allows defining multiple roles (e.g., 'minter', 'admin', 'pauser').

OpenZeppelin's AccessControl contract helps manage these roles efficiently.

Using OpenZeppelin `AccessControl`

Define roles as bytes32 constants. The deployer automatically gets DEFAULT_ADMIN_ROLE, which can grant/revoke other roles.

pragma solidity ^0.8.0;

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

contract MyRBACContract is AccessControl {
  bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");

  constructor() {
    _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
    _grantRole(MINTER_ROLE, msg.sender); // Deployer is also a minter
  }

  function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
    // Logic to mint tokens
  }

  function systemPause() public onlyRole(PAUSER_ROLE) {
    // Logic to pause critical system functions
  }
}

Access Control Check

Which of the following are benefits of implementing access control patterns like Ownable, Pausable, or AccessControl in smart contracts?

Recap: Access Control Patterns

You've learned about essential access control patterns in Solidity:

  • Ownable: Restricts functions to a single owner, often the contract deployer.
  • Pausable: Allows for emergency pausing/unpausing of contract functionality.
  • AccessControl (RBAC): Provides flexible, role-based permissions for more complex scenarios.

These patterns are critical for building robust and secure smart contracts, often leveraged from OpenZeppelin's battle-tested libraries.

よくある質問

「アクセス制御パターン」レッスンは無料ですか?

はい。「アクセス制御パターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Blockchain Smart Contracts with Solidityコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。

「アクセス制御パターン」で何を学びますか?

`Ownable`、`Pausable`、ロールベースアクセス制御(RBAC)パターンを使って、堅牢なアクセス制御機構を実装します。 ブラウザで直接実行するハンズオンコードでBlockchain Smart Contracts with Solidityを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのBlockchain Smart Contracts with Solidityは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「アクセス制御パターン」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?

はい。すべてのBlockchain Smart Contracts with Solidityレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 一般的な脆弱性(リエントランシーなど)
  2. アクセス制御パターン
  3. SafeMathによるセキュアコーディング
  4. 監査、テスト、バグバウンティ
← Blockchain Smart Contracts with Solidityに戻る