@bloxchain/contracts 1.0.0-alpha.13 → 1.0.0-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,394 +1,394 @@
1
- // SPDX-License-Identifier: MPL-2.0
2
- pragma solidity 0.8.34;
3
-
4
- // Contracts imports
5
- import "../base/BaseStateMachine.sol";
6
- import "./lib/definitions/SecureOwnableDefinitions.sol";
7
- import "../lib/interfaces/IDefinition.sol";
8
- import "../lib/utils/SharedValidation.sol";
9
- import "./interface/ISecureOwnable.sol";
10
-
11
- /**
12
- * @title SecureOwnable
13
- * @dev Security-focused contract extending BaseStateMachine with ownership management
14
- *
15
- * SecureOwnable provides security-specific functionality built on top of the base state machine:
16
- * - Multi-role security model with Owner, Broadcaster, and Recovery roles
17
- * - Secure ownership transfer with time-locked operations
18
- * - Broadcaster and recovery address management
19
- * - Time-lock period configuration
20
- *
21
- * The contract implements four primary secure operation types:
22
- * 1. OWNERSHIP_TRANSFER - For securely transferring contract ownership
23
- * 2. BROADCASTER_UPDATE - For changing the broadcaster address
24
- * 3. RECOVERY_UPDATE - For updating the recovery address
25
- * 4. TIMELOCK_UPDATE - For modifying the time lock period
26
- *
27
- * Each operation follows a request -> approval workflow with appropriate time locks
28
- * and authorization checks. Operations can be cancelled within specific time windows.
29
- *
30
- * At most one ownership-transfer or broadcaster-update request may be pending at a time:
31
- * a pending request of either type blocks new requests until it is approved or cancelled.
32
- *
33
- * This contract focuses purely on security logic while leveraging the BaseStateMachine
34
- * for transaction management, meta-transactions, and state machine operations.
35
- */
36
- abstract contract SecureOwnable is BaseStateMachine, ISecureOwnable {
37
- using SharedValidation for *;
38
-
39
- /// @dev True while any pending ownership transfer or broadcaster update request exists; blocks new requests until handled.
40
- bool private _hasOpenRequest;
41
-
42
- /**
43
- * @notice Initializer to initialize SecureOwnable state
44
- * @param initialOwner The initial owner address
45
- * @param broadcaster The broadcaster address
46
- * @param recovery The recovery address
47
- * @param timeLockPeriodSec The timelock period in seconds
48
- * @param eventForwarder The event forwarder address
49
- */
50
- function initialize(
51
- address initialOwner,
52
- address broadcaster,
53
- address recovery,
54
- uint256 timeLockPeriodSec,
55
- address eventForwarder
56
- ) public virtual onlyInitializing {
57
- _initializeBaseStateMachine(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
58
-
59
- // Load SecureOwnable-specific definitions
60
- IDefinition.RolePermission memory secureOwnablePermissions = SecureOwnableDefinitions.getRolePermissions();
61
- _loadDefinitions(
62
- SecureOwnableDefinitions.getFunctionSchemas(),
63
- secureOwnablePermissions.roleHashes,
64
- secureOwnablePermissions.functionPermissions,
65
- true // Enforce all function schemas are protected
66
- );
67
- }
68
-
69
- // ============ INTERFACE SUPPORT ============
70
-
71
- /**
72
- * @dev See {IERC165-supportsInterface}.
73
- * @notice Adds ISecureOwnable interface ID for component detection
74
- */
75
- function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
76
- return interfaceId == type(ISecureOwnable).interfaceId || super.supportsInterface(interfaceId);
77
- }
78
-
79
- // Ownership Management
80
- /**
81
- * @dev Requests a transfer of ownership
82
- * @return txId The transaction ID (use getTransaction(txId) for full record)
83
- */
84
- function transferOwnershipRequest() public returns (uint256 txId) {
85
- SharedValidation.validateRecovery(getRecovery());
86
- _requireNoPendingRequest();
87
-
88
- EngineBlox.TxRecord memory txRecord = _requestTransaction(
89
- msg.sender,
90
- address(this),
91
- 0, // value
92
- 0, // no gas limit
93
- SecureOwnableDefinitions.OWNERSHIP_TRANSFER,
94
- SecureOwnableDefinitions.TRANSFER_OWNERSHIP_SELECTOR,
95
- abi.encode(getRecovery())
96
- );
97
-
98
- _hasOpenRequest = true;
99
- _logAddressPairEvent(owner(), getRecovery());
100
- return txRecord.txId;
101
- }
102
-
103
- /**
104
- * @dev Approves a pending ownership transfer transaction after the release time
105
- * @param txId The transaction ID
106
- * @return The transaction ID
107
- */
108
- function transferOwnershipDelayedApproval(uint256 txId) public returns (uint256) {
109
- SharedValidation.validateOwnerOrRecovery(owner(), getRecovery());
110
- return _completeApprove(_approveTransaction(txId));
111
- }
112
-
113
- /**
114
- * @dev Approves a pending ownership transfer transaction using a meta-transaction
115
- * @param metaTx The meta-transaction
116
- * @return The transaction ID
117
- */
118
- function transferOwnershipApprovalWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
119
- _validateBroadcasterAndOwnerSigner(metaTx);
120
- return _completeApprove(_approveTransactionWithMetaTx(metaTx));
121
- }
122
-
123
- /**
124
- * @dev Cancels a pending ownership transfer transaction
125
- * @param txId The transaction ID
126
- * @return The transaction ID
127
- */
128
- function transferOwnershipCancellation(uint256 txId) public returns (uint256) {
129
- SharedValidation.validateRecovery(getRecovery());
130
- return _completeCancel(_cancelTransaction(txId));
131
- }
132
-
133
- /**
134
- * @dev Cancels a pending ownership transfer transaction using a meta-transaction
135
- * @param metaTx The meta-transaction
136
- * @return The transaction ID
137
- */
138
- function transferOwnershipCancellationWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
139
- _validateBroadcasterAndOwnerSigner(metaTx);
140
- return _completeCancel(_cancelTransactionWithMetaTx(metaTx));
141
- }
142
-
143
- // Broadcaster Management
144
- /**
145
- * @dev Requests an update to the broadcaster at a specific location (index).
146
- * @param newBroadcaster The new broadcaster address (zero address to revoke at location)
147
- * @param location The index in the broadcaster role's authorized wallets set
148
- * @return txId The transaction ID for the pending request (use getTransaction(txId) for full record)
149
- */
150
- function updateBroadcasterRequest(address newBroadcaster, uint256 location) public returns (uint256 txId) {
151
- SharedValidation.validateOwner(owner());
152
- _requireNoPendingRequest();
153
-
154
- // Get the current broadcaster at the specified location. zero address if no broadcaster at location.
155
- address currentBroadcaster = location < _getSecureState().roles[EngineBlox.BROADCASTER_ROLE].walletCount
156
- ? _getAuthorizedWalletAt(EngineBlox.BROADCASTER_ROLE, location)
157
- : address(0);
158
-
159
- EngineBlox.TxRecord memory txRecord = _requestTransaction(
160
- msg.sender,
161
- address(this),
162
- 0, // value
163
- 0, // gas limit
164
- SecureOwnableDefinitions.BROADCASTER_UPDATE,
165
- SecureOwnableDefinitions.UPDATE_BROADCASTER_SELECTOR,
166
- abi.encode(newBroadcaster, location)
167
- );
168
-
169
- _hasOpenRequest = true;
170
- _logAddressPairEvent(currentBroadcaster, newBroadcaster);
171
- return txRecord.txId;
172
- }
173
-
174
- /**
175
- * @dev Approves a pending broadcaster update transaction after the release time
176
- * @param txId The transaction ID
177
- * @return The transaction ID
178
- */
179
- function updateBroadcasterDelayedApproval(uint256 txId) public returns (uint256) {
180
- SharedValidation.validateOwner(owner());
181
- return _completeApprove(_approveTransaction(txId));
182
- }
183
-
184
- /**
185
- * @dev Approves a pending broadcaster update transaction using a meta-transaction
186
- * @param metaTx The meta-transaction
187
- * @return The transaction ID
188
- */
189
- function updateBroadcasterApprovalWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
190
- _validateBroadcasterAndOwnerSigner(metaTx);
191
- return _completeApprove(_approveTransactionWithMetaTx(metaTx));
192
- }
193
-
194
- /**
195
- * @dev Cancels a pending broadcaster update transaction
196
- * @param txId The transaction ID
197
- * @return The transaction ID
198
- */
199
- function updateBroadcasterCancellation(uint256 txId) public returns (uint256) {
200
- SharedValidation.validateOwner(owner());
201
- return _completeCancel(_cancelTransaction(txId));
202
- }
203
-
204
- /**
205
- * @dev Cancels a pending broadcaster update transaction using a meta-transaction
206
- * @param metaTx The meta-transaction
207
- * @return The transaction ID
208
- */
209
- function updateBroadcasterCancellationWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
210
- _validateBroadcasterAndOwnerSigner(metaTx);
211
- return _completeCancel(_cancelTransactionWithMetaTx(metaTx));
212
- }
213
-
214
- // Recovery Management
215
-
216
- /**
217
- * @dev Requests and approves a recovery address update using a meta-transaction
218
- * @param metaTx The meta-transaction
219
- * @return The transaction ID
220
- */
221
- function updateRecoveryRequestAndApprove(
222
- EngineBlox.MetaTransaction memory metaTx
223
- ) public returns (uint256) {
224
- _validateBroadcasterAndOwnerSigner(metaTx);
225
- EngineBlox.TxRecord memory txRecord = _requestAndApproveTransaction(metaTx);
226
- return txRecord.txId;
227
- }
228
-
229
- // TimeLock Management
230
-
231
- /**
232
- * @dev Requests and approves a time lock period update using a meta-transaction
233
- * @param metaTx The meta-transaction
234
- * @return The transaction ID
235
- */
236
- function updateTimeLockRequestAndApprove(
237
- EngineBlox.MetaTransaction memory metaTx
238
- ) public returns (uint256) {
239
- _validateBroadcasterAndOwnerSigner(metaTx);
240
- EngineBlox.TxRecord memory txRecord = _requestAndApproveTransaction(metaTx);
241
- return txRecord.txId;
242
- }
243
-
244
- // Execution Functions
245
- /**
246
- * @dev External function that can only be called by the contract itself to execute ownership transfer
247
- * @param newOwner The new owner address
248
- */
249
- function executeTransferOwnership(address newOwner) external {
250
- _validateExecuteBySelf();
251
- _transferOwnership(newOwner);
252
- }
253
-
254
- /**
255
- * @dev External function that can only be called by the contract itself to execute broadcaster update
256
- * @param newBroadcaster The new broadcaster address (zero address to revoke at location)
257
- * @param location The index in the broadcaster role's authorized wallets set
258
- */
259
- function executeBroadcasterUpdate(address newBroadcaster, uint256 location) external {
260
- _validateExecuteBySelf();
261
- _updateBroadcaster(newBroadcaster, location);
262
- }
263
-
264
- /**
265
- * @dev External function that can only be called by the contract itself to execute recovery update
266
- * @param newRecoveryAddress The new recovery address
267
- */
268
- function executeRecoveryUpdate(address newRecoveryAddress) external {
269
- _validateExecuteBySelf();
270
- _updateRecoveryAddress(newRecoveryAddress);
271
- }
272
-
273
- /**
274
- * @dev External function that can only be called by the contract itself to execute timelock update
275
- * @param newTimeLockPeriodSec The new timelock period in seconds
276
- */
277
- function executeTimeLockUpdate(uint256 newTimeLockPeriodSec) external {
278
- _validateExecuteBySelf();
279
- _updateTimeLockPeriod(newTimeLockPeriodSec);
280
- }
281
-
282
- // ============ INTERNAL FUNCTIONS ============
283
-
284
- /**
285
- * @dev Reverts if an ownership-transfer or broadcaster-update request is already pending.
286
- */
287
- function _requireNoPendingRequest() internal view {
288
- if (_hasOpenRequest) revert SharedValidation.PendingSecureRequest();
289
- }
290
-
291
- /**
292
- * @dev Validates that the caller is the broadcaster and that the meta-tx signer is the owner.
293
- * @param metaTx The meta-transaction to validate
294
- */
295
- function _validateBroadcasterAndOwnerSigner(EngineBlox.MetaTransaction memory metaTx) internal view {
296
- _validateBroadcaster(msg.sender);
297
- SharedValidation.validateOwnerIsSigner(metaTx.params.signer, owner());
298
- }
299
-
300
- /**
301
- * @dev Completes ownership/broadcaster flow after approval: resets flag and returns txId.
302
- * @param updatedRecord The updated transaction record from approval
303
- * @return txId The transaction ID
304
- */
305
- function _completeApprove(EngineBlox.TxRecord memory updatedRecord) internal returns (uint256 txId) {
306
- _hasOpenRequest = false;
307
- return updatedRecord.txId;
308
- }
309
-
310
- /**
311
- * @dev Completes ownership/broadcaster flow after cancellation: resets flag, logs txId, returns txId.
312
- * @param updatedRecord The updated transaction record from cancellation
313
- * @return txId The transaction ID
314
- */
315
- function _completeCancel(EngineBlox.TxRecord memory updatedRecord) internal returns (uint256 txId) {
316
- _hasOpenRequest = false;
317
- return updatedRecord.txId;
318
- }
319
-
320
- /**
321
- * @dev Transfers ownership of the contract
322
- * @param newOwner The new owner of the contract
323
- */
324
- function _transferOwnership(address newOwner) internal virtual {
325
- address oldOwner = owner();
326
- _updateAssignedWallet(EngineBlox.OWNER_ROLE, newOwner, oldOwner);
327
- _logAddressPairEvent(oldOwner, newOwner);
328
- }
329
-
330
- /**
331
- * @dev Updates the broadcaster role at a specific index (location)
332
- * @param newBroadcaster The new broadcaster address (zero address to revoke)
333
- * @param location The index in the broadcaster role's authorized wallets set
334
- *
335
- * Logic:
336
- * - If a broadcaster exists at `location` and `newBroadcaster` is non-zero,
337
- * update that slot from old to new (role remains full).
338
- * - If no broadcaster exists at `location` and `newBroadcaster` is non-zero,
339
- * assign `newBroadcaster` to the broadcaster role (respecting maxWallets).
340
- * - If `newBroadcaster` is the zero address and a broadcaster exists at `location`,
341
- * revoke that broadcaster from the role.
342
- */
343
- function _updateBroadcaster(address newBroadcaster, uint256 location) internal virtual {
344
- EngineBlox.Role storage role = _getSecureState().roles[EngineBlox.BROADCASTER_ROLE];
345
-
346
- address oldBroadcaster;
347
- uint256 length = role.walletCount;
348
-
349
- if (location < length) {
350
- oldBroadcaster = _getAuthorizedWalletAt(EngineBlox.BROADCASTER_ROLE, location);
351
- } else {
352
- oldBroadcaster = address(0);
353
- }
354
-
355
- // Case 1: Revoke existing broadcaster at location
356
- if (newBroadcaster == address(0)) {
357
- if (oldBroadcaster != address(0)) {
358
- _revokeWallet(EngineBlox.BROADCASTER_ROLE, oldBroadcaster);
359
- _logAddressPairEvent(oldBroadcaster, address(0));
360
- }
361
- return;
362
- }
363
-
364
- // Case 2: Update existing broadcaster at location
365
- if (oldBroadcaster != address(0)) {
366
- _updateAssignedWallet(EngineBlox.BROADCASTER_ROLE, newBroadcaster, oldBroadcaster);
367
- _logAddressPairEvent(oldBroadcaster, newBroadcaster);
368
- return;
369
- }
370
-
371
- // Case 3: No broadcaster at location, assign a new one (will respect maxWallets)
372
- _assignWallet(EngineBlox.BROADCASTER_ROLE, newBroadcaster);
373
- _logAddressPairEvent(address(0), newBroadcaster);
374
- }
375
-
376
- /**
377
- * @dev Updates the recovery address
378
- * @param newRecoveryAddress The new recovery address
379
- */
380
- function _updateRecoveryAddress(address newRecoveryAddress) internal virtual {
381
- address oldRecovery = getRecovery();
382
- _updateAssignedWallet(EngineBlox.RECOVERY_ROLE, newRecoveryAddress, oldRecovery);
383
- _logAddressPairEvent(oldRecovery, newRecoveryAddress);
384
- }
385
-
386
- /**
387
- * @dev Emits ComponentEvent with ABI-encoded (address, address) payload. Reused to reduce contract size.
388
- * @param a First address
389
- * @param b Second address
390
- */
391
- function _logAddressPairEvent(address a, address b) internal {
392
- _logComponentEvent(abi.encode(a, b));
393
- }
394
- }
1
+ // SPDX-License-Identifier: MPL-2.0
2
+ pragma solidity 0.8.34;
3
+
4
+ // Contracts imports
5
+ import "../base/BaseStateMachine.sol";
6
+ import "./lib/definitions/SecureOwnableDefinitions.sol";
7
+ import "../lib/interfaces/IDefinition.sol";
8
+ import "../lib/utils/SharedValidation.sol";
9
+ import "./interface/ISecureOwnable.sol";
10
+
11
+ /**
12
+ * @title SecureOwnable
13
+ * @dev Security-focused contract extending BaseStateMachine with ownership management
14
+ *
15
+ * SecureOwnable provides security-specific functionality built on top of the base state machine:
16
+ * - Multi-role security model with Owner, Broadcaster, and Recovery roles
17
+ * - Secure ownership transfer with time-locked operations
18
+ * - Broadcaster and recovery address management
19
+ * - Time-lock period configuration
20
+ *
21
+ * The contract implements four primary secure operation types:
22
+ * 1. OWNERSHIP_TRANSFER - For securely transferring contract ownership
23
+ * 2. BROADCASTER_UPDATE - For changing the broadcaster address
24
+ * 3. RECOVERY_UPDATE - For updating the recovery address
25
+ * 4. TIMELOCK_UPDATE - For modifying the time lock period
26
+ *
27
+ * Each operation follows a request -> approval workflow with appropriate time locks
28
+ * and authorization checks. Operations can be cancelled within specific time windows.
29
+ *
30
+ * At most one ownership-transfer or broadcaster-update request may be pending at a time:
31
+ * a pending request of either type blocks new requests until it is approved or cancelled.
32
+ *
33
+ * This contract focuses purely on security logic while leveraging the BaseStateMachine
34
+ * for transaction management, meta-transactions, and state machine operations.
35
+ */
36
+ abstract contract SecureOwnable is BaseStateMachine, ISecureOwnable {
37
+ using SharedValidation for *;
38
+
39
+ /// @dev True while any pending ownership transfer or broadcaster update request exists; blocks new requests until handled.
40
+ bool private _hasOpenRequest;
41
+
42
+ /**
43
+ * @notice Initializer to initialize SecureOwnable state
44
+ * @param initialOwner The initial owner address
45
+ * @param broadcaster The broadcaster address
46
+ * @param recovery The recovery address
47
+ * @param timeLockPeriodSec The timelock period in seconds
48
+ * @param eventForwarder The event forwarder address
49
+ */
50
+ function initialize(
51
+ address initialOwner,
52
+ address broadcaster,
53
+ address recovery,
54
+ uint256 timeLockPeriodSec,
55
+ address eventForwarder
56
+ ) public virtual onlyInitializing {
57
+ _initializeBaseStateMachine(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
58
+
59
+ // Load SecureOwnable-specific definitions
60
+ IDefinition.RolePermission memory secureOwnablePermissions = SecureOwnableDefinitions.getRolePermissions();
61
+ _loadDefinitions(
62
+ SecureOwnableDefinitions.getFunctionSchemas(),
63
+ secureOwnablePermissions.roleHashes,
64
+ secureOwnablePermissions.functionPermissions,
65
+ true // Enforce all function schemas are protected
66
+ );
67
+ }
68
+
69
+ // ============ INTERFACE SUPPORT ============
70
+
71
+ /**
72
+ * @dev See {IERC165-supportsInterface}.
73
+ * @notice Adds ISecureOwnable interface ID for component detection
74
+ */
75
+ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
76
+ return interfaceId == type(ISecureOwnable).interfaceId || super.supportsInterface(interfaceId);
77
+ }
78
+
79
+ // Ownership Management
80
+ /**
81
+ * @dev Requests a transfer of ownership
82
+ * @return txId The transaction ID (use getTransaction(txId) for full record)
83
+ */
84
+ function transferOwnershipRequest() public returns (uint256 txId) {
85
+ SharedValidation.validateRecovery(getRecovery());
86
+ _requireNoPendingRequest();
87
+
88
+ EngineBlox.TxRecord memory txRecord = _requestTransaction(
89
+ msg.sender,
90
+ address(this),
91
+ 0, // value
92
+ 0, // no gas limit
93
+ SecureOwnableDefinitions.OWNERSHIP_TRANSFER,
94
+ SecureOwnableDefinitions.TRANSFER_OWNERSHIP_SELECTOR,
95
+ abi.encode(getRecovery())
96
+ );
97
+
98
+ _hasOpenRequest = true;
99
+ _logAddressPairEvent(owner(), getRecovery());
100
+ return txRecord.txId;
101
+ }
102
+
103
+ /**
104
+ * @dev Approves a pending ownership transfer transaction after the release time
105
+ * @param txId The transaction ID
106
+ * @return The transaction ID
107
+ */
108
+ function transferOwnershipDelayedApproval(uint256 txId) public returns (uint256) {
109
+ SharedValidation.validateOwnerOrRecovery(owner(), getRecovery());
110
+ return _completeApprove(_approveTransaction(txId));
111
+ }
112
+
113
+ /**
114
+ * @dev Approves a pending ownership transfer transaction using a meta-transaction
115
+ * @param metaTx The meta-transaction
116
+ * @return The transaction ID
117
+ */
118
+ function transferOwnershipApprovalWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
119
+ _validateBroadcasterAndOwnerSigner(metaTx);
120
+ return _completeApprove(_approveTransactionWithMetaTx(metaTx));
121
+ }
122
+
123
+ /**
124
+ * @dev Cancels a pending ownership transfer transaction
125
+ * @param txId The transaction ID
126
+ * @return The transaction ID
127
+ */
128
+ function transferOwnershipCancellation(uint256 txId) public returns (uint256) {
129
+ SharedValidation.validateRecovery(getRecovery());
130
+ return _completeCancel(_cancelTransaction(txId));
131
+ }
132
+
133
+ /**
134
+ * @dev Cancels a pending ownership transfer transaction using a meta-transaction
135
+ * @param metaTx The meta-transaction
136
+ * @return The transaction ID
137
+ */
138
+ function transferOwnershipCancellationWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
139
+ _validateBroadcasterAndOwnerSigner(metaTx);
140
+ return _completeCancel(_cancelTransactionWithMetaTx(metaTx));
141
+ }
142
+
143
+ // Broadcaster Management
144
+ /**
145
+ * @dev Requests an update to the broadcaster at a specific location (index).
146
+ * @param newBroadcaster The new broadcaster address (zero address to revoke at location)
147
+ * @param location The index in the broadcaster role's authorized wallets set
148
+ * @return txId The transaction ID for the pending request (use getTransaction(txId) for full record)
149
+ */
150
+ function updateBroadcasterRequest(address newBroadcaster, uint256 location) public returns (uint256 txId) {
151
+ SharedValidation.validateOwner(owner());
152
+ _requireNoPendingRequest();
153
+
154
+ // Get the current broadcaster at the specified location. zero address if no broadcaster at location.
155
+ address currentBroadcaster = location < _getSecureState().roles[EngineBlox.BROADCASTER_ROLE].walletCount
156
+ ? _getAuthorizedWalletAt(EngineBlox.BROADCASTER_ROLE, location)
157
+ : address(0);
158
+
159
+ EngineBlox.TxRecord memory txRecord = _requestTransaction(
160
+ msg.sender,
161
+ address(this),
162
+ 0, // value
163
+ 0, // gas limit
164
+ SecureOwnableDefinitions.BROADCASTER_UPDATE,
165
+ SecureOwnableDefinitions.UPDATE_BROADCASTER_SELECTOR,
166
+ abi.encode(newBroadcaster, location)
167
+ );
168
+
169
+ _hasOpenRequest = true;
170
+ _logAddressPairEvent(currentBroadcaster, newBroadcaster);
171
+ return txRecord.txId;
172
+ }
173
+
174
+ /**
175
+ * @dev Approves a pending broadcaster update transaction after the release time
176
+ * @param txId The transaction ID
177
+ * @return The transaction ID
178
+ */
179
+ function updateBroadcasterDelayedApproval(uint256 txId) public returns (uint256) {
180
+ SharedValidation.validateOwner(owner());
181
+ return _completeApprove(_approveTransaction(txId));
182
+ }
183
+
184
+ /**
185
+ * @dev Approves a pending broadcaster update transaction using a meta-transaction
186
+ * @param metaTx The meta-transaction
187
+ * @return The transaction ID
188
+ */
189
+ function updateBroadcasterApprovalWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
190
+ _validateBroadcasterAndOwnerSigner(metaTx);
191
+ return _completeApprove(_approveTransactionWithMetaTx(metaTx));
192
+ }
193
+
194
+ /**
195
+ * @dev Cancels a pending broadcaster update transaction
196
+ * @param txId The transaction ID
197
+ * @return The transaction ID
198
+ */
199
+ function updateBroadcasterCancellation(uint256 txId) public returns (uint256) {
200
+ SharedValidation.validateOwner(owner());
201
+ return _completeCancel(_cancelTransaction(txId));
202
+ }
203
+
204
+ /**
205
+ * @dev Cancels a pending broadcaster update transaction using a meta-transaction
206
+ * @param metaTx The meta-transaction
207
+ * @return The transaction ID
208
+ */
209
+ function updateBroadcasterCancellationWithMetaTx(EngineBlox.MetaTransaction memory metaTx) public returns (uint256) {
210
+ _validateBroadcasterAndOwnerSigner(metaTx);
211
+ return _completeCancel(_cancelTransactionWithMetaTx(metaTx));
212
+ }
213
+
214
+ // Recovery Management
215
+
216
+ /**
217
+ * @dev Requests and approves a recovery address update using a meta-transaction
218
+ * @param metaTx The meta-transaction
219
+ * @return The transaction ID
220
+ */
221
+ function updateRecoveryRequestAndApprove(
222
+ EngineBlox.MetaTransaction memory metaTx
223
+ ) public returns (uint256) {
224
+ _validateBroadcasterAndOwnerSigner(metaTx);
225
+ EngineBlox.TxRecord memory txRecord = _requestAndApproveTransaction(metaTx);
226
+ return txRecord.txId;
227
+ }
228
+
229
+ // TimeLock Management
230
+
231
+ /**
232
+ * @dev Requests and approves a time lock period update using a meta-transaction
233
+ * @param metaTx The meta-transaction
234
+ * @return The transaction ID
235
+ */
236
+ function updateTimeLockRequestAndApprove(
237
+ EngineBlox.MetaTransaction memory metaTx
238
+ ) public returns (uint256) {
239
+ _validateBroadcasterAndOwnerSigner(metaTx);
240
+ EngineBlox.TxRecord memory txRecord = _requestAndApproveTransaction(metaTx);
241
+ return txRecord.txId;
242
+ }
243
+
244
+ // Execution Functions
245
+ /**
246
+ * @dev External function that can only be called by the contract itself to execute ownership transfer
247
+ * @param newOwner The new owner address
248
+ */
249
+ function executeTransferOwnership(address newOwner) external {
250
+ _validateExecuteBySelf();
251
+ _transferOwnership(newOwner);
252
+ }
253
+
254
+ /**
255
+ * @dev External function that can only be called by the contract itself to execute broadcaster update
256
+ * @param newBroadcaster The new broadcaster address (zero address to revoke at location)
257
+ * @param location The index in the broadcaster role's authorized wallets set
258
+ */
259
+ function executeBroadcasterUpdate(address newBroadcaster, uint256 location) external {
260
+ _validateExecuteBySelf();
261
+ _updateBroadcaster(newBroadcaster, location);
262
+ }
263
+
264
+ /**
265
+ * @dev External function that can only be called by the contract itself to execute recovery update
266
+ * @param newRecoveryAddress The new recovery address
267
+ */
268
+ function executeRecoveryUpdate(address newRecoveryAddress) external {
269
+ _validateExecuteBySelf();
270
+ _updateRecoveryAddress(newRecoveryAddress);
271
+ }
272
+
273
+ /**
274
+ * @dev External function that can only be called by the contract itself to execute timelock update
275
+ * @param newTimeLockPeriodSec The new timelock period in seconds
276
+ */
277
+ function executeTimeLockUpdate(uint256 newTimeLockPeriodSec) external {
278
+ _validateExecuteBySelf();
279
+ _updateTimeLockPeriod(newTimeLockPeriodSec);
280
+ }
281
+
282
+ // ============ INTERNAL FUNCTIONS ============
283
+
284
+ /**
285
+ * @dev Reverts if an ownership-transfer or broadcaster-update request is already pending.
286
+ */
287
+ function _requireNoPendingRequest() internal view {
288
+ if (_hasOpenRequest) revert SharedValidation.PendingSecureRequest();
289
+ }
290
+
291
+ /**
292
+ * @dev Validates that the caller is the broadcaster and that the meta-tx signer is the owner.
293
+ * @param metaTx The meta-transaction to validate
294
+ */
295
+ function _validateBroadcasterAndOwnerSigner(EngineBlox.MetaTransaction memory metaTx) internal view {
296
+ _validateBroadcaster(msg.sender);
297
+ SharedValidation.validateOwnerIsSigner(metaTx.params.signer, owner());
298
+ }
299
+
300
+ /**
301
+ * @dev Completes ownership/broadcaster flow after approval: resets flag and returns txId.
302
+ * @param updatedRecord The updated transaction record from approval
303
+ * @return txId The transaction ID
304
+ */
305
+ function _completeApprove(EngineBlox.TxRecord memory updatedRecord) internal returns (uint256 txId) {
306
+ _hasOpenRequest = false;
307
+ return updatedRecord.txId;
308
+ }
309
+
310
+ /**
311
+ * @dev Completes ownership/broadcaster flow after cancellation: resets flag, logs txId, returns txId.
312
+ * @param updatedRecord The updated transaction record from cancellation
313
+ * @return txId The transaction ID
314
+ */
315
+ function _completeCancel(EngineBlox.TxRecord memory updatedRecord) internal returns (uint256 txId) {
316
+ _hasOpenRequest = false;
317
+ return updatedRecord.txId;
318
+ }
319
+
320
+ /**
321
+ * @dev Transfers ownership of the contract
322
+ * @param newOwner The new owner of the contract
323
+ */
324
+ function _transferOwnership(address newOwner) internal virtual {
325
+ address oldOwner = owner();
326
+ _updateWallet(EngineBlox.OWNER_ROLE, newOwner, oldOwner);
327
+ _logAddressPairEvent(oldOwner, newOwner);
328
+ }
329
+
330
+ /**
331
+ * @dev Updates the broadcaster role at a specific index (location)
332
+ * @param newBroadcaster The new broadcaster address (zero address to revoke)
333
+ * @param location The index in the broadcaster role's authorized wallets set
334
+ *
335
+ * Logic:
336
+ * - If a broadcaster exists at `location` and `newBroadcaster` is non-zero,
337
+ * update that slot from old to new (role remains full).
338
+ * - If no broadcaster exists at `location` and `newBroadcaster` is non-zero,
339
+ * assign `newBroadcaster` to the broadcaster role (respecting maxWallets).
340
+ * - If `newBroadcaster` is the zero address and a broadcaster exists at `location`,
341
+ * revoke that broadcaster from the role.
342
+ */
343
+ function _updateBroadcaster(address newBroadcaster, uint256 location) internal virtual {
344
+ EngineBlox.Role storage role = _getSecureState().roles[EngineBlox.BROADCASTER_ROLE];
345
+
346
+ address oldBroadcaster;
347
+ uint256 length = role.walletCount;
348
+
349
+ if (location < length) {
350
+ oldBroadcaster = _getAuthorizedWalletAt(EngineBlox.BROADCASTER_ROLE, location);
351
+ } else {
352
+ oldBroadcaster = address(0);
353
+ }
354
+
355
+ // Case 1: Revoke existing broadcaster at location
356
+ if (newBroadcaster == address(0)) {
357
+ if (oldBroadcaster != address(0)) {
358
+ _revokeWallet(EngineBlox.BROADCASTER_ROLE, oldBroadcaster);
359
+ _logAddressPairEvent(oldBroadcaster, address(0));
360
+ }
361
+ return;
362
+ }
363
+
364
+ // Case 2: Update existing broadcaster at location
365
+ if (oldBroadcaster != address(0)) {
366
+ _updateWallet(EngineBlox.BROADCASTER_ROLE, newBroadcaster, oldBroadcaster);
367
+ _logAddressPairEvent(oldBroadcaster, newBroadcaster);
368
+ return;
369
+ }
370
+
371
+ // Case 3: No broadcaster at location, assign a new one (will respect maxWallets)
372
+ _assignWallet(EngineBlox.BROADCASTER_ROLE, newBroadcaster);
373
+ _logAddressPairEvent(address(0), newBroadcaster);
374
+ }
375
+
376
+ /**
377
+ * @dev Updates the recovery address
378
+ * @param newRecoveryAddress The new recovery address
379
+ */
380
+ function _updateRecoveryAddress(address newRecoveryAddress) internal virtual {
381
+ address oldRecovery = getRecovery();
382
+ _updateWallet(EngineBlox.RECOVERY_ROLE, newRecoveryAddress, oldRecovery);
383
+ _logAddressPairEvent(oldRecovery, newRecoveryAddress);
384
+ }
385
+
386
+ /**
387
+ * @dev Emits ComponentEvent with ABI-encoded (address, address) payload. Reused to reduce contract size.
388
+ * @param a First address
389
+ * @param b Second address
390
+ */
391
+ function _logAddressPairEvent(address a, address b) internal {
392
+ _logComponentEvent(abi.encode(a, b));
393
+ }
394
+ }