@openzeppelin/compact-contracts 0.1.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.
package/README.md ADDED
@@ -0,0 +1,278 @@
1
+ [![Generic badge](https://img.shields.io/badge/Compact%20Compiler-0.29.0-1abc9c.svg)](https://docs.midnight.network/relnotes/compact/minokawa-0-18-26-0)
2
+ [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
3
+
4
+ This project is built on the Midnight Network.
5
+
6
+ # OpenZeppelin Contracts for Compact
7
+
8
+ **A library for secure smart contract development** written in Compact for [Midnight](https://midnight.network/).
9
+
10
+
11
+ > ## ⚠️ WARNING! ⚠️
12
+ >
13
+ > This repo contains highly experimental code.
14
+ > Expect rapid iteration.
15
+ > **Use at your own risk.**
16
+
17
+ > ## ⚠️ Witnesses Are Test-Only Material ⚠️
18
+ >
19
+ > Every TypeScript witness in this repo lives under `contracts/src/<module>/test/witnesses/` and exists **solely to drive the Compact circuits during off-chain tests**. They are not part of the published package and are not maintained as a public API.
20
+ >
21
+ > **Witness implementations are security-critical.** A witness controls the private state a circuit reads from — a buggy or malicious witness can leak secrets, produce invalid proofs, or undermine the guarantees of the contract it pairs with. Consumers of this library **must author and audit their own witnesses** for production use; the ones shipped here are reference test doubles only.
22
+ >
23
+ > OpenZeppelin does not publish witnesses as a consumable artifact and takes no responsibility for any witness implementation reused outside its test context.
24
+
25
+ ## Learn
26
+
27
+ ### Documentation
28
+
29
+ Check out the [full documentation site](https://docs.openzeppelin.com/contracts-compact)!
30
+
31
+ ## Usage
32
+
33
+ Make sure you have [nvm](https://github.com/nvm-sh/nvm) and [yarn](https://yarnpkg.com/getting-started/install) installed on your machine.
34
+
35
+ Follow Midnight's [Compact Developer Tools installation guide](https://docs.midnight.network/develop/tutorial/building/#midnight-compact-compiler) and confirm that `compact` is in the `PATH` env variable.
36
+
37
+ ```bash
38
+ $ compact compile --version
39
+
40
+ Compactc version: 0.29.0
41
+ 0.29.0
42
+ ```
43
+
44
+ ### Installation
45
+
46
+ Create a directory for your project.
47
+
48
+ ```bash
49
+ mkdir my-project
50
+ cd my-project
51
+ ```
52
+
53
+ Install the package.
54
+
55
+ ```bash
56
+ yarn add @openzeppelin/compact-contracts
57
+ ```
58
+
59
+ ### Write a custom contract using library modules
60
+
61
+ In the root of `my-project`, create a custom contract using OpenZeppelin Compact modules.
62
+ Import the modules through `./node_modules/@openzeppelin/compact-contracts/...`.
63
+
64
+ ```typescript
65
+ // MyContract.compact
66
+
67
+ pragma language_version >= 0.21.0;
68
+
69
+ import CompactStandardLibrary;
70
+ import "./node_modules/@openzeppelin/compact-contracts/access/Ownable"
71
+ prefix Ownable_;
72
+ import "./node_modules/@openzeppelin/compact-contracts/security/Pausable"
73
+ prefix Pausable_;
74
+ import "./node_modules/@openzeppelin/compact-contracts/token/FungibleToken"
75
+ prefix FungibleToken_;
76
+
77
+ constructor(
78
+ _name: Opaque<"string">,
79
+ _symbol: Opaque<"string">,
80
+ _decimals: Uint<8>,
81
+ _recipient: Either<Bytes<32>, ContractAddress>,
82
+ _amount: Uint<128>,
83
+ _initOwner: Either<Bytes<32>, ContractAddress>,
84
+ ) {
85
+ Ownable_initialize(_initOwner);
86
+ FungibleToken_initialize(_name, _symbol, _decimals);
87
+ FungibleToken__mint(_recipient, _amount);
88
+ }
89
+
90
+ /** IFungibleToken */
91
+
92
+ export circuit name(): Opaque<"string"> {
93
+ return FungibleToken_name();
94
+ }
95
+
96
+ export circuit symbol(): Opaque<"string"> {
97
+ return FungibleToken_symbol();
98
+ }
99
+
100
+ export circuit decimals(): Uint<8> {
101
+ return FungibleToken_decimals();
102
+ }
103
+
104
+ export circuit totalSupply(): Uint<128> {
105
+ return FungibleToken_totalSupply();
106
+ }
107
+
108
+ export circuit balanceOf(account: Either<Bytes<32>, ContractAddress>): Uint<128> {
109
+ return FungibleToken_balanceOf(account);
110
+ }
111
+
112
+ export circuit allowance(
113
+ owner: Either<Bytes<32>, ContractAddress>,
114
+ spender: Either<Bytes<32>, ContractAddress>
115
+ ): Uint<128> {
116
+ return FungibleToken_allowance(owner, spender);
117
+ }
118
+
119
+ export circuit transfer(
120
+ to: Either<Bytes<32>, ContractAddress>,
121
+ value: Uint<128>,
122
+ ): Boolean {
123
+ Pausable_assertNotPaused();
124
+ return FungibleToken_transfer(to, value);
125
+ }
126
+
127
+ export circuit transferFrom(
128
+ fromAddress: Either<Bytes<32>, ContractAddress>,
129
+ to: Either<Bytes<32>, ContractAddress>,
130
+ value: Uint<128>
131
+ ): Boolean {
132
+ Pausable_assertNotPaused();
133
+ return FungibleToken_transferFrom(fromAddress, to, value);
134
+ }
135
+
136
+ export circuit approve(spender: Either<Bytes<32>, ContractAddress>, value: Uint<128>): Boolean {
137
+ Pausable_assertNotPaused();
138
+ return FungibleToken_approve(spender, value);
139
+ }
140
+
141
+ /** IOwnable */
142
+
143
+ export circuit owner(): Either<Bytes<32>, ContractAddress> {
144
+ return Ownable_owner();
145
+ }
146
+
147
+ export circuit transferOwnership(newOwner: Either<Bytes<32>, ContractAddress>): [] {
148
+ return Ownable_transferOwnership(newOwner);
149
+ }
150
+
151
+ export circuit renounceOwnership(): [] {
152
+ return Ownable_renounceOwnership();
153
+ }
154
+
155
+ /** IPausable */
156
+
157
+ export circuit pause(): [] {
158
+ Ownable_assertOnlyOwner();
159
+ Pausable__pause();
160
+ }
161
+
162
+ export circuit unpause(): [] {
163
+ Ownable_assertOnlyOwner();
164
+ Pausable__unpause();
165
+ }
166
+ ```
167
+
168
+ ### Compile the contract
169
+
170
+ Compile the contract.
171
+
172
+ ```bash
173
+ % compact compile MyContract.compact artifacts/MyContract
174
+ Compiling 14 circuits:
175
+ circuit "allowance" (k=11, rows=1352)
176
+ circuit "approve" (k=13, rows=3080)
177
+ circuit "balanceOf" (k=10, rows=673)
178
+ circuit "decimals" (k=6, rows=28)
179
+ circuit "name" (k=6, rows=28)
180
+ circuit "owner" (k=7, rows=76)
181
+ circuit "pause" (k=13, rows=2365)
182
+ circuit "renounceOwnership" (k=13, rows=2364)
183
+ circuit "symbol" (k=6, rows=28)
184
+ circuit "totalSupply" (k=6, rows=28)
185
+ circuit "transfer" (k=13, rows=3990)
186
+ circuit "transferFrom" (k=13, rows=4977)
187
+ circuit "transferOwnership" (k=13, rows=2959)
188
+ circuit "unpause" (k=13, rows=2362)
189
+ Overall progress [====================] 14/14
190
+ ```
191
+
192
+ ## Development
193
+
194
+ OpenZeppelin Contracts for Compact exists thanks to its contributors.
195
+ There are many ways you can participate and help build high quality software,
196
+ make sure to check out the [contribution guide](CONTRIBUTING.md) in advance.
197
+
198
+ > ### Requirements
199
+ >
200
+ > - [Node.js](https://nodejs.org/)
201
+ > - [Yarn](https://yarnpkg.com/getting-started/install)
202
+ > - [Turbo](https://turborepo.com/docs/getting-started/installation)
203
+ > - [Compact](https://docs.midnight.network/blog/compact-developer-tools)
204
+
205
+ ### Set up the project
206
+
207
+ Clone the OpenZeppelin Contracts for Compact library.
208
+
209
+ ```bash
210
+ git clone git@github.com:OpenZeppelin/compact-contracts.git
211
+ ```
212
+
213
+ `cd` into it and then install dependencies and prepare the environment.
214
+
215
+ ```bash
216
+ nvm install && \
217
+ yarn && \
218
+ turbo compact
219
+ ```
220
+
221
+ ### Run tests
222
+
223
+ ```bash
224
+ turbo test
225
+ ```
226
+
227
+ ### Check/apply Biome formatter
228
+
229
+ ```bash
230
+ turbo fmt-and-lint
231
+ turbo fmt-and-lint:fix
232
+ ```
233
+
234
+ ### Advanced
235
+
236
+ #### Targeted compilation
237
+
238
+ ```bash
239
+ turbo compact:access
240
+ turbo compact:archive
241
+ ...
242
+ ```
243
+
244
+ #### Skip ZK prover/verifier keys
245
+
246
+ ZK key generation is slow and usually unnecessary during development.
247
+
248
+ ```bash
249
+ # Individual module compilation (recommended for development)
250
+ turbo compact:token --filter=@openzeppelin/compact-contracts -- --skip-zk
251
+
252
+ # Full compilation with skip-zk (use environment variable)
253
+ SKIP_ZK=true turbo compact
254
+ ```
255
+
256
+ #### Clean environment
257
+
258
+ ```bash
259
+ # WARNING!
260
+ # These are destructive commands
261
+ turbo clean
262
+ rm -rf .turbo/
263
+ ```
264
+
265
+ ### Troubleshooting
266
+
267
+ - **Issues with turbo's cache?** Try cleaning: `turbo clean && rm -rf .turbo/`
268
+ - **Node version issues?** Use `nvm use` to switch to the correct version
269
+
270
+ ## Security
271
+
272
+ This project is still in a very early and experimental phase. It has never been audited nor thoroughly reviewed for security vulnerabilities. DO NOT USE IT IN PRODUCTION.
273
+
274
+ Please report any security issues you find to <security@openzeppelin.com>.
275
+
276
+ ### Provenance
277
+
278
+ Releases are published from GitHub Actions with [npm provenance](https://docs.npmjs.com/generating-provenance-statements) enabled. Each published version carries a signed attestation in the public Sigstore transparency log linking the package to its source commit and build workflow run. View the verified source commit, build, and transparency-log links in the **Provenance** panel on the [npm package page](https://www.npmjs.com/package/@openzeppelin/compact-contracts).