openzeppelin学习笔记 - 2

Ownable.sol合约的作用是实现对合约所有权的控制,通过确定合约所有权, 来对合约函数执行  权限进行控制,合约的所有权只能属于单一地址。同时支持转移以及放弃合约所有权。合约所有权默认属于部署合约的账户。

事件

  • 合约所有权转移事件
1
2
3
4
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
 装饰器

  •  仅限合约所有者执行
1
2
3
4
modifier onlyOwner() {
require(isOwner());
_;
}
方法

  • 返回合约拥有者地址
1
function owner() public view returns(address)
  • 返回合约调用地址是否是合约拥有者
1
function isOwner() public view returns(bool)
  • 放弃合约所有权(合约不属于任何地址)
1
function renounceOwnership() public onlyOwner
  • 转移合约所有权
1
function transferOwnership(address newOwner) public onlyOwner
合约代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
pragma solidity ^0.4.24;

/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
* @dev Ownable合约拥有一个owner字段,提供一个基础的权限控制功能,
*/
contract Ownable {
// 合约的拥有者,一般是合约的部署账户
address private _owner;

// 转移合约拥有者事件,当合约的拥有者转移是发送
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);

/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
* @dev Ownable合约的构造函数,这个函数去设置合约拥有者为合约的部署账户(调用构造函数的账户),address(0)为空地址
*/
constructor() internal {
_owner = msg.sender;
emit OwnershipTransferred(address(0), _owner);
}

/**
* @return the address of the owner.
* @return 返回合约的拥有者
*/
function owner() public view returns(address) {
return _owner;
}

/**
* @dev Throws if called by any account other than the owner.
* @dev onlyOwner修饰器,保证函数的执行者是合约拥有者
*/
modifier onlyOwner() {
require(isOwner());
_;
}

/**
* @return true if `msg.sender` is the owner of the contract.
* @return 返回当前合约调用者是否是合约的拥有者
*/
function isOwner() public view returns(bool) {
return msg.sender == _owner;
}

/**
* @dev Allows the current owner to relinquish control of the contract.
* @notice Renouncing to ownership will leave the contract without an owner.
* It will not be possible to call the functions with the `onlyOwner`
* modifier anymore.
* @dev 当前合约拥有者放弃合约所有权
* @notice 注意的是放弃合约的所有权,意味着这个合约不再存在拥有者,这回导致这个合约的
* 拥有`onlyOwner`修饰器的函数无法再被调用
*/
function renounceOwnership() public onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}

/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
* @dev 当前合约拥有者将合约的所有权转移给其他账户
* @param newOwner 接收合约所有权的账户
*/
function transferOwnership(address newOwner) public onlyOwner {
_transferOwnership(newOwner);
}

/**
* @dev Transfers control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
* @dev 当前合约拥有者将合约的所有权转移给其他账户(内部方法),目标账户不能是空地址(等于放弃合约所有权)
* @param newOwner 接收合约所有权的账户
*/
function _transferOwnership(address newOwner) internal {
require(newOwner != address(0));
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}