@proto-kit/common 0.1.1-develop.1086

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 (96) hide show
  1. package/LICENSE.md +201 -0
  2. package/dist/config/ChildContainerCreatable.d.ts +5 -0
  3. package/dist/config/ChildContainerCreatable.d.ts.map +1 -0
  4. package/dist/config/ChildContainerCreatable.js +1 -0
  5. package/dist/config/ChildContainerProvider.d.ts +5 -0
  6. package/dist/config/ChildContainerProvider.d.ts.map +1 -0
  7. package/dist/config/ChildContainerProvider.js +1 -0
  8. package/dist/config/ConfigurableModule.d.ts +25 -0
  9. package/dist/config/ConfigurableModule.d.ts.map +1 -0
  10. package/dist/config/ConfigurableModule.js +23 -0
  11. package/dist/config/ModuleContainer.d.ts +161 -0
  12. package/dist/config/ModuleContainer.d.ts.map +1 -0
  13. package/dist/config/ModuleContainer.js +278 -0
  14. package/dist/dependencyFactory/DependencyFactory.d.ts +29 -0
  15. package/dist/dependencyFactory/DependencyFactory.d.ts.map +1 -0
  16. package/dist/dependencyFactory/DependencyFactory.js +1 -0
  17. package/dist/dependencyFactory/injectOptional.d.ts +16 -0
  18. package/dist/dependencyFactory/injectOptional.d.ts.map +1 -0
  19. package/dist/dependencyFactory/injectOptional.js +39 -0
  20. package/dist/events/EventEmitter.d.ts +19 -0
  21. package/dist/events/EventEmitter.d.ts.map +1 -0
  22. package/dist/events/EventEmitter.js +34 -0
  23. package/dist/events/EventEmitterProxy.d.ts +17 -0
  24. package/dist/events/EventEmitterProxy.d.ts.map +1 -0
  25. package/dist/events/EventEmitterProxy.js +23 -0
  26. package/dist/events/EventEmittingComponent.d.ts +6 -0
  27. package/dist/events/EventEmittingComponent.d.ts.map +1 -0
  28. package/dist/events/EventEmittingComponent.js +1 -0
  29. package/dist/index.d.ts +20 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +19 -0
  32. package/dist/log.d.ts +20 -0
  33. package/dist/log.d.ts.map +1 -0
  34. package/dist/log.js +75 -0
  35. package/dist/test/equalProvable.d.ts +20 -0
  36. package/dist/test/equalProvable.d.ts.map +1 -0
  37. package/dist/test/equalProvable.js +13 -0
  38. package/dist/trees/InMemoryMerkleTreeStorage.d.ts +11 -0
  39. package/dist/trees/InMemoryMerkleTreeStorage.d.ts.map +1 -0
  40. package/dist/trees/InMemoryMerkleTreeStorage.js +12 -0
  41. package/dist/trees/MerkleTreeStore.d.ts +5 -0
  42. package/dist/trees/MerkleTreeStore.d.ts.map +1 -0
  43. package/dist/trees/MerkleTreeStore.js +1 -0
  44. package/dist/trees/MockAsyncMerkleStore.d.ts +9 -0
  45. package/dist/trees/MockAsyncMerkleStore.d.ts.map +1 -0
  46. package/dist/trees/MockAsyncMerkleStore.js +19 -0
  47. package/dist/trees/RollupMerkleTree.d.ts +147 -0
  48. package/dist/trees/RollupMerkleTree.d.ts.map +1 -0
  49. package/dist/trees/RollupMerkleTree.js +217 -0
  50. package/dist/trees/VirtualMerkleTreeStore.d.ts +13 -0
  51. package/dist/trees/VirtualMerkleTreeStore.d.ts.map +1 -0
  52. package/dist/trees/VirtualMerkleTreeStore.js +17 -0
  53. package/dist/types.d.ts +26 -0
  54. package/dist/types.d.ts.map +1 -0
  55. package/dist/types.js +11 -0
  56. package/dist/utils.d.ts +35 -0
  57. package/dist/utils.d.ts.map +1 -0
  58. package/dist/utils.js +73 -0
  59. package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts +54 -0
  60. package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts.map +1 -0
  61. package/dist/zkProgrammable/ProvableMethodExecutionContext.js +96 -0
  62. package/dist/zkProgrammable/ZkProgrammable.d.ts +39 -0
  63. package/dist/zkProgrammable/ZkProgrammable.d.ts.map +1 -0
  64. package/dist/zkProgrammable/ZkProgrammable.js +67 -0
  65. package/dist/zkProgrammable/provableMethod.d.ts +19 -0
  66. package/dist/zkProgrammable/provableMethod.d.ts.map +1 -0
  67. package/dist/zkProgrammable/provableMethod.js +73 -0
  68. package/jest.config.cjs +1 -0
  69. package/package.json +34 -0
  70. package/src/config/ChildContainerCreatable.ts +5 -0
  71. package/src/config/ChildContainerProvider.ts +5 -0
  72. package/src/config/ConfigurableModule.ts +57 -0
  73. package/src/config/ModuleContainer.ts +472 -0
  74. package/src/dependencyFactory/DependencyFactory.ts +57 -0
  75. package/src/dependencyFactory/injectOptional.ts +41 -0
  76. package/src/events/EventEmitter.ts +61 -0
  77. package/src/events/EventEmitterProxy.ts +59 -0
  78. package/src/events/EventEmittingComponent.ts +7 -0
  79. package/src/index.ts +19 -0
  80. package/src/log.ts +97 -0
  81. package/src/trees/InMemoryMerkleTreeStorage.ts +17 -0
  82. package/src/trees/MerkleTreeStore.ts +5 -0
  83. package/src/trees/MockAsyncMerkleStore.ts +30 -0
  84. package/src/trees/RollupMerkleTree.ts +356 -0
  85. package/src/trees/VirtualMerkleTreeStore.ts +20 -0
  86. package/src/types.ts +49 -0
  87. package/src/utils.ts +149 -0
  88. package/src/zkProgrammable/ProvableMethodExecutionContext.ts +122 -0
  89. package/src/zkProgrammable/ZkProgrammable.ts +131 -0
  90. package/src/zkProgrammable/provableMethod.ts +123 -0
  91. package/test/config/ContainerEvents.test.ts +67 -0
  92. package/test/config/ModuleContainer.test.ts +172 -0
  93. package/test/trees/MerkleTree.test.ts +106 -0
  94. package/test/tsconfig.json +7 -0
  95. package/test/zkProgrammable/ZkProgrammable.test.ts +304 -0
  96. package/tsconfig.json +8 -0
@@ -0,0 +1,172 @@
1
+ import "reflect-metadata";
2
+ import { container as tsyringeContainer, inject, injectable } from "tsyringe";
3
+
4
+ import {
5
+ ConfigurableModule,
6
+ NoConfig,
7
+ } from "../../src/config/ConfigurableModule";
8
+ import {
9
+ ModuleContainer,
10
+ ModulesRecord,
11
+ } from "../../src/config/ModuleContainer";
12
+ import { TypedClass } from "../../src/types";
13
+ import { DependencyFactory } from "../../src";
14
+
15
+ // module container will accept modules that extend this type
16
+ class BaseTestModule<Config> extends ConfigurableModule<Config> {}
17
+
18
+ type TestModulesRecord = ModulesRecord<TypedClass<BaseTestModule<unknown>>>;
19
+
20
+ interface TestModuleConfig {
21
+ testConfigProperty: number;
22
+ testConfigProperty2?: number;
23
+ testConfigProperty3?: number;
24
+ }
25
+
26
+ @injectable()
27
+ class ChildModule extends BaseTestModule<NoConfig> {
28
+ public constructor(@inject("TestModule") public readonly testModule: any) {
29
+ super();
30
+ }
31
+
32
+ x() {
33
+ return "dependency factory works";
34
+ }
35
+ }
36
+
37
+ class TestModule
38
+ extends BaseTestModule<TestModuleConfig>
39
+ implements DependencyFactory
40
+ {
41
+ public dependencies() {
42
+ return {
43
+ dependencyModule1: {
44
+ useClass: ChildModule,
45
+ },
46
+ };
47
+ }
48
+ }
49
+
50
+ interface OtherTestModuleConfig {
51
+ otherTestConfigProperty: number;
52
+ }
53
+
54
+ class OtherTestModule extends BaseTestModule<OtherTestModuleConfig> {
55
+ public x() {
56
+ return "";
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Showcases a wrongly typed/defined module as
62
+ * per the TestModuleContainer requirements
63
+ */
64
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
65
+ class WrongTestModule {}
66
+
67
+ class TestModuleContainer<
68
+ Modules extends TestModulesRecord,
69
+ > extends ModuleContainer<Modules> {}
70
+
71
+ describe("moduleContainer", () => {
72
+ let container: TestModuleContainer<{
73
+ TestModule: typeof TestModule;
74
+ OtherTestModule: typeof OtherTestModule;
75
+ }>;
76
+ const testConfigProperty = 0;
77
+
78
+ beforeEach(() => {
79
+ container = new TestModuleContainer({
80
+ modules: {
81
+ TestModule,
82
+ OtherTestModule,
83
+ // this module would not be assignable to TestModuleContainer
84
+ // WrongTestModule,
85
+ },
86
+ });
87
+ });
88
+
89
+ it("should resolve dependency factory dependencies correctly", () => {
90
+ container.configure({
91
+ TestModule: {
92
+ testConfigProperty,
93
+ },
94
+
95
+ OtherTestModule: {
96
+ otherTestConfigProperty: testConfigProperty,
97
+ },
98
+ });
99
+
100
+ container.create(() => tsyringeContainer.createChildContainer());
101
+
102
+ // Unfortunately we still need this so that the dependencies are registered
103
+ container.resolve("TestModule");
104
+ const dm = container.resolve("DependencyModule1");
105
+
106
+ expect(dm.x()).toBe("dependency factory works");
107
+ expect(dm.testModule).toBeDefined();
108
+ });
109
+
110
+ it("should throw on resolution, if config was not provided", () => {
111
+ expect.assertions(1);
112
+
113
+ container.create(() => tsyringeContainer.createChildContainer());
114
+
115
+ expect(() => {
116
+ container.resolve("TestModule");
117
+ }).toThrow();
118
+ });
119
+
120
+ it("should resolve the registered module with the provided config", () => {
121
+ expect.assertions(1);
122
+
123
+ container.configure({
124
+ TestModule: {
125
+ testConfigProperty,
126
+ },
127
+
128
+ OtherTestModule: {
129
+ otherTestConfigProperty: testConfigProperty,
130
+ },
131
+ });
132
+ container.create(() => tsyringeContainer.createChildContainer());
133
+
134
+ const testModule = container.resolve("TestModule");
135
+
136
+ expect(testModule.config.testConfigProperty).toBe(testConfigProperty);
137
+
138
+ const dependency = container.resolve("DependencyModule1");
139
+ dependency.x();
140
+ });
141
+
142
+ it("should stack configurations correctly", () => {
143
+ container.configure({
144
+ TestModule: {
145
+ testConfigProperty: 1,
146
+ },
147
+ OtherTestModule: {
148
+ otherTestConfigProperty: 4,
149
+ },
150
+ });
151
+
152
+ container.configurePartial({
153
+ TestModule: {
154
+ testConfigProperty2: 2,
155
+ },
156
+ });
157
+
158
+ container.configurePartial({
159
+ TestModule: {
160
+ testConfigProperty: 3,
161
+ },
162
+ });
163
+
164
+ container.create(() => tsyringeContainer.createChildContainer());
165
+
166
+ const { config } = container.resolve("TestModule");
167
+
168
+ expect(config.testConfigProperty).toBe(3);
169
+ expect(config.testConfigProperty2).toBe(2);
170
+ expect(config.testConfigProperty3).toBe(undefined);
171
+ });
172
+ });
@@ -0,0 +1,106 @@
1
+ import { beforeEach } from "@jest/globals";
2
+ import { Field } from "o1js";
3
+
4
+ import { createMerkleTree, InMemoryMerkleTreeStorage, log } from "../../src";
5
+
6
+ describe.each([4, 16, 256])("cachedMerkleTree - %s", (height) => {
7
+ class RollupMerkleTree extends createMerkleTree(height) {}
8
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
9
+ class RollupMerkleTreeWitness extends RollupMerkleTree.WITNESS {}
10
+
11
+ let store: InMemoryMerkleTreeStorage;
12
+ let tree: RollupMerkleTree;
13
+
14
+ beforeEach(() => {
15
+ log.setLevel("INFO");
16
+
17
+ store = new InMemoryMerkleTreeStorage();
18
+ tree = new RollupMerkleTree(store);
19
+ });
20
+
21
+ it("should have the same root when empty", () => {
22
+ expect.assertions(1);
23
+
24
+ expect(tree.getRoot().toBigInt()).toStrictEqual(
25
+ RollupMerkleTree.EMPTY_ROOT
26
+ );
27
+ });
28
+
29
+ it("should have a different root when not empty", () => {
30
+ expect.assertions(1);
31
+
32
+ tree.setLeaf(1n, Field(1));
33
+
34
+ expect(tree.getRoot().toBigInt()).not.toStrictEqual(
35
+ RollupMerkleTree.EMPTY_ROOT
36
+ );
37
+ });
38
+
39
+ it("should have the same root after adding and removing item", () => {
40
+ expect.assertions(1);
41
+
42
+ tree.setLeaf(1n, Field(1));
43
+
44
+ const root = tree.getRoot();
45
+
46
+ tree.setLeaf(5n, Field(5));
47
+ tree.setLeaf(5n, Field(0));
48
+
49
+ expect(tree.getRoot().toBigInt()).toStrictEqual(root.toBigInt());
50
+ });
51
+
52
+ it("should provide correct witnesses", () => {
53
+ expect.assertions(1);
54
+
55
+ tree.setLeaf(1n, Field(1));
56
+ tree.setLeaf(5n, Field(5));
57
+
58
+ const witness = tree.getWitness(5n);
59
+
60
+ expect(witness.calculateRoot(Field(5)).toBigInt()).toStrictEqual(
61
+ tree.getRoot().toBigInt()
62
+ );
63
+ });
64
+
65
+ it("should have invalid witnesses with wrong values", () => {
66
+ expect.assertions(1);
67
+
68
+ tree.setLeaf(1n, Field(1));
69
+ tree.setLeaf(5n, Field(5));
70
+
71
+ const witness = tree.getWitness(5n);
72
+
73
+ expect(witness.calculateRoot(Field(6)).toBigInt()).not.toStrictEqual(
74
+ tree.getRoot().toBigInt()
75
+ );
76
+ });
77
+
78
+ it("should have valid witnesses with changed value on the same leafs", () => {
79
+ expect.assertions(1);
80
+
81
+ tree.setLeaf(1n, Field(1));
82
+ tree.setLeaf(5n, Field(5));
83
+
84
+ const witness = tree.getWitness(5n);
85
+
86
+ tree.setLeaf(5n, Field(10));
87
+
88
+ expect(witness.calculateRoot(Field(10)).toBigInt()).toStrictEqual(
89
+ tree.getRoot().toBigInt()
90
+ );
91
+ });
92
+
93
+ it("should throw for invalid index", () => {
94
+ expect.assertions(2);
95
+
96
+ const index = 2n ** BigInt(height) + 1n;
97
+
98
+ expect(() => {
99
+ tree.setLeaf(index, Field(1));
100
+ }).toThrow("Index greater than maximum leaf number");
101
+
102
+ expect(() => {
103
+ tree.getNode(0, index);
104
+ }).toThrow("Index greater than maximum leaf number");
105
+ });
106
+ });
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "experimentalDecorators": true
5
+ },
6
+ "include": ["./**/*.ts", "./*.ts"]
7
+ }
@@ -0,0 +1,304 @@
1
+ import "reflect-metadata";
2
+ import { jest } from "@jest/globals";
3
+ import { container } from "tsyringe";
4
+ import { Field, Struct, Proof, ZkProgram } from "o1js";
5
+
6
+ import {
7
+ MOCK_PROOF,
8
+ provableMethod,
9
+ } from "../../src/zkProgrammable/provableMethod";
10
+ import {
11
+ AreProofsEnabled,
12
+ CompileArtifact,
13
+ MOCK_VERIFICATION_KEY,
14
+ ZkProgrammable,
15
+ } from "../../src/zkProgrammable/ZkProgrammable";
16
+ import { ProvableMethodExecutionContext } from "../../src/zkProgrammable/ProvableMethodExecutionContext";
17
+
18
+ const appChainMock: AreProofsEnabled = {
19
+ areProofsEnabled: false,
20
+
21
+ setProofsEnabled(areProofsEnabled: boolean) {
22
+ this.areProofsEnabled = areProofsEnabled;
23
+ },
24
+ };
25
+
26
+ class TestPublicInput extends Struct({
27
+ foo: Field,
28
+ }) {}
29
+
30
+ class TestPublicOutput extends Struct({
31
+ bar: Field,
32
+ }) {}
33
+
34
+ const failErrorMessage = "test failure";
35
+
36
+ type Balance = Field;
37
+
38
+ class TestProgrammable extends ZkProgrammable<
39
+ TestPublicInput,
40
+ TestPublicOutput
41
+ > {
42
+ public appChain: AreProofsEnabled = appChainMock;
43
+
44
+ @provableMethod()
45
+ public async foo(publicInput: TestPublicInput, bar: Balance) {
46
+ // expose the private input as public output again for testing purposes
47
+ return new TestPublicOutput({
48
+ bar,
49
+ });
50
+ }
51
+
52
+ @provableMethod()
53
+ public async fail(publicInput: TestPublicInput) {
54
+ publicInput.foo.assertEquals(1, failErrorMessage);
55
+
56
+ return new TestPublicOutput({
57
+ bar: Field(0),
58
+ });
59
+ }
60
+
61
+ public zkProgramFactory() {
62
+ const program = ZkProgram({
63
+ name: "testprogram",
64
+ publicInput: TestPublicInput,
65
+ publicOutput: TestPublicOutput,
66
+
67
+ methods: {
68
+ foo: {
69
+ privateInputs: [Field],
70
+ method: this.foo.bind(this),
71
+ },
72
+
73
+ fail: {
74
+ privateInputs: [],
75
+ method: this.fail.bind(this),
76
+ },
77
+ },
78
+ });
79
+
80
+ const SelfProof = ZkProgram.Proof(program);
81
+
82
+ const methods = {
83
+ foo: program.foo.bind(program),
84
+ fail: program.fail.bind(program),
85
+ };
86
+
87
+ return [
88
+ {
89
+ compile: program.compile.bind(program),
90
+ verify: program.verify.bind(program),
91
+ analyzeMethods: program.analyzeMethods.bind(program),
92
+ Proof: SelfProof,
93
+ methods,
94
+ },
95
+ ];
96
+ }
97
+ }
98
+
99
+ class OtherTestProgrammable extends ZkProgrammable<undefined, void> {
100
+ public appChain: AreProofsEnabled = appChainMock;
101
+
102
+ public constructor(public testProgrammable: TestProgrammable) {
103
+ super();
104
+ }
105
+
106
+ proofType = this.testProgrammable.zkProgram[0].Proof;
107
+
108
+ @provableMethod()
109
+ public async bar(testProgrammableProof: InstanceType<typeof this.proofType>) {
110
+ testProgrammableProof.verify();
111
+ }
112
+
113
+ public zkProgramFactory() {
114
+ const program = ZkProgram({
115
+ name: "testprogram2",
116
+ methods: {
117
+ bar: {
118
+ privateInputs: [this.testProgrammable.zkProgram[0].Proof],
119
+ method: this.bar.bind(this),
120
+ },
121
+ },
122
+ });
123
+
124
+ const methods = {
125
+ bar: program.bar.bind(program),
126
+ };
127
+
128
+ const SelfProof = ZkProgram.Proof(program);
129
+
130
+ return [
131
+ {
132
+ compile: program.compile.bind(program),
133
+ verify: program.verify.bind(program),
134
+ analyzeMethods: program.analyzeMethods.bind(program),
135
+ Proof: SelfProof,
136
+ methods,
137
+ },
138
+ ];
139
+ }
140
+ }
141
+
142
+ const testWithProofs = false;
143
+
144
+ describe("zkProgrammable", () => {
145
+ let testProgrammable: TestProgrammable;
146
+ let artifact: CompileArtifact;
147
+ let zkProgramFactorySpy: ReturnType<typeof jest.spyOn>;
148
+
149
+ const testCases: [
150
+ boolean,
151
+ {
152
+ verificationKey: CompileArtifact["verificationKey"];
153
+ shouldVerifyMockProofs: boolean;
154
+ },
155
+ ][] = [
156
+ [
157
+ false,
158
+ {
159
+ verificationKey: MOCK_VERIFICATION_KEY,
160
+ shouldVerifyMockProofs: true,
161
+ },
162
+ ],
163
+ ];
164
+
165
+ // We want to disable testing with proofs enabled for the CI and other stuff,
166
+ // as it is quite expensive. But we should find a better pattern than this
167
+ if (testWithProofs) {
168
+ // TODO Migrate to new o1js compile artifact format
169
+ // testCases.push([
170
+ // true,
171
+ // {
172
+ // verificationKey:
173
+ // eslint-disable-next-line max-len
174
+ // "AAA32/LNoPxEfDF5UkwfEetd5jiVLDF/Ul3N+Q2wKNcqFZm7FRScVoJylKe73IAPgAcadZ/vFAeIuDuAPFx1FaoIIoGAq5LAKNNrkU8EnWUJgSmKm2rJ3uNkJifAf116Aja8pacHExKqq5WblExBpsV/ET9JavLBZSql4zYEIvj9KfYfAz2DV6a0/jRWJAiF2xBaK2UIyga33djCkw3Lk/UC3DjV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E18n1bhexuNgU6dVWRSs=",
175
+ //
176
+ // shouldVerifyMockProofs: false,
177
+ // },
178
+ // ]);
179
+ }
180
+
181
+ describe.each(testCases)(
182
+ "areProofsEnabled",
183
+ (areProofsEnabled, { verificationKey, shouldVerifyMockProofs }) => {
184
+ beforeAll(async () => {
185
+ testProgrammable = new TestProgrammable();
186
+ testProgrammable.appChain.setProofsEnabled(areProofsEnabled);
187
+ zkProgramFactorySpy = jest.spyOn(testProgrammable, "zkProgramFactory");
188
+ artifact = await testProgrammable.zkProgram[0].compile();
189
+ }, 500_000);
190
+
191
+ describe("zkProgramFactory", () => {
192
+ it("should create and cache a ZkProgram", () => {
193
+ expect.assertions(1);
194
+
195
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
196
+ const programs = {
197
+ 1: testProgrammable.zkProgram,
198
+ 2: testProgrammable.zkProgram,
199
+ 3: testProgrammable.zkProgram,
200
+ };
201
+
202
+ expect(zkProgramFactorySpy).toHaveBeenCalledTimes(1);
203
+ });
204
+ });
205
+
206
+ it("compile should return the correct verification key", () => {
207
+ expect.assertions(1);
208
+
209
+ expect(artifact.verificationKey).toBe(verificationKey);
210
+ });
211
+
212
+ it("if proofs are disabled, it should successfully verify mock proofs", async () => {
213
+ expect.assertions(1);
214
+
215
+ const proof = new testProgrammable.zkProgram[0].Proof({
216
+ proof: MOCK_PROOF,
217
+
218
+ publicInput: new TestPublicInput({
219
+ foo: Field(0),
220
+ }),
221
+
222
+ publicOutput: new TestPublicOutput({
223
+ bar: Field(0),
224
+ }),
225
+
226
+ maxProofsVerified: 0,
227
+ });
228
+
229
+ const verified = await testProgrammable.zkProgram[0].verify(proof);
230
+
231
+ expect(verified).toBe(shouldVerifyMockProofs);
232
+
233
+ // Check if toJSON works on mockproofs
234
+ // const json = proof.toJSON();
235
+ // expect(json).toBeDefined();
236
+ });
237
+
238
+ describe("provableMethod", () => {
239
+ const executionContext =
240
+ container.resolve<ProvableMethodExecutionContext>(
241
+ ProvableMethodExecutionContext
242
+ );
243
+
244
+ let otherTestProgrammable: OtherTestProgrammable;
245
+
246
+ const testPublicInput = new TestPublicInput({
247
+ foo: Field(0),
248
+ });
249
+
250
+ describe("zkProgram interoperability", () => {
251
+ beforeAll(async () => {
252
+ otherTestProgrammable = new OtherTestProgrammable(testProgrammable);
253
+ await otherTestProgrammable.zkProgram[0].compile();
254
+ }, 500_000);
255
+
256
+ it("should successfully pass proof of one zkProgram as input to another zkProgram", async () => {
257
+ expect.assertions(3);
258
+
259
+ // execute foo
260
+ await testProgrammable.foo(testPublicInput, Field(0));
261
+
262
+ // prove foo
263
+ const testProof = await executionContext
264
+ .current()
265
+ .result.prove<Proof<TestPublicInput, TestPublicOutput>>();
266
+ const testProofVerified =
267
+ await testProgrammable.zkProgram[0].verify(testProof);
268
+
269
+ // execute bar
270
+ await otherTestProgrammable.bar(testProof);
271
+
272
+ // proof bar
273
+ const otherTestProof = await executionContext
274
+ .current()
275
+ .result.prove<Proof<undefined, void>>();
276
+ const otherTestProofVerified =
277
+ await otherTestProgrammable.zkProgram[0].verify(otherTestProof);
278
+
279
+ expect(testProof.publicOutput.bar.toString()).toBe(
280
+ testPublicInput.foo.toString()
281
+ );
282
+
283
+ expect(testProofVerified).toBe(true);
284
+ expect(otherTestProofVerified).toBe(true);
285
+ }, 500_000);
286
+ });
287
+
288
+ describe("failed method execution", () => {
289
+ it("if the method fails, it should fail to execute and prove", async () => {
290
+ expect.assertions(2);
291
+
292
+ await expect(
293
+ testProgrammable.fail(testPublicInput)
294
+ ).rejects.toThrow(failErrorMessage);
295
+
296
+ await expect(async () => {
297
+ await executionContext.current().result.prove();
298
+ }).rejects.toThrow(failErrorMessage);
299
+ });
300
+ });
301
+ });
302
+ }
303
+ );
304
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "./../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist"
5
+ },
6
+ "include": ["./src/**/*.ts", "./src/*.ts"],
7
+ "exclude": ["./dist/**/*.ts"]
8
+ }