흔자
반응형

스마트 컨트랙트에서 소유권을 관리하고 변경하는 방법에 대해 살펴보겠다. 이를 위해 OpenZeppelin의 Ownable.sol 컨트랙트에서 제공하는 세 가지 함수 renounceOwnership, transferOwnership을 사용한다.

 

 

Ownable.sol

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

pragma solidity ^0.8.0;

import "./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. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling 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);
    }
}

1. 컨트랙트는 Context.sol로부터 상속받다. Context 통해 msg.sender를 처리하는 _msgSender() 함수를 사용할 수 있다.

아래는 Context.sol의 일부이다.

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

 

2. 소유자의 주소를 저장하는 _owner라는 private 변수를 선언한다.

address private _owner;

 

3. 소유권이 이전될 때 발생하는 OwnershipTransferred 이벤트를 선언한다.

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

 

4. 컨트랙트 생성자에서 _transferOwnership(_msgSender())를 호출하여 배포자를 초기 소유자로 설정한다.

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

 

5. onlyOwner 수정자를 선언하여 소유자만이 호출할 수 있는 함수를 생성할 수 있다. 이 수정자는 _checkOwner() 함수를 사용하여 호출자가 소유자인지 확인한다.

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

 

6. owner() 함수는 현재 소유자의 주소를 반환한다.

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

 

7. _checkOwner() 함수는 호출자가 소유자인지 확인하고, 그렇지 않은 경우 "Ownable: caller is not the owner"라는 오류 메시지와 함께 예외를 발생시킨다.

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

 

이후에는 위에서 설명한 renounceOwnership, transferOwnership, 및 _transferOwnership 함수가 구현된다. 이러한 함수들은 컨트랙트에서 소유권을 관리하고 변경하는 데 사용된다. 

 

 

renounceOwnership

renounceOwnership() 함수는 현재 컨트랙트의 소유권을 포기한다. 이 함수는 오직 현재 컨트랙트의 소유자만 호출할 수 있다. 소유권을 포기하면 컨트랙트의 소유자가 없게 되어 onlyOwner로 제한된 기능을 사용할 수 없게 됩니다. 이 함수는 컨트랙트의 소유권을 영구적으로 제거하기 때문에 주의해서 사용해야 한다. 

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

 

transferOwnership

transferOwnership(address newOwner) 함수는 컨트랙트의 소유권을 다른 계정으로 이전한다. 이 함수는 오직 현재 컨트랙트의 소유자만 호출할 수 있다.  newOwner 매개변수로 새로운 소유자의 주소를 받으며 새로운 소유자의 주소가 영 주소(0x0)가 아닌 경우에만 소유권 이전이 가능하다. 

/**
 * @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);
}

 

 

_transferOwnership 

_transferOwnership(address newOwner) 함수는 내부적으로 사용되는 함수로 소유권을 이전하는 과정을 처리한다. 이 함수는 접근 제한이 없으며 renounceOwnership()와 transferOwnership() 함수에서 호출된다. OwnershipTransferred 이벤트를 발생시켜 이전 소유자와 새로운 소유자를 기록하며 소유권을 변경한다.

/**
 * @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);
}

 

 

반응형
profile

흔자

@heun_n

즐겁게 개발하고 싶은 사람입니다.