-
Notifications
You must be signed in to change notification settings - Fork 122
/
IdRegistry.sol
493 lines (419 loc) · 14.8 KB
/
IdRegistry.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
import {SignatureChecker} from "openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
import {IIdRegistry} from "./interfaces/IIdRegistry.sol";
import {EIP712} from "./abstract/EIP712.sol";
import {Nonces} from "./abstract/Nonces.sol";
import {Signatures} from "./abstract/Signatures.sol";
import {Migration} from "./abstract/Migration.sol";
/**
* @title Farcaster IdRegistry
*
* @notice See https://github.com/farcasterxyz/contracts/blob/v3.1.0/docs/docs.md for an overview.
*
* @custom:security-contact [email protected]
*/
contract IdRegistry is IIdRegistry, Migration, Signatures, EIP712, Nonces {
/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
string public constant name = "Farcaster FID";
/**
* @inheritdoc IIdRegistry
*/
string public constant VERSION = "2023.11.15";
/**
* @inheritdoc IIdRegistry
*/
bytes32 public constant TRANSFER_TYPEHASH =
keccak256("Transfer(uint256 fid,address to,uint256 nonce,uint256 deadline)");
/**
* @inheritdoc IIdRegistry
*/
bytes32 public constant TRANSFER_AND_CHANGE_RECOVERY_TYPEHASH =
keccak256("TransferAndChangeRecovery(uint256 fid,address to,address recovery,uint256 nonce,uint256 deadline)");
/**
* @inheritdoc IIdRegistry
*/
bytes32 public constant CHANGE_RECOVERY_ADDRESS_TYPEHASH =
keccak256("ChangeRecoveryAddress(uint256 fid,address from,address to,uint256 nonce,uint256 deadline)");
/*//////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
address public idGateway;
/**
* @inheritdoc IIdRegistry
*/
bool public gatewayFrozen;
/**
* @inheritdoc IIdRegistry
*/
uint256 public idCounter;
/**
* @inheritdoc IIdRegistry
*/
mapping(address owner => uint256 fid) public idOf;
/**
* @inheritdoc IIdRegistry
*/
mapping(uint256 fid => address custody) public custodyOf;
/**
* @inheritdoc IIdRegistry
*/
mapping(uint256 fid => address recovery) public recoveryOf;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Set the owner of the contract to the provided _owner.
*
* @param _migrator Migrator address.
* @param _initialOwner Initial owner address.
*
*/
// solhint-disable-next-line no-empty-blocks
constructor(
address _migrator,
address _initialOwner
) Migration(24 hours, _migrator, _initialOwner) EIP712("Farcaster IdRegistry", "1") {}
/*//////////////////////////////////////////////////////////////
REGISTRATION LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
function register(address to, address recovery) external whenNotPaused returns (uint256 fid) {
if (msg.sender != idGateway) revert Unauthorized();
/* Revert if the target(to) has an fid */
if (idOf[to] != 0) revert HasId();
/* Safety: idCounter won't realistically overflow. */
unchecked {
/* Incrementing before assignment ensures that no one gets the 0 fid. */
fid = ++idCounter;
}
_unsafeRegister(fid, to, recovery);
}
/*//////////////////////////////////////////////////////////////
TRANSFER LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
function transfer(address to, uint256 deadline, bytes calldata sig) external {
uint256 fromId = _validateTransfer(msg.sender, to);
/* Revert if signature is invalid */
_verifyTransferSig({fid: fromId, to: to, deadline: deadline, signer: to, sig: sig});
_unsafeTransfer(fromId, msg.sender, to);
}
/**
* @inheritdoc IIdRegistry
*/
function transferFor(
address from,
address to,
uint256 fromDeadline,
bytes calldata fromSig,
uint256 toDeadline,
bytes calldata toSig
) external {
uint256 fromId = _validateTransfer(from, to);
/* Revert if either signature is invalid */
_verifyTransferSig({fid: fromId, to: to, deadline: fromDeadline, signer: from, sig: fromSig});
_verifyTransferSig({fid: fromId, to: to, deadline: toDeadline, signer: to, sig: toSig});
_unsafeTransfer(fromId, from, to);
}
/**
* @inheritdoc IIdRegistry
*/
function transferAndChangeRecovery(address to, address recovery, uint256 deadline, bytes calldata sig) external {
uint256 fromId = _validateTransfer(msg.sender, to);
/* Revert if signature is invalid */
_verifyTransferAndChangeRecoverySig({
fid: fromId,
to: to,
recovery: recovery,
deadline: deadline,
signer: to,
sig: sig
});
_unsafeTransfer(fromId, msg.sender, to);
_unsafeChangeRecovery(fromId, recovery);
}
/**
* @inheritdoc IIdRegistry
*/
function transferAndChangeRecoveryFor(
address from,
address to,
address recovery,
uint256 fromDeadline,
bytes calldata fromSig,
uint256 toDeadline,
bytes calldata toSig
) external {
uint256 fromId = _validateTransfer(from, to);
/* Revert if either signature is invalid */
_verifyTransferAndChangeRecoverySig({
fid: fromId,
to: to,
recovery: recovery,
deadline: fromDeadline,
signer: from,
sig: fromSig
});
_verifyTransferAndChangeRecoverySig({
fid: fromId,
to: to,
recovery: recovery,
deadline: toDeadline,
signer: to,
sig: toSig
});
_unsafeTransfer(fromId, from, to);
_unsafeChangeRecovery(fromId, recovery);
}
/**
* @dev Retrieve fid and validate sender/recipient
*/
function _validateTransfer(address from, address to) internal view returns (uint256 fromId) {
fromId = idOf[from];
/* Revert if the sender has no id */
if (fromId == 0) revert HasNoId();
/* Revert if recipient has an id */
if (idOf[to] != 0) revert HasId();
}
/**
* @dev Register the fid without checking invariants.
*/
function _unsafeRegister(uint256 id, address to, address recovery) internal {
idOf[to] = id;
custodyOf[id] = to;
recoveryOf[id] = recovery;
emit Register(to, id, recovery);
}
/**
* @dev Transfer the fid to another address without checking invariants.
*/
function _unsafeTransfer(uint256 id, address from, address to) internal whenNotPaused {
idOf[to] = id;
custodyOf[id] = to;
delete idOf[from];
emit Transfer(from, to, id);
}
/*//////////////////////////////////////////////////////////////
RECOVERY LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
function changeRecoveryAddress(address recovery) external whenNotPaused {
/* Revert if the caller does not own an fid */
uint256 ownerId = idOf[msg.sender];
if (ownerId == 0) revert HasNoId();
_unsafeChangeRecovery(ownerId, recovery);
}
/**
* @inheritdoc IIdRegistry
*/
function changeRecoveryAddressFor(
address owner,
address recovery,
uint256 deadline,
bytes calldata sig
) external whenNotPaused {
/* Revert if the caller does not own an fid */
uint256 ownerId = idOf[owner];
if (ownerId == 0) revert HasNoId();
_verifyChangeRecoveryAddressSig({
fid: ownerId,
from: recoveryOf[ownerId],
to: recovery,
deadline: deadline,
signer: owner,
sig: sig
});
_unsafeChangeRecovery(ownerId, recovery);
}
/**
* @dev Change recovery address without checking invariants.
*/
function _unsafeChangeRecovery(uint256 id, address recovery) internal whenNotPaused {
/* Change the recovery address */
recoveryOf[id] = recovery;
emit ChangeRecoveryAddress(id, recovery);
}
/**
* @inheritdoc IIdRegistry
*/
function recover(address from, address to, uint256 deadline, bytes calldata sig) external {
/* Revert if from does not own an fid */
uint256 fromId = idOf[from];
if (fromId == 0) revert HasNoId();
/* Revert if the caller is not the recovery address */
address caller = msg.sender;
if (recoveryOf[fromId] != caller) revert Unauthorized();
/* Revert if destination(to) already has an fid */
if (idOf[to] != 0) revert HasId();
/* Revert if signature is invalid */
_verifyTransferSig({fid: fromId, to: to, deadline: deadline, signer: to, sig: sig});
emit Recover(from, to, fromId);
_unsafeTransfer(fromId, from, to);
}
/**
* @inheritdoc IIdRegistry
*/
function recoverFor(
address from,
address to,
uint256 recoveryDeadline,
bytes calldata recoverySig,
uint256 toDeadline,
bytes calldata toSig
) external {
/* Revert if from does not own an fid */
uint256 fromId = idOf[from];
if (fromId == 0) revert HasNoId();
/* Revert if destination(to) already has an fid */
if (idOf[to] != 0) revert HasId();
/* Revert if either signature is invalid */
_verifyTransferSig({
fid: fromId,
to: to,
deadline: recoveryDeadline,
signer: recoveryOf[fromId],
sig: recoverySig
});
_verifyTransferSig({fid: fromId, to: to, deadline: toDeadline, signer: to, sig: toSig});
emit Recover(from, to, fromId);
_unsafeTransfer(fromId, from, to);
}
/*//////////////////////////////////////////////////////////////
PERMISSIONED ACTIONS
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
function setIdGateway(address _idGateway) external onlyOwner {
if (gatewayFrozen) revert GatewayFrozen();
emit SetIdGateway(idGateway, _idGateway);
idGateway = _idGateway;
}
/**
* @inheritdoc IIdRegistry
*/
function freezeIdGateway() external onlyOwner {
if (gatewayFrozen) revert GatewayFrozen();
emit FreezeIdGateway(idGateway);
gatewayFrozen = true;
}
/*//////////////////////////////////////////////////////////////
MIGRATION
//////////////////////////////////////////////////////////////*/
function bulkRegisterIds(BulkRegisterData[] calldata ids) external onlyMigrator {
// Safety: i can be incremented unchecked since it is bound by ids.length.
unchecked {
for (uint256 i = 0; i < ids.length; i++) {
BulkRegisterData calldata id = ids[i];
if (idOf[id.custody] != 0) revert HasId();
_unsafeRegister(id.fid, id.custody, id.recovery);
}
}
}
function bulkRegisterIdsWithDefaultRecovery(
BulkRegisterDefaultRecoveryData[] calldata ids,
address recovery
) external onlyMigrator {
// Safety: i can be incremented unchecked since it is bound by ids.length.
unchecked {
for (uint256 i = 0; i < ids.length; i++) {
BulkRegisterDefaultRecoveryData calldata id = ids[i];
if (idOf[id.custody] != 0) revert HasId();
_unsafeRegister(id.fid, id.custody, recovery);
}
}
}
function bulkResetIds(uint24[] calldata ids) external onlyMigrator {
// Safety: i can be incremented unchecked since it is bound by ids.length.
unchecked {
for (uint256 i = 0; i < ids.length; i++) {
uint256 id = ids[i];
address custody = custodyOf[id];
idOf[custody] = 0;
custodyOf[id] = address(0);
recoveryOf[id] = address(0);
emit AdminReset(id);
}
}
}
function setIdCounter(uint256 _counter) external onlyMigrator {
emit SetIdCounter(idCounter, _counter);
idCounter = _counter;
}
/*//////////////////////////////////////////////////////////////
VIEWS
//////////////////////////////////////////////////////////////*/
/**
* @inheritdoc IIdRegistry
*/
function verifyFidSignature(
address custodyAddress,
uint256 fid,
bytes32 digest,
bytes calldata sig
) external view returns (bool isValid) {
isValid = idOf[custodyAddress] == fid && SignatureChecker.isValidSignatureNow(custodyAddress, digest, sig);
}
/*//////////////////////////////////////////////////////////////
SIGNATURE VERIFICATION HELPERS
//////////////////////////////////////////////////////////////*/
function _verifyTransferSig(uint256 fid, address to, uint256 deadline, address signer, bytes memory sig) internal {
_verifySig(
_hashTypedDataV4(keccak256(abi.encode(TRANSFER_TYPEHASH, fid, to, _useNonce(signer), deadline))),
signer,
deadline,
sig
);
}
function _verifyTransferAndChangeRecoverySig(
uint256 fid,
address to,
address recovery,
uint256 deadline,
address signer,
bytes memory sig
) internal {
_verifySig(
_hashTypedDataV4(
keccak256(
abi.encode(TRANSFER_AND_CHANGE_RECOVERY_TYPEHASH, fid, to, recovery, _useNonce(signer), deadline)
)
),
signer,
deadline,
sig
);
}
function _verifyChangeRecoveryAddressSig(
uint256 fid,
address from,
address to,
uint256 deadline,
address signer,
bytes memory sig
) internal {
_verifySig(
_hashTypedDataV4(
keccak256(abi.encode(CHANGE_RECOVERY_ADDRESS_TYPEHASH, fid, from, to, _useNonce(signer), deadline))
),
signer,
deadline,
sig
);
}
}