-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQuickJoin.sol
More file actions
515 lines (430 loc) · 20.4 KB
/
QuickJoin.sol
File metadata and controls
515 lines (430 loc) · 20.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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.21;
/*────────────────────────── OpenZeppelin v5.3 Upgradeables ────────────────────*/
import {Initializable} from "@openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol";
import {ContextUpgradeable} from "@openzeppelin-contracts-upgradeable/contracts/utils/ContextUpgradeable.sol";
import {
ReentrancyGuardUpgradeable
} from "@openzeppelin-contracts-upgradeable/contracts/utils/ReentrancyGuardUpgradeable.sol";
/*───────────────────────── Interface minimal stubs ───────────────────────*/
import {IHats} from "@hats-protocol/src/Interfaces/IHats.sol";
import {HatManager} from "./libs/HatManager.sol";
import {WebAuthnLib} from "./libs/WebAuthnLib.sol";
interface IUniversalAccountRegistry {
function getUsername(address account) external view returns (string memory);
function registerAccountBySig(
address user,
string calldata username,
uint256 deadline,
uint256 nonce,
bytes calldata signature
) external;
function registerAccountByPasskeySig(
bytes32 credentialId,
bytes32 pubKeyX,
bytes32 pubKeyY,
uint256 salt,
string calldata username,
uint256 deadline,
uint256 nonce,
WebAuthnLib.WebAuthnAuth calldata auth
) external;
}
interface IExecutorHatMinter {
function mintHatsForUser(address user, uint256[] calldata hatIds) external;
}
interface IUniversalPasskeyAccountFactory {
function createAccount(bytes32 credentialId, bytes32 pubKeyX, bytes32 pubKeyY, uint256 salt)
external
returns (address account);
}
/*────────────────────────────── Contract ───────────────────────────────*/
contract QuickJoin is Initializable, ContextUpgradeable, ReentrancyGuardUpgradeable {
/* ───────── Errors ───────── */
error InvalidAddress();
error OnlyMasterDeploy();
error ZeroUser();
error NoUsername();
error Unauthorized();
error PasskeyFactoryNotSet();
/* ───────── Constants ────── */
bytes4 public constant MODULE_ID = bytes4(keccak256("QuickJoin"));
/* ───────── ERC-7201 Storage ──────── */
/// @custom:storage-location erc7201:poa.quickjoin.storage
struct Layout {
IHats hats;
IUniversalAccountRegistry accountRegistry;
address masterDeployAddress;
address executor;
uint256[] memberHatIds; // hat IDs to mint when users join
IUniversalPasskeyAccountFactory universalFactory; // Universal factory for passkey accounts
}
/* ───────── Passkey Enrollment Struct ──────── */
struct PasskeyEnrollment {
bytes32 credentialId;
bytes32 publicKeyX;
bytes32 publicKeyY;
uint256 salt;
}
bytes32 private constant _STORAGE_SLOT = keccak256("poa.quickjoin.storage");
function _layout() private pure returns (Layout storage s) {
bytes32 slot = _STORAGE_SLOT;
assembly {
s.slot := slot
}
}
/* ───────── Events ───────── */
event AddressesUpdated(address hats, address registry, address master);
event ExecutorUpdated(address newExecutor);
event MemberHatIdsUpdated(uint256[] hatIds);
event QuickJoined(address indexed user, uint256[] hatIds);
event QuickJoinedByMaster(address indexed master, address indexed user, uint256[] hatIds);
event UniversalFactoryUpdated(address indexed universalFactory);
event QuickJoinedWithPasskeyByMaster(
address indexed master, address indexed account, bytes32 indexed credentialId, uint256[] hatIds
);
event RegisterAndQuickJoined(address indexed user, string username, uint256[] hatIds);
event RegisterAndQuickJoinedWithPasskey(
address indexed account, bytes32 indexed credentialId, string username, uint256[] hatIds
);
event RegisterAndQuickJoinedWithPasskeyByMaster(
address indexed master, address indexed account, bytes32 indexed credentialId, string username, uint256[] hatIds
);
event HatsClaimed(address indexed user, uint256[] claimHatIds);
event RegisterAndClaimedHats(address indexed user, string username, uint256[] claimHatIds);
event RegisterAndClaimedHatsWithPasskey(
address indexed account, bytes32 indexed credentialId, string username, uint256[] claimHatIds
);
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/* ───────── Initialiser ───── */
function initialize(
address executor_,
address hats_,
address accountRegistry_,
address masterDeploy_,
uint256[] calldata memberHatIds_
) external initializer {
if (
executor_ == address(0) || hats_ == address(0) || accountRegistry_ == address(0)
|| masterDeploy_ == address(0)
) revert InvalidAddress();
__Context_init();
__ReentrancyGuard_init();
Layout storage l = _layout();
l.executor = executor_;
l.hats = IHats(hats_);
l.accountRegistry = IUniversalAccountRegistry(accountRegistry_);
l.masterDeployAddress = masterDeploy_;
// Set member hat IDs using HatManager
for (uint256 i = 0; i < memberHatIds_.length; i++) {
HatManager.setHatInArray(l.memberHatIds, memberHatIds_[i], true);
}
emit AddressesUpdated(hats_, accountRegistry_, masterDeploy_);
emit ExecutorUpdated(executor_);
emit MemberHatIdsUpdated(memberHatIds_);
}
/* ───────── Modifiers ─────── */
modifier onlyMasterDeploy() {
Layout storage l = _layout();
if (_msgSender() != l.executor && _msgSender() != l.masterDeployAddress) revert OnlyMasterDeploy();
_;
}
modifier onlyExecutor() {
if (_msgSender() != _layout().executor) revert Unauthorized();
_;
}
/* ─────── Admin / DAO setters (executor-gated) ─────── */
function updateAddresses(address hats_, address accountRegistry_, address masterDeploy_) external onlyExecutor {
if (hats_ == address(0) || accountRegistry_ == address(0) || masterDeploy_ == address(0)) {
revert InvalidAddress();
}
Layout storage l = _layout();
l.hats = IHats(hats_);
l.accountRegistry = IUniversalAccountRegistry(accountRegistry_);
l.masterDeployAddress = masterDeploy_;
emit AddressesUpdated(hats_, accountRegistry_, masterDeploy_);
}
function updateMemberHatIds(uint256[] calldata memberHatIds_) external onlyExecutor {
Layout storage l = _layout();
// Clear existing hat IDs using HatManager
HatManager.clearHatArray(l.memberHatIds);
// Set new hat IDs using HatManager
for (uint256 i = 0; i < memberHatIds_.length; i++) {
HatManager.setHatInArray(l.memberHatIds, memberHatIds_[i], true);
}
emit MemberHatIdsUpdated(memberHatIds_);
}
function setExecutor(address newExec) external onlyExecutor {
if (newExec == address(0)) revert InvalidAddress();
_layout().executor = newExec;
emit ExecutorUpdated(newExec);
}
function setUniversalFactory(address factory) external onlyMasterDeploy {
_layout().universalFactory = IUniversalPasskeyAccountFactory(factory);
emit UniversalFactoryUpdated(factory);
}
/* ───────── Internal helper ─────── */
function _quickJoin(address user) private nonReentrant {
if (user == address(0)) revert ZeroUser();
Layout storage l = _layout();
// Request executor to mint all configured member hats to the user
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(user, l.memberHatIds);
}
emit QuickJoined(user, l.memberHatIds);
}
/* ───────── Public user paths ─────── */
/// caller already registered a username elsewhere
function quickJoinWithUser() external nonReentrant {
Layout storage l = _layout();
string memory existing = l.accountRegistry.getUsername(_msgSender());
if (bytes(existing).length == 0) revert NoUsername();
// Request executor to mint all configured member hats to the user
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(_msgSender(), l.memberHatIds);
}
emit QuickJoined(_msgSender(), l.memberHatIds);
}
/* ───────── Passkey join paths ─────── */
/// @notice Master-deploy path for passkey onboarding
/// @param passkey Passkey enrollment data
/// @return account The created passkey account address
function quickJoinWithPasskeyMasterDeploy(PasskeyEnrollment calldata passkey)
external
onlyMasterDeploy
nonReentrant
returns (address account)
{
Layout storage l = _layout();
if (address(l.universalFactory) == address(0)) revert PasskeyFactoryNotSet();
// 1. Create PasskeyAccount via universal factory (returns existing if already deployed)
account = l.universalFactory
.createAccount(passkey.credentialId, passkey.publicKeyX, passkey.publicKeyY, passkey.salt);
// 2. Mint member hats to the account
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(account, l.memberHatIds);
}
emit QuickJoinedWithPasskeyByMaster(_msgSender(), account, passkey.credentialId, l.memberHatIds);
}
/* ───────── Register + join paths ─────── */
/// @notice Register a username and join the org in one transaction (EOA users).
/// @dev The sponsor (msg.sender) pays gas; the user proves consent via EIP-712 signature.
/// @param user The EOA address to register and onboard.
/// @param username The desired username.
/// @param deadline Signature expiration timestamp.
/// @param nonce The user's current nonce on the registry.
/// @param signature The user's EIP-712 signature authorizing registration.
function registerAndQuickJoin(
address user,
string calldata username,
uint256 deadline,
uint256 nonce,
bytes calldata signature
) external nonReentrant {
if (user == address(0)) revert ZeroUser();
Layout storage l = _layout();
// 1. Register the username via signature (reverts if sig invalid)
l.accountRegistry.registerAccountBySig(user, username, deadline, nonce, signature);
// 2. Mint member hats
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(user, l.memberHatIds);
}
emit RegisterAndQuickJoined(user, username, l.memberHatIds);
}
/// @notice Create a passkey account, register a username, and join the org in one transaction.
/// @dev The sponsor pays gas; the user proves consent via WebAuthn passkey assertion.
/// The account address is derived from the passkey enrollment data (never passed in).
/// @param passkey Passkey enrollment data (credentialId, publicKeyX, publicKeyY, salt).
/// @param username The desired username for the new passkey account.
/// @param deadline Assertion expiration timestamp.
/// @param nonce The account's current nonce on the registry.
/// @param auth The WebAuthn assertion data proving passkey ownership.
/// @return account The created/existing passkey account address.
function registerAndQuickJoinWithPasskey(
PasskeyEnrollment calldata passkey,
string calldata username,
uint256 deadline,
uint256 nonce,
WebAuthnLib.WebAuthnAuth calldata auth
) external nonReentrant returns (address account) {
Layout storage l = _layout();
if (address(l.universalFactory) == address(0)) revert PasskeyFactoryNotSet();
// 1. Register the username via passkey sig (reverts if invalid)
l.accountRegistry
.registerAccountByPasskeySig(
passkey.credentialId,
passkey.publicKeyX,
passkey.publicKeyY,
passkey.salt,
username,
deadline,
nonce,
auth
);
// 2. Create PasskeyAccount (returns existing if already deployed)
account = l.universalFactory
.createAccount(passkey.credentialId, passkey.publicKeyX, passkey.publicKeyY, passkey.salt);
// 3. Mint member hats
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(account, l.memberHatIds);
}
emit RegisterAndQuickJoinedWithPasskey(account, passkey.credentialId, username, l.memberHatIds);
}
/* ───────── Vouch-claim paths: mint caller-specified hats ─────── */
/// @notice Claim specific hats for an EOA user who already has a username.
/// @dev Used by vouch-first flow: user was vouched, now claims the specific hat(s).
/// Hats Protocol enforces eligibility via EligibilityModule — if the user
/// isn't vouched/eligible for a hat, mintHat reverts with NotEligible.
/// @param claimHatIds Hat IDs to mint (e.g., the Executive hat the user was vouched for)
function claimHatsWithUser(uint256[] calldata claimHatIds) external nonReentrant {
Layout storage l = _layout();
string memory existing = l.accountRegistry.getUsername(_msgSender());
if (bytes(existing).length == 0) revert NoUsername();
if (claimHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(_msgSender(), claimHatIds);
}
emit HatsClaimed(_msgSender(), claimHatIds);
}
/// @notice Register username + claim specific hats for an EOA user.
/// @param user The EOA address to register and mint hats to.
/// @param username The desired username.
/// @param deadline EIP-712 signature deadline.
/// @param nonce User's current nonce on the registry.
/// @param signature EIP-712 ECDSA signature for registration.
/// @param claimHatIds Hat IDs to mint.
function registerAndClaimHats(
address user,
string calldata username,
uint256 deadline,
uint256 nonce,
bytes calldata signature,
uint256[] calldata claimHatIds
) external nonReentrant {
if (user == address(0)) revert ZeroUser();
Layout storage l = _layout();
// 1. Register username
l.accountRegistry.registerAccountBySig(user, username, deadline, nonce, signature);
// 2. Mint claimed hats
if (claimHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(user, claimHatIds);
}
emit RegisterAndClaimedHats(user, username, claimHatIds);
}
/// @notice Create passkey account, register username, and claim specific hats.
/// @param passkey Passkey enrollment data.
/// @param username The desired username.
/// @param deadline Assertion expiration timestamp.
/// @param nonce Account's current nonce on the registry.
/// @param auth WebAuthn assertion data proving passkey ownership.
/// @param claimHatIds Hat IDs to mint.
/// @return account The created/existing passkey account address.
function registerAndClaimHatsWithPasskey(
PasskeyEnrollment calldata passkey,
string calldata username,
uint256 deadline,
uint256 nonce,
WebAuthnLib.WebAuthnAuth calldata auth,
uint256[] calldata claimHatIds
) external nonReentrant returns (address account) {
Layout storage l = _layout();
if (address(l.universalFactory) == address(0)) revert PasskeyFactoryNotSet();
// 1. Register username via passkey sig
l.accountRegistry
.registerAccountByPasskeySig(
passkey.credentialId,
passkey.publicKeyX,
passkey.publicKeyY,
passkey.salt,
username,
deadline,
nonce,
auth
);
// 2. Create PasskeyAccount (returns existing if already deployed)
account = l.universalFactory
.createAccount(passkey.credentialId, passkey.publicKeyX, passkey.publicKeyY, passkey.salt);
// 3. Mint claimed hats
if (claimHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(account, claimHatIds);
}
emit RegisterAndClaimedHatsWithPasskey(account, passkey.credentialId, username, claimHatIds);
}
/// @notice Master-deploy path: create passkey account, register username, and join.
function registerAndQuickJoinWithPasskeyMasterDeploy(
PasskeyEnrollment calldata passkey,
string calldata username,
uint256 deadline,
uint256 nonce,
WebAuthnLib.WebAuthnAuth calldata auth
) external onlyMasterDeploy nonReentrant returns (address account) {
Layout storage l = _layout();
if (address(l.universalFactory) == address(0)) revert PasskeyFactoryNotSet();
// 1. Register the username via passkey sig (reverts if invalid)
l.accountRegistry
.registerAccountByPasskeySig(
passkey.credentialId,
passkey.publicKeyX,
passkey.publicKeyY,
passkey.salt,
username,
deadline,
nonce,
auth
);
// 2. Create PasskeyAccount
account = l.universalFactory
.createAccount(passkey.credentialId, passkey.publicKeyX, passkey.publicKeyY, passkey.salt);
// 3. Mint member hats
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(account, l.memberHatIds);
}
emit RegisterAndQuickJoinedWithPasskeyByMaster(
_msgSender(), account, passkey.credentialId, username, l.memberHatIds
);
}
/* ───────── Master-deploy helper paths ─────── */
function quickJoinNoUserMasterDeploy(address newUser) external onlyMasterDeploy {
_quickJoin(newUser);
emit QuickJoinedByMaster(_msgSender(), newUser, _layout().memberHatIds);
}
function quickJoinWithUserMasterDeploy(address newUser) external onlyMasterDeploy nonReentrant {
if (newUser == address(0)) revert ZeroUser();
Layout storage l = _layout();
string memory existing = l.accountRegistry.getUsername(newUser);
if (bytes(existing).length == 0) revert NoUsername();
// Request executor to mint all configured member hats to the user
if (l.memberHatIds.length > 0) {
IExecutorHatMinter(l.executor).mintHatsForUser(newUser, l.memberHatIds);
}
emit QuickJoinedByMaster(_msgSender(), newUser, l.memberHatIds);
}
/* ───────── Misc view helpers ─────── */
function memberHatIds() external view returns (uint256[] memory) {
return HatManager.getHatArray(_layout().memberHatIds);
}
function hats() external view returns (IHats) {
return _layout().hats;
}
function accountRegistry() external view returns (IUniversalAccountRegistry) {
return _layout().accountRegistry;
}
function executor() external view returns (address) {
return _layout().executor;
}
function masterDeployAddress() external view returns (address) {
return _layout().masterDeployAddress;
}
/* ───────── Hat Management View Functions ─────────── */
function memberHatCount() external view returns (uint256) {
return HatManager.getHatCount(_layout().memberHatIds);
}
function isMemberHat(uint256 hatId) external view returns (bool) {
return HatManager.isHatInArray(_layout().memberHatIds, hatId);
}
function universalFactory() external view returns (IUniversalPasskeyAccountFactory) {
return _layout().universalFactory;
}
}