@ensuro/access-managed-proxy 0.1.0 → 0.2.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 (38) hide show
  1. package/README.md +38 -2
  2. package/build/build-info.json +1 -1
  3. package/build/contracts/AMPUtils.sol/AMPUtils.json +22 -0
  4. package/build/contracts/amps/AccessManagedProxyS25.sol/AccessManagedProxyS25.json +123 -0
  5. package/build/contracts/amps/AccessManagedProxyS26.sol/AccessManagedProxyS26.json +123 -0
  6. package/build/contracts/amps/AccessManagedProxyS27.sol/AccessManagedProxyS27.json +123 -0
  7. package/build/contracts/amps/AccessManagedProxyS28.sol/AccessManagedProxyS28.json +123 -0
  8. package/build/contracts/amps/AccessManagedProxyS29.sol/AccessManagedProxyS29.json +123 -0
  9. package/build/contracts/amps/AccessManagedProxyS30.sol/AccessManagedProxyS30.json +123 -0
  10. package/build/contracts/amps/AccessManagedProxyS31.sol/AccessManagedProxyS31.json +123 -0
  11. package/build/contracts/amps/AccessManagedProxyS32.sol/AccessManagedProxyS32.json +123 -0
  12. package/build/contracts/amps/AccessManagedProxyS33.sol/AccessManagedProxyS33.json +123 -0
  13. package/build/contracts/amps/AccessManagedProxyS34.sol/AccessManagedProxyS34.json +123 -0
  14. package/build/contracts/amps/AccessManagedProxyS35.sol/AccessManagedProxyS35.json +123 -0
  15. package/build/contracts/amps/AccessManagedProxyS36.sol/AccessManagedProxyS36.json +123 -0
  16. package/build/contracts/amps/AccessManagedProxyS37.sol/AccessManagedProxyS37.json +123 -0
  17. package/build/contracts/amps/AccessManagedProxyS38.sol/AccessManagedProxyS38.json +123 -0
  18. package/build/contracts/amps/AccessManagedProxyS39.sol/AccessManagedProxyS39.json +123 -0
  19. package/build/contracts/amps/AccessManagedProxyS40.sol/AccessManagedProxyS40.json +123 -0
  20. package/contracts/AMPUtils.sol +37 -0
  21. package/contracts/amps/AccessManagedProxyS25.sol +159 -0
  22. package/contracts/amps/AccessManagedProxyS26.sol +163 -0
  23. package/contracts/amps/AccessManagedProxyS27.sol +167 -0
  24. package/contracts/amps/AccessManagedProxyS28.sol +171 -0
  25. package/contracts/amps/AccessManagedProxyS29.sol +175 -0
  26. package/contracts/amps/AccessManagedProxyS30.sol +179 -0
  27. package/contracts/amps/AccessManagedProxyS31.sol +183 -0
  28. package/contracts/amps/AccessManagedProxyS32.sol +187 -0
  29. package/contracts/amps/AccessManagedProxyS33.sol +191 -0
  30. package/contracts/amps/AccessManagedProxyS34.sol +195 -0
  31. package/contracts/amps/AccessManagedProxyS35.sol +199 -0
  32. package/contracts/amps/AccessManagedProxyS36.sol +203 -0
  33. package/contracts/amps/AccessManagedProxyS37.sol +207 -0
  34. package/contracts/amps/AccessManagedProxyS38.sol +211 -0
  35. package/contracts/amps/AccessManagedProxyS39.sol +215 -0
  36. package/contracts/amps/AccessManagedProxyS40.sol +219 -0
  37. package/js/deployProxy.js +75 -0
  38. package/package.json +1 -1
@@ -0,0 +1,219 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {AccessManagedProxy} from "../AccessManagedProxy.sol";
5
+ import {IAccessManager} from "@openzeppelin/contracts/access/manager/IAccessManager.sol";
6
+
7
+ /**
8
+ * @title AccessManagedProxyS40
9
+ * @notice Specialization of AccessManagedProxy with pass thru (skips AM) for some messages for gas optimization
10
+ *
11
+ * @custom:security-contact security@ensuro.co
12
+ * @author Ensuro
13
+ */
14
+ contract AccessManagedProxyS40 is AccessManagedProxy {
15
+ bytes4 internal immutable PASS_THRU_METHODS_0;
16
+ bytes4 internal immutable PASS_THRU_METHODS_1;
17
+ bytes4 internal immutable PASS_THRU_METHODS_2;
18
+ bytes4 internal immutable PASS_THRU_METHODS_3;
19
+ bytes4 internal immutable PASS_THRU_METHODS_4;
20
+ bytes4 internal immutable PASS_THRU_METHODS_5;
21
+ bytes4 internal immutable PASS_THRU_METHODS_6;
22
+ bytes4 internal immutable PASS_THRU_METHODS_7;
23
+ bytes4 internal immutable PASS_THRU_METHODS_8;
24
+ bytes4 internal immutable PASS_THRU_METHODS_9;
25
+ bytes4 internal immutable PASS_THRU_METHODS_10;
26
+ bytes4 internal immutable PASS_THRU_METHODS_11;
27
+ bytes4 internal immutable PASS_THRU_METHODS_12;
28
+ bytes4 internal immutable PASS_THRU_METHODS_13;
29
+ bytes4 internal immutable PASS_THRU_METHODS_14;
30
+ bytes4 internal immutable PASS_THRU_METHODS_15;
31
+ bytes4 internal immutable PASS_THRU_METHODS_16;
32
+ bytes4 internal immutable PASS_THRU_METHODS_17;
33
+ bytes4 internal immutable PASS_THRU_METHODS_18;
34
+ bytes4 internal immutable PASS_THRU_METHODS_19;
35
+ bytes4 internal immutable PASS_THRU_METHODS_20;
36
+ bytes4 internal immutable PASS_THRU_METHODS_21;
37
+ bytes4 internal immutable PASS_THRU_METHODS_22;
38
+ bytes4 internal immutable PASS_THRU_METHODS_23;
39
+ bytes4 internal immutable PASS_THRU_METHODS_24;
40
+ bytes4 internal immutable PASS_THRU_METHODS_25;
41
+ bytes4 internal immutable PASS_THRU_METHODS_26;
42
+ bytes4 internal immutable PASS_THRU_METHODS_27;
43
+ bytes4 internal immutable PASS_THRU_METHODS_28;
44
+ bytes4 internal immutable PASS_THRU_METHODS_29;
45
+ bytes4 internal immutable PASS_THRU_METHODS_30;
46
+ bytes4 internal immutable PASS_THRU_METHODS_31;
47
+ bytes4 internal immutable PASS_THRU_METHODS_32;
48
+ bytes4 internal immutable PASS_THRU_METHODS_33;
49
+ bytes4 internal immutable PASS_THRU_METHODS_34;
50
+ bytes4 internal immutable PASS_THRU_METHODS_35;
51
+ bytes4 internal immutable PASS_THRU_METHODS_36;
52
+ bytes4 internal immutable PASS_THRU_METHODS_37;
53
+ bytes4 internal immutable PASS_THRU_METHODS_38;
54
+ bytes4 internal immutable PASS_THRU_METHODS_39;
55
+
56
+ /**
57
+ * @notice Constructor of the proxy, defining the implementation and the access manager
58
+ * @dev Initializes the upgradeable proxy with an initial implementation specified by `implementation` and
59
+ * with `manager` as the ACCESS_MANAGER that will handle access control.
60
+ *
61
+ * @param implementation The initial implementation contract.
62
+ * @param _data If nonempty, it's used as data in a delegate call to `implementation`. This will typically be an
63
+ * encoded function call, and allows initializing the storage of the proxy like a Solidity constructor.
64
+ * @param manager The access manager that will handle access control
65
+ * @param passThruMethods The selector of methods that will skip the access control validation, typically used for
66
+ * views and other methods for gas optimization.
67
+ *
68
+ * Requirements:
69
+ *
70
+ * - If `data` is empty, `msg.value` must be zero.
71
+ */
72
+ constructor(
73
+ address implementation,
74
+ bytes memory _data,
75
+ IAccessManager manager,
76
+ bytes4[40] memory passThruMethods
77
+ ) payable AccessManagedProxy(implementation, _data, manager) {
78
+ PASS_THRU_METHODS_0 = passThruMethods[0];
79
+ PASS_THRU_METHODS_1 = passThruMethods[1];
80
+ PASS_THRU_METHODS_2 = passThruMethods[2];
81
+ PASS_THRU_METHODS_3 = passThruMethods[3];
82
+ PASS_THRU_METHODS_4 = passThruMethods[4];
83
+ PASS_THRU_METHODS_5 = passThruMethods[5];
84
+ PASS_THRU_METHODS_6 = passThruMethods[6];
85
+ PASS_THRU_METHODS_7 = passThruMethods[7];
86
+ PASS_THRU_METHODS_8 = passThruMethods[8];
87
+ PASS_THRU_METHODS_9 = passThruMethods[9];
88
+ PASS_THRU_METHODS_10 = passThruMethods[10];
89
+ PASS_THRU_METHODS_11 = passThruMethods[11];
90
+ PASS_THRU_METHODS_12 = passThruMethods[12];
91
+ PASS_THRU_METHODS_13 = passThruMethods[13];
92
+ PASS_THRU_METHODS_14 = passThruMethods[14];
93
+ PASS_THRU_METHODS_15 = passThruMethods[15];
94
+ PASS_THRU_METHODS_16 = passThruMethods[16];
95
+ PASS_THRU_METHODS_17 = passThruMethods[17];
96
+ PASS_THRU_METHODS_18 = passThruMethods[18];
97
+ PASS_THRU_METHODS_19 = passThruMethods[19];
98
+ PASS_THRU_METHODS_20 = passThruMethods[20];
99
+ PASS_THRU_METHODS_21 = passThruMethods[21];
100
+ PASS_THRU_METHODS_22 = passThruMethods[22];
101
+ PASS_THRU_METHODS_23 = passThruMethods[23];
102
+ PASS_THRU_METHODS_24 = passThruMethods[24];
103
+ PASS_THRU_METHODS_25 = passThruMethods[25];
104
+ PASS_THRU_METHODS_26 = passThruMethods[26];
105
+ PASS_THRU_METHODS_27 = passThruMethods[27];
106
+ PASS_THRU_METHODS_28 = passThruMethods[28];
107
+ PASS_THRU_METHODS_29 = passThruMethods[29];
108
+ PASS_THRU_METHODS_30 = passThruMethods[30];
109
+ PASS_THRU_METHODS_31 = passThruMethods[31];
110
+ PASS_THRU_METHODS_32 = passThruMethods[32];
111
+ PASS_THRU_METHODS_33 = passThruMethods[33];
112
+ PASS_THRU_METHODS_34 = passThruMethods[34];
113
+ PASS_THRU_METHODS_35 = passThruMethods[35];
114
+ PASS_THRU_METHODS_36 = passThruMethods[36];
115
+ PASS_THRU_METHODS_37 = passThruMethods[37];
116
+ PASS_THRU_METHODS_38 = passThruMethods[38];
117
+ PASS_THRU_METHODS_39 = passThruMethods[39];
118
+ }
119
+
120
+ /*
121
+ * @notice Skips the access control if the method called is one of the passThruMethods
122
+ * @dev See {PASS_THRU_METHODS()}
123
+ * @param selector The selector of the method called
124
+ * @return Whether the access control using ACCESS_MANAGER should be skipped or not
125
+ */
126
+ function _skipAC(bytes4 selector) internal view override returns (bool) {
127
+ return
128
+ selector == PASS_THRU_METHODS_0 ||
129
+ selector == PASS_THRU_METHODS_1 ||
130
+ selector == PASS_THRU_METHODS_2 ||
131
+ selector == PASS_THRU_METHODS_3 ||
132
+ selector == PASS_THRU_METHODS_4 ||
133
+ selector == PASS_THRU_METHODS_5 ||
134
+ selector == PASS_THRU_METHODS_6 ||
135
+ selector == PASS_THRU_METHODS_7 ||
136
+ selector == PASS_THRU_METHODS_8 ||
137
+ selector == PASS_THRU_METHODS_9 ||
138
+ selector == PASS_THRU_METHODS_10 ||
139
+ selector == PASS_THRU_METHODS_11 ||
140
+ selector == PASS_THRU_METHODS_12 ||
141
+ selector == PASS_THRU_METHODS_13 ||
142
+ selector == PASS_THRU_METHODS_14 ||
143
+ selector == PASS_THRU_METHODS_15 ||
144
+ selector == PASS_THRU_METHODS_16 ||
145
+ selector == PASS_THRU_METHODS_17 ||
146
+ selector == PASS_THRU_METHODS_18 ||
147
+ selector == PASS_THRU_METHODS_19 ||
148
+ selector == PASS_THRU_METHODS_20 ||
149
+ selector == PASS_THRU_METHODS_21 ||
150
+ selector == PASS_THRU_METHODS_22 ||
151
+ selector == PASS_THRU_METHODS_23 ||
152
+ selector == PASS_THRU_METHODS_24 ||
153
+ selector == PASS_THRU_METHODS_25 ||
154
+ selector == PASS_THRU_METHODS_26 ||
155
+ selector == PASS_THRU_METHODS_27 ||
156
+ selector == PASS_THRU_METHODS_28 ||
157
+ selector == PASS_THRU_METHODS_29 ||
158
+ selector == PASS_THRU_METHODS_30 ||
159
+ selector == PASS_THRU_METHODS_31 ||
160
+ selector == PASS_THRU_METHODS_32 ||
161
+ selector == PASS_THRU_METHODS_33 ||
162
+ selector == PASS_THRU_METHODS_34 ||
163
+ selector == PASS_THRU_METHODS_35 ||
164
+ selector == PASS_THRU_METHODS_36 ||
165
+ selector == PASS_THRU_METHODS_37 ||
166
+ selector == PASS_THRU_METHODS_38 ||
167
+ selector == PASS_THRU_METHODS_39;
168
+ }
169
+
170
+ /**
171
+ * @notice Gives observability to the methods that are skipped from access control
172
+ * @dev This list is fixed and defined on contract construction
173
+ * @return methods The list of method selectors that skip ACCESS_MANAGER access control
174
+ */
175
+ // solhint-disable-next-line func-name-mixedcase
176
+ function PASS_THRU_METHODS() external view returns (bytes4[] memory methods) {
177
+ methods = new bytes4[](40);
178
+ methods[0] = PASS_THRU_METHODS_0;
179
+ methods[1] = PASS_THRU_METHODS_1;
180
+ methods[2] = PASS_THRU_METHODS_2;
181
+ methods[3] = PASS_THRU_METHODS_3;
182
+ methods[4] = PASS_THRU_METHODS_4;
183
+ methods[5] = PASS_THRU_METHODS_5;
184
+ methods[6] = PASS_THRU_METHODS_6;
185
+ methods[7] = PASS_THRU_METHODS_7;
186
+ methods[8] = PASS_THRU_METHODS_8;
187
+ methods[9] = PASS_THRU_METHODS_9;
188
+ methods[10] = PASS_THRU_METHODS_10;
189
+ methods[11] = PASS_THRU_METHODS_11;
190
+ methods[12] = PASS_THRU_METHODS_12;
191
+ methods[13] = PASS_THRU_METHODS_13;
192
+ methods[14] = PASS_THRU_METHODS_14;
193
+ methods[15] = PASS_THRU_METHODS_15;
194
+ methods[16] = PASS_THRU_METHODS_16;
195
+ methods[17] = PASS_THRU_METHODS_17;
196
+ methods[18] = PASS_THRU_METHODS_18;
197
+ methods[19] = PASS_THRU_METHODS_19;
198
+ methods[20] = PASS_THRU_METHODS_20;
199
+ methods[21] = PASS_THRU_METHODS_21;
200
+ methods[22] = PASS_THRU_METHODS_22;
201
+ methods[23] = PASS_THRU_METHODS_23;
202
+ methods[24] = PASS_THRU_METHODS_24;
203
+ methods[25] = PASS_THRU_METHODS_25;
204
+ methods[26] = PASS_THRU_METHODS_26;
205
+ methods[27] = PASS_THRU_METHODS_27;
206
+ methods[28] = PASS_THRU_METHODS_28;
207
+ methods[29] = PASS_THRU_METHODS_29;
208
+ methods[30] = PASS_THRU_METHODS_30;
209
+ methods[31] = PASS_THRU_METHODS_31;
210
+ methods[32] = PASS_THRU_METHODS_32;
211
+ methods[33] = PASS_THRU_METHODS_33;
212
+ methods[34] = PASS_THRU_METHODS_34;
213
+ methods[35] = PASS_THRU_METHODS_35;
214
+ methods[36] = PASS_THRU_METHODS_36;
215
+ methods[37] = PASS_THRU_METHODS_37;
216
+ methods[38] = PASS_THRU_METHODS_38;
217
+ methods[39] = PASS_THRU_METHODS_39;
218
+ }
219
+ }
@@ -0,0 +1,75 @@
1
+ const hre = require("hardhat");
2
+ const { ethers } = hre;
3
+ const { deploy: ozUpgradesDeploy } = require("@openzeppelin/hardhat-upgrades/dist/utils");
4
+
5
+ /**
6
+ * Deploys a contract using an AccessManagedProxy. Similar to hre.upgrades.deployProxy, but using AccessManagedProxy
7
+ *
8
+ * @param {contractFactory} The contract factory of the implementation contract
9
+ * @param {initializeArgs} Arguments for `initialize`
10
+ * @param {opts} Options for hre.upgrades.deployProxy with some AccessManagedProxy additions:
11
+ * - skipViewsAndPure: if true, deploys a proxy that will skip the access control for all the view and
12
+ * pure methods
13
+ * - skipMethods: list of method names that will skip the access control. Added to views and pure, if
14
+ * skipViewsAndPure is true.
15
+ * - acMgr: mandatory argument that will be used for the AMP
16
+ * @returns {contract} Promise<Contract>
17
+ */
18
+ async function deployAMPProxy(contractFactory, initializeArgs = [], opts = {}) {
19
+ const { acMgr, skipViewsAndPure, skipMethods } = opts;
20
+ if (acMgr === undefined) throw new Error("Missing required `acMgr` in opts");
21
+ let skipSelectors = [];
22
+ if (skipViewsAndPure) {
23
+ skipSelectors = contractFactory.interface.fragments
24
+ .filter(
25
+ (fragment) =>
26
+ fragment.type === "function" && (fragment.stateMutability === "pure" || fragment.stateMutability === "view")
27
+ )
28
+ .map((fragment) => fragment.selector);
29
+ }
30
+ if (skipMethods !== undefined && skipMethods.length > 0) {
31
+ skipSelectors.push(
32
+ ...skipMethods.map((method) =>
33
+ method.startsWith("0x") ? method : contractFactory.interface.getFunction(method).selector
34
+ )
35
+ );
36
+ }
37
+ let proxyFactory, deployFunction;
38
+ if (skipSelectors.length > 0) {
39
+ proxyFactory = await ethers.getContractFactory(`AccessManagedProxyS${skipSelectors.length}`);
40
+ deployFunction = async (hre_, opts, factory, ...args) =>
41
+ ozUpgradesDeploy(hre_, opts, factory, ...args, acMgr, skipSelectors);
42
+ } else {
43
+ proxyFactory = await ethers.getContractFactory("AccessManagedProxy");
44
+ deployFunction = async (hre_, opts, factory, ...args) => ozUpgradesDeploy(hre_, opts, factory, ...args, acMgr);
45
+ }
46
+
47
+ return hre.upgrades.deployProxy(contractFactory, initializeArgs, {
48
+ ...opts,
49
+ kind: "uups",
50
+ proxyFactory,
51
+ deployFunction,
52
+ });
53
+ }
54
+
55
+ async function attachAsAMP(contract, ampContractFactory = undefined) {
56
+ ampContractFactory = ampContractFactory || (await ethers.getContractFactory("AccessManagedProxyS1"));
57
+ return ampContractFactory.attach(contract);
58
+ }
59
+
60
+ async function getAccessManager(contract, ampContractFactory = undefined, accessManagerFactory="AccessManager") {
61
+ const contractAsAMP = await attachAsAMP(contract, ampContractFactory);
62
+ return ethers.getContractAt(accessManagerFactory, await contractAsAMP.ACCESS_MANAGER());
63
+ }
64
+
65
+ function makeSelector(role) {
66
+ return ethers.keccak256(ethers.toUtf8Bytes(role)).slice(0, 10);
67
+ }
68
+
69
+
70
+ module.exports = {
71
+ deployAMPProxy,
72
+ attachAsAMP,
73
+ getAccessManager,
74
+ makeSelector,
75
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ensuro/access-managed-proxy",
3
3
  "description": "Variant of ERC-1967 proxy with built-in access control using OZ 5.x AccessManager",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "files": [
6
6
  "**/*.sol",
7
7
  "/build",