@proto-kit/module 0.1.1-develop.153
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/LICENSE.md +201 -0
- package/README.md +114 -0
- package/dist/chain/Chain.d.ts +109 -0
- package/dist/chain/Chain.d.ts.map +1 -0
- package/dist/chain/Chain.js +229 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/method/MethodExecutionContext.d.ts +73 -0
- package/dist/method/MethodExecutionContext.d.ts.map +1 -0
- package/dist/method/MethodExecutionContext.js +112 -0
- package/dist/method/MethodParameterDecoder.d.ts +20 -0
- package/dist/method/MethodParameterDecoder.d.ts.map +1 -0
- package/dist/method/MethodParameterDecoder.js +30 -0
- package/dist/method/RuntimeMethodExecutionContext.d.ts +57 -0
- package/dist/method/RuntimeMethodExecutionContext.d.ts.map +1 -0
- package/dist/method/RuntimeMethodExecutionContext.js +92 -0
- package/dist/method/assert.d.ts +12 -0
- package/dist/method/assert.d.ts.map +1 -0
- package/dist/method/assert.js +20 -0
- package/dist/method/decorator.d.ts +45 -0
- package/dist/method/decorator.d.ts.map +1 -0
- package/dist/method/decorator.js +140 -0
- package/dist/method/runtimeMethod.d.ts +18 -0
- package/dist/method/runtimeMethod.d.ts.map +1 -0
- package/dist/method/runtimeMethod.js +114 -0
- package/dist/module/decorator.d.ts +8 -0
- package/dist/module/decorator.d.ts.map +1 -0
- package/dist/module/decorator.js +15 -0
- package/dist/runtime/Runtime.d.ts +71 -0
- package/dist/runtime/Runtime.d.ts.map +1 -0
- package/dist/runtime/Runtime.js +185 -0
- package/dist/runtime/RuntimeModule.d.ts +30 -0
- package/dist/runtime/RuntimeModule.d.ts.map +1 -0
- package/dist/runtime/RuntimeModule.js +44 -0
- package/dist/state/InMemoryStateService.d.ts +14 -0
- package/dist/state/InMemoryStateService.d.ts.map +1 -0
- package/dist/state/InMemoryStateService.js +21 -0
- package/dist/state/State.d.ts +65 -0
- package/dist/state/State.d.ts.map +1 -0
- package/dist/state/State.js +114 -0
- package/dist/state/StateMap.d.ts +37 -0
- package/dist/state/StateMap.d.ts.map +1 -0
- package/dist/state/StateMap.js +56 -0
- package/dist/state/StateServiceProvider.d.ts +10 -0
- package/dist/state/StateServiceProvider.d.ts.map +1 -0
- package/dist/state/StateServiceProvider.js +34 -0
- package/dist/state/decorator.d.ts +7 -0
- package/dist/state/decorator.d.ts.map +1 -0
- package/dist/state/decorator.js +42 -0
- package/jest.config.cjs +1 -0
- package/package.json +36 -0
- package/src/index.ts +12 -0
- package/src/method/MethodParameterDecoder.ts +55 -0
- package/src/method/RuntimeMethodExecutionContext.ts +111 -0
- package/src/method/assert.test.ts +49 -0
- package/src/method/assert.ts +23 -0
- package/src/method/decorator.test.ts +46 -0
- package/src/method/runtimeMethod.ts +192 -0
- package/src/module/decorator.ts +21 -0
- package/src/runtime/Runtime.ts +304 -0
- package/src/runtime/RuntimeModule.ts +68 -0
- package/src/state/InMemoryStateService.ts +29 -0
- package/src/state/State.ts +154 -0
- package/src/state/StateMap.ts +69 -0
- package/src/state/StateServiceProvider.ts +24 -0
- package/src/state/decorator.ts +65 -0
- package/test/Runtime.test.ts +37 -0
- package/test/modules/Admin.ts +19 -0
- package/test/modules/Balances.test.ts +340 -0
- package/test/modules/Balances.ts +51 -0
- package/test/runtimeMethod.test.ts +43 -0
- package/test/transaction.test.ts +82 -0
- package/tsconfig.json +8 -0
- package/tsconfig.test.json +9 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2023] [Stove Labs]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# YAB: Module
|
|
2
|
+
|
|
3
|
+
Set of APIs to define YAB runtime modules and application chains.
|
|
4
|
+
|
|
5
|
+
**Application chain** consists of multiple _runtime modules_. Runtime modules can be either implemented as part of the chain directly, or imported from 3rd parties. Here's an example of how a chain with two runtime modules can be defined:
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
const chain = Chain.from({
|
|
9
|
+
state: new InMemoryStateService(),
|
|
10
|
+
// specify which modules should the chain consist of
|
|
11
|
+
modules: {
|
|
12
|
+
Balances,
|
|
13
|
+
Admin,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// compile the chain into a verification key
|
|
18
|
+
const { verificationKey } = await chain.compile();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Chain works as a wrapper for all circuit logic contained by the runtime module methods. Compilation produces a _wrap circuit_ including all known methods of the configured runtime modules.
|
|
22
|
+
|
|
23
|
+
Here's an example `Balances` runtime module:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
@runtimeModule()
|
|
27
|
+
export class Balances extends RuntimeModule {
|
|
28
|
+
@state() public totalSupply = State.from<UInt64>(UInt64);
|
|
29
|
+
|
|
30
|
+
@state() public balances = StateMap.from<PublicKey, UInt64>(
|
|
31
|
+
PublicKey,
|
|
32
|
+
UInt64
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
public constructor(public admin: Admin) {
|
|
36
|
+
super();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@runtimeMethod()
|
|
40
|
+
public getTotalSupply() {
|
|
41
|
+
this.totalSupply.get();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@runtimeMethod()
|
|
45
|
+
public setTotalSupply() {
|
|
46
|
+
this.totalSupply.set(UInt64.from(20));
|
|
47
|
+
this.admin.isAdmin();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@runtimeMethod()
|
|
51
|
+
public getBalance(address: PublicKey): Option<UInt64> {
|
|
52
|
+
return this.balances.get(address);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The Balances runtime module shows how the YAB runtime module API can be used:
|
|
58
|
+
|
|
59
|
+
- `runtimeModule()` - marks the class as a runtime module
|
|
60
|
+
- `class Balance extends RuntimeModule` - introduces runtime module APIs into the runtime module
|
|
61
|
+
- `@state() public totalSupply = State.from<UInt64>(UInt64)` - defines a runtime module state of type `UInt64`, which can be either get or set
|
|
62
|
+
- `@state() public balances = StateMap.from<PublicKey, UInt64>(PublicKey, UInt64)` - defines a runtime module map state, which allows values to be stored under keys, respective of the given key & value types.
|
|
63
|
+
- `public constructor(public admin: Admin)` - injects a runtime module dependency to another runtime module `Admin`, which is a standalone runtime module
|
|
64
|
+
- `@runtimeMethod() public getTotalSupply()` - defines a runtime module method containing circuit logic for a runtime state transition. Methods define how the chain state is transformed from the initial state to the resulting state.
|
|
65
|
+
- `this.admin.isAdmin()` - shows how runtime module interoperability works, a configured runtime module is injected in the constructor, and can be used within the existing methods. Code of the called runtime module becomes part of the original method circuit (setTotalSupply in this case).
|
|
66
|
+
|
|
67
|
+
## Method wrapper circuit
|
|
68
|
+
|
|
69
|
+
Every runtime module method gets wrapped into an extra outter circuit, which allows the underlying runtime to make assertions about the results of the method execution, such as:
|
|
70
|
+
|
|
71
|
+
- State transitions
|
|
72
|
+
- Execution status
|
|
73
|
+
- Transaction hash (method invocation arguments)
|
|
74
|
+
- Network state (TODO)
|
|
75
|
+
|
|
76
|
+
The good thing is, the YAB runtime module API does this for you automatically, but it pays off to keep this in mind when debugging your runtime modules. Due to the nature of the underlying ZK method lifecycle, methods cannot produce any side effects beyond the state transitions, which are implicitly generated using the `State` API. Again due to method lifecycle implications, your method code will run multiple times at different stages of its lifecycle, such as:
|
|
77
|
+
|
|
78
|
+
- Compilation
|
|
79
|
+
- Simulation
|
|
80
|
+
- Proving
|
|
81
|
+
|
|
82
|
+
> Please be careful about keeping your runtime module methods side-effect free, since it may introduce inconsistencies in the method lifecycle which may lead to the inability to generate an execution proof.
|
|
83
|
+
|
|
84
|
+
## Testing
|
|
85
|
+
|
|
86
|
+
Runtime module methods can be ran or tested in two different modes:
|
|
87
|
+
|
|
88
|
+
- Simulation
|
|
89
|
+
- Proving
|
|
90
|
+
|
|
91
|
+
Simulation mode means that only the method javascript code will run, and no proving will be done. This is the fastest way of testing your module methods.
|
|
92
|
+
|
|
93
|
+
Provnig mode means that the whole chain needs to be compiled, and then every method execution can be additionally proven by accessing the provers generated during method simulation.
|
|
94
|
+
|
|
95
|
+
Here's an example of how a runtime module method execution proof can be generated, but please keep in mind you also need to enable proofs and compile the chain. More detailed examples can be found in `test/modules`:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const chain = Chain.from({
|
|
99
|
+
state,
|
|
100
|
+
|
|
101
|
+
modules: {
|
|
102
|
+
Balances,
|
|
103
|
+
Admin,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const balances = chain.resolve("Balances");
|
|
108
|
+
balances.getTotalSupply();
|
|
109
|
+
|
|
110
|
+
const {
|
|
111
|
+
result: { prove },
|
|
112
|
+
} = container.resolve(MethodExecutionContext);
|
|
113
|
+
const proof = await prove();
|
|
114
|
+
```
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Experimental, Proof } from "snarkyjs";
|
|
2
|
+
import { MethodPublicInput, Subclass } from "@yab/protocol";
|
|
3
|
+
import { type AnyConstructor } from "../module/decorator.js";
|
|
4
|
+
import type { StateService } from "../state/InMemoryStateService.js";
|
|
5
|
+
export interface RuntimeModules {
|
|
6
|
+
[name: string]: AnyConstructor;
|
|
7
|
+
}
|
|
8
|
+
export interface ChainConfig<ChainRuntimeModules extends RuntimeModules> {
|
|
9
|
+
state: StateService;
|
|
10
|
+
runtimeModules: ChainRuntimeModules;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Wrapper for an application specific chain, which helps orchestrate
|
|
14
|
+
* runtime modules into an interoperable runtime.
|
|
15
|
+
*/
|
|
16
|
+
export declare class Chain<ChainRuntimeModules extends RuntimeModules> {
|
|
17
|
+
config: ChainConfig<ChainRuntimeModules>;
|
|
18
|
+
/**
|
|
19
|
+
* Alternative constructor for `Chain`.
|
|
20
|
+
*
|
|
21
|
+
* @param config - Configuration for the returned Chain
|
|
22
|
+
* @returns Chain with the provided config
|
|
23
|
+
*/
|
|
24
|
+
static from<ChainRuntimeModules extends RuntimeModules>(config: ChainConfig<ChainRuntimeModules>): Chain<ChainRuntimeModules>;
|
|
25
|
+
private readonly runtimeContainer;
|
|
26
|
+
areProofsEnabled: boolean;
|
|
27
|
+
program?: ReturnType<typeof Experimental.ZkProgram>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new Chain from the provided config
|
|
30
|
+
*
|
|
31
|
+
* @param config - Configuration object for the constructed Chain
|
|
32
|
+
*/
|
|
33
|
+
constructor(config: ChainConfig<ChainRuntimeModules>);
|
|
34
|
+
/**
|
|
35
|
+
* Add a name and other respective properties required by RuntimeModules,
|
|
36
|
+
* that come from the current Chain
|
|
37
|
+
*
|
|
38
|
+
* @param name - Name of the runtime module to decorate
|
|
39
|
+
*/
|
|
40
|
+
private decorateRuntimeModule;
|
|
41
|
+
/**
|
|
42
|
+
* @returns A list of
|
|
43
|
+
*/
|
|
44
|
+
get runtimeModuleNames(): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Returns a runtime module registred under the given key.
|
|
47
|
+
*
|
|
48
|
+
* @param name - Name of the runtime module to get
|
|
49
|
+
* @returns A runtime module stored under the given key
|
|
50
|
+
*/
|
|
51
|
+
getRuntimeModule<Key extends keyof ChainRuntimeModules>(name: Key): InstanceType<ChainRuntimeModules[Key]>;
|
|
52
|
+
/**
|
|
53
|
+
* Registers a runtime module under the given name.
|
|
54
|
+
*
|
|
55
|
+
* @param name - Name of the runtime module to identify it by
|
|
56
|
+
* @param runtimeModule - Runtime module to register
|
|
57
|
+
*/
|
|
58
|
+
registerRuntimeModule(name: string, runtimeModule: AnyConstructor): void;
|
|
59
|
+
/**
|
|
60
|
+
* Sets if proofs are enabled or not
|
|
61
|
+
* @param areProofsEnabled
|
|
62
|
+
*/
|
|
63
|
+
setProofsEnabled(areProofsEnabled: boolean): void;
|
|
64
|
+
/**
|
|
65
|
+
* Precompiles the current runtime modules into a ZkProgram.
|
|
66
|
+
*
|
|
67
|
+
* @returns - Analysis of the precompiled ZkProgram
|
|
68
|
+
*/
|
|
69
|
+
precompile(): {
|
|
70
|
+
analyze: (this: Chain<RuntimeModules>) => {
|
|
71
|
+
methodName: string;
|
|
72
|
+
analysis: {
|
|
73
|
+
rows: number;
|
|
74
|
+
gates: import("snarkyjs/dist/node/snarky.js").Gate[];
|
|
75
|
+
inputs: [] | [import("snarkyjs/dist/node/snarky.js").Provable<any> | ((new (...args: any) => Proof<unknown>) & {
|
|
76
|
+
prototype: Proof<any>;
|
|
77
|
+
publicInputType: import("snarkyjs/dist/node/lib/circuit_value.js").FlexibleProvablePure<any>;
|
|
78
|
+
tag: () => {
|
|
79
|
+
name: string;
|
|
80
|
+
};
|
|
81
|
+
fromJSON: typeof Proof.fromJSON;
|
|
82
|
+
} & {
|
|
83
|
+
prototype: Proof<unknown>;
|
|
84
|
+
}), ...(import("snarkyjs/dist/node/snarky.js").Provable<any> | ((new (...args: any) => Proof<unknown>) & {
|
|
85
|
+
prototype: Proof<any>;
|
|
86
|
+
publicInputType: import("snarkyjs/dist/node/lib/circuit_value.js").FlexibleProvablePure<any>;
|
|
87
|
+
tag: () => {
|
|
88
|
+
name: string;
|
|
89
|
+
};
|
|
90
|
+
fromJSON: typeof Proof.fromJSON;
|
|
91
|
+
} & {
|
|
92
|
+
prototype: Proof<unknown>;
|
|
93
|
+
}))[]];
|
|
94
|
+
};
|
|
95
|
+
}[];
|
|
96
|
+
toPretty: () => void;
|
|
97
|
+
};
|
|
98
|
+
getProofClass(): Subclass<typeof Proof<MethodPublicInput>>;
|
|
99
|
+
/**
|
|
100
|
+
* Compiles the current runtime modules configuration
|
|
101
|
+
* into a ZkProgram and then into a verification key.
|
|
102
|
+
*
|
|
103
|
+
* @returns The resulting artifact of ZkProgram compilation (verification key)
|
|
104
|
+
*/
|
|
105
|
+
compile(): Promise<{
|
|
106
|
+
verificationKey: string;
|
|
107
|
+
}>;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=Chain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chain.d.ts","sourceRoot":"","sources":["../../src/chain/Chain.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAO5D,OAAO,EAAE,KAAK,cAAc,EAAmB,MAAM,wBAAwB,CAAC;AAE9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAErE,MAAM,WAAW,cAAc;IAC7B,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC;CAChC;AAED,MAAM,WAAW,WAAW,CAAC,mBAAmB,SAAS,cAAc;IACrE,KAAK,EAAE,YAAY,CAAC;IACpB,cAAc,EAAE,mBAAmB,CAAC;CACrC;AAyCD;;;GAGG;AACH,qBAAa,KAAK,CAAC,mBAAmB,SAAS,cAAc;IA2BjC,MAAM,EAAE,WAAW,CAAC,mBAAmB,CAAC;IA1BlE;;;;;OAKG;WACW,IAAI,CAAC,mBAAmB,SAAS,cAAc,EAC3D,MAAM,EAAE,WAAW,CAAC,mBAAmB,CAAC;IAM1C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAsB;IAGhD,gBAAgB,UAAS;IAGzB,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,SAAS,CAAC,CAAC;IAE3D;;;;OAIG;gBACuB,MAAM,EAAE,WAAW,CAAC,mBAAmB,CAAC;IASlE;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IAO7B;;OAEG;IACH,IAAW,kBAAkB,aAE5B;IAED;;;;;OAKG;IACI,gBAAgB,CAAC,GAAG,SAAS,MAAM,mBAAmB,EAAE,IAAI,EAAE,GAAG;IAexE;;;;;OAKG;IACI,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,cAAc;IAkCxE;;;OAGG;IACI,gBAAgB,CAAC,gBAAgB,EAAE,OAAO;IAIjD;;;;OAIG;IACI,UAAU;wBA8EQ,MAAM,cAAc,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;IA4CvC,aAAa,IAAI,QAAQ,CAAC,OAAO,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAcjE;;;;;OAKG;IACU,OAAO;;;CAcrB"}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/* eslint-disable max-lines */
|
|
2
|
+
import { container, Lifecycle } from "tsyringe";
|
|
3
|
+
import { Experimental, Proof } from "snarkyjs";
|
|
4
|
+
import { MethodPublicInput } from "@yab/protocol";
|
|
5
|
+
import { combineMethodName, isMethod, toWrappedMethod, } from "../method/decorator.js";
|
|
6
|
+
import { isRuntimeModule } from "../module/decorator.js";
|
|
7
|
+
const errors = {
|
|
8
|
+
onlyStringNames: () => new TypeError("Only string names are supported"),
|
|
9
|
+
notRegistredRuntimeModule: (name) => new Error(`Unable to retrieve module: ${name}, it is not registred as a runtime module for this chain`),
|
|
10
|
+
missingDecorator: (name, runtimeModuleName) => new Error(`Unable to register module: ${name} / ${runtimeModuleName},
|
|
11
|
+
did you forget to add @runtimeModule()?`),
|
|
12
|
+
nonModuleDependecy: (runtimeModuleName) => new Error(`
|
|
13
|
+
Unable to register module: ${runtimeModuleName}, attempting to inject a non-module dependency`),
|
|
14
|
+
unknownDependency: (runtimeModuleName, name) => new Error(`Unable to register module: ${runtimeModuleName},
|
|
15
|
+
attempting to inject a dependency that is not registred
|
|
16
|
+
as a runtime module for this chain: ${name}`),
|
|
17
|
+
unableToAnalyze: (name) => new Error(`Unable to analyze program for chain: ${name}`),
|
|
18
|
+
precompileFirst: () => new Error("You have to call precompile() before being able to create the proof class"),
|
|
19
|
+
zkProgramMissing: () => new Error("Unable to compile chain, pre-compilation did not produce a zkProgram"),
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Wrapper for an application specific chain, which helps orchestrate
|
|
23
|
+
* runtime modules into an interoperable runtime.
|
|
24
|
+
*/
|
|
25
|
+
export class Chain {
|
|
26
|
+
/**
|
|
27
|
+
* Alternative constructor for `Chain`.
|
|
28
|
+
*
|
|
29
|
+
* @param config - Configuration for the returned Chain
|
|
30
|
+
* @returns Chain with the provided config
|
|
31
|
+
*/
|
|
32
|
+
static from(config) {
|
|
33
|
+
return new Chain(config);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new Chain from the provided config
|
|
37
|
+
*
|
|
38
|
+
* @param config - Configuration object for the constructed Chain
|
|
39
|
+
*/
|
|
40
|
+
constructor(config) {
|
|
41
|
+
this.config = config;
|
|
42
|
+
// determines whether any proving should be done when running methods
|
|
43
|
+
this.areProofsEnabled = false;
|
|
44
|
+
this.runtimeContainer = container.createChildContainer();
|
|
45
|
+
Object.entries(this.config.runtimeModules).forEach(([name, runtimeModule]) => {
|
|
46
|
+
this.registerRuntimeModule(name, runtimeModule);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Add a name and other respective properties required by RuntimeModules,
|
|
51
|
+
* that come from the current Chain
|
|
52
|
+
*
|
|
53
|
+
* @param name - Name of the runtime module to decorate
|
|
54
|
+
*/
|
|
55
|
+
decorateRuntimeModule(name) {
|
|
56
|
+
const runtimeModuleInstance = this.runtimeContainer.resolve(name);
|
|
57
|
+
runtimeModuleInstance.name = name;
|
|
58
|
+
runtimeModuleInstance.chain = this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @returns A list of
|
|
62
|
+
*/
|
|
63
|
+
get runtimeModuleNames() {
|
|
64
|
+
return Object.keys(this.config.runtimeModules);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Returns a runtime module registred under the given key.
|
|
68
|
+
*
|
|
69
|
+
* @param name - Name of the runtime module to get
|
|
70
|
+
* @returns A runtime module stored under the given key
|
|
71
|
+
*/
|
|
72
|
+
getRuntimeModule(name) {
|
|
73
|
+
if (typeof name !== "string") {
|
|
74
|
+
throw errors.onlyStringNames();
|
|
75
|
+
}
|
|
76
|
+
if (!this.runtimeModuleNames.includes(name)) {
|
|
77
|
+
throw errors.notRegistredRuntimeModule(name);
|
|
78
|
+
}
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
80
|
+
return this.runtimeContainer.resolve(name);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Registers a runtime module under the given name.
|
|
84
|
+
*
|
|
85
|
+
* @param name - Name of the runtime module to identify it by
|
|
86
|
+
* @param runtimeModule - Runtime module to register
|
|
87
|
+
*/
|
|
88
|
+
registerRuntimeModule(name, runtimeModule) {
|
|
89
|
+
if (!isRuntimeModule(runtimeModule)) {
|
|
90
|
+
throw errors.missingDecorator(name, runtimeModule.name);
|
|
91
|
+
}
|
|
92
|
+
this.runtimeContainer.register(name, {
|
|
93
|
+
useClass: runtimeModule,
|
|
94
|
+
}, {
|
|
95
|
+
lifecycle: Lifecycle.ContainerScoped,
|
|
96
|
+
});
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
98
|
+
const dependencies = Reflect.getMetadata("design:paramtypes", runtimeModule);
|
|
99
|
+
dependencies?.forEach((dependency) => {
|
|
100
|
+
const name = typeof dependency === "string" ? dependency : dependency.name;
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
102
|
+
if (!name) {
|
|
103
|
+
throw errors.nonModuleDependecy(runtimeModule.name);
|
|
104
|
+
}
|
|
105
|
+
if (!this.runtimeModuleNames.includes(name)) {
|
|
106
|
+
throw errors.unknownDependency(runtimeModule.name, name);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
this.decorateRuntimeModule(name);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Sets if proofs are enabled or not
|
|
113
|
+
* @param areProofsEnabled
|
|
114
|
+
*/
|
|
115
|
+
setProofsEnabled(areProofsEnabled) {
|
|
116
|
+
this.areProofsEnabled = areProofsEnabled;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Precompiles the current runtime modules into a ZkProgram.
|
|
120
|
+
*
|
|
121
|
+
* @returns - Analysis of the precompiled ZkProgram
|
|
122
|
+
*/
|
|
123
|
+
precompile() {
|
|
124
|
+
const methods = this.runtimeModuleNames.reduce((allMethods, runtimeModuleName) => {
|
|
125
|
+
// eslint-disable-next-line max-len
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
127
|
+
const runtimeModule = this.getRuntimeModule(runtimeModuleName);
|
|
128
|
+
// eslint-disable-next-line max-len
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
130
|
+
const modulePrototype = Object.getPrototypeOf(runtimeModule);
|
|
131
|
+
const modulePrototypeMethods = Object.getOwnPropertyNames(modulePrototype);
|
|
132
|
+
const moduleMethods = modulePrototypeMethods.reduce((allModuleMethods, methodName) => {
|
|
133
|
+
if (isMethod(runtimeModule, methodName)) {
|
|
134
|
+
const combinedMethodName = combineMethodName(runtimeModuleName, methodName);
|
|
135
|
+
const method = modulePrototype[methodName];
|
|
136
|
+
const wrappedMethod = Reflect.apply(toWrappedMethod, runtimeModule, [methodName, method]);
|
|
137
|
+
// eslint-disable-next-line no-warning-comments
|
|
138
|
+
// TODO: find out how to import the Tuple type
|
|
139
|
+
// eslint-disable-next-line max-len
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
141
|
+
const privateInputs = Reflect.getMetadata("design:paramtypes", runtimeModule, methodName);
|
|
142
|
+
return {
|
|
143
|
+
...allModuleMethods,
|
|
144
|
+
[combinedMethodName]: {
|
|
145
|
+
// eslint-disable-next-line max-len
|
|
146
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
147
|
+
privateInputs,
|
|
148
|
+
method: wrappedMethod,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return allModuleMethods;
|
|
153
|
+
}, {});
|
|
154
|
+
return {
|
|
155
|
+
...allMethods,
|
|
156
|
+
...moduleMethods,
|
|
157
|
+
};
|
|
158
|
+
}, {});
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/require-array-sort-compare
|
|
160
|
+
const sortedMethods = Object.fromEntries(Object.entries(methods).sort());
|
|
161
|
+
this.program = Experimental.ZkProgram({
|
|
162
|
+
publicInput: MethodPublicInput,
|
|
163
|
+
methods: sortedMethods,
|
|
164
|
+
});
|
|
165
|
+
function analyze() {
|
|
166
|
+
if (!this.program) {
|
|
167
|
+
throw errors.unableToAnalyze(this.constructor.name);
|
|
168
|
+
}
|
|
169
|
+
const zkProgramAnalysis = this.program.analyzeMethods();
|
|
170
|
+
return Object.keys(sortedMethods).map((methodName, index) => {
|
|
171
|
+
const { rows, gates } = zkProgramAnalysis[index];
|
|
172
|
+
const { privateInputs: inputs } = sortedMethods[methodName];
|
|
173
|
+
return {
|
|
174
|
+
methodName,
|
|
175
|
+
analysis: {
|
|
176
|
+
rows,
|
|
177
|
+
gates,
|
|
178
|
+
inputs,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
analyze,
|
|
185
|
+
toPretty: () => {
|
|
186
|
+
Reflect.apply(analyze, this, []).forEach(({ methodName, analysis: methodAnalysis }) => {
|
|
187
|
+
const inputs = methodAnalysis.inputs.map(
|
|
188
|
+
// eslint-disable-next-line max-len
|
|
189
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
|
190
|
+
(input) => input.name);
|
|
191
|
+
console.log(`
|
|
192
|
+
Method: ${methodName}
|
|
193
|
+
Rows: ${methodAnalysis.rows},
|
|
194
|
+
Gates: ${methodAnalysis.gates.length}
|
|
195
|
+
Inputs: [${inputs.join(", ")}]
|
|
196
|
+
`);
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
getProofClass() {
|
|
202
|
+
if (this.program === undefined) {
|
|
203
|
+
throw errors.precompileFirst();
|
|
204
|
+
}
|
|
205
|
+
const { program } = this;
|
|
206
|
+
return ((programClosure) => { var _a; return _a = class AppChainProof extends Proof {
|
|
207
|
+
},
|
|
208
|
+
_a.publicInputType = MethodPublicInput,
|
|
209
|
+
_a.tag = () => programClosure,
|
|
210
|
+
_a; })(program);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Compiles the current runtime modules configuration
|
|
214
|
+
* into a ZkProgram and then into a verification key.
|
|
215
|
+
*
|
|
216
|
+
* @returns The resulting artifact of ZkProgram compilation (verification key)
|
|
217
|
+
*/
|
|
218
|
+
async compile() {
|
|
219
|
+
this.precompile();
|
|
220
|
+
if (!this.program) {
|
|
221
|
+
throw errors.zkProgramMissing();
|
|
222
|
+
}
|
|
223
|
+
const { areProofsEnabled, program } = this;
|
|
224
|
+
this.setProofsEnabled(false);
|
|
225
|
+
const artifact = await program.compile();
|
|
226
|
+
this.setProofsEnabled(areProofsEnabled);
|
|
227
|
+
return artifact;
|
|
228
|
+
}
|
|
229
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./method/RuntimeMethodExecutionContext";
|
|
2
|
+
export * from "./method/assert";
|
|
3
|
+
export * from "./method/runtimeMethod";
|
|
4
|
+
export * from "./module/decorator";
|
|
5
|
+
export * from "./runtime/RuntimeModule";
|
|
6
|
+
export * from "./runtime/Runtime";
|
|
7
|
+
export * from "./state/InMemoryStateService";
|
|
8
|
+
export * from "./state/State";
|
|
9
|
+
export * from "./state/StateMap";
|
|
10
|
+
export * from "./state/StateServiceProvider";
|
|
11
|
+
export * from "./state/decorator";
|
|
12
|
+
export * from "./method/MethodParameterDecoder";
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mBAAmB,CAAC;AAClC,cAAc,iCAAiC,CAAC"}
|