@buenos_andres/contracts 0.0.1-test.2 → 0.0.1-test.26
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.
- package/README.md +1 -0
- package/package.json +3 -14
- package/{dist/utils → utils}/Utils.compact +1 -0
- package/utils/witnesses/UtilsWitnesses.js +4 -0
- package/utils/witnesses/UtilsWitnesses.js.map +1 -0
- package/dist/access/AccessControl.compact +0 -321
- package/dist/access/Ownable.compact +0 -212
- package/dist/access/witnesses/AccessControlWitnesses.d.ts +0 -2
- package/dist/access/witnesses/AccessControlWitnesses.js +0 -1
- package/dist/access/witnesses/AccessControlWitnesses.ts +0 -3
- package/dist/access/witnesses/OwnableWitnesses.d.ts +0 -2
- package/dist/access/witnesses/OwnableWitnesses.js +0 -1
- package/dist/access/witnesses/OwnableWitnesses.ts +0 -3
- package/dist/security/Initializable.compact +0 -59
- package/dist/security/Pausable.compact +0 -88
- package/dist/security/witnesses/InitializableWitnesses.d.ts +0 -2
- package/dist/security/witnesses/InitializableWitnesses.js +0 -1
- package/dist/security/witnesses/InitializableWitnesses.ts +0 -3
- package/dist/security/witnesses/PausableWitnesses.d.ts +0 -2
- package/dist/security/witnesses/PausableWitnesses.js +0 -1
- package/dist/security/witnesses/PausableWitnesses.ts +0 -3
- package/dist/token/FungibleToken.compact +0 -606
- package/dist/token/MultiToken.compact +0 -545
- package/dist/token/NonFungibleToken.compact +0 -805
- package/dist/token/witnesses/FungibleTokenWitnesses.d.ts +0 -2
- package/dist/token/witnesses/FungibleTokenWitnesses.js +0 -1
- package/dist/token/witnesses/FungibleTokenWitnesses.ts +0 -3
- package/dist/token/witnesses/MultiTokenWitnesses.d.ts +0 -2
- package/dist/token/witnesses/MultiTokenWitnesses.js +0 -1
- package/dist/token/witnesses/MultiTokenWitnesses.ts +0 -3
- package/dist/token/witnesses/NonFungibleTokenWitnesses.d.ts +0 -2
- package/dist/token/witnesses/NonFungibleTokenWitnesses.js +0 -1
- package/dist/token/witnesses/NonFungibleTokenWitnesses.ts +0 -3
- package/dist/utils/witnesses/UtilsWitnesses.js +0 -1
- package/dist/utils/witnesses/UtilsWitnesses.ts +0 -3
- /package/{dist/utils → utils}/witnesses/UtilsWitnesses.d.ts +0 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Test package
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buenos_andres/contracts",
|
|
3
|
-
"version": "0.0.1-test.
|
|
3
|
+
"version": "0.0.1-test.26",
|
|
4
4
|
"description": "test contract stuff",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"foo",
|
|
@@ -9,27 +9,16 @@
|
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"author": "daddy",
|
|
11
11
|
"type": "module",
|
|
12
|
-
"main": "dist/index.js",
|
|
13
|
-
"module": "dist/index.js",
|
|
14
|
-
"types": "./dist/index.d.ts",
|
|
15
|
-
"exports": {
|
|
16
|
-
"./*": "./dist/*"
|
|
17
|
-
},
|
|
18
|
-
"files": [
|
|
19
|
-
"dist/**"
|
|
20
|
-
],
|
|
21
12
|
"scripts": {
|
|
22
13
|
"compact": "compact-compiler",
|
|
23
|
-
"build": "compact-builder",
|
|
24
|
-
"
|
|
14
|
+
"build": "compact-builder && tsc",
|
|
15
|
+
"package": "compact-packager",
|
|
25
16
|
"test": "vitest run",
|
|
26
17
|
"types": "tsc -p tsconfig.json --noEmit",
|
|
27
18
|
"clean": "git clean -fXd"
|
|
28
19
|
},
|
|
29
20
|
"devDependencies": {
|
|
30
21
|
"@buenos_andres/compact": "workspace:^",
|
|
31
|
-
"@types/node": "22.14.0",
|
|
32
|
-
"ts-node": "^10.9.2",
|
|
33
22
|
"typescript": "^5.2.2",
|
|
34
23
|
"vitest": "^3.1.3"
|
|
35
24
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UtilsWitnesses.js","sourceRoot":"","sources":["../../../src/utils/witnesses/UtilsWitnesses.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,qEAAqE;AAIrE,MAAM,CAAC,MAAM,cAAc,GAAG,EAAE,CAAC"}
|
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
|
|
3
|
-
pragma language_version >= 0.16.0;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @module AccessControl
|
|
7
|
-
* @description An unshielded AccessControl library.
|
|
8
|
-
* This module provides a role-based access control mechanism, where roles can be used to
|
|
9
|
-
* represent a set of permissions.
|
|
10
|
-
*
|
|
11
|
-
* Roles are referred to by their `Bytes<32>` identifier. These should be exposed
|
|
12
|
-
* in the top-level contract and be unique. One way to achieve this is by
|
|
13
|
-
* using `export sealed ledger` hash digests that are initialized in the top-level contract:
|
|
14
|
-
*
|
|
15
|
-
* ```typescript
|
|
16
|
-
* import CompactStandardLibrary;
|
|
17
|
-
* import "./node_modules/@daddy-compact/accessControl/src/AccessControl" prefix AccessControl_;
|
|
18
|
-
*
|
|
19
|
-
* export sealed ledger MY_ROLE: Bytes<32>;
|
|
20
|
-
*
|
|
21
|
-
* constructor() {
|
|
22
|
-
* MY_ROLE = persistentHash<Bytes<32>>(pad(32, "MY_ROLE"));
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* To restrict access to a circuit, use {assertOnlyRole}:
|
|
27
|
-
*
|
|
28
|
-
* ```typescript
|
|
29
|
-
* circuit foo(): [] {
|
|
30
|
-
* assertOnlyRole(MY_ROLE);
|
|
31
|
-
* ...
|
|
32
|
-
* }
|
|
33
|
-
* ```
|
|
34
|
-
*
|
|
35
|
-
* Roles can be granted and revoked dynamically via the {grantRole} and
|
|
36
|
-
* {revokeRole} circuits. Each role has an associated admin role, and only
|
|
37
|
-
* accounts that have a role's admin role can call {grantRole} and {revokeRole}.
|
|
38
|
-
*
|
|
39
|
-
* By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
|
|
40
|
-
* that only accounts with this role will be able to grant or revoke other
|
|
41
|
-
* roles. More complex role relationships can be created by using
|
|
42
|
-
* {_setRoleAdmin}. To set a custom `DEFAULT_ADMIN_ROLE`, implement the `Initializable`
|
|
43
|
-
* module and set `DEFAULT_ADMIN_ROLE` in the `initialize()` circuit.
|
|
44
|
-
*
|
|
45
|
-
* WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
|
|
46
|
-
* grant and revoke this role. Extra precautions should be taken to secure
|
|
47
|
-
* accounts that have been granted it.
|
|
48
|
-
*
|
|
49
|
-
* @notice Roles can only be granted to ZswapCoinPublicKeys
|
|
50
|
-
* through the main role approval circuits (`grantRole` and `_grantRole`).
|
|
51
|
-
* In other words, role approvals to contract addresses are disallowed through these
|
|
52
|
-
* circuits.
|
|
53
|
-
* This is because Compact currently does not support contract-to-contract calls which means
|
|
54
|
-
* if a contract is granted a role, the contract cannot directly call the protected
|
|
55
|
-
* circuit.
|
|
56
|
-
*
|
|
57
|
-
* @notice This module does offer an experimental circuit that allows roles to be granted
|
|
58
|
-
* to contract addresses (`_unsafeGrantRole`).
|
|
59
|
-
* Note that the circuit name is very explicit ("unsafe") with this experimental circuit.
|
|
60
|
-
* Until contract-to-contract calls are supported,
|
|
61
|
-
* there is no direct way for a contract to call protected circuits.
|
|
62
|
-
*
|
|
63
|
-
* @notice The unsafe circuits are planned to become deprecated once contract-to-contract calls
|
|
64
|
-
* are supported.
|
|
65
|
-
*
|
|
66
|
-
* @notice Missing Features and Improvements:
|
|
67
|
-
*
|
|
68
|
-
* - Role events
|
|
69
|
-
* - An ERC165-like interface
|
|
70
|
-
*/
|
|
71
|
-
module AccessControl {
|
|
72
|
-
import CompactStandardLibrary;
|
|
73
|
-
import "../utils/Utils" prefix Utils_;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @description Mapping from a role identifier -> account -> its permissions.
|
|
77
|
-
* @type {Bytes<32>} roleId - A hash representing a role identifier.
|
|
78
|
-
* @type {Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>} hasRole - A mapping from an account to a
|
|
79
|
-
* Boolean determining if the account is approved for a role.
|
|
80
|
-
* @type {Map<roleId, hasRole>}
|
|
81
|
-
* @type {Map<Bytes<32>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>} _operatorRoles
|
|
82
|
-
*/
|
|
83
|
-
export ledger _operatorRoles: Map<Bytes<32>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>>;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* @description Mapping from a role identifier to an admin role identifier.
|
|
87
|
-
* @type {Bytes<32>} roleId - A hash representing a role identifier.
|
|
88
|
-
* @type {Bytes<32>} adminId - A hash representing an admin identifier.
|
|
89
|
-
* @type {Map<roleId, adminId>}
|
|
90
|
-
* @type {Map<Bytes<32>, Bytes<32>>} _adminRoles
|
|
91
|
-
*/
|
|
92
|
-
export ledger _adminRoles: Map<Bytes<32>, Bytes<32>>;
|
|
93
|
-
|
|
94
|
-
export ledger DEFAULT_ADMIN_ROLE: Bytes<32>;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @description Returns `true` if `account` has been granted `roleId`.
|
|
98
|
-
*
|
|
99
|
-
* @circuitInfo k=10, rows=487
|
|
100
|
-
*
|
|
101
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
102
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} account - The account to query.
|
|
103
|
-
* @return {Boolean} - Whether the account has the specified role.
|
|
104
|
-
*/
|
|
105
|
-
export circuit hasRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
|
|
106
|
-
if (
|
|
107
|
-
_operatorRoles.member(disclose(roleId)) &&
|
|
108
|
-
_operatorRoles
|
|
109
|
-
.lookup(roleId)
|
|
110
|
-
.member(disclose(account))
|
|
111
|
-
) {
|
|
112
|
-
return _operatorRoles
|
|
113
|
-
.lookup(roleId)
|
|
114
|
-
.lookup(disclose(account));
|
|
115
|
-
} else {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* @description Reverts if `ownPublicKey()` is missing `roleId`.
|
|
122
|
-
*
|
|
123
|
-
* @circuitInfo k=10, rows=345
|
|
124
|
-
*
|
|
125
|
-
* Requirements:
|
|
126
|
-
*
|
|
127
|
-
* - The caller must have `roleId`.
|
|
128
|
-
* - The caller must not be a ContractAddress
|
|
129
|
-
*
|
|
130
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
131
|
-
* @return {[]} - Empty tuple.
|
|
132
|
-
*/
|
|
133
|
-
export circuit assertOnlyRole(roleId: Bytes<32>): [] {
|
|
134
|
-
_checkRole(roleId, left<ZswapCoinPublicKey,ContractAddress>(ownPublicKey()));
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* @description Reverts if `account` is missing `roleId`.
|
|
139
|
-
*
|
|
140
|
-
* @circuitInfo k=10, rows=467
|
|
141
|
-
*
|
|
142
|
-
* Requirements:
|
|
143
|
-
*
|
|
144
|
-
* - `account` must have `roleId`.
|
|
145
|
-
*
|
|
146
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
147
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} account - The account to query.
|
|
148
|
-
* @return {[]} - Empty tuple.
|
|
149
|
-
*/
|
|
150
|
-
export circuit _checkRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
151
|
-
assert(hasRole(roleId, account), "AccessControl: unauthorized account");
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* @description Returns the admin role that controls `roleId` or
|
|
156
|
-
* a byte array with all zero bytes if `roleId` doesn't exist. See {grantRole} and {revokeRole}.
|
|
157
|
-
*
|
|
158
|
-
* To change a role’s admin use {_setRoleAdmin}.
|
|
159
|
-
*
|
|
160
|
-
* @circuitInfo k=10, rows=207
|
|
161
|
-
*
|
|
162
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
163
|
-
* @return {Bytes<32>} roleAdmin - The admin role that controls `roleId`.
|
|
164
|
-
*/
|
|
165
|
-
export circuit getRoleAdmin(roleId: Bytes<32>): Bytes<32> {
|
|
166
|
-
if (_adminRoles.member(disclose(roleId))) {
|
|
167
|
-
return _adminRoles.lookup(disclose(roleId));
|
|
168
|
-
}
|
|
169
|
-
return default<Bytes<32>>;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* @description Grants `roleId` to `account`.
|
|
174
|
-
*
|
|
175
|
-
* @circuitInfo k=10, rows=994
|
|
176
|
-
*
|
|
177
|
-
* Requirements:
|
|
178
|
-
*
|
|
179
|
-
* - `account` must not be a ContractAddress.
|
|
180
|
-
* - The caller must have `roleId`'s admin role.
|
|
181
|
-
*
|
|
182
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
183
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} account - A ZswapCoinPublicKey or ContractAddress.
|
|
184
|
-
* @return {[]} - Empty tuple.
|
|
185
|
-
*/
|
|
186
|
-
export circuit grantRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
187
|
-
assertOnlyRole(getRoleAdmin(roleId));
|
|
188
|
-
_grantRole(roleId, account);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* @description Revokes `roleId` from `account`.
|
|
193
|
-
*
|
|
194
|
-
* @circuitInfo k=10, rows=827
|
|
195
|
-
*
|
|
196
|
-
* Requirements:
|
|
197
|
-
*
|
|
198
|
-
* - The caller must have `roleId`'s admin role.
|
|
199
|
-
*
|
|
200
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
201
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} account - A ZswapCoinPublicKey or ContractAddress.
|
|
202
|
-
* @return {[]} - Empty tuple.
|
|
203
|
-
*/
|
|
204
|
-
export circuit revokeRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
205
|
-
assertOnlyRole(getRoleAdmin(roleId));
|
|
206
|
-
_revokeRole(roleId, account);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* @description Revokes `roleId` from the calling account.
|
|
211
|
-
*
|
|
212
|
-
* @notice Roles are often managed via {grantRole} and {revokeRole}: this circuit's
|
|
213
|
-
* purpose is to provide a mechanism for accounts to lose their privileges
|
|
214
|
-
* if they are compromised (such as when a trusted device is misplaced).
|
|
215
|
-
*
|
|
216
|
-
* @circuitInfo k=10, rows=640
|
|
217
|
-
*
|
|
218
|
-
* Requirements:
|
|
219
|
-
*
|
|
220
|
-
* - The caller must be `callerConfirmation`.
|
|
221
|
-
* - The caller must not be a `ContractAddress`.
|
|
222
|
-
*
|
|
223
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
224
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} callerConfirmation - A ZswapCoinPublicKey or ContractAddress.
|
|
225
|
-
* @return {[]} - Empty tuple.
|
|
226
|
-
*/
|
|
227
|
-
export circuit renounceRole(roleId: Bytes<32>, callerConfirmation: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
228
|
-
assert(callerConfirmation == left<ZswapCoinPublicKey,ContractAddress>(ownPublicKey()), "AccessControl: bad confirmation");
|
|
229
|
-
|
|
230
|
-
_revokeRole(roleId, callerConfirmation);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* @description Sets `adminRole` as `roleId`'s admin role.
|
|
235
|
-
*
|
|
236
|
-
* @circuitInfo k=10, rows=209
|
|
237
|
-
*
|
|
238
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
239
|
-
* @param {Bytes<32>} adminRole - The admin role identifier.
|
|
240
|
-
* @return {[]} - Empty tuple.
|
|
241
|
-
*/
|
|
242
|
-
export circuit _setRoleAdmin(roleId: Bytes<32>, adminRole: Bytes<32>): [] {
|
|
243
|
-
_adminRoles.insert(disclose(roleId), disclose(adminRole));
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* @description Attempts to grant `roleId` to `account` and returns a boolean indicating if `roleId` was granted.
|
|
248
|
-
* Internal circuit without access restriction.
|
|
249
|
-
*
|
|
250
|
-
* @circuitInfo k=10, rows=734
|
|
251
|
-
*
|
|
252
|
-
* Requirements:
|
|
253
|
-
*
|
|
254
|
-
* - `account` must not be a ContractAddress.
|
|
255
|
-
*
|
|
256
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
257
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} account - A ZswapCoinPublicKey or ContractAddress.
|
|
258
|
-
* @return {Boolean} roleGranted - A boolean indicating if `roleId` was granted.
|
|
259
|
-
*/
|
|
260
|
-
export circuit _grantRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
|
|
261
|
-
assert(!Utils_isContractAddress(account), "AccessControl: unsafe role approval");
|
|
262
|
-
return _unsafeGrantRole(roleId, account);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* @description Attempts to grant `roleId` to `account` and returns a boolean indicating if `roleId` was granted.
|
|
267
|
-
* Internal circuit without access restriction. It does NOT check if the role is granted to a ContractAddress.
|
|
268
|
-
*
|
|
269
|
-
* @circuitInfo k=10, rows=733
|
|
270
|
-
*
|
|
271
|
-
* @notice External smart contracts cannot call the token contract at this time, so granting a role to an ContractAddress may
|
|
272
|
-
* render a circuit permanently inaccessible.
|
|
273
|
-
*
|
|
274
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
275
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} account - A ZswapCoinPublicKey or ContractAddress.
|
|
276
|
-
* @return {Boolean} roleGranted - A boolean indicating if `role` was granted.
|
|
277
|
-
*/
|
|
278
|
-
export circuit _unsafeGrantRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
|
|
279
|
-
if (hasRole(roleId, account)) {
|
|
280
|
-
return false;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (!_operatorRoles.member(disclose(roleId))) {
|
|
284
|
-
_operatorRoles.insert(
|
|
285
|
-
disclose(roleId),
|
|
286
|
-
default<Map<
|
|
287
|
-
Either<ZswapCoinPublicKey, ContractAddress>,
|
|
288
|
-
Boolean
|
|
289
|
-
>>
|
|
290
|
-
);
|
|
291
|
-
_operatorRoles
|
|
292
|
-
.lookup(roleId)
|
|
293
|
-
.insert(disclose(account), true);
|
|
294
|
-
return true;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
_operatorRoles.lookup(roleId).insert(disclose(account), true);
|
|
298
|
-
return true;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* @description Attempts to revoke `roleId` from `account` and returns a boolean indicating if `roleId` was revoked.
|
|
303
|
-
* Internal circuit without access restriction.
|
|
304
|
-
*
|
|
305
|
-
* @circuitInfo k=10, rows=563
|
|
306
|
-
*
|
|
307
|
-
* @param {Bytes<32>} roleId - The role identifier.
|
|
308
|
-
* @param {Bytes<32>} adminRole - The admin role identifier.
|
|
309
|
-
* @return {Boolean} roleRevoked - A boolean indicating if `roleId` was revoked.
|
|
310
|
-
*/
|
|
311
|
-
export circuit _revokeRole(roleId: Bytes<32>, account: Either<ZswapCoinPublicKey, ContractAddress>): Boolean {
|
|
312
|
-
if (!hasRole(roleId, account)) {
|
|
313
|
-
return false;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
_operatorRoles
|
|
317
|
-
.lookup(roleId)
|
|
318
|
-
.insert(disclose(account), false);
|
|
319
|
-
return true;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
|
|
3
|
-
pragma language_version >= 0.16.0;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @module Ownable
|
|
7
|
-
* @description An unshielded Ownable library.
|
|
8
|
-
* This modules provides a basic access control mechanism, where there is an owner
|
|
9
|
-
* that can be granted exclusive access to specific circuits.
|
|
10
|
-
* This approach is perfectly reasonable for contracts that have a single administrative user.
|
|
11
|
-
*
|
|
12
|
-
* The initial owner must be set by using the `initialize` circuit during construction.
|
|
13
|
-
* This can later be changed with `transferOwnership`.
|
|
14
|
-
*
|
|
15
|
-
* @notice Ownership can only be transferred to ZswapCoinPublicKeys
|
|
16
|
-
* through the main transfer circuits (`transferOwnership` and `_transferOwnership`).
|
|
17
|
-
* In other words, ownership transfers to contract addresses are disallowed through these
|
|
18
|
-
* circuits.
|
|
19
|
-
* This is because Compact currently does not support contract-to-contract calls which means
|
|
20
|
-
* if a contract is granted ownership, the owner contract cannot directly call the protected
|
|
21
|
-
* circuit.
|
|
22
|
-
*
|
|
23
|
-
* @notice This module does offer experimental circuits that allow ownership to be granted
|
|
24
|
-
* to contract addresses (`_unsafeTransferOwnership` and `_unsafeUncheckedTransferOwnership`).
|
|
25
|
-
* Note that the circuit names are very explicit ("unsafe") with these experimental circuits.
|
|
26
|
-
* Until contract-to-contract calls are supported,
|
|
27
|
-
* there is no direct way for a contract to call circuits of other contracts
|
|
28
|
-
* or transfer ownership back to a user.
|
|
29
|
-
*
|
|
30
|
-
* @notice The unsafe circuits are planned to become deprecated once contract-to-contract calls
|
|
31
|
-
* are supported.
|
|
32
|
-
*/
|
|
33
|
-
module Ownable {
|
|
34
|
-
import CompactStandardLibrary;
|
|
35
|
-
import "../security/Initializable" prefix Initializable_;
|
|
36
|
-
import "../utils/Utils" prefix Utils_;
|
|
37
|
-
|
|
38
|
-
export ledger _owner: Either<ZswapCoinPublicKey, ContractAddress>;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @description Initializes the contract by setting the `initialOwner`.
|
|
42
|
-
* This must be called in the contract's constructor.
|
|
43
|
-
*
|
|
44
|
-
* @circuitInfo k=10, rows=258
|
|
45
|
-
*
|
|
46
|
-
* Requirements:
|
|
47
|
-
*
|
|
48
|
-
* - Contract is not already initialized.
|
|
49
|
-
* - `initialOwner` is not a ContractAddress.
|
|
50
|
-
* - `initialOwner` is not the zero address.
|
|
51
|
-
*
|
|
52
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} initialOwner - The initial owner of the contract.
|
|
53
|
-
* @returns {[]} Empty tuple.
|
|
54
|
-
*/
|
|
55
|
-
export circuit initialize(initialOwner: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
56
|
-
Initializable_initialize();
|
|
57
|
-
assert(!Utils_isKeyOrAddressZero(initialOwner), "Ownable: invalid initial owner");
|
|
58
|
-
_transferOwnership(initialOwner);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @description Returns the current contract owner.
|
|
63
|
-
*
|
|
64
|
-
* @circuitInfo k=10, rows=84
|
|
65
|
-
*
|
|
66
|
-
* Requirements:
|
|
67
|
-
*
|
|
68
|
-
* - Contract is initialized.
|
|
69
|
-
*
|
|
70
|
-
* @returns {Either<ZswapCoinPublicKey, ContractAddress> } - The contract owner.
|
|
71
|
-
*/
|
|
72
|
-
export circuit owner(): Either<ZswapCoinPublicKey, ContractAddress> {
|
|
73
|
-
Initializable_assertInitialized();
|
|
74
|
-
return _owner;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @description Transfers ownership of the contract to `newOwner`.
|
|
79
|
-
*
|
|
80
|
-
* @circuitInfo k=10, rows=338
|
|
81
|
-
*
|
|
82
|
-
* @notice Ownership transfers to contract addresses are currently disallowed until contract-to-contract
|
|
83
|
-
* interactions are supported in Compact.
|
|
84
|
-
* This restriction prevents permanently disabling access to a circuit.
|
|
85
|
-
*
|
|
86
|
-
* Requirements:
|
|
87
|
-
*
|
|
88
|
-
* - Contract is initialized.
|
|
89
|
-
* - The caller is the current contract owner.
|
|
90
|
-
* - `newOwner` is not a ContractAddress.
|
|
91
|
-
* - `newOwner` is not the zero address.
|
|
92
|
-
*
|
|
93
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} newOwner - The new owner.
|
|
94
|
-
* @returns {[]} Empty tuple.
|
|
95
|
-
*/
|
|
96
|
-
export circuit transferOwnership(newOwner: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
97
|
-
Initializable_assertInitialized();
|
|
98
|
-
assert(!Utils_isContractAddress(newOwner), "Ownable: unsafe ownership transfer");
|
|
99
|
-
_unsafeTransferOwnership(newOwner);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @description Unsafe variant of `transferOwnership`.
|
|
104
|
-
*
|
|
105
|
-
* @circuitInfo k=10, rows=335
|
|
106
|
-
*
|
|
107
|
-
* @warning Ownership transfers to contract addresses are considered unsafe because contract-to-contract
|
|
108
|
-
* calls are not currently supported.
|
|
109
|
-
* Ownership privileges sent to a contract address may become uncallable.
|
|
110
|
-
* Once contract-to-contract calls are supported, this circuit may be deprecated.
|
|
111
|
-
*
|
|
112
|
-
* Requirements:
|
|
113
|
-
*
|
|
114
|
-
* - Contract is initialized.
|
|
115
|
-
* - The caller is the current contract owner.
|
|
116
|
-
* - `newOwner` is not the zero address.
|
|
117
|
-
*
|
|
118
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} newOwner - The new owner.
|
|
119
|
-
* @returns {[]} Empty tuple.
|
|
120
|
-
*/
|
|
121
|
-
export circuit _unsafeTransferOwnership(newOwner: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
122
|
-
Initializable_assertInitialized();
|
|
123
|
-
assertOnlyOwner();
|
|
124
|
-
assert(!Utils_isKeyOrAddressZero(newOwner), "Ownable: invalid new owner");
|
|
125
|
-
_unsafeUncheckedTransferOwnership(newOwner);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @description Leaves the contract without an owner.
|
|
130
|
-
* It will not be possible to call `assertOnlyOnwer` circuits anymore.
|
|
131
|
-
* Can only be called by the current owner.
|
|
132
|
-
*
|
|
133
|
-
* @circuitInfo k=10, rows=124
|
|
134
|
-
*
|
|
135
|
-
* Requirements:
|
|
136
|
-
*
|
|
137
|
-
* - Contract is initialized.
|
|
138
|
-
* - The caller is the current contract owner.
|
|
139
|
-
*
|
|
140
|
-
* @returns {[]} Empty tuple.
|
|
141
|
-
*/
|
|
142
|
-
export circuit renounceOwnership(): [] {
|
|
143
|
-
Initializable_assertInitialized();
|
|
144
|
-
assertOnlyOwner();
|
|
145
|
-
_transferOwnership(burnAddress());
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* @description Throws if called by any account other than the owner.
|
|
150
|
-
* Use this to restrict access of specific circuits to the owner.
|
|
151
|
-
*
|
|
152
|
-
* @circuitInfo k=10, rows=115
|
|
153
|
-
*
|
|
154
|
-
* Requirements:
|
|
155
|
-
*
|
|
156
|
-
* - Contract is initialized.
|
|
157
|
-
* - The caller is the current contract owner.
|
|
158
|
-
*
|
|
159
|
-
* @returns {[]} Empty tuple.
|
|
160
|
-
*/
|
|
161
|
-
export circuit assertOnlyOwner(): [] {
|
|
162
|
-
Initializable_assertInitialized();
|
|
163
|
-
const caller = ownPublicKey();
|
|
164
|
-
assert(caller == _owner.left, "Ownable: caller is not the owner");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* @description Transfers ownership of the contract to `newOwner` without
|
|
169
|
-
* enforcing permission checks on the caller.
|
|
170
|
-
*
|
|
171
|
-
* @circuitInfo k=10, rows=219
|
|
172
|
-
*
|
|
173
|
-
* @notice Ownership transfers to contract addresses are currently disallowed until contract-to-contract
|
|
174
|
-
* interactions are supported in Compact.
|
|
175
|
-
* This restriction prevents circuits from being inadvertently locked in contracts.
|
|
176
|
-
*
|
|
177
|
-
* Requirements:
|
|
178
|
-
*
|
|
179
|
-
* - Contract is initialized.
|
|
180
|
-
* - `newOwner` is not a ContractAddress.
|
|
181
|
-
*
|
|
182
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} newOwner - The new owner.
|
|
183
|
-
* @returns {[]} Empty tuple.
|
|
184
|
-
*/
|
|
185
|
-
export circuit _transferOwnership(newOwner: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
186
|
-
Initializable_assertInitialized();
|
|
187
|
-
assert(!Utils_isContractAddress(newOwner), "Ownable: unsafe ownership transfer");
|
|
188
|
-
_unsafeUncheckedTransferOwnership(newOwner);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* @description Unsafe variant of `_transferOwnership`.
|
|
193
|
-
*
|
|
194
|
-
* @circuitInfo k=10, rows=216
|
|
195
|
-
*
|
|
196
|
-
* @warning Ownership transfers to contract addresses are considered unsafe because contract-to-contract
|
|
197
|
-
* calls are not currently supported.
|
|
198
|
-
* Ownership privileges sent to a contract address may become uncallable.
|
|
199
|
-
* Once contract-to-contract calls are supported, this circuit may be deprecated.
|
|
200
|
-
*
|
|
201
|
-
* Requirements:
|
|
202
|
-
*
|
|
203
|
-
* - Contract is initialized.
|
|
204
|
-
*
|
|
205
|
-
* @param {Either<ZswapCoinPublicKey, ContractAddress>} newOwner - The new owner.
|
|
206
|
-
* @returns {[]} Empty tuple.
|
|
207
|
-
*/
|
|
208
|
-
export circuit _unsafeUncheckedTransferOwnership(newOwner: Either<ZswapCoinPublicKey, ContractAddress>): [] {
|
|
209
|
-
Initializable_assertInitialized();
|
|
210
|
-
_owner = disclose(newOwner);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const AccessControlWitnesses = {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const OwnableWitnesses = {};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
|
|
3
|
-
pragma language_version >= 0.16.0;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @module Initializable
|
|
7
|
-
* @description Initializable provides a simple mechanism that mimics the functionality of a constructor.
|
|
8
|
-
*/
|
|
9
|
-
module Initializable {
|
|
10
|
-
import CompactStandardLibrary;
|
|
11
|
-
|
|
12
|
-
export ledger _isInitialized: Boolean;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @description Initializes the state thus ensuring the calling circuit can only be called once.
|
|
16
|
-
*
|
|
17
|
-
* @circuitInfo k=10, rows=38
|
|
18
|
-
*
|
|
19
|
-
* Requirements:
|
|
20
|
-
*
|
|
21
|
-
* - Contract must not be initialized.
|
|
22
|
-
*
|
|
23
|
-
* @return {[]} - Empty tuple.
|
|
24
|
-
*/
|
|
25
|
-
export circuit initialize(): [] {
|
|
26
|
-
assertNotInitialized();
|
|
27
|
-
_isInitialized = true;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @description Asserts that the contract has been initialized, throwing an error if not.
|
|
32
|
-
*
|
|
33
|
-
* @circuitInfo k=10, rows=31
|
|
34
|
-
*
|
|
35
|
-
* Requirements:
|
|
36
|
-
*
|
|
37
|
-
* - Contract must be initialized.
|
|
38
|
-
*
|
|
39
|
-
* @return {[]} - Empty tuple.
|
|
40
|
-
*/
|
|
41
|
-
export circuit assertInitialized(): [] {
|
|
42
|
-
assert(_isInitialized, "Initializable: contract not initialized");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @description Asserts that the contract has not been initialized, throwing an error if it has.
|
|
47
|
-
*
|
|
48
|
-
* @circuitInfo k=10, rows=35
|
|
49
|
-
*
|
|
50
|
-
* Requirements:
|
|
51
|
-
*
|
|
52
|
-
* - Contract must not be initialized.
|
|
53
|
-
*
|
|
54
|
-
* @return {[]} - Empty tuple.
|
|
55
|
-
*/
|
|
56
|
-
export circuit assertNotInitialized(): [] {
|
|
57
|
-
assert(!_isInitialized, "Initializable: contract already initialized");
|
|
58
|
-
}
|
|
59
|
-
}
|