forked from delegatexyz/delegate-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelegationRegistry.sol
446 lines (415 loc) · 17.6 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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 ryley-o (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
/// @dev vault -> vaultVersion -> delegationHash
mapping(address => mapping(uint256 => EnumerableSet.Bytes32Set)) 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 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, 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, 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, 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,
bool value,
IDelegationRegistry.DelegationType type_,
address vault,
address contract_,
uint256 tokenId
)
internal
{
if (value) {
delegations[vault][vaultVersion[vault]].add(delegateHash);
delegationHashes[delegate].add(delegateHash);
delegationInfo[delegateHash] =
DelegationInfo({vault: vault, delegate: delegate, type_: type_, contract_: contract_, tokenId: tokenId});
} else {
delegations[vault][vaultVersion[vault]].remove(delegateHash);
delegationHashes[delegate].remove(delegateHash);
delete delegationInfo[delegateHash];
}
}
/**
* @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];
// For enumerations, 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 delegates) {
return _getDelegatesForLevel(vault, IDelegationRegistry.DelegationType.ALL, address(0), 0);
}
/**
* @inheritdoc IDelegationRegistry
*/
function getDelegatesForContract(address vault, address contract_)
external
view
override
returns (address[] memory delegates)
{
return _getDelegatesForLevel(vault, IDelegationRegistry.DelegationType.CONTRACT, contract_, 0);
}
/**
* @inheritdoc IDelegationRegistry
*/
function getDelegatesForToken(address vault, address contract_, uint256 tokenId)
external
view
override
returns (address[] memory delegates)
{
return _getDelegatesForLevel(vault, IDelegationRegistry.DelegationType.TOKEN, contract_, tokenId);
}
function _getDelegatesForLevel(
address vault,
IDelegationRegistry.DelegationType delegationType,
address contract_,
uint256 tokenId
)
internal
view
returns (address[] memory delegates)
{
EnumerableSet.Bytes32Set storage delegationHashes_ = delegations[vault][vaultVersion[vault]];
uint256 potentialDelegatesLength = delegationHashes_.length();
uint256 delegatesCount = 0;
delegates = new address[](potentialDelegatesLength);
for (uint256 i = 0; i < potentialDelegatesLength;) {
bytes32 delegationHash = delegationHashes_.at(i);
DelegationInfo storage delegationInfo_ = delegationInfo[delegationHash];
if (delegationInfo_.type_ == delegationType) {
if (delegationType == IDelegationRegistry.DelegationType.ALL) {
// check delegate version by validating the hash
if (delegationHash == _computeAllDelegationHash(vault, delegationInfo_.delegate)) {
delegates[delegatesCount++] = delegationInfo_.delegate;
}
} else if (delegationType == IDelegationRegistry.DelegationType.CONTRACT) {
if (delegationInfo_.contract_ == contract_) {
// check delegate version by validating the hash
if (
delegationHash == _computeContractDelegationHash(vault, delegationInfo_.delegate, contract_)
) {
delegates[delegatesCount++] = delegationInfo_.delegate;
}
}
} else if (delegationType == IDelegationRegistry.DelegationType.TOKEN) {
if (delegationInfo_.contract_ == contract_ && delegationInfo_.tokenId == tokenId) {
// check delegate version by validating the hash
if (
delegationHash
== _computeTokenDelegationHash(vault, delegationInfo_.delegate, contract_, tokenId)
) {
delegates[delegatesCount++] = delegationInfo_.delegate;
}
}
}
}
unchecked {
++i;
}
}
if (potentialDelegatesLength > delegatesCount) {
assembly {
let decrease := sub(potentialDelegatesLength, delegatesCount)
mstore(delegates, sub(mload(delegates), decrease))
}
}
}
/**
* @inheritdoc IDelegationRegistry
*/
function getContractLevelDelegations(address vault)
external
view
returns (address[] memory contracts, address[] memory delegates)
{
EnumerableSet.Bytes32Set storage delegationHashes_ = delegations[vault][vaultVersion[vault]];
uint256 potentialLength = delegationHashes_.length();
uint256 delegationCount = 0;
contracts = new address[](potentialLength);
delegates = new address[](potentialLength);
for (uint256 i = 0; i < potentialLength;) {
bytes32 delegationHash = delegationHashes_.at(i);
DelegationInfo storage delegationInfo_ = delegationInfo[delegationHash];
if (delegationInfo_.type_ == IDelegationRegistry.DelegationType.CONTRACT) {
// check delegate version by validating the hash
if (
delegationHash
== _computeContractDelegationHash(vault, delegationInfo_.delegate, delegationInfo_.contract_)
) {
contracts[delegationCount] = delegationInfo_.contract_;
delegates[delegationCount++] = delegationInfo_.delegate;
}
}
unchecked {
++i;
}
}
if (potentialLength > delegationCount) {
assembly {
let decrease := sub(potentialLength, delegationCount)
mstore(contracts, sub(mload(delegates), decrease))
mstore(delegates, sub(mload(delegates), decrease))
}
}
}
/**
* @inheritdoc IDelegationRegistry
*/
function getTokenLevelDelegations(address vault)
external
view
returns (address[] memory contracts, uint256[] memory tokenIds, address[] memory delegates)
{
EnumerableSet.Bytes32Set storage delegationHashes_ = delegations[vault][vaultVersion[vault]];
uint256 potentialLength = delegationHashes_.length();
uint256 delegationCount = 0;
contracts = new address[](potentialLength);
tokenIds = new uint256[](potentialLength);
delegates = new address[](potentialLength);
for (uint256 i = 0; i < potentialLength;) {
bytes32 delegationHash = delegationHashes_.at(i);
DelegationInfo storage delegationInfo_ = delegationInfo[delegationHash];
if (delegationInfo_.type_ == IDelegationRegistry.DelegationType.TOKEN) {
// check delegate version by validating the hash
if (
delegationHash
== _computeTokenDelegationHash(vault, delegationInfo_.delegate, delegationInfo_.contract_, delegationInfo_.tokenId)
) {
contracts[delegationCount] = delegationInfo_.contract_;
tokenIds[delegationCount] = delegationInfo_.tokenId;
delegates[delegationCount++] = delegationInfo_.delegate;
}
}
unchecked {
++i;
}
}
if (potentialLength > delegationCount) {
assembly {
let decrease := sub(potentialLength, delegationCount)
mstore(contracts, sub(mload(delegates), decrease))
mstore(tokenIds, sub(mload(delegates), decrease))
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[vault][vaultVersion[vault]].contains(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[vault][vaultVersion[vault]].contains(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[vault][vaultVersion[vault]].contains(delegateHash)
? true
: checkDelegateForContract(delegate, vault, contract_);
}
}