This repository was archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathKaliWhiteListManager.sol
251 lines (183 loc) · 7.88 KB
/
KaliWhiteListManager.sol
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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.4;
import '../utils/Multicall.sol';
/// @notice Kali DAO whitelist manager.
/// @author Modified from SushiSwap
/// (https://github.com/sushiswap/trident/blob/master/contracts/pool/franchised/WhiteListManager.sol)
contract KaliWhitelistManager {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event WhitelistCreated(uint256 indexed listId, address indexed operator);
event AccountWhitelisted(uint256 indexed listId, address indexed account, bool approved);
event MerkleRootSet(uint256 indexed listId, bytes32 merkleRoot);
event WhitelistJoined(uint256 indexed listId, uint256 indexed index, address indexed account);
/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error NullId();
error IdExists();
error NotOperator();
error SignatureExpired();
error InvalidSignature();
error WhitelistClaimed();
error NotRooted();
/*///////////////////////////////////////////////////////////////
EIP-712 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
bytes32 internal constant WHITELIST_TYPEHASH =
keccak256('Whitelist(address account,bool approved,uint256 deadline)');
/*///////////////////////////////////////////////////////////////
WHITELIST STORAGE
//////////////////////////////////////////////////////////////*/
mapping(uint256 => address) public operatorOf;
mapping(uint256 => bytes32) public merkleRoots;
mapping(uint256 => mapping(address => bool)) public whitelistedAccounts;
mapping(uint256 => mapping(uint256 => uint256)) internal whitelistedBitmaps;
/*///////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor() {
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = _computeDomainSeparator();
}
/*///////////////////////////////////////////////////////////////
EIP-712 LOGIC
//////////////////////////////////////////////////////////////*/
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : _computeDomainSeparator();
}
function _computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
keccak256(bytes('KaliWhitelistManager')),
keccak256('1'),
block.chainid,
address(this)
)
);
}
/*///////////////////////////////////////////////////////////////
WHITELIST LOGIC
//////////////////////////////////////////////////////////////*/
function createWhitelist(
uint256 listId,
address[] calldata accounts,
bytes32 merkleRoot
) public virtual {
if (listId == 0) revert NullId();
if (operatorOf[listId] != address(0)) revert IdExists();
operatorOf[listId] = msg.sender;
if (accounts.length != 0) {
// cannot realistically overflow on human timescales
unchecked {
for (uint256 i; i < accounts.length; i++) {
_whitelistAccount(listId, accounts[i], true);
}
}
emit WhitelistCreated(listId, msg.sender);
}
if (merkleRoot != '') {
merkleRoots[listId] = merkleRoot;
emit MerkleRootSet(listId, merkleRoot);
}
}
function isWhitelisted(uint256 listId, uint256 index) public view virtual returns (bool) {
uint256 whitelistedWordIndex = index / 256;
uint256 whitelistedBitIndex = index % 256;
uint256 claimedWord = whitelistedBitmaps[listId][whitelistedWordIndex];
uint256 mask = 1 << whitelistedBitIndex;
return claimedWord & mask == mask;
}
function whitelistAccounts(
uint256 listId,
address[] calldata accounts,
bool[] calldata approvals
) public virtual {
if (msg.sender != operatorOf[listId]) revert NotOperator();
// cannot realistically overflow on human timescales
unchecked {
for (uint256 i; i < accounts.length; i++) {
_whitelistAccount(listId, accounts[i], approvals[i]);
}
}
}
function whitelistAccountBySig(
uint256 listId,
address account,
bool approved,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
if (block.timestamp > deadline) revert SignatureExpired();
bytes32 digest = keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR(),
keccak256(abi.encode(WHITELIST_TYPEHASH, account, approved, deadline))
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
if (recoveredAddress != operatorOf[listId]) revert InvalidSignature();
_whitelistAccount(listId, account, approved);
}
function _whitelistAccount(
uint256 listId,
address account,
bool approved
) internal virtual {
whitelistedAccounts[listId][account] = approved;
emit AccountWhitelisted(listId, account, approved);
}
/*///////////////////////////////////////////////////////////////
MERKLE LOGIC
//////////////////////////////////////////////////////////////*/
function setMerkleRoot(uint256 listId, bytes32 merkleRoot) public virtual {
if (msg.sender != operatorOf[listId]) revert NotOperator();
merkleRoots[listId] = merkleRoot;
emit MerkleRootSet(listId, merkleRoot);
}
function joinWhitelist(
uint256 listId,
uint256 index,
address account,
bytes32[] calldata merkleProof
) public virtual {
if (isWhitelisted(listId, index)) revert WhitelistClaimed();
bytes32 computedHash;
assembly {
mstore(0x00, index)
mstore(0x20, account)
computedHash := keccak256(0x00, 0x40)
}
for (uint256 i = 0; i < merkleProof.length; i++) {
bytes32 proofElement = merkleProof[i];
if (computedHash <= proofElement) {
computedHash = _efficientHash(computedHash, proofElement);
} else {
computedHash = _efficientHash(proofElement, computedHash);
}
}
if (computedHash != merkleRoots[listId]) revert NotRooted();
uint256 whitelistedWordIndex = index / 256;
uint256 whitelistedBitIndex = index % 256;
whitelistedBitmaps[listId][whitelistedWordIndex] = whitelistedBitmaps[listId][whitelistedWordIndex]
| (1 << whitelistedBitIndex);
_whitelistAccount(listId, account, true);
emit WhitelistJoined(listId, index, account);
}
/// @dev modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol)
function _efficientHash(bytes32 a, bytes32 b) internal pure virtual returns (bytes32 value) {
assembly {
mstore(0x00, a)
mstore(0x20, b)
value := keccak256(0x00, 0x40)
}
}
}