forked from delegatexyz/delegate-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelegationRegistry.sol
380 lines (349 loc) · 14.3 KB
/
DelegationRegistry.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
import {IDelegationRegistry} from "./IDelegationRegistry.sol";
import {EnumerableSet} from "openzeppelin-contracts/contracts/utils/structs/EnumerableSet.sol";
import {ERC165} from "openzeppelin-contracts/contracts/utils/introspection/ERC165.sol";
/**
* @title DelegationRegistry
* @custom:version 0.1
* @notice An immutable registry contract to be deployed as a standalone primitive.
* New project launches can read previous cold wallet -> hot wallet delegations
* from here and integrate those permissions into their flow.
* @custom:coauthor foobar (0xfoobar)
* @custom:coauthor wwchung (manifoldxyz)
* @custom:coauthor purplehat (artblocks)
* @custom:coauthor andy8052 (tessera)
* @custom:coauthor punk6529 (open metaverse)
* @custom:coauthor loopify (loopiverse)
* @custom:coauthor emiliano (nftrentals)
* @custom:coauthor arran (proof)
* @custom:coauthor james (collabland)
* @custom:coauthor john (gnosis safe)
* @custom:coauthor 0xrusowsky
*/
contract DelegationRegistry is IDelegationRegistry, ERC165 {
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.Bytes32Set;
/// @notice The global mapping and single source of truth for delegations
mapping(bytes32 => bool) internal delegations;
/// @notice A mapping of wallets to versions (for cheap revocation)
mapping(address => uint256) internal vaultVersion;
/// @notice A mapping of wallets to delegates to versions (for cheap revocation)
mapping(address => mapping(address => uint256)) internal delegateVersion;
/// @notice A secondary mapping to return onchain enumerability of wallet-level delegations
/// @dev vault -> vaultVersion -> delegates
mapping(address => mapping(uint256 => EnumerableSet.AddressSet)) internal delegationsForAll;
/// @notice A secondary mapping to return onchain enumerability of contract-level delegations
/// @dev vault -> vaultVersion -> contract -> delegates
mapping(address => mapping(uint256 => mapping(address => EnumerableSet.AddressSet))) internal delegationsForContract;
/// @notice A secondary mapping to return onchain enumerability of token-level delegations
/// @dev vault -> vaultVersion -> contract -> tokenId -> delegates
mapping(address => mapping(uint256 => mapping(address => mapping(uint256 => EnumerableSet.AddressSet)))) internal
delegationsForToken;
/// @notice A secondary mapping to return onchain enumerability of delegations that a given address can perform
/// @dev delegate -> delegationHashes
mapping(address => EnumerableSet.Bytes32Set) internal delegationHashes;
/// @notice A secondary mapping used to return delegation information about a delegation
/// @dev delegationHash -> DelegateInfo
mapping(bytes32 => IDelegationRegistry.DelegationInfo) internal delegationInfo;
/**
* @inheritdoc ERC165
*/
function supportsInterface(bytes4 interfaceId) public view virtual override (ERC165) returns (bool) {
return interfaceId == type(IDelegationRegistry).interfaceId || super.supportsInterface(interfaceId);
}
/**
* ----------- WRITE -----------
*/
/**
* @inheritdoc IDelegationRegistry
*/
function delegateForAll(address delegate, bool value) external override {
bytes32 delegationHash = _computeAllDelegationHash(msg.sender, delegate);
_setDelegationValues(
delegate,
delegationHash,
delegationsForAll[msg.sender][vaultVersion[msg.sender]],
value,
IDelegationRegistry.DelegationType.ALL,
msg.sender,
address(0),
0
);
emit IDelegationRegistry.DelegateForAll(msg.sender, delegate, value);
}
/**
* @inheritdoc IDelegationRegistry
*/
function delegateForContract(address delegate, address contract_, bool value) external override {
bytes32 delegationHash = _computeContractDelegationHash(msg.sender, delegate, contract_);
_setDelegationValues(
delegate,
delegationHash,
delegationsForContract[msg.sender][vaultVersion[msg.sender]][contract_],
value,
IDelegationRegistry.DelegationType.CONTRACT,
msg.sender,
contract_,
0
);
emit IDelegationRegistry.DelegateForContract(msg.sender, delegate, contract_, value);
}
/**
* @inheritdoc IDelegationRegistry
*/
function delegateForToken(address delegate, address contract_, uint256 tokenId, bool value) external override {
bytes32 delegationHash = _computeTokenDelegationHash(msg.sender, delegate, contract_, tokenId);
_setDelegationValues(
delegate,
delegationHash,
delegationsForToken[msg.sender][vaultVersion[msg.sender]][contract_][tokenId],
value,
IDelegationRegistry.DelegationType.TOKEN,
msg.sender,
contract_,
tokenId
);
emit IDelegationRegistry.DelegateForToken(msg.sender, delegate, contract_, tokenId, value);
}
/**
* @dev Helper function to set all delegation values and enumeration sets
*/
function _setDelegationValues(
address delegate,
bytes32 delegateHash,
EnumerableSet.AddressSet storage enumerationSet,
bool value,
IDelegationRegistry.DelegationType type_,
address vault,
address contract_,
uint256 tokenId
)
internal
{
delegations[delegateHash] = value;
if (value) {
delegationHashes[delegate].add(delegateHash);
delegationInfo[delegateHash] =
DelegationInfo({vault: vault, type_: type_, contract_: contract_, tokenId: tokenId});
enumerationSet.add(delegate);
} else {
delegationHashes[delegate].remove(delegateHash);
delete delegationInfo[delegateHash];
enumerationSet.remove(delegate);
}
}
/**
* @dev Helper function to compute delegation hash for wallet delegation
*/
function _computeAllDelegationHash(address vault, address delegate) internal view returns (bytes32) {
uint256 vaultVersion_ = vaultVersion[vault];
uint256 delegateVersion_ = delegateVersion[vault][delegate];
return keccak256(abi.encode(delegate, vault, vaultVersion_, delegateVersion_));
}
/**
* @dev Helper function to compute delegation hash for contract delegation
*/
function _computeContractDelegationHash(address vault, address delegate, address contract_)
internal
view
returns (bytes32)
{
uint256 vaultVersion_ = vaultVersion[vault];
uint256 delegateVersion_ = delegateVersion[vault][delegate];
return keccak256(abi.encode(delegate, vault, contract_, vaultVersion_, delegateVersion_));
}
/**
* @dev Helper function to compute delegation hash for token delegation
*/
function _computeTokenDelegationHash(address vault, address delegate, address contract_, uint256 tokenId)
internal
view
returns (bytes32)
{
uint256 vaultVersion_ = vaultVersion[vault];
uint256 delegateVersion_ = delegateVersion[vault][delegate];
return keccak256(abi.encode(delegate, vault, contract_, tokenId, vaultVersion_, delegateVersion_));
}
/**
* @inheritdoc IDelegationRegistry
*/
function revokeAllDelegates() external override {
++vaultVersion[msg.sender];
emit IDelegationRegistry.RevokeAllDelegates(msg.sender);
}
/**
* @inheritdoc IDelegationRegistry
*/
function revokeDelegate(address delegate) external override {
_revokeDelegate(delegate, msg.sender);
}
/**
* @inheritdoc IDelegationRegistry
*/
function revokeSelf(address vault) external override {
_revokeDelegate(msg.sender, vault);
}
/**
* @dev Revoke the `delegate` hotwallet from the `vault` coldwallet.
*/
function _revokeDelegate(address delegate, address vault) internal {
++delegateVersion[vault][delegate];
// Remove delegate from enumerations
delegationsForAll[vault][vaultVersion[vault]].remove(delegate);
// For delegationsForContract and delegationsForToken, filter in the view functions
emit IDelegationRegistry.RevokeDelegate(vault, msg.sender);
}
/**
* ----------- READ -----------
*/
/**
* @inheritdoc IDelegationRegistry
*/
function getDelegationsByDelegate(address delegate)
external
view
returns (IDelegationRegistry.DelegationInfo[] memory info)
{
EnumerableSet.Bytes32Set storage potentialDelegationHashes = delegationHashes[delegate];
uint256 potentialDelegationHashesLength = potentialDelegationHashes.length();
uint256 delegationCount = 0;
info = new IDelegationRegistry.DelegationInfo[](potentialDelegationHashesLength);
for (uint256 i = 0; i < potentialDelegationHashesLength;) {
bytes32 delegateHash = potentialDelegationHashes.at(i);
IDelegationRegistry.DelegationInfo memory delegationInfo_ = delegationInfo[delegateHash];
address vault = delegationInfo_.vault;
IDelegationRegistry.DelegationType type_ = delegationInfo_.type_;
bool valid = false;
if (type_ == IDelegationRegistry.DelegationType.ALL) {
if (delegateHash == _computeAllDelegationHash(vault, delegate)) {
valid = true;
}
} else if (type_ == IDelegationRegistry.DelegationType.CONTRACT) {
if (delegateHash == _computeContractDelegationHash(vault, delegate, delegationInfo_.contract_)) {
valid = true;
}
} else if (type_ == IDelegationRegistry.DelegationType.TOKEN) {
if (
delegateHash
== _computeTokenDelegationHash(vault, delegate, delegationInfo_.contract_, delegationInfo_.tokenId)
) {
valid = true;
}
}
if (valid) {
info[delegationCount++] = delegationInfo_;
}
unchecked {
++i;
}
}
if (potentialDelegationHashesLength > delegationCount) {
assembly {
let decrease := sub(potentialDelegationHashesLength, delegationCount)
mstore(info, sub(mload(info), decrease))
}
}
}
/**
* @inheritdoc IDelegationRegistry
*/
function getDelegatesForAll(address vault) external view returns (address[] memory) {
return delegationsForAll[vault][vaultVersion[vault]].values();
}
/**
* @inheritdoc IDelegationRegistry
*/
function getDelegatesForContract(address vault, address contract_)
external
view
override
returns (address[] memory delegates)
{
EnumerableSet.AddressSet storage potentialDelegates =
delegationsForContract[vault][vaultVersion[vault]][contract_];
uint256 potentialDelegatesLength = potentialDelegates.length();
uint256 delegateCount = 0;
delegates = new address[](potentialDelegatesLength);
for (uint256 i = 0; i < potentialDelegatesLength;) {
if (checkDelegateForContract(potentialDelegates.at(i), vault, contract_)) {
delegates[delegateCount++] = potentialDelegates.at(i);
}
unchecked {
++i;
}
}
if (potentialDelegatesLength > delegateCount) {
assembly {
let decrease := sub(potentialDelegatesLength, delegateCount)
mstore(delegates, sub(mload(delegates), decrease))
}
}
}
/**
* @inheritdoc IDelegationRegistry
*/
function getDelegatesForToken(address vault, address contract_, uint256 tokenId)
external
view
override
returns (address[] memory delegates)
{
// Since we cannot easily invalidate delegates on the enumeration (see revokeDelegates)
// we will need to filter out invalid entries
EnumerableSet.AddressSet storage potentialDelegates =
delegationsForToken[vault][vaultVersion[vault]][contract_][tokenId];
uint256 potentialDelegatesLength = potentialDelegates.length();
uint256 delegateCount = 0;
delegates = new address[](potentialDelegatesLength);
for (uint256 i = 0; i < potentialDelegatesLength;) {
if (checkDelegateForToken(potentialDelegates.at(i), vault, contract_, tokenId)) {
delegates[delegateCount++] = potentialDelegates.at(i);
}
unchecked {
++i;
}
}
if (potentialDelegatesLength > delegateCount) {
assembly {
let decrease := sub(potentialDelegatesLength, delegateCount)
mstore(delegates, sub(mload(delegates), decrease))
}
}
}
/**
* @inheritdoc IDelegationRegistry
*/
function checkDelegateForAll(address delegate, address vault) public view override returns (bool) {
bytes32 delegateHash =
keccak256(abi.encode(delegate, vault, vaultVersion[vault], delegateVersion[vault][delegate]));
return delegations[delegateHash];
}
/**
* @inheritdoc IDelegationRegistry
*/
function checkDelegateForContract(address delegate, address vault, address contract_)
public
view
override
returns (bool)
{
bytes32 delegateHash =
keccak256(abi.encode(delegate, vault, contract_, vaultVersion[vault], delegateVersion[vault][delegate]));
return delegations[delegateHash] ? true : checkDelegateForAll(delegate, vault);
}
/**
* @inheritdoc IDelegationRegistry
*/
function checkDelegateForToken(address delegate, address vault, address contract_, uint256 tokenId)
public
view
override
returns (bool)
{
bytes32 delegateHash = keccak256(
abi.encode(delegate, vault, contract_, tokenId, vaultVersion[vault], delegateVersion[vault][delegate])
);
return delegations[delegateHash] ? true : checkDelegateForContract(delegate, vault, contract_);
}
}