@bloxchain/contracts 1.0.0-alpha.7 → 1.0.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +8 -9
  3. package/abi/BaseStateMachine.abi.json +773 -822
  4. package/abi/EngineBlox.abi.json +562 -552
  5. package/abi/GuardController.abi.json +1597 -1609
  6. package/abi/GuardControllerDefinitions.abi.json +235 -199
  7. package/abi/IDefinition.abi.json +57 -47
  8. package/abi/RuntimeRBAC.abi.json +841 -842
  9. package/abi/RuntimeRBACDefinitions.abi.json +212 -202
  10. package/abi/SecureOwnable.abi.json +1365 -1349
  11. package/abi/SecureOwnableDefinitions.abi.json +174 -164
  12. package/core/AUDIT.md +45 -0
  13. package/core/access/RuntimeRBAC.sol +130 -61
  14. package/core/access/interface/IRuntimeRBAC.sol +3 -3
  15. package/core/access/lib/definitions/RuntimeRBACDefinitions.sol +7 -3
  16. package/core/base/BaseStateMachine.sol +971 -967
  17. package/core/base/interface/IBaseStateMachine.sol +153 -160
  18. package/core/execution/GuardController.sol +89 -75
  19. package/core/execution/interface/IGuardController.sol +146 -160
  20. package/core/execution/lib/definitions/GuardControllerDefinitions.sol +136 -25
  21. package/core/lib/EngineBlox.sol +577 -327
  22. package/core/lib/interfaces/IDefinition.sol +49 -49
  23. package/core/lib/interfaces/IEventForwarder.sol +4 -2
  24. package/core/lib/utils/SharedValidation.sol +534 -490
  25. package/core/pattern/Account.sol +84 -75
  26. package/core/security/SecureOwnable.sol +446 -390
  27. package/core/security/interface/ISecureOwnable.sol +105 -105
  28. package/core/security/lib/definitions/SecureOwnableDefinitions.sol +49 -17
  29. package/package.json +51 -49
  30. package/standards/behavior/ICopyable.sol +3 -11
  31. package/standards/hooks/IOnActionHook.sol +1 -1
  32. package/abi/AccountBlox.abi.json +0 -3935
  33. package/abi/BareBlox.abi.json +0 -1378
  34. package/abi/RoleBlox.abi.json +0 -2983
  35. package/abi/SecureBlox.abi.json +0 -2753
  36. package/abi/SimpleRWA20.abi.json +0 -4032
  37. package/abi/SimpleRWA20Definitions.abi.json +0 -191
  38. package/abi/SimpleVault.abi.json +0 -3407
  39. package/abi/SimpleVaultDefinitions.abi.json +0 -269
  40. package/core/research/BloxchainWallet.sol +0 -133
  41. package/core/research/FactoryBlox/FactoryBlox.sol +0 -343
  42. package/core/research/FactoryBlox/FactoryBloxDefinitions.sol +0 -143
  43. package/core/research/erc1155-blox/ERC1155Blox.sol +0 -169
  44. package/core/research/erc1155-blox/lib/definitions/ERC1155BloxDefinitions.sol +0 -203
  45. package/core/research/erc20-blox/ERC20Blox.sol +0 -167
  46. package/core/research/erc20-blox/lib/definitions/ERC20BloxDefinitions.sol +0 -185
  47. package/core/research/erc721-blox/ERC721Blox.sol +0 -131
  48. package/core/research/erc721-blox/lib/definitions/ERC721BloxDefinitions.sol +0 -172
  49. package/core/research/lending-blox/.gitkeep +0 -1
  50. package/core/research/p2p-blox/P2PBlox.sol +0 -266
  51. package/core/research/p2p-blox/README.md +0 -85
  52. package/core/research/p2p-blox/lib/definitions/P2PBloxDefinitions.sol +0 -19
@@ -1,203 +0,0 @@
1
- // SPDX-License-Identifier: MPL-2.0
2
- pragma solidity 0.8.33;
3
-
4
- import "../../../../lib/EngineBlox.sol";
5
- import "../../../../lib/interfaces/IDefinition.sol";
6
-
7
- /**
8
- * @title ERC1155BloxDefinitions
9
- * @dev Definition library for ERC1155Blox execution selectors (safeTransferFrom, safeBatchTransferFrom, mint, mintBatch, burn, burnBatch).
10
- * Registers function schemas and role permissions so the GuardController can execute these functions
11
- * via time-lock and meta-transaction workflows.
12
- * @custom:security-contact security@particlecrypto.com
13
- */
14
- library ERC1155BloxDefinitions {
15
-
16
- // System macro selectors (allowed to target address(this) for GuardController execution)
17
- bytes4 public constant SAFE_TRANSFER_FROM_SELECTOR = bytes4(keccak256("safeTransferFrom(address,address,uint256,uint256,bytes)"));
18
- bytes4 public constant SAFE_BATCH_TRANSFER_FROM_SELECTOR = bytes4(keccak256("safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)"));
19
- bytes4 public constant MINT_SELECTOR = bytes4(keccak256("mint(address,uint256,uint256,bytes)"));
20
- bytes4 public constant MINT_BATCH_SELECTOR = bytes4(keccak256("mintBatch(address,uint256[],uint256[],bytes)"));
21
- bytes4 public constant BURN_SELECTOR = bytes4(keccak256("burn(address,uint256,uint256)"));
22
- bytes4 public constant BURN_BATCH_SELECTOR = bytes4(keccak256("burnBatch(address,uint256[],uint256[])"));
23
-
24
- bytes32 public constant ERC1155_OPERATION = keccak256("ERC1155_OPERATION");
25
-
26
- /**
27
- * @dev Returns function schemas for ERC1155Blox execution selectors (used by controller).
28
- */
29
- function getFunctionSchemas() public pure returns (EngineBlox.FunctionSchema[] memory) {
30
- EngineBlox.FunctionSchema[] memory schemas = new EngineBlox.FunctionSchema[](6);
31
-
32
- EngineBlox.TxAction[] memory timeDelayRequestActions = new EngineBlox.TxAction[](1);
33
- timeDelayRequestActions[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_REQUEST;
34
- EngineBlox.TxAction[] memory timeDelayApproveActions = new EngineBlox.TxAction[](1);
35
- timeDelayApproveActions[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_APPROVE;
36
- EngineBlox.TxAction[] memory timeDelayCancelActions = new EngineBlox.TxAction[](1);
37
- timeDelayCancelActions[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_CANCEL;
38
- EngineBlox.TxAction[] memory metaTxRequestApproveActions = new EngineBlox.TxAction[](2);
39
- metaTxRequestApproveActions[0] = EngineBlox.TxAction.SIGN_META_REQUEST_AND_APPROVE;
40
- metaTxRequestApproveActions[1] = EngineBlox.TxAction.EXECUTE_META_REQUEST_AND_APPROVE;
41
- EngineBlox.TxAction[] memory metaTxApproveActions = new EngineBlox.TxAction[](2);
42
- metaTxApproveActions[0] = EngineBlox.TxAction.SIGN_META_APPROVE;
43
- metaTxApproveActions[1] = EngineBlox.TxAction.EXECUTE_META_APPROVE;
44
- EngineBlox.TxAction[] memory metaTxCancelActions = new EngineBlox.TxAction[](2);
45
- metaTxCancelActions[0] = EngineBlox.TxAction.SIGN_META_CANCEL;
46
- metaTxCancelActions[1] = EngineBlox.TxAction.EXECUTE_META_CANCEL;
47
-
48
- uint16 actionsBitmap = EngineBlox.createBitmapFromActions(timeDelayRequestActions)
49
- | EngineBlox.createBitmapFromActions(timeDelayApproveActions)
50
- | EngineBlox.createBitmapFromActions(timeDelayCancelActions)
51
- | EngineBlox.createBitmapFromActions(metaTxRequestApproveActions)
52
- | EngineBlox.createBitmapFromActions(metaTxApproveActions)
53
- | EngineBlox.createBitmapFromActions(metaTxCancelActions);
54
-
55
- bytes4[] memory safeTransferFromHandlers = new bytes4[](1);
56
- safeTransferFromHandlers[0] = SAFE_TRANSFER_FROM_SELECTOR;
57
- bytes4[] memory safeBatchTransferFromHandlers = new bytes4[](1);
58
- safeBatchTransferFromHandlers[0] = SAFE_BATCH_TRANSFER_FROM_SELECTOR;
59
- bytes4[] memory mintHandlers = new bytes4[](1);
60
- mintHandlers[0] = MINT_SELECTOR;
61
- bytes4[] memory mintBatchHandlers = new bytes4[](1);
62
- mintBatchHandlers[0] = MINT_BATCH_SELECTOR;
63
- bytes4[] memory burnHandlers = new bytes4[](1);
64
- burnHandlers[0] = BURN_SELECTOR;
65
- bytes4[] memory burnBatchHandlers = new bytes4[](1);
66
- burnBatchHandlers[0] = BURN_BATCH_SELECTOR;
67
-
68
- schemas[0] = EngineBlox.FunctionSchema({
69
- functionSignature: "safeTransferFrom(address,address,uint256,uint256,bytes)",
70
- functionSelector: SAFE_TRANSFER_FROM_SELECTOR,
71
- operationType: ERC1155_OPERATION,
72
- operationName: "ERC1155_SAFE_TRANSFER_FROM",
73
- supportedActionsBitmap: actionsBitmap,
74
- isProtected: true,
75
- handlerForSelectors: safeTransferFromHandlers
76
- });
77
- schemas[1] = EngineBlox.FunctionSchema({
78
- functionSignature: "safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)",
79
- functionSelector: SAFE_BATCH_TRANSFER_FROM_SELECTOR,
80
- operationType: ERC1155_OPERATION,
81
- operationName: "ERC1155_SAFE_BATCH_TRANSFER_FROM",
82
- supportedActionsBitmap: actionsBitmap,
83
- isProtected: true,
84
- handlerForSelectors: safeBatchTransferFromHandlers
85
- });
86
- schemas[2] = EngineBlox.FunctionSchema({
87
- functionSignature: "mint(address,uint256,uint256,bytes)",
88
- functionSelector: MINT_SELECTOR,
89
- operationType: ERC1155_OPERATION,
90
- operationName: "ERC1155_MINT",
91
- supportedActionsBitmap: actionsBitmap,
92
- isProtected: true,
93
- handlerForSelectors: mintHandlers
94
- });
95
- schemas[3] = EngineBlox.FunctionSchema({
96
- functionSignature: "mintBatch(address,uint256[],uint256[],bytes)",
97
- functionSelector: MINT_BATCH_SELECTOR,
98
- operationType: ERC1155_OPERATION,
99
- operationName: "ERC1155_MINT_BATCH",
100
- supportedActionsBitmap: actionsBitmap,
101
- isProtected: true,
102
- handlerForSelectors: mintBatchHandlers
103
- });
104
- schemas[4] = EngineBlox.FunctionSchema({
105
- functionSignature: "burn(address,uint256,uint256)",
106
- functionSelector: BURN_SELECTOR,
107
- operationType: ERC1155_OPERATION,
108
- operationName: "ERC1155_BURN",
109
- supportedActionsBitmap: actionsBitmap,
110
- isProtected: true,
111
- handlerForSelectors: burnHandlers
112
- });
113
- schemas[5] = EngineBlox.FunctionSchema({
114
- functionSignature: "burnBatch(address,uint256[],uint256[])",
115
- functionSelector: BURN_BATCH_SELECTOR,
116
- operationType: ERC1155_OPERATION,
117
- operationName: "ERC1155_BURN_BATCH",
118
- supportedActionsBitmap: actionsBitmap,
119
- isProtected: true,
120
- handlerForSelectors: burnBatchHandlers
121
- });
122
-
123
- return schemas;
124
- }
125
-
126
- /**
127
- * @dev Returns role permissions for ERC1155Blox execution selectors (OWNER and BROADCASTER).
128
- */
129
- function getRolePermissions() public pure returns (IDefinition.RolePermission memory) {
130
- bytes32[] memory roleHashes = new bytes32[](12);
131
- EngineBlox.FunctionPermission[] memory functionPermissions = new EngineBlox.FunctionPermission[](12);
132
-
133
- EngineBlox.TxAction[] memory ownerTimeLockRequest = new EngineBlox.TxAction[](1);
134
- ownerTimeLockRequest[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_REQUEST;
135
- EngineBlox.TxAction[] memory ownerTimeLockApprove = new EngineBlox.TxAction[](1);
136
- ownerTimeLockApprove[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_APPROVE;
137
- EngineBlox.TxAction[] memory ownerTimeLockCancel = new EngineBlox.TxAction[](1);
138
- ownerTimeLockCancel[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_CANCEL;
139
- EngineBlox.TxAction[] memory ownerMetaSign = new EngineBlox.TxAction[](1);
140
- ownerMetaSign[0] = EngineBlox.TxAction.SIGN_META_REQUEST_AND_APPROVE;
141
- EngineBlox.TxAction[] memory ownerMetaApprove = new EngineBlox.TxAction[](1);
142
- ownerMetaApprove[0] = EngineBlox.TxAction.SIGN_META_APPROVE;
143
- EngineBlox.TxAction[] memory ownerMetaCancel = new EngineBlox.TxAction[](1);
144
- ownerMetaCancel[0] = EngineBlox.TxAction.SIGN_META_CANCEL;
145
- EngineBlox.TxAction[] memory broadcasterMetaExec = new EngineBlox.TxAction[](1);
146
- broadcasterMetaExec[0] = EngineBlox.TxAction.EXECUTE_META_REQUEST_AND_APPROVE;
147
- EngineBlox.TxAction[] memory broadcasterMetaApprove = new EngineBlox.TxAction[](1);
148
- broadcasterMetaApprove[0] = EngineBlox.TxAction.EXECUTE_META_APPROVE;
149
- EngineBlox.TxAction[] memory broadcasterMetaCancel = new EngineBlox.TxAction[](1);
150
- broadcasterMetaCancel[0] = EngineBlox.TxAction.EXECUTE_META_CANCEL;
151
-
152
- uint16 ownerBitmap = EngineBlox.createBitmapFromActions(ownerTimeLockRequest)
153
- | EngineBlox.createBitmapFromActions(ownerTimeLockApprove)
154
- | EngineBlox.createBitmapFromActions(ownerTimeLockCancel)
155
- | EngineBlox.createBitmapFromActions(ownerMetaSign)
156
- | EngineBlox.createBitmapFromActions(ownerMetaApprove)
157
- | EngineBlox.createBitmapFromActions(ownerMetaCancel);
158
- uint16 broadcasterBitmap = EngineBlox.createBitmapFromActions(broadcasterMetaExec)
159
- | EngineBlox.createBitmapFromActions(broadcasterMetaApprove)
160
- | EngineBlox.createBitmapFromActions(broadcasterMetaCancel);
161
-
162
- bytes4[6] memory selectors = [
163
- SAFE_TRANSFER_FROM_SELECTOR,
164
- SAFE_BATCH_TRANSFER_FROM_SELECTOR,
165
- MINT_SELECTOR,
166
- MINT_BATCH_SELECTOR,
167
- BURN_SELECTOR,
168
- BURN_BATCH_SELECTOR
169
- ];
170
- for (uint256 i = 0; i < 6; i++) {
171
- bytes4[] memory selfRef = new bytes4[](1);
172
- selfRef[0] = selectors[i];
173
- roleHashes[i] = EngineBlox.OWNER_ROLE;
174
- functionPermissions[i] = EngineBlox.FunctionPermission({
175
- functionSelector: selectors[i],
176
- grantedActionsBitmap: ownerBitmap,
177
- handlerForSelectors: selfRef
178
- });
179
- }
180
- for (uint256 i = 0; i < 6; i++) {
181
- bytes4[] memory selfRef = new bytes4[](1);
182
- selfRef[0] = selectors[i];
183
- roleHashes[6 + i] = EngineBlox.BROADCASTER_ROLE;
184
- functionPermissions[6 + i] = EngineBlox.FunctionPermission({
185
- functionSelector: selectors[i],
186
- grantedActionsBitmap: broadcasterBitmap,
187
- handlerForSelectors: selfRef
188
- });
189
- }
190
-
191
- return IDefinition.RolePermission({
192
- roleHashes: roleHashes,
193
- functionPermissions: functionPermissions
194
- });
195
- }
196
-
197
- /**
198
- * @dev ERC165: report support for IDefinition when this library is used at an address
199
- */
200
- function supportsInterface(bytes4 interfaceId) external pure returns (bool) {
201
- return interfaceId == type(IDefinition).interfaceId;
202
- }
203
- }
@@ -1,167 +0,0 @@
1
- // SPDX-License-Identifier: MPL-2.0
2
- pragma solidity 0.8.33;
3
-
4
- // OpenZeppelin
5
- import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
6
- import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";
7
- import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
8
-
9
- // Core
10
- import "../../security/SecureOwnable.sol";
11
- import "../../access/RuntimeRBAC.sol";
12
- import "../../execution/GuardController.sol";
13
- import "../../base/BaseStateMachine.sol";
14
- import "../../lib/interfaces/IDefinition.sol";
15
- import "./lib/definitions/ERC20BloxDefinitions.sol";
16
-
17
- /**
18
- * @title ERC20Blox
19
- * @dev ERC20 token (IERC20) with SecureOwnable, RuntimeRBAC, and GuardController.
20
- * Exposes transfer (ERC20), mint (owner-only), and burn (ERC20Burnable).
21
- * @custom:security-contact security@particlecrypto.com
22
- */
23
- contract ERC20Blox is
24
- IERC20,
25
- ERC20Upgradeable,
26
- ERC20BurnableUpgradeable,
27
- SecureOwnable,
28
- RuntimeRBAC,
29
- GuardController
30
- {
31
- /// @custom:oz-upgrades-unsafe-allow constructor
32
- constructor() {
33
- _disableInitializers();
34
- }
35
-
36
- /**
37
- * @dev Override to resolve diamond: SecureOwnable, RuntimeRBAC, GuardController all define initialize(5 params).
38
- * Call this for first init without token metadata; then call initializeToken(name, symbol) to set ERC20 metadata.
39
- */
40
- function initialize(
41
- address initialOwner,
42
- address broadcaster,
43
- address recovery,
44
- uint256 timeLockPeriodSec,
45
- address eventForwarder
46
- ) public virtual override(GuardController, RuntimeRBAC, SecureOwnable) initializer {
47
- _initialize(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
48
- }
49
-
50
- // /**
51
- // * @notice One-step init: security params plus ERC20 name and symbol. Use instead of initialize(5) + initializeToken(name, symbol).
52
- // * @param initialOwner Initial owner
53
- // * @param broadcaster Broadcaster address
54
- // * @param recovery Recovery address
55
- // * @param timeLockPeriodSec Time-lock period in seconds
56
- // * @param eventForwarder Event forwarder address
57
- // * @param name Token name
58
- // * @param symbol Token symbol
59
- // */
60
- // function initializeToken(
61
- // address initialOwner,
62
- // address broadcaster,
63
- // address recovery,
64
- // uint256 timeLockPeriodSec,
65
- // address eventForwarder,
66
- // string memory name,
67
- // string memory symbol
68
- // ) public virtual initializer {
69
- // _initialize(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
70
- // __ERC20_init(name, symbol);
71
- // }
72
-
73
- /**
74
- * @dev Shared setup for GuardController, RuntimeRBAC, SecureOwnable and ERC20Blox definitions.
75
- * Used by initialize(5 params) and initializeToken(5 params, name, symbol).
76
- */
77
- function _initialize(
78
- address initialOwner,
79
- address broadcaster,
80
- address recovery,
81
- uint256 timeLockPeriodSec,
82
- address eventForwarder
83
- ) internal virtual {
84
- GuardController.initialize(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
85
- RuntimeRBAC.initialize(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
86
- SecureOwnable.initialize(initialOwner, broadcaster, recovery, timeLockPeriodSec, eventForwarder);
87
-
88
- // Load ERC20Blox execution selectors (transfer, transferFrom, mint, burn, burnFrom) so controller can execute them
89
- IDefinition.RolePermission memory erc20Permissions = ERC20BloxDefinitions.getRolePermissions();
90
- _loadDefinitions(
91
- ERC20BloxDefinitions.getFunctionSchemas(),
92
- erc20Permissions.roleHashes,
93
- erc20Permissions.functionPermissions,
94
- true
95
- );
96
-
97
- // Register ERC20Blox system macro selectors (allowed to target address(this) for time-lock/meta-tx execution)
98
- _addMacroSelector(ERC20BloxDefinitions.TRANSFER_SELECTOR);
99
- _addMacroSelector(ERC20BloxDefinitions.TRANSFER_FROM_SELECTOR);
100
- _addMacroSelector(ERC20BloxDefinitions.MINT_SELECTOR);
101
- _addMacroSelector(ERC20BloxDefinitions.BURN_SELECTOR);
102
- _addMacroSelector(ERC20BloxDefinitions.BURN_FROM_SELECTOR);
103
- }
104
-
105
- /**
106
- * @notice Initialize the ERC20 token name and symbol. Call after initialize(5 params) for two-step init.
107
- * @param name Token name
108
- * @param symbol Token symbol
109
- */
110
- function initializeToken(string memory name, string memory symbol) public virtual reinitializer(2) {
111
- __ERC20_init(name, symbol);
112
- }
113
-
114
- /**
115
- * @notice Transfer tokens to an account (callable only by this contract via GuardController)
116
- * @param to Recipient address
117
- * @param value Amount to transfer
118
- */
119
- function transfer(address to, uint256 value) public virtual override(ERC20Upgradeable, IERC20) returns (bool) {
120
- _validateExecuteBySelf();
121
- return super.transfer(to, value);
122
- }
123
-
124
- /**
125
- * @notice Transfer tokens from one account to another (with allowance); callable only by this contract via GuardController
126
- * @param from Sender address
127
- * @param to Recipient address
128
- * @param value Amount to transfer
129
- */
130
- function transferFrom(address from, address to, uint256 value) public virtual override(ERC20Upgradeable, IERC20) returns (bool) {
131
- _validateExecuteBySelf();
132
- return super.transferFrom(from, to, value);
133
- }
134
-
135
- /**
136
- * @notice Mint tokens to an account (callable only by this contract via GuardController)
137
- * @param to Recipient address
138
- * @param amount Amount to mint
139
- */
140
- function mint(address to, uint256 amount) external virtual {
141
- _validateExecuteBySelf();
142
- _mint(to, amount);
143
- }
144
-
145
- /**
146
- * @notice Burn tokens from caller (callable only by this contract via GuardController)
147
- * @param value Amount to burn
148
- */
149
- function burn(uint256 value) public virtual override(ERC20BurnableUpgradeable) {
150
- _validateExecuteBySelf();
151
- super.burn(value);
152
- }
153
-
154
- /**
155
- * @notice Burn tokens from an account (with allowance); callable only by this contract via GuardController
156
- * @param account Account to burn from
157
- * @param value Amount to burn
158
- */
159
- function burnFrom(address account, uint256 value) public virtual override(ERC20BurnableUpgradeable) {
160
- _validateExecuteBySelf();
161
- super.burnFrom(account, value);
162
- }
163
-
164
- function supportsInterface(bytes4 interfaceId) public view virtual override(SecureOwnable, RuntimeRBAC, GuardController) returns (bool) {
165
- return interfaceId == type(IERC20).interfaceId || GuardController.supportsInterface(interfaceId);
166
- }
167
- }
@@ -1,185 +0,0 @@
1
- // SPDX-License-Identifier: MPL-2.0
2
- pragma solidity 0.8.33;
3
-
4
- import "../../../../lib/EngineBlox.sol";
5
- import "../../../../lib/interfaces/IDefinition.sol";
6
-
7
- /**
8
- * @title ERC20BloxDefinitions
9
- * @dev Definition library for ERC20Blox execution selectors (transfer, transferFrom, mint, burn, burnFrom).
10
- * Registers function schemas and role permissions so the GuardController can execute these functions
11
- * via time-lock and meta-transaction workflows. Handler permissions (executeWithTimeLock, etc.) are
12
- * defined in GuardControllerDefinitions.
13
- * @custom:security-contact security@particlecrypto.com
14
- */
15
- library ERC20BloxDefinitions {
16
-
17
- // System macro selectors (allowed to target address(this) for GuardController execution)
18
- bytes4 public constant TRANSFER_SELECTOR = bytes4(keccak256("transfer(address,uint256)"));
19
- bytes4 public constant TRANSFER_FROM_SELECTOR = bytes4(keccak256("transferFrom(address,address,uint256)"));
20
- bytes4 public constant MINT_SELECTOR = bytes4(keccak256("mint(address,uint256)"));
21
- bytes4 public constant BURN_SELECTOR = bytes4(keccak256("burn(uint256)"));
22
- bytes4 public constant BURN_FROM_SELECTOR = bytes4(keccak256("burnFrom(address,uint256)"));
23
-
24
- bytes32 public constant ERC20_OPERATION = keccak256("ERC20_OPERATION");
25
-
26
- /**
27
- * @dev Returns function schemas for ERC20Blox execution selectors (used by controller).
28
- */
29
- function getFunctionSchemas() public pure returns (EngineBlox.FunctionSchema[] memory) {
30
- EngineBlox.FunctionSchema[] memory schemas = new EngineBlox.FunctionSchema[](5);
31
-
32
- EngineBlox.TxAction[] memory timeDelayRequestActions = new EngineBlox.TxAction[](1);
33
- timeDelayRequestActions[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_REQUEST;
34
- EngineBlox.TxAction[] memory timeDelayApproveActions = new EngineBlox.TxAction[](1);
35
- timeDelayApproveActions[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_APPROVE;
36
- EngineBlox.TxAction[] memory timeDelayCancelActions = new EngineBlox.TxAction[](1);
37
- timeDelayCancelActions[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_CANCEL;
38
- EngineBlox.TxAction[] memory metaTxRequestApproveActions = new EngineBlox.TxAction[](2);
39
- metaTxRequestApproveActions[0] = EngineBlox.TxAction.SIGN_META_REQUEST_AND_APPROVE;
40
- metaTxRequestApproveActions[1] = EngineBlox.TxAction.EXECUTE_META_REQUEST_AND_APPROVE;
41
- EngineBlox.TxAction[] memory metaTxApproveActions = new EngineBlox.TxAction[](2);
42
- metaTxApproveActions[0] = EngineBlox.TxAction.SIGN_META_APPROVE;
43
- metaTxApproveActions[1] = EngineBlox.TxAction.EXECUTE_META_APPROVE;
44
- EngineBlox.TxAction[] memory metaTxCancelActions = new EngineBlox.TxAction[](2);
45
- metaTxCancelActions[0] = EngineBlox.TxAction.SIGN_META_CANCEL;
46
- metaTxCancelActions[1] = EngineBlox.TxAction.EXECUTE_META_CANCEL;
47
-
48
- uint16 actionsBitmap = EngineBlox.createBitmapFromActions(timeDelayRequestActions)
49
- | EngineBlox.createBitmapFromActions(timeDelayApproveActions)
50
- | EngineBlox.createBitmapFromActions(timeDelayCancelActions)
51
- | EngineBlox.createBitmapFromActions(metaTxRequestApproveActions)
52
- | EngineBlox.createBitmapFromActions(metaTxApproveActions)
53
- | EngineBlox.createBitmapFromActions(metaTxCancelActions);
54
-
55
- bytes4[] memory transferHandlers = new bytes4[](1);
56
- transferHandlers[0] = TRANSFER_SELECTOR;
57
- bytes4[] memory transferFromHandlers = new bytes4[](1);
58
- transferFromHandlers[0] = TRANSFER_FROM_SELECTOR;
59
- bytes4[] memory mintHandlers = new bytes4[](1);
60
- mintHandlers[0] = MINT_SELECTOR;
61
- bytes4[] memory burnHandlers = new bytes4[](1);
62
- burnHandlers[0] = BURN_SELECTOR;
63
- bytes4[] memory burnFromHandlers = new bytes4[](1);
64
- burnFromHandlers[0] = BURN_FROM_SELECTOR;
65
-
66
- schemas[0] = EngineBlox.FunctionSchema({
67
- functionSignature: "transfer(address,uint256)",
68
- functionSelector: TRANSFER_SELECTOR,
69
- operationType: ERC20_OPERATION,
70
- operationName: "ERC20_TRANSFER",
71
- supportedActionsBitmap: actionsBitmap,
72
- isProtected: true,
73
- handlerForSelectors: transferHandlers
74
- });
75
- schemas[1] = EngineBlox.FunctionSchema({
76
- functionSignature: "transferFrom(address,address,uint256)",
77
- functionSelector: TRANSFER_FROM_SELECTOR,
78
- operationType: ERC20_OPERATION,
79
- operationName: "ERC20_TRANSFER_FROM",
80
- supportedActionsBitmap: actionsBitmap,
81
- isProtected: true,
82
- handlerForSelectors: transferFromHandlers
83
- });
84
- schemas[2] = EngineBlox.FunctionSchema({
85
- functionSignature: "mint(address,uint256)",
86
- functionSelector: MINT_SELECTOR,
87
- operationType: ERC20_OPERATION,
88
- operationName: "ERC20_MINT",
89
- supportedActionsBitmap: actionsBitmap,
90
- isProtected: true,
91
- handlerForSelectors: mintHandlers
92
- });
93
- schemas[3] = EngineBlox.FunctionSchema({
94
- functionSignature: "burn(uint256)",
95
- functionSelector: BURN_SELECTOR,
96
- operationType: ERC20_OPERATION,
97
- operationName: "ERC20_BURN",
98
- supportedActionsBitmap: actionsBitmap,
99
- isProtected: true,
100
- handlerForSelectors: burnHandlers
101
- });
102
- schemas[4] = EngineBlox.FunctionSchema({
103
- functionSignature: "burnFrom(address,uint256)",
104
- functionSelector: BURN_FROM_SELECTOR,
105
- operationType: ERC20_OPERATION,
106
- operationName: "ERC20_BURN_FROM",
107
- supportedActionsBitmap: actionsBitmap,
108
- isProtected: true,
109
- handlerForSelectors: burnFromHandlers
110
- });
111
-
112
- return schemas;
113
- }
114
-
115
- /**
116
- * @dev Returns role permissions for ERC20Blox execution selectors (OWNER and BROADCASTER).
117
- */
118
- function getRolePermissions() public pure returns (IDefinition.RolePermission memory) {
119
- bytes32[] memory roleHashes = new bytes32[](10);
120
- EngineBlox.FunctionPermission[] memory functionPermissions = new EngineBlox.FunctionPermission[](10);
121
-
122
- EngineBlox.TxAction[] memory ownerTimeLockRequest = new EngineBlox.TxAction[](1);
123
- ownerTimeLockRequest[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_REQUEST;
124
- EngineBlox.TxAction[] memory ownerTimeLockApprove = new EngineBlox.TxAction[](1);
125
- ownerTimeLockApprove[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_APPROVE;
126
- EngineBlox.TxAction[] memory ownerTimeLockCancel = new EngineBlox.TxAction[](1);
127
- ownerTimeLockCancel[0] = EngineBlox.TxAction.EXECUTE_TIME_DELAY_CANCEL;
128
- EngineBlox.TxAction[] memory ownerMetaSign = new EngineBlox.TxAction[](1);
129
- ownerMetaSign[0] = EngineBlox.TxAction.SIGN_META_REQUEST_AND_APPROVE;
130
- EngineBlox.TxAction[] memory ownerMetaApprove = new EngineBlox.TxAction[](1);
131
- ownerMetaApprove[0] = EngineBlox.TxAction.SIGN_META_APPROVE;
132
- EngineBlox.TxAction[] memory ownerMetaCancel = new EngineBlox.TxAction[](1);
133
- ownerMetaCancel[0] = EngineBlox.TxAction.SIGN_META_CANCEL;
134
- EngineBlox.TxAction[] memory broadcasterMetaExec = new EngineBlox.TxAction[](1);
135
- broadcasterMetaExec[0] = EngineBlox.TxAction.EXECUTE_META_REQUEST_AND_APPROVE;
136
- EngineBlox.TxAction[] memory broadcasterMetaApprove = new EngineBlox.TxAction[](1);
137
- broadcasterMetaApprove[0] = EngineBlox.TxAction.EXECUTE_META_APPROVE;
138
- EngineBlox.TxAction[] memory broadcasterMetaCancel = new EngineBlox.TxAction[](1);
139
- broadcasterMetaCancel[0] = EngineBlox.TxAction.EXECUTE_META_CANCEL;
140
-
141
- uint16 ownerBitmap = EngineBlox.createBitmapFromActions(ownerTimeLockRequest)
142
- | EngineBlox.createBitmapFromActions(ownerTimeLockApprove)
143
- | EngineBlox.createBitmapFromActions(ownerTimeLockCancel)
144
- | EngineBlox.createBitmapFromActions(ownerMetaSign)
145
- | EngineBlox.createBitmapFromActions(ownerMetaApprove)
146
- | EngineBlox.createBitmapFromActions(ownerMetaCancel);
147
- uint16 broadcasterBitmap = EngineBlox.createBitmapFromActions(broadcasterMetaExec)
148
- | EngineBlox.createBitmapFromActions(broadcasterMetaApprove)
149
- | EngineBlox.createBitmapFromActions(broadcasterMetaCancel);
150
-
151
- bytes4[5] memory selectors = [TRANSFER_SELECTOR, TRANSFER_FROM_SELECTOR, MINT_SELECTOR, BURN_SELECTOR, BURN_FROM_SELECTOR];
152
- for (uint256 i = 0; i < 5; i++) {
153
- bytes4[] memory selfRef = new bytes4[](1);
154
- selfRef[0] = selectors[i];
155
- roleHashes[i] = EngineBlox.OWNER_ROLE;
156
- functionPermissions[i] = EngineBlox.FunctionPermission({
157
- functionSelector: selectors[i],
158
- grantedActionsBitmap: ownerBitmap,
159
- handlerForSelectors: selfRef
160
- });
161
- }
162
- for (uint256 i = 0; i < 5; i++) {
163
- bytes4[] memory selfRef = new bytes4[](1);
164
- selfRef[0] = selectors[i];
165
- roleHashes[5 + i] = EngineBlox.BROADCASTER_ROLE;
166
- functionPermissions[5 + i] = EngineBlox.FunctionPermission({
167
- functionSelector: selectors[i],
168
- grantedActionsBitmap: broadcasterBitmap,
169
- handlerForSelectors: selfRef
170
- });
171
- }
172
-
173
- return IDefinition.RolePermission({
174
- roleHashes: roleHashes,
175
- functionPermissions: functionPermissions
176
- });
177
- }
178
-
179
- /**
180
- * @dev ERC165: report support for IDefinition when this library is used at an address
181
- */
182
- function supportsInterface(bytes4 interfaceId) external pure returns (bool) {
183
- return interfaceId == type(IDefinition).interfaceId;
184
- }
185
- }