@proto-kit/common 0.1.1-develop.600 → 0.1.1-develop.833
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 -201
- package/dist/config/ChildContainerFactory.d.ts +10 -0
- package/dist/config/ChildContainerFactory.d.ts.map +1 -0
- package/dist/config/ChildContainerFactory.js +23 -0
- package/dist/config/ChildContainerStartable.d.ts +5 -0
- package/dist/config/ChildContainerStartable.d.ts.map +1 -0
- package/dist/config/ChildContainerStartable.js +1 -0
- package/dist/config/ConfigurableModule.d.ts +2 -2
- package/dist/config/ConfigurableModule.d.ts.map +1 -1
- package/dist/config/ConfigurableModule.js +0 -1
- package/dist/config/ModuleContainer.d.ts +14 -2
- package/dist/config/ModuleContainer.d.ts.map +1 -1
- package/dist/config/ModuleContainer.js +8 -13
- package/dist/dependencyFactory/DependencyFactory.d.ts.map +1 -1
- package/dist/dependencyFactory/injectOptional.d.ts +16 -0
- package/dist/dependencyFactory/injectOptional.d.ts.map +1 -0
- package/dist/dependencyFactory/injectOptional.js +39 -0
- package/dist/events/EventEmitter.d.ts.map +1 -1
- package/dist/events/EventEmitter.js +0 -1
- package/dist/events/EventEmitterProxy.d.ts.map +1 -1
- package/dist/events/EventEmitterProxy.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/log.d.ts +3 -2
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +11 -4
- package/dist/quickmaths.d.ts +7 -0
- package/dist/quickmaths.d.ts.map +1 -0
- package/dist/quickmaths.js +24 -0
- package/dist/test/equalProvable.d.ts +20 -0
- package/dist/test/equalProvable.d.ts.map +1 -0
- package/dist/test/equalProvable.js +13 -0
- package/dist/trees/MockAsyncMerkleStore.d.ts.map +1 -1
- package/dist/trees/MockAsyncMerkleStore.js +1 -1
- package/dist/trees/RollupMerkleTree.d.ts +35 -16
- package/dist/trees/RollupMerkleTree.d.ts.map +1 -1
- package/dist/trees/RollupMerkleTree.js +8 -13
- package/dist/trees/VirtualMerkleTreeStore.d.ts +13 -0
- package/dist/trees/VirtualMerkleTreeStore.d.ts.map +1 -0
- package/dist/trees/VirtualMerkleTreeStore.js +17 -0
- package/dist/types.d.ts +5 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +12 -7
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +31 -10
- package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts.map +1 -1
- package/dist/zkProgrammable/ProvableMethodExecutionContext.js +1 -3
- package/dist/zkProgrammable/ZkProgrammable.d.ts +11 -4
- package/dist/zkProgrammable/ZkProgrammable.d.ts.map +1 -1
- package/dist/zkProgrammable/ZkProgrammable.js +7 -2
- package/dist/zkProgrammable/provableMethod.d.ts +5 -6
- package/dist/zkProgrammable/provableMethod.d.ts.map +1 -1
- package/dist/zkProgrammable/provableMethod.js +2 -5
- package/package.json +5 -5
- package/src/config/ChildContainerCreatable.ts +1 -1
- package/src/config/ConfigurableModule.ts +2 -3
- package/src/config/ModuleContainer.ts +23 -17
- package/src/dependencyFactory/DependencyFactory.ts +5 -4
- package/src/dependencyFactory/injectOptional.ts +41 -0
- package/src/events/EventEmitter.ts +0 -2
- package/src/events/EventEmitterProxy.ts +7 -5
- package/src/index.ts +1 -0
- package/src/log.ts +20 -6
- package/src/trees/MockAsyncMerkleStore.ts +2 -1
- package/src/trees/RollupMerkleTree.ts +11 -17
- package/src/trees/VirtualMerkleTreeStore.ts +2 -3
- package/src/types.ts +6 -4
- package/src/utils.ts +77 -19
- package/src/zkProgrammable/ProvableMethodExecutionContext.ts +2 -4
- package/src/zkProgrammable/ZkProgrammable.ts +19 -12
- package/src/zkProgrammable/provableMethod.ts +13 -14
- package/test/config/ContainerEvents.test.ts +8 -27
- package/test/config/ModuleContainer.test.ts +10 -25
- package/test/trees/MerkleTree.test.ts +3 -2
- package/test/tsconfig.json +5 -2
- package/test/zkProgrammable/ZkProgrammable.test.ts +104 -83
- package/tsconfig.json +1 -1
- package/tsconfig.test.json +0 -9
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/* eslint-disable id-length */
|
|
2
|
-
/* eslint-disable line-comment-position */
|
|
3
|
-
/* eslint-disable no-inline-comments */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
5
|
-
/* eslint-disable @typescript-eslint/method-signature-style */
|
|
6
1
|
import { Bool, Field, Poseidon, Provable, Struct } from "o1js";
|
|
7
2
|
|
|
8
3
|
import { range } from "../utils";
|
|
@@ -141,7 +136,8 @@ export function createMerkleTree(height: number): AbstractMerkleTreeClass {
|
|
|
141
136
|
* @param leaf Value of the leaf node that belongs to this Witness.
|
|
142
137
|
* @returns The calculated root.
|
|
143
138
|
*/
|
|
144
|
-
public calculateRoot(
|
|
139
|
+
public calculateRoot(leaf: Field): Field {
|
|
140
|
+
let hash = leaf;
|
|
145
141
|
const n = this.height();
|
|
146
142
|
|
|
147
143
|
for (let index = 1; index < n; ++index) {
|
|
@@ -163,13 +159,8 @@ export function createMerkleTree(height: number): AbstractMerkleTreeClass {
|
|
|
163
159
|
let index = Field(0);
|
|
164
160
|
const n = this.height();
|
|
165
161
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
index = Provable.if(
|
|
169
|
-
this.isLeft[index_ - 1],
|
|
170
|
-
index,
|
|
171
|
-
index.add(powerOfTwo)
|
|
172
|
-
);
|
|
162
|
+
for (let i = 1; i < n; ++i) {
|
|
163
|
+
index = Provable.if(this.isLeft[i - 1], index, index.add(powerOfTwo));
|
|
173
164
|
powerOfTwo = powerOfTwo.mul(2);
|
|
174
165
|
}
|
|
175
166
|
|
|
@@ -308,16 +299,20 @@ export function createMerkleTree(height: number): AbstractMerkleTreeClass {
|
|
|
308
299
|
|
|
309
300
|
const path = [];
|
|
310
301
|
const isLefts = [];
|
|
302
|
+
let currentIndex = index;
|
|
311
303
|
for (
|
|
312
304
|
let level = 0;
|
|
313
305
|
level < AbstractRollupMerkleTree.HEIGHT - 1;
|
|
314
306
|
level += 1
|
|
315
307
|
) {
|
|
316
|
-
const isLeft =
|
|
317
|
-
const sibling = this.getNode(
|
|
308
|
+
const isLeft = currentIndex % 2n === 0n;
|
|
309
|
+
const sibling = this.getNode(
|
|
310
|
+
level,
|
|
311
|
+
isLeft ? currentIndex + 1n : currentIndex - 1n
|
|
312
|
+
);
|
|
318
313
|
isLefts.push(Bool(isLeft));
|
|
319
314
|
path.push(sibling);
|
|
320
|
-
|
|
315
|
+
currentIndex /= 2n;
|
|
321
316
|
}
|
|
322
317
|
return new RollupMerkleWitness({
|
|
323
318
|
isLeft: isLefts,
|
|
@@ -325,7 +320,6 @@ export function createMerkleTree(height: number): AbstractMerkleTreeClass {
|
|
|
325
320
|
});
|
|
326
321
|
}
|
|
327
322
|
|
|
328
|
-
// eslint-disable-next-line no-warning-comments, max-len
|
|
329
323
|
// TODO: should this take an optional offset? should it fail if the array is too long?
|
|
330
324
|
/**
|
|
331
325
|
* Fills all leaves of the tree.
|
|
@@ -7,7 +7,7 @@ import { InMemoryMerkleTreeStorage } from "./InMemoryMerkleTreeStorage";
|
|
|
7
7
|
*/
|
|
8
8
|
export class VirtualMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
9
9
|
public constructor(private readonly parent: MerkleTreeStore) {
|
|
10
|
-
super()
|
|
10
|
+
super();
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
public getNode(key: bigint, level: number): bigint | undefined {
|
|
@@ -17,5 +17,4 @@ export class VirtualMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
|
17
17
|
public setNode(key: bigint, level: number, value: bigint): void {
|
|
18
18
|
super.setNode(key, level, value);
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
}
|
|
20
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// allows to reference interfaces as 'classes' rather than instances
|
|
2
|
-
import { Bool, Field,
|
|
2
|
+
import { Bool, Field, PublicKey } from "o1js";
|
|
3
3
|
|
|
4
4
|
export type TypedClass<Class> = new (...args: any[]) => Class;
|
|
5
5
|
|
|
6
|
-
export type UnTypedClass = new (...args: any[]) =>
|
|
6
|
+
export type UnTypedClass = new (...args: any[]) => unknown;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Using simple `keyof Target` would result into the key
|
|
@@ -11,13 +11,11 @@ export type UnTypedClass = new (...args: any[]) => any;
|
|
|
11
11
|
*/
|
|
12
12
|
export type StringKeyOf<Target extends object> = Extract<keyof Target, string> &
|
|
13
13
|
string;
|
|
14
|
-
// export type StringKeyOf<Target extends object> = keyof Target
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Utility type to infer element type from an array type
|
|
18
17
|
*/
|
|
19
18
|
export type ArrayElement<ArrayType extends readonly unknown[]> =
|
|
20
|
-
// eslint-disable-next-line putout/putout
|
|
21
19
|
ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
22
20
|
|
|
23
21
|
/**
|
|
@@ -45,3 +43,7 @@ export const EMPTY_PUBLICKEY = PublicKey.fromObject({
|
|
|
45
43
|
x: EMPTY_PUBLICKEY_X,
|
|
46
44
|
isOdd: Bool(true),
|
|
47
45
|
});
|
|
46
|
+
|
|
47
|
+
export type OverwriteObjectType<Base, New> = {
|
|
48
|
+
[Key in keyof Base]: Key extends keyof New ? New[Key] : Base[Key];
|
|
49
|
+
} & New;
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Field,
|
|
3
|
+
FlexibleProvablePure,
|
|
4
|
+
Poseidon,
|
|
5
|
+
DynamicProof,
|
|
6
|
+
Proof,
|
|
7
|
+
} from "o1js";
|
|
2
8
|
|
|
3
9
|
export function requireTrue(
|
|
4
10
|
condition: boolean,
|
|
@@ -11,17 +17,50 @@ export function requireTrue(
|
|
|
11
17
|
}
|
|
12
18
|
}
|
|
13
19
|
|
|
14
|
-
export function range(
|
|
20
|
+
export function range(
|
|
21
|
+
startOrEnd: number,
|
|
22
|
+
endOrNothing: number | undefined
|
|
23
|
+
): number[] {
|
|
24
|
+
let end = endOrNothing;
|
|
25
|
+
let start = startOrEnd;
|
|
15
26
|
if (end === undefined) {
|
|
16
27
|
end = startOrEnd;
|
|
17
|
-
|
|
28
|
+
start = 0;
|
|
18
29
|
}
|
|
19
|
-
return Array.from(
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
return Array.from({ length: end - start }, (ignored, index) => index + start);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function reduceSequential<T, U>(
|
|
34
|
+
array: T[],
|
|
35
|
+
callbackfn: (
|
|
36
|
+
previousValue: U,
|
|
37
|
+
currentValue: T,
|
|
38
|
+
currentIndex: number,
|
|
39
|
+
array: T[]
|
|
40
|
+
) => Promise<U>,
|
|
41
|
+
initialValue: U
|
|
42
|
+
) {
|
|
43
|
+
return array.reduce<Promise<U>>(
|
|
44
|
+
async (previousPromise, current, index, arr) => {
|
|
45
|
+
const previous = await previousPromise;
|
|
46
|
+
return await callbackfn(previous, current, index, arr);
|
|
47
|
+
},
|
|
48
|
+
Promise.resolve(initialValue)
|
|
22
49
|
);
|
|
23
50
|
}
|
|
24
51
|
|
|
52
|
+
export function mapSequential<T, R>(
|
|
53
|
+
array: T[],
|
|
54
|
+
f: (element: T, index: number, array: T[]) => Promise<R>
|
|
55
|
+
) {
|
|
56
|
+
return array.reduce<Promise<R[]>>(async (r, element, index, a) => {
|
|
57
|
+
const ret = await r;
|
|
58
|
+
const next = await f(element, index, a);
|
|
59
|
+
ret.push(next);
|
|
60
|
+
return ret;
|
|
61
|
+
}, Promise.resolve([]));
|
|
62
|
+
}
|
|
63
|
+
|
|
25
64
|
/**
|
|
26
65
|
* Computes a dummy value for the given value type.
|
|
27
66
|
*
|
|
@@ -38,7 +77,6 @@ export function dummyValue<Value>(
|
|
|
38
77
|
return valueType.fromFields(fields) as Value;
|
|
39
78
|
}
|
|
40
79
|
|
|
41
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
42
80
|
export function noop(): void {}
|
|
43
81
|
|
|
44
82
|
export interface ToFieldable {
|
|
@@ -53,14 +91,19 @@ export interface ToJSONableStatic {
|
|
|
53
91
|
toJSON: (value: unknown) => any;
|
|
54
92
|
}
|
|
55
93
|
|
|
56
|
-
export interface ProofTypes {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
94
|
+
// export interface ProofTypes {
|
|
95
|
+
// publicOutputType?: ToFieldableStatic;
|
|
96
|
+
// publicInputType?: ToFieldableStatic;
|
|
97
|
+
// }
|
|
98
|
+
|
|
99
|
+
export type ProofTypes =
|
|
100
|
+
| typeof Proof<unknown, unknown>
|
|
101
|
+
| typeof DynamicProof<unknown, unknown>;
|
|
60
102
|
|
|
61
103
|
export async function sleep(ms: number) {
|
|
62
|
-
|
|
63
|
-
|
|
104
|
+
await new Promise((resolve) => {
|
|
105
|
+
setTimeout(resolve, ms);
|
|
106
|
+
});
|
|
64
107
|
}
|
|
65
108
|
|
|
66
109
|
export function filterNonNull<Type>(value: Type | null): value is Type {
|
|
@@ -73,19 +116,34 @@ export function filterNonUndefined<Type>(
|
|
|
73
116
|
return value !== undefined;
|
|
74
117
|
}
|
|
75
118
|
|
|
76
|
-
|
|
119
|
+
const encoder = new TextEncoder();
|
|
77
120
|
|
|
78
121
|
// Copied from o1js binable.ts:317
|
|
79
122
|
export function prefixToField(prefix: string): Field {
|
|
80
|
-
|
|
123
|
+
const fieldSize = Field.sizeInBytes;
|
|
81
124
|
if (prefix.length >= fieldSize) throw Error("prefix too long");
|
|
82
|
-
|
|
125
|
+
const stringBytes = [...encoder.encode(prefix)];
|
|
83
126
|
return Field.fromBytes(
|
|
84
127
|
stringBytes.concat(Array(fieldSize - stringBytes.length).fill(0))
|
|
85
128
|
);
|
|
86
129
|
}
|
|
87
130
|
|
|
88
131
|
export function hashWithPrefix(prefix: string, input: Field[]) {
|
|
89
|
-
const salt = Poseidon.update(
|
|
90
|
-
|
|
91
|
-
|
|
132
|
+
const salt = Poseidon.update(
|
|
133
|
+
[Field(0), Field(0), Field(0)],
|
|
134
|
+
[prefixToField(prefix)]
|
|
135
|
+
);
|
|
136
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
137
|
+
return Poseidon.update(salt as [Field, Field, Field], input)[0];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// end copy
|
|
141
|
+
|
|
142
|
+
export function expectDefined<T>(value: T | undefined): asserts value is T {
|
|
143
|
+
expect(value).toBeDefined();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
type NonMethodKeys<Type> = {
|
|
147
|
+
[Key in keyof Type]: Type[Key] extends Function ? never : Key;
|
|
148
|
+
}[keyof Type];
|
|
149
|
+
export type NonMethods<Type> = Pick<Type, NonMethodKeys<Type>>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Proof } from "o1js";
|
|
2
2
|
import { singleton } from "tsyringe";
|
|
3
3
|
import uniqueId from "lodash/uniqueId";
|
|
4
4
|
|
|
@@ -23,8 +23,7 @@ export class ProvableMethodExecutionResult {
|
|
|
23
23
|
public prover?: () => Promise<Proof<unknown, unknown>>;
|
|
24
24
|
|
|
25
25
|
public async prove<
|
|
26
|
-
|
|
27
|
-
ProofType extends Proof<unknown, unknown>
|
|
26
|
+
ProofType extends Proof<unknown, unknown>,
|
|
28
27
|
>(): Promise<ProofType> {
|
|
29
28
|
if (!this.prover) {
|
|
30
29
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
@@ -54,7 +53,6 @@ export class ProvableMethodExecutionContext {
|
|
|
54
53
|
public result: ProvableMethodExecutionResult =
|
|
55
54
|
new ProvableMethodExecutionResult();
|
|
56
55
|
|
|
57
|
-
// eslint-disable-next-line no-warning-comments,max-len
|
|
58
56
|
// TODO See if we should make this class generic, bc I think we can persist the type
|
|
59
57
|
/**
|
|
60
58
|
* Adds a method prover to the current execution context,
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ZkProgram, FlexibleProvablePure, Proof, Field, Provable } from "o1js";
|
|
2
2
|
import { Memoize } from "typescript-memoize";
|
|
3
3
|
|
|
4
|
+
import { log } from "../log";
|
|
5
|
+
|
|
4
6
|
import { MOCK_PROOF } from "./provableMethod";
|
|
5
7
|
|
|
6
8
|
const errors = {
|
|
@@ -9,7 +11,10 @@ const errors = {
|
|
|
9
11
|
};
|
|
10
12
|
|
|
11
13
|
export interface CompileArtifact {
|
|
12
|
-
verificationKey:
|
|
14
|
+
verificationKey: {
|
|
15
|
+
data: string;
|
|
16
|
+
hash: Field;
|
|
17
|
+
};
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
export interface AreProofsEnabled {
|
|
@@ -29,23 +34,22 @@ export interface PlainZkProgram<PublicInput = undefined, PublicOutput = void> {
|
|
|
29
34
|
compile: Compile;
|
|
30
35
|
verify: Verify<PublicInput, PublicOutput>;
|
|
31
36
|
Proof: ReturnType<
|
|
32
|
-
typeof
|
|
37
|
+
typeof ZkProgram.Proof<
|
|
33
38
|
FlexibleProvablePure<PublicInput>,
|
|
34
39
|
FlexibleProvablePure<PublicOutput>
|
|
35
40
|
>
|
|
36
41
|
>;
|
|
37
42
|
methods: Record<
|
|
38
43
|
string,
|
|
39
|
-
| ((
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
-
...args: any
|
|
42
|
-
) => Promise<Proof<PublicInput, PublicOutput>>)
|
|
44
|
+
| ((...args: any) => Promise<Proof<PublicInput, PublicOutput>>)
|
|
43
45
|
| ((
|
|
44
46
|
publicInput: PublicInput,
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
47
|
...args: any
|
|
47
48
|
) => Promise<Proof<PublicInput, PublicOutput>>)
|
|
48
49
|
>;
|
|
50
|
+
analyzeMethods: () => Promise<
|
|
51
|
+
Record<string, Awaited<ReturnType<typeof Provable.constraintSystem>>>
|
|
52
|
+
>;
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
export function verifyToMockable<PublicInput, PublicOutput>(
|
|
@@ -60,7 +64,7 @@ export function verifyToMockable<PublicInput, PublicOutput>(
|
|
|
60
64
|
verified = await verify(proof);
|
|
61
65
|
} catch (error: unknown) {
|
|
62
66
|
// silently fail verification
|
|
63
|
-
|
|
67
|
+
log.error(error);
|
|
64
68
|
verified = false;
|
|
65
69
|
}
|
|
66
70
|
|
|
@@ -71,7 +75,10 @@ export function verifyToMockable<PublicInput, PublicOutput>(
|
|
|
71
75
|
};
|
|
72
76
|
}
|
|
73
77
|
|
|
74
|
-
export const MOCK_VERIFICATION_KEY =
|
|
78
|
+
export const MOCK_VERIFICATION_KEY = {
|
|
79
|
+
data: "mock-verification-key",
|
|
80
|
+
hash: Field(0),
|
|
81
|
+
};
|
|
75
82
|
|
|
76
83
|
export function compileToMockable(
|
|
77
84
|
compile: Compile,
|
|
@@ -90,7 +97,7 @@ export function compileToMockable(
|
|
|
90
97
|
|
|
91
98
|
export abstract class ZkProgrammable<
|
|
92
99
|
PublicInput = undefined,
|
|
93
|
-
PublicOutput = void
|
|
100
|
+
PublicOutput = void,
|
|
94
101
|
> {
|
|
95
102
|
public abstract get appChain(): AreProofsEnabled | undefined;
|
|
96
103
|
|
|
@@ -114,7 +121,7 @@ export abstract class ZkProgrammable<
|
|
|
114
121
|
|
|
115
122
|
export interface WithZkProgrammable<
|
|
116
123
|
PublicInput = undefined,
|
|
117
|
-
PublicOutput = void
|
|
124
|
+
PublicOutput = void,
|
|
118
125
|
> {
|
|
119
126
|
zkProgrammable: ZkProgrammable<PublicInput, PublicOutput>;
|
|
120
127
|
}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Proof, DynamicProof } from "o1js";
|
|
2
2
|
import { container } from "tsyringe";
|
|
3
3
|
|
|
4
4
|
import { ProvableMethodExecutionContext } from "./ProvableMethodExecutionContext";
|
|
5
5
|
import type { WithZkProgrammable, ZkProgrammable } from "./ZkProgrammable";
|
|
6
|
-
import { ToFieldable } from "../utils";
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
export type
|
|
7
|
+
// Now, in o1js provable types are normal js objects, therefore any
|
|
8
|
+
export type O1JSPrimitive = object | string | boolean | number;
|
|
9
|
+
export type ArgumentTypes = (
|
|
10
|
+
| O1JSPrimitive
|
|
11
|
+
| Proof<unknown, unknown>
|
|
12
|
+
| DynamicProof<unknown, unknown>
|
|
13
|
+
)[];
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
export type DecoratedMethod = (...args: ArgumentTypes) => unknown;
|
|
15
|
+
export type DecoratedMethod = (...args: ArgumentTypes) => Promise<unknown>;
|
|
13
16
|
|
|
14
17
|
export const MOCK_PROOF = "mock-proof";
|
|
15
18
|
|
|
@@ -19,7 +22,6 @@ export function toProver(
|
|
|
19
22
|
isFirstParameterPublicInput: boolean,
|
|
20
23
|
...args: ArgumentTypes
|
|
21
24
|
) {
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
25
|
return async function prover(this: ZkProgrammable<any, any>) {
|
|
24
26
|
const areProofsEnabled = this.appChain?.areProofsEnabled;
|
|
25
27
|
if (areProofsEnabled ?? false) {
|
|
@@ -28,12 +30,11 @@ export function toProver(
|
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
// create a mock proof by simulating method execution in JS
|
|
31
|
-
const publicOutput = Reflect.apply(simulatedMethod, this, args);
|
|
33
|
+
const publicOutput = await Reflect.apply(simulatedMethod, this, args);
|
|
32
34
|
|
|
33
35
|
return new this.zkProgram.Proof({
|
|
34
36
|
proof: MOCK_PROOF,
|
|
35
37
|
|
|
36
|
-
// eslint-disable-next-line no-warning-comments
|
|
37
38
|
// TODO: provide undefined if public input is not used
|
|
38
39
|
publicInput: isFirstParameterPublicInput ? args[0] : undefined,
|
|
39
40
|
publicOutput,
|
|
@@ -63,17 +64,16 @@ export function provableMethod(
|
|
|
63
64
|
)
|
|
64
65
|
) {
|
|
65
66
|
return <
|
|
66
|
-
|
|
67
|
-
Target extends WithZkProgrammable<any, any> | ZkProgrammable<any, any>
|
|
67
|
+
Target extends WithZkProgrammable<any, any> | ZkProgrammable<any, any>,
|
|
68
68
|
>(
|
|
69
69
|
target: Target,
|
|
70
70
|
methodName: string,
|
|
71
|
-
descriptor:
|
|
71
|
+
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any> | any>
|
|
72
72
|
) => {
|
|
73
73
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
74
74
|
const simulatedMethod = descriptor.value as DecoratedMethod;
|
|
75
75
|
|
|
76
|
-
descriptor.value = function value(
|
|
76
|
+
descriptor.value = async function value(
|
|
77
77
|
this: ZkProgrammable<unknown, unknown>,
|
|
78
78
|
...args: ArgumentTypes
|
|
79
79
|
) {
|
|
@@ -99,7 +99,6 @@ export function provableMethod(
|
|
|
99
99
|
* or not, execute its simulated (Javascript) version and
|
|
100
100
|
* return the result.
|
|
101
101
|
*/
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
103
102
|
let result: unknown;
|
|
104
103
|
try {
|
|
105
104
|
result = Reflect.apply(simulatedMethod, this, args);
|
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
+
import { injectable, container as tsyringeContainer } from "tsyringe";
|
|
3
|
+
|
|
2
4
|
import {
|
|
3
5
|
BaseModuleInstanceType,
|
|
4
|
-
BaseModuleType,
|
|
5
6
|
ConfigurableModule,
|
|
6
7
|
EventEmitter,
|
|
7
8
|
EventEmittingComponent,
|
|
8
|
-
EventsRecord,
|
|
9
9
|
ModuleContainer,
|
|
10
10
|
ModulesRecord,
|
|
11
|
-
TypedClass,
|
|
12
11
|
} from "../../src";
|
|
13
|
-
import { injectable, container as tsyringeContainer } from "tsyringe";
|
|
14
|
-
import {
|
|
15
|
-
CastToEventsRecord,
|
|
16
|
-
ContainerEvents,
|
|
17
|
-
FlattenedContainerEvents,
|
|
18
|
-
FlattenObject,
|
|
19
|
-
} from "../../src/events/EventEmitterProxy";
|
|
20
12
|
|
|
21
13
|
class TestContainer<
|
|
22
|
-
Modules extends ModulesRecord
|
|
14
|
+
Modules extends ModulesRecord,
|
|
23
15
|
> extends ModuleContainer<Modules> {}
|
|
24
16
|
|
|
25
|
-
|
|
17
|
+
type TestEvents = {
|
|
26
18
|
test: [string];
|
|
27
|
-
}
|
|
19
|
+
};
|
|
28
20
|
|
|
29
21
|
@injectable()
|
|
30
22
|
class TestModule
|
|
@@ -34,9 +26,9 @@ class TestModule
|
|
|
34
26
|
events = new EventEmitter<TestEvents>();
|
|
35
27
|
}
|
|
36
28
|
|
|
37
|
-
|
|
29
|
+
type TestEvents2 = {
|
|
38
30
|
test2: [number];
|
|
39
|
-
}
|
|
31
|
+
};
|
|
40
32
|
|
|
41
33
|
class TestModule2
|
|
42
34
|
extends ConfigurableModule<{}>
|
|
@@ -45,17 +37,6 @@ class TestModule2
|
|
|
45
37
|
events = new EventEmitter<TestEvents2>();
|
|
46
38
|
}
|
|
47
39
|
|
|
48
|
-
type X = {
|
|
49
|
-
test: TypedClass<TestModule>;
|
|
50
|
-
test2: TypedClass<TestModule2>;
|
|
51
|
-
};
|
|
52
|
-
type Y = FlattenObject<ContainerEvents<X>>;
|
|
53
|
-
type Z = CastToEventsRecord<FlattenedContainerEvents<X>>;
|
|
54
|
-
// const y: Y = {
|
|
55
|
-
// test: ["asd"],
|
|
56
|
-
// // test2: [2]
|
|
57
|
-
// }
|
|
58
|
-
|
|
59
40
|
describe("test event propagation", () => {
|
|
60
41
|
it("should propagate events up", () => {
|
|
61
42
|
expect.assertions(1);
|
|
@@ -69,7 +50,7 @@ describe("test event propagation", () => {
|
|
|
69
50
|
|
|
70
51
|
container.configure({
|
|
71
52
|
test: {},
|
|
72
|
-
test2: {}
|
|
53
|
+
test2: {},
|
|
73
54
|
});
|
|
74
55
|
|
|
75
56
|
container.create(() => tsyringeContainer.createChildContainer());
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable max-classes-per-file */
|
|
2
1
|
import "reflect-metadata";
|
|
3
2
|
import { container as tsyringeContainer, inject, injectable } from "tsyringe";
|
|
4
3
|
|
|
@@ -9,10 +8,9 @@ import {
|
|
|
9
8
|
import {
|
|
10
9
|
ModuleContainer,
|
|
11
10
|
ModulesRecord,
|
|
12
|
-
DependenciesFromModules,
|
|
13
11
|
} from "../../src/config/ModuleContainer";
|
|
14
12
|
import { TypedClass } from "../../src/types";
|
|
15
|
-
import {
|
|
13
|
+
import { DependencyFactory } from "../../src";
|
|
16
14
|
|
|
17
15
|
// module container will accept modules that extend this type
|
|
18
16
|
class BaseTestModule<Config> extends ConfigurableModule<Config> {}
|
|
@@ -63,27 +61,12 @@ class OtherTestModule extends BaseTestModule<OtherTestModuleConfig> {
|
|
|
63
61
|
* Showcases a wrongly typed/defined module as
|
|
64
62
|
* per the TestModuleContainer requirements
|
|
65
63
|
*/
|
|
66
|
-
// eslint-disable-next-line
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/no-extraneous-class, @typescript-eslint/no-unused-vars
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
65
|
class WrongTestModule {}
|
|
69
66
|
|
|
70
67
|
class TestModuleContainer<
|
|
71
|
-
Modules extends TestModulesRecord
|
|
72
|
-
> extends ModuleContainer<Modules> {
|
|
73
|
-
public create(childContainerProvider: ChildContainerProvider) {
|
|
74
|
-
super.create(childContainerProvider);
|
|
75
|
-
this.registerDependencyFactories(["TestModule" as any]);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
type inferred = DependenciesFromModules<{
|
|
80
|
-
TestModule: typeof TestModule;
|
|
81
|
-
OtherTestModule: typeof OtherTestModule;
|
|
82
|
-
}>;
|
|
83
|
-
|
|
84
|
-
// const merged2T: merged2 = {
|
|
85
|
-
// dependencyModule1: ""
|
|
86
|
-
// }
|
|
68
|
+
Modules extends TestModulesRecord,
|
|
69
|
+
> extends ModuleContainer<Modules> {}
|
|
87
70
|
|
|
88
71
|
describe("moduleContainer", () => {
|
|
89
72
|
let container: TestModuleContainer<{
|
|
@@ -103,7 +86,7 @@ describe("moduleContainer", () => {
|
|
|
103
86
|
});
|
|
104
87
|
});
|
|
105
88
|
|
|
106
|
-
it
|
|
89
|
+
it("should resolve dependency factory dependencies correctly", () => {
|
|
107
90
|
container.configure({
|
|
108
91
|
TestModule: {
|
|
109
92
|
testConfigProperty,
|
|
@@ -116,7 +99,9 @@ describe("moduleContainer", () => {
|
|
|
116
99
|
|
|
117
100
|
container.create(() => tsyringeContainer.createChildContainer());
|
|
118
101
|
|
|
119
|
-
|
|
102
|
+
// Unfortunately we still need this so that the dependencies are registered
|
|
103
|
+
container.resolve("TestModule");
|
|
104
|
+
const dm = container.resolve("DependencyModule1");
|
|
120
105
|
|
|
121
106
|
expect(dm.x()).toBe("dependency factory works");
|
|
122
107
|
expect(dm.testModule).toBeDefined();
|
|
@@ -150,7 +135,7 @@ describe("moduleContainer", () => {
|
|
|
150
135
|
|
|
151
136
|
expect(testModule.config.testConfigProperty).toBe(testConfigProperty);
|
|
152
137
|
|
|
153
|
-
const dependency = container.resolve("
|
|
138
|
+
const dependency = container.resolve("DependencyModule1");
|
|
154
139
|
dependency.x();
|
|
155
140
|
});
|
|
156
141
|
|
|
@@ -178,7 +163,7 @@ describe("moduleContainer", () => {
|
|
|
178
163
|
|
|
179
164
|
container.create(() => tsyringeContainer.createChildContainer());
|
|
180
165
|
|
|
181
|
-
const config = container.resolve("TestModule")
|
|
166
|
+
const { config } = container.resolve("TestModule");
|
|
182
167
|
|
|
183
168
|
expect(config.testConfigProperty).toBe(3);
|
|
184
169
|
expect(config.testConfigProperty2).toBe(2);
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { beforeEach } from "@jest/globals";
|
|
2
|
-
import { Field
|
|
2
|
+
import { Field } from "o1js";
|
|
3
3
|
|
|
4
4
|
import { createMerkleTree, InMemoryMerkleTreeStorage, log } from "../../src";
|
|
5
5
|
|
|
6
6
|
describe.each([4, 16, 256])("cachedMerkleTree - %s", (height) => {
|
|
7
7
|
class RollupMerkleTree extends createMerkleTree(height) {}
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
9
|
class RollupMerkleTreeWitness extends RollupMerkleTree.WITNESS {}
|
|
9
10
|
|
|
10
11
|
let store: InMemoryMerkleTreeStorage;
|
|
11
12
|
let tree: RollupMerkleTree;
|
|
12
13
|
|
|
13
14
|
beforeEach(() => {
|
|
14
|
-
log.setLevel("
|
|
15
|
+
log.setLevel("INFO");
|
|
15
16
|
|
|
16
17
|
store = new InMemoryMerkleTreeStorage();
|
|
17
18
|
tree = new RollupMerkleTree(store);
|
package/test/tsconfig.json
CHANGED