-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathAccessControlLibV1.sol
More file actions
324 lines (288 loc) · 10.4 KB
/
AccessControlLibV1.sol
File metadata and controls
324 lines (288 loc) · 10.4 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
/* solhint-disable ordering */
pragma solidity ^0.8.0;
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-solidity/contracts/access/IAccessControl.sol";
import "./ProtoUtilV1.sol";
library AccessControlLibV1 {
using ProtoUtilV1 for IStore;
using StoreKeyUtil for IStore;
bytes32 public constant NS_ROLES_ADMIN = 0x00; // SAME AS "DEFAULT_ADMIN_ROLE"
bytes32 public constant NS_ROLES_COVER_MANAGER = "role:cover:manager";
bytes32 public constant NS_ROLES_LIQUIDITY_MANAGER = "role:liquidity:manager";
bytes32 public constant NS_ROLES_GOVERNANCE_AGENT = "role:governance:agent";
bytes32 public constant NS_ROLES_GOVERNANCE_ADMIN = "role:governance:admin";
bytes32 public constant NS_ROLES_UPGRADE_AGENT = "role:upgrade:agent";
bytes32 public constant NS_ROLES_RECOVERY_AGENT = "role:recovery:agent";
bytes32 public constant NS_ROLES_PAUSE_AGENT = "role:pause:agent";
bytes32 public constant NS_ROLES_UNPAUSE_AGENT = "role:unpause:agent";
/**
* @dev Reverts if the sender is not the protocol admin.
*/
function mustBeAdmin(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_ADMIN, msg.sender);
}
/**
* @dev Reverts if the sender is not the cover manager.
*/
function mustBeCoverManager(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_COVER_MANAGER, msg.sender);
}
/**
* @dev Reverts if the sender is not the liquidity manager.
*/
function mustBeLiquidityManager(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_LIQUIDITY_MANAGER, msg.sender);
}
/**
* @dev Reverts if the sender is not a governance agent.
*/
function mustBeGovernanceAgent(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_GOVERNANCE_AGENT, msg.sender);
}
/**
* @dev Reverts if the sender is not a governance admin.
*/
function mustBeGovernanceAdmin(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_GOVERNANCE_ADMIN, msg.sender);
}
/**
* @dev Reverts if the sender is not an upgrade agent.
*/
function mustBeUpgradeAgent(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_UPGRADE_AGENT, msg.sender);
}
/**
* @dev Reverts if the sender is not a recovery agent.
*/
function mustBeRecoveryAgent(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_RECOVERY_AGENT, msg.sender);
}
/**
* @dev Reverts if the sender is not the pause agent.
*/
function mustBePauseAgent(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_PAUSE_AGENT, msg.sender);
}
/**
* @dev Reverts if the sender is not the unpause agent.
*/
function mustBeUnpauseAgent(IStore s) external view {
_mustHaveAccess(s, NS_ROLES_UNPAUSE_AGENT, msg.sender);
}
/**
* @dev Reverts if the caller is not the protocol admin.
*/
function callerMustBeAdmin(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_ADMIN, caller);
}
/**
* @dev Reverts if the caller is not the cover manager.
*/
function callerMustBeCoverManager(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_COVER_MANAGER, caller);
}
/**
* @dev Reverts if the caller is not the liquidity manager.
*/
function callerMustBeLiquidityManager(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_LIQUIDITY_MANAGER, caller);
}
/**
* @dev Reverts if the caller is not a governance agent.
*/
function callerMustBeGovernanceAgent(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_GOVERNANCE_AGENT, caller);
}
/**
* @dev Reverts if the caller is not a governance admin.
*/
function callerMustBeGovernanceAdmin(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_GOVERNANCE_ADMIN, caller);
}
/**
* @dev Reverts if the caller is not an upgrade agent.
*/
function callerMustBeUpgradeAgent(IStore s, address caller) public view {
_mustHaveAccess(s, NS_ROLES_UPGRADE_AGENT, caller);
}
/**
* @dev Reverts if the caller is not a recovery agent.
*/
function callerMustBeRecoveryAgent(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_RECOVERY_AGENT, caller);
}
/**
* @dev Reverts if the caller is not the pause agent.
*/
function callerMustBePauseAgent(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_PAUSE_AGENT, caller);
}
/**
* @dev Reverts if the caller is not the unpause agent.
*/
function callerMustBeUnpauseAgent(IStore s, address caller) external view {
_mustHaveAccess(s, NS_ROLES_UNPAUSE_AGENT, caller);
}
/**
* @dev Reverts if the caller does not have access to the given role.
*/
function _mustHaveAccess(
IStore s,
bytes32 role,
address caller
) private view {
require(hasAccessInternal(s, role, caller), "Forbidden");
}
/**
* @dev Checks if a given user has access to the given role
* @param role Specify the role name
* @param user Enter the user account
* @return Returns true if the user is a member of the specified role
*/
function hasAccessInternal(
IStore s,
bytes32 role,
address user
) public view returns (bool) {
address protocol = s.getProtocolAddressInternal();
// The protocol is not deployed yet. Therefore, no role to check
if (protocol == address(0)) {
return false;
}
// You must have the same role in the protocol contract if you're don't have this role here
return IAccessControl(protocol).hasRole(role, user);
}
/**
* @dev Adds a protocol member contract
*
* @custom:suppress-address-trust-issue This feature can only be accessed internally within the protocol.
*
* @param s Enter the store instance
* @param namespace Enter the contract namespace
* @param key Enter the contract key
* @param contractAddress Enter the contract address
*/
function addContractInternal(
IStore s,
bytes32 namespace,
bytes32 key,
address contractAddress
) external {
// Not only the msg.sender needs to be an upgrade agent
// but the contract using this library (and this function)
// must also be an upgrade agent
callerMustBeUpgradeAgent(s, address(this));
_addContract(s, namespace, key, contractAddress);
}
function _addContract(
IStore s,
bytes32 namespace,
bytes32 key,
address contractAddress
) private {
if (key > 0) {
s.setAddressByKeys(ProtoUtilV1.NS_CONTRACTS, namespace, key, contractAddress);
} else {
s.setAddressByKeys(ProtoUtilV1.NS_CONTRACTS, namespace, contractAddress);
}
_addMember(s, contractAddress);
}
function _deleteContract(
IStore s,
bytes32 namespace,
bytes32 key,
address contractAddress
) private {
if (key > 0) {
s.deleteAddressByKeys(ProtoUtilV1.NS_CONTRACTS, namespace, key);
} else {
s.deleteAddressByKeys(ProtoUtilV1.NS_CONTRACTS, namespace);
}
_removeMember(s, contractAddress);
}
/**
* @dev Upgrades a contract at the given namespace and key.
*
* The previous contract's protocol membership is revoked and
* the current immediately starts assuming responsibility of
* whatever the contract needs to do at the supplied namespace and key.
*
* @custom:warning Warning:
*
* This feature is only accessible to an upgrade agent.
* Since adding member to the protocol is a highly risky activity,
* the role `Upgrade Agent` is considered to be one of the most `Critical` roles.
*
* @custom:suppress-address-trust-issue This feature can only be accessed internally within the protocol.
*
* @param s Provide store instance
* @param namespace Enter a unique namespace for this contract
* @param key Enter a key if this contract has siblings
* @param previous Enter the existing contract address at this namespace and key.
* @param current Enter the contract address which will replace the previous contract.
*/
function upgradeContractInternal(
IStore s,
bytes32 namespace,
bytes32 key,
address previous,
address current
) external {
// Not only the msg.sender needs to be an upgrade agent
// but the contract using this library (and this function)
// must also be an upgrade agent
callerMustBeUpgradeAgent(s, address(this));
bool isMember = s.isProtocolMemberInternal(previous);
require(isMember, "Not a protocol member");
_deleteContract(s, namespace, key, previous);
_addContract(s, namespace, key, current);
}
/**
* @dev Adds member to the protocol
*
* A member is a trusted EOA or a contract that was added to the protocol using `addContract`
* function. When a contract is removed using `upgradeContract` function, the membership of previous
* contract is also removed.
*
* @custom:warning Warning:
*
* This feature is only accessible to an upgrade agent.
* Since adding member to the protocol is a highly risky activity,
* the role `Upgrade Agent` is considered to be one of the most `Critical` roles.
*
* @custom:suppress-address-trust-issue This feature can only be accessed internally within the protocol.
*
* @param member Enter an address to add as a protocol member
*/
function addMemberInternal(IStore s, address member) external {
// Not only the msg.sender needs to be an upgrade agent
// but the contract using this library (and this function)
// must also be an upgrade agent
callerMustBeUpgradeAgent(s, address(this));
_addMember(s, member);
}
/**
* @dev Removes a member from the protocol. This function is only accessible
* to an upgrade agent.
*
* @custom:suppress-address-trust-issue This feature can only be accessed internally within the protocol.
*
* @param member Enter an address to remove as a protocol member
*/
function removeMemberInternal(IStore s, address member) external {
// Not only the msg.sender needs to be an upgrade agent
// but the contract using this library (and this function)
// must also be an upgrade agent
callerMustBeUpgradeAgent(s, address(this));
_removeMember(s, member);
}
function _addMember(IStore s, address member) private {
require(s.getBoolByKeys(ProtoUtilV1.NS_MEMBERS, member) == false, "Already exists");
s.setBoolByKeys(ProtoUtilV1.NS_MEMBERS, member, true);
}
function _removeMember(IStore s, address member) private {
s.deleteBoolByKeys(ProtoUtilV1.NS_MEMBERS, member);
}
}