@proto-kit/common 0.1.1-develop.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/LICENSE.md +201 -0
- package/dist/compiling/AtomicCompileHelper.d.ts +13 -0
- package/dist/compiling/AtomicCompileHelper.d.ts.map +1 -0
- package/dist/compiling/AtomicCompileHelper.js +40 -0
- package/dist/compiling/AtomicCompileHelper.js.map +1 -0
- package/dist/compiling/CompilableModule.d.ts +6 -0
- package/dist/compiling/CompilableModule.d.ts.map +1 -0
- package/dist/compiling/CompilableModule.js +2 -0
- package/dist/compiling/CompilableModule.js.map +1 -0
- package/dist/compiling/CompileRegistry.d.ts +26 -0
- package/dist/compiling/CompileRegistry.d.ts.map +1 -0
- package/dist/compiling/CompileRegistry.js +68 -0
- package/dist/compiling/CompileRegistry.js.map +1 -0
- package/dist/compiling/services/ChildVerificationKeyService.d.ts +10 -0
- package/dist/compiling/services/ChildVerificationKeyService.d.ts.map +1 -0
- package/dist/compiling/services/ChildVerificationKeyService.js +27 -0
- package/dist/compiling/services/ChildVerificationKeyService.js.map +1 -0
- package/dist/config/ChildContainerCreatable.d.ts +5 -0
- package/dist/config/ChildContainerCreatable.d.ts.map +1 -0
- package/dist/config/ChildContainerCreatable.js +2 -0
- package/dist/config/ChildContainerCreatable.js.map +1 -0
- package/dist/config/ChildContainerProvider.d.ts +5 -0
- package/dist/config/ChildContainerProvider.d.ts.map +1 -0
- package/dist/config/ChildContainerProvider.js +2 -0
- package/dist/config/ChildContainerProvider.js.map +1 -0
- package/dist/config/ConfigurableModule.d.ts +25 -0
- package/dist/config/ConfigurableModule.d.ts.map +1 -0
- package/dist/config/ConfigurableModule.js +24 -0
- package/dist/config/ConfigurableModule.js.map +1 -0
- package/dist/config/ModuleContainer.d.ts +162 -0
- package/dist/config/ModuleContainer.d.ts.map +1 -0
- package/dist/config/ModuleContainer.js +289 -0
- package/dist/config/ModuleContainer.js.map +1 -0
- package/dist/config/injectAlias.d.ts +18 -0
- package/dist/config/injectAlias.d.ts.map +1 -0
- package/dist/config/injectAlias.js +47 -0
- package/dist/config/injectAlias.js.map +1 -0
- package/dist/dependencyFactory/DependencyFactory.d.ts +29 -0
- package/dist/dependencyFactory/DependencyFactory.d.ts.map +1 -0
- package/dist/dependencyFactory/DependencyFactory.js +2 -0
- package/dist/dependencyFactory/DependencyFactory.js.map +1 -0
- package/dist/dependencyFactory/injectOptional.d.ts +16 -0
- package/dist/dependencyFactory/injectOptional.d.ts.map +1 -0
- package/dist/dependencyFactory/injectOptional.js +40 -0
- package/dist/dependencyFactory/injectOptional.js.map +1 -0
- package/dist/dummyVerificationKey.d.ts +3 -0
- package/dist/dummyVerificationKey.d.ts.map +1 -0
- package/dist/dummyVerificationKey.js +8 -0
- package/dist/dummyVerificationKey.js.map +1 -0
- package/dist/events/EventEmitter.d.ts +19 -0
- package/dist/events/EventEmitter.d.ts.map +1 -0
- package/dist/events/EventEmitter.js +35 -0
- package/dist/events/EventEmitter.js.map +1 -0
- package/dist/events/EventEmitterProxy.d.ts +18 -0
- package/dist/events/EventEmitterProxy.d.ts.map +1 -0
- package/dist/events/EventEmitterProxy.js +35 -0
- package/dist/events/EventEmitterProxy.js.map +1 -0
- package/dist/events/EventEmittingComponent.d.ts +9 -0
- package/dist/events/EventEmittingComponent.d.ts.map +1 -0
- package/dist/events/EventEmittingComponent.js +2 -0
- package/dist/events/EventEmittingComponent.js.map +1 -0
- package/dist/events/ReplayingSingleUseEventEmitter.d.ts +17 -0
- package/dist/events/ReplayingSingleUseEventEmitter.d.ts.map +1 -0
- package/dist/events/ReplayingSingleUseEventEmitter.js +34 -0
- package/dist/events/ReplayingSingleUseEventEmitter.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +37 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +114 -0
- package/dist/log.js.map +1 -0
- package/dist/trees/InMemoryMerkleTreeStorage.d.ts +11 -0
- package/dist/trees/InMemoryMerkleTreeStorage.d.ts.map +1 -0
- package/dist/trees/InMemoryMerkleTreeStorage.js +13 -0
- package/dist/trees/InMemoryMerkleTreeStorage.js.map +1 -0
- package/dist/trees/MerkleTreeStore.d.ts +5 -0
- package/dist/trees/MerkleTreeStore.d.ts.map +1 -0
- package/dist/trees/MerkleTreeStore.js +2 -0
- package/dist/trees/MerkleTreeStore.js.map +1 -0
- package/dist/trees/MockAsyncMerkleStore.d.ts +9 -0
- package/dist/trees/MockAsyncMerkleStore.d.ts.map +1 -0
- package/dist/trees/MockAsyncMerkleStore.js +20 -0
- package/dist/trees/MockAsyncMerkleStore.js.map +1 -0
- package/dist/trees/RollupMerkleTree.d.ts +147 -0
- package/dist/trees/RollupMerkleTree.d.ts.map +1 -0
- package/dist/trees/RollupMerkleTree.js +218 -0
- package/dist/trees/RollupMerkleTree.js.map +1 -0
- package/dist/trees/VirtualMerkleTreeStore.d.ts +13 -0
- package/dist/trees/VirtualMerkleTreeStore.d.ts.map +1 -0
- package/dist/trees/VirtualMerkleTreeStore.js +18 -0
- package/dist/trees/VirtualMerkleTreeStore.js.map +1 -0
- package/dist/types.d.ts +27 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +48 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +113 -0
- package/dist/utils.js.map +1 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts +54 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts.map +1 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.js +97 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.js.map +1 -0
- package/dist/zkProgrammable/ZkProgrammable.d.ts +40 -0
- package/dist/zkProgrammable/ZkProgrammable.d.ts.map +1 -0
- package/dist/zkProgrammable/ZkProgrammable.js +79 -0
- package/dist/zkProgrammable/ZkProgrammable.js.map +1 -0
- package/dist/zkProgrammable/provableMethod.d.ts +19 -0
- package/dist/zkProgrammable/provableMethod.d.ts.map +1 -0
- package/dist/zkProgrammable/provableMethod.js +71 -0
- package/dist/zkProgrammable/provableMethod.js.map +1 -0
- package/jest.config.cjs +12 -0
- package/package.json +34 -0
- package/src/compiling/AtomicCompileHelper.ts +62 -0
- package/src/compiling/CompilableModule.ts +6 -0
- package/src/compiling/CompileRegistry.ts +78 -0
- package/src/compiling/services/ChildVerificationKeyService.ts +26 -0
- package/src/config/ChildContainerCreatable.ts +5 -0
- package/src/config/ChildContainerProvider.ts +5 -0
- package/src/config/ConfigurableModule.ts +57 -0
- package/src/config/ModuleContainer.ts +487 -0
- package/src/config/injectAlias.ts +70 -0
- package/src/dependencyFactory/DependencyFactory.ts +57 -0
- package/src/dependencyFactory/injectOptional.ts +41 -0
- package/src/dummyVerificationKey.ts +10 -0
- package/src/events/EventEmitter.ts +61 -0
- package/src/events/EventEmitterProxy.ts +83 -0
- package/src/events/EventEmittingComponent.ts +11 -0
- package/src/events/ReplayingSingleUseEventEmitter.ts +42 -0
- package/src/index.ts +25 -0
- package/src/log.ts +143 -0
- package/src/trees/InMemoryMerkleTreeStorage.ts +17 -0
- package/src/trees/MerkleTreeStore.ts +5 -0
- package/src/trees/MockAsyncMerkleStore.ts +30 -0
- package/src/trees/RollupMerkleTree.ts +356 -0
- package/src/trees/VirtualMerkleTreeStore.ts +20 -0
- package/src/types.ts +58 -0
- package/src/utils.ts +200 -0
- package/src/zkProgrammable/ProvableMethodExecutionContext.ts +122 -0
- package/src/zkProgrammable/ZkProgrammable.ts +151 -0
- package/src/zkProgrammable/provableMethod.ts +124 -0
- package/test/config/ContainerEvents.test.ts +67 -0
- package/test/config/ModuleContainer.test.ts +215 -0
- package/test/config/injectAlias.test.ts +28 -0
- package/test/trees/MerkleTree.test.ts +106 -0
- package/test/tsconfig.json +7 -0
- package/test/zkProgrammable/ZkProgrammable.test.ts +304 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Bool, Field } from "o1js";
|
|
2
|
+
import { TypedClass } from "../types";
|
|
3
|
+
import { MerkleTreeStore } from "./MerkleTreeStore";
|
|
4
|
+
declare const StructTemplate_base: (new (value: {
|
|
5
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
6
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
7
|
+
}) => {
|
|
8
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
9
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
10
|
+
}) & {
|
|
11
|
+
_isStruct: true;
|
|
12
|
+
} & Omit<import("o1js/dist/node/lib/provable/types/provable-intf").Provable<{
|
|
13
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
14
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
15
|
+
}, {
|
|
16
|
+
path: bigint[];
|
|
17
|
+
isLeft: boolean[];
|
|
18
|
+
}>, "fromFields"> & {
|
|
19
|
+
fromFields: (fields: import("o1js/dist/node/lib/provable/field").Field[]) => {
|
|
20
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
21
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
22
|
+
};
|
|
23
|
+
} & {
|
|
24
|
+
fromValue: (value: {
|
|
25
|
+
path: import("o1js/dist/node/lib/provable/field").Field[] | bigint[];
|
|
26
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[] | boolean[];
|
|
27
|
+
}) => {
|
|
28
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
29
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
30
|
+
};
|
|
31
|
+
toInput: (x: {
|
|
32
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
33
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
34
|
+
}) => {
|
|
35
|
+
fields?: import("o1js/dist/node/lib/provable/field").Field[] | undefined;
|
|
36
|
+
packed?: [import("o1js/dist/node/lib/provable/field").Field, number][] | undefined;
|
|
37
|
+
};
|
|
38
|
+
toJSON: (x: {
|
|
39
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
40
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
41
|
+
}) => {
|
|
42
|
+
path: string[];
|
|
43
|
+
isLeft: boolean[];
|
|
44
|
+
};
|
|
45
|
+
fromJSON: (x: {
|
|
46
|
+
path: string[];
|
|
47
|
+
isLeft: boolean[];
|
|
48
|
+
}) => {
|
|
49
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
50
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
51
|
+
};
|
|
52
|
+
empty: () => {
|
|
53
|
+
path: import("o1js/dist/node/lib/provable/field").Field[];
|
|
54
|
+
isLeft: import("o1js/dist/node/lib/provable/bool").Bool[];
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
declare class StructTemplate extends StructTemplate_base {
|
|
58
|
+
}
|
|
59
|
+
export interface AbstractMerkleWitness extends StructTemplate {
|
|
60
|
+
height(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Calculates a root depending on the leaf value.
|
|
63
|
+
* @param leaf Value of the leaf node that belongs to this Witness.
|
|
64
|
+
* @returns The calculated root.
|
|
65
|
+
*/
|
|
66
|
+
calculateRoot(hash: Field): Field;
|
|
67
|
+
/**
|
|
68
|
+
* Calculates the index of the leaf node that belongs to this Witness.
|
|
69
|
+
* @returns Index of the leaf.
|
|
70
|
+
*/
|
|
71
|
+
calculateIndex(): Field;
|
|
72
|
+
checkMembership(root: Field, key: Field, value: Field): Bool;
|
|
73
|
+
checkMembershipGetRoots(root: Field, key: Field, value: Field): [Bool, Field, Field];
|
|
74
|
+
toShortenedEntries(): string[];
|
|
75
|
+
}
|
|
76
|
+
export interface AbstractMerkleTree {
|
|
77
|
+
store: MerkleTreeStore;
|
|
78
|
+
readonly leafCount: bigint;
|
|
79
|
+
assertIndexRange(index: bigint): void;
|
|
80
|
+
/**
|
|
81
|
+
* Returns a node which lives at a given index and level.
|
|
82
|
+
* @param level Level of the node.
|
|
83
|
+
* @param index Index of the node.
|
|
84
|
+
* @returns The data of the node.
|
|
85
|
+
*/
|
|
86
|
+
getNode(level: number, index: bigint): Field;
|
|
87
|
+
/**
|
|
88
|
+
* Returns the root of the [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree).
|
|
89
|
+
* @returns The root of the Merkle Tree.
|
|
90
|
+
*/
|
|
91
|
+
getRoot(): Field;
|
|
92
|
+
/**
|
|
93
|
+
* Sets the value of a leaf node at a given index to a given value.
|
|
94
|
+
* @param index Position of the leaf node.
|
|
95
|
+
* @param leaf New value.
|
|
96
|
+
*/
|
|
97
|
+
setLeaf(index: bigint, leaf: Field): void;
|
|
98
|
+
/**
|
|
99
|
+
* Returns the witness (also known as
|
|
100
|
+
* [Merkle Proof or Merkle Witness](https://computersciencewiki.org/index.php/Merkle_proof))
|
|
101
|
+
* for the leaf at the given index.
|
|
102
|
+
* @param index Position of the leaf node.
|
|
103
|
+
* @returns The witness that belongs to the leaf.
|
|
104
|
+
*/
|
|
105
|
+
getWitness(index: bigint): AbstractMerkleWitness;
|
|
106
|
+
/**
|
|
107
|
+
* Fills all leaves of the tree.
|
|
108
|
+
* @param leaves Values to fill the leaves with.
|
|
109
|
+
*/
|
|
110
|
+
fill(leaves: Field[]): void;
|
|
111
|
+
}
|
|
112
|
+
export interface AbstractMerkleTreeClass {
|
|
113
|
+
new (store: MerkleTreeStore): AbstractMerkleTree;
|
|
114
|
+
WITNESS: TypedClass<AbstractMerkleWitness> & typeof StructTemplate & {
|
|
115
|
+
dummy: () => AbstractMerkleWitness;
|
|
116
|
+
};
|
|
117
|
+
HEIGHT: number;
|
|
118
|
+
EMPTY_ROOT: bigint;
|
|
119
|
+
get leafCount(): bigint;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* A [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) is a binary tree in
|
|
123
|
+
* which every leaf is the cryptography hash of a piece of data,
|
|
124
|
+
* and every node is the hash of the concatenation of its two child nodes.
|
|
125
|
+
*
|
|
126
|
+
* A Merkle Tree allows developers to easily and securely verify
|
|
127
|
+
* the integrity of large amounts of data.
|
|
128
|
+
*
|
|
129
|
+
* Take a look at our [documentation](https://docs.minaprotocol.com/en/zkapps)
|
|
130
|
+
* on how to use Merkle Trees in combination with zkApps and
|
|
131
|
+
* zero knowledge programming!
|
|
132
|
+
*
|
|
133
|
+
* Levels are indexed from leaves (level 0) to root (level N - 1).
|
|
134
|
+
*
|
|
135
|
+
* This function takes a height as argument and returns a class
|
|
136
|
+
* that implements a merkletree with that specified height.
|
|
137
|
+
*
|
|
138
|
+
* It also holds the Witness class under tree.WITNESS
|
|
139
|
+
*/
|
|
140
|
+
export declare function createMerkleTree(height: number): AbstractMerkleTreeClass;
|
|
141
|
+
declare const RollupMerkleTree_base: AbstractMerkleTreeClass;
|
|
142
|
+
export declare class RollupMerkleTree extends RollupMerkleTree_base {
|
|
143
|
+
}
|
|
144
|
+
export declare class RollupMerkleTreeWitness extends RollupMerkleTree.WITNESS {
|
|
145
|
+
}
|
|
146
|
+
export {};
|
|
147
|
+
//# sourceMappingURL=RollupMerkleTree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RollupMerkleTree.d.ts","sourceRoot":"","sources":["../../src/trees/RollupMerkleTree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAA8B,MAAM,MAAM,CAAC;AAG/D,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGpD,cAAM,cAA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AC;IAEjE,MAAM,EAAE,MAAM,CAAC;IAEf,UAAU,EAAE,MAAM,CAAC;IAEnB,IAAI,SAAS,IAAI,MAAM,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,uBAAuB,CAkOxE;;AAED,qBAAa,gBAAiB,SAAQ,qBAAqB;CAAG;AAC9D,qBAAa,uBAAwB,SAAQ,gBAAgB,CAAC,OAAO;CAAG"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { Bool, Field, Poseidon, Provable, Struct } from "o1js";
|
|
2
|
+
import { range } from "../utils";
|
|
3
|
+
import { InMemoryMerkleTreeStorage } from "./InMemoryMerkleTreeStorage";
|
|
4
|
+
class StructTemplate extends Struct({
|
|
5
|
+
path: Provable.Array(Field, 0),
|
|
6
|
+
isLeft: Provable.Array(Bool, 0),
|
|
7
|
+
}) {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree) is a binary tree in
|
|
11
|
+
* which every leaf is the cryptography hash of a piece of data,
|
|
12
|
+
* and every node is the hash of the concatenation of its two child nodes.
|
|
13
|
+
*
|
|
14
|
+
* A Merkle Tree allows developers to easily and securely verify
|
|
15
|
+
* the integrity of large amounts of data.
|
|
16
|
+
*
|
|
17
|
+
* Take a look at our [documentation](https://docs.minaprotocol.com/en/zkapps)
|
|
18
|
+
* on how to use Merkle Trees in combination with zkApps and
|
|
19
|
+
* zero knowledge programming!
|
|
20
|
+
*
|
|
21
|
+
* Levels are indexed from leaves (level 0) to root (level N - 1).
|
|
22
|
+
*
|
|
23
|
+
* This function takes a height as argument and returns a class
|
|
24
|
+
* that implements a merkletree with that specified height.
|
|
25
|
+
*
|
|
26
|
+
* It also holds the Witness class under tree.WITNESS
|
|
27
|
+
*/
|
|
28
|
+
export function createMerkleTree(height) {
|
|
29
|
+
var _a;
|
|
30
|
+
/**
|
|
31
|
+
* The {@link BaseMerkleWitness} class defines a circuit-compatible base class
|
|
32
|
+
* for [Merkle Witness'](https://computersciencewiki.org/index.php/Merkle_proof).
|
|
33
|
+
*/
|
|
34
|
+
class RollupMerkleWitness extends Struct({
|
|
35
|
+
path: Provable.Array(Field, height - 1),
|
|
36
|
+
isLeft: Provable.Array(Bool, height - 1),
|
|
37
|
+
}) {
|
|
38
|
+
height() {
|
|
39
|
+
return RollupMerkleWitness.height;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Calculates a root depending on the leaf value.
|
|
43
|
+
* @param leaf Value of the leaf node that belongs to this Witness.
|
|
44
|
+
* @returns The calculated root.
|
|
45
|
+
*/
|
|
46
|
+
calculateRoot(leaf) {
|
|
47
|
+
let hash = leaf;
|
|
48
|
+
const n = this.height();
|
|
49
|
+
for (let index = 1; index < n; ++index) {
|
|
50
|
+
const isLeft = this.isLeft[index - 1];
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
52
|
+
const [left, right] = maybeSwap(isLeft, hash, this.path[index - 1]);
|
|
53
|
+
hash = Poseidon.hash([left, right]);
|
|
54
|
+
}
|
|
55
|
+
return hash;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Calculates the index of the leaf node that belongs to this Witness.
|
|
59
|
+
* @returns Index of the leaf.
|
|
60
|
+
*/
|
|
61
|
+
calculateIndex() {
|
|
62
|
+
let powerOfTwo = Field(1);
|
|
63
|
+
let index = Field(0);
|
|
64
|
+
const n = this.height();
|
|
65
|
+
for (let i = 1; i < n; ++i) {
|
|
66
|
+
index = Provable.if(this.isLeft[i - 1], index, index.add(powerOfTwo));
|
|
67
|
+
powerOfTwo = powerOfTwo.mul(2);
|
|
68
|
+
}
|
|
69
|
+
return index;
|
|
70
|
+
}
|
|
71
|
+
checkMembership(root, key, value) {
|
|
72
|
+
const calculatedRoot = this.calculateRoot(value);
|
|
73
|
+
const calculatedKey = this.calculateIndex();
|
|
74
|
+
// We don't have to range-check the key, because if it would be greater
|
|
75
|
+
// than leafCount, it would not match the computedKey
|
|
76
|
+
key.assertEquals(calculatedKey, "Keys of MerkleWitness does not match");
|
|
77
|
+
return root.equals(calculatedRoot);
|
|
78
|
+
}
|
|
79
|
+
checkMembershipGetRoots(root, key, value) {
|
|
80
|
+
const calculatedRoot = this.calculateRoot(value);
|
|
81
|
+
const calculatedKey = this.calculateIndex();
|
|
82
|
+
key.assertEquals(calculatedKey, "Keys of MerkleWitness does not match");
|
|
83
|
+
return [root.equals(calculatedRoot), root, calculatedRoot];
|
|
84
|
+
}
|
|
85
|
+
toShortenedEntries() {
|
|
86
|
+
return range(0, 5)
|
|
87
|
+
.concat(range(this.height() - 4, this.height()))
|
|
88
|
+
.map((index) => [
|
|
89
|
+
this.path[index].toString(),
|
|
90
|
+
this.isLeft[index].toString(),
|
|
91
|
+
].toString());
|
|
92
|
+
}
|
|
93
|
+
static dummy() {
|
|
94
|
+
return new RollupMerkleWitness({
|
|
95
|
+
isLeft: Array(height - 1).fill(Bool(false)),
|
|
96
|
+
path: Array(height - 1).fill(Field(0)),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
RollupMerkleWitness.height = height;
|
|
101
|
+
return _a = class AbstractRollupMerkleTree {
|
|
102
|
+
static get leafCount() {
|
|
103
|
+
return 2n ** BigInt(AbstractRollupMerkleTree.HEIGHT - 1);
|
|
104
|
+
}
|
|
105
|
+
constructor(store) {
|
|
106
|
+
this.store = store;
|
|
107
|
+
this.zeroes = [0n];
|
|
108
|
+
for (let index = 1; index < AbstractRollupMerkleTree.HEIGHT; index += 1) {
|
|
109
|
+
const previousLevel = Field(this.zeroes[index - 1]);
|
|
110
|
+
this.zeroes.push(Poseidon.hash([previousLevel, previousLevel]).toBigInt());
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
assertIndexRange(index) {
|
|
114
|
+
if (index > this.leafCount) {
|
|
115
|
+
throw new Error("Index greater than maximum leaf number");
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns a node which lives at a given index and level.
|
|
120
|
+
* @param level Level of the node.
|
|
121
|
+
* @param index Index of the node.
|
|
122
|
+
* @returns The data of the node.
|
|
123
|
+
*/
|
|
124
|
+
getNode(level, index) {
|
|
125
|
+
this.assertIndexRange(index);
|
|
126
|
+
return Field(this.store.getNode(index, level) ?? this.zeroes[level]);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Returns the root of the [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree).
|
|
130
|
+
* @returns The root of the Merkle Tree.
|
|
131
|
+
*/
|
|
132
|
+
getRoot() {
|
|
133
|
+
return this.getNode(AbstractRollupMerkleTree.HEIGHT - 1, 0n).toConstant();
|
|
134
|
+
}
|
|
135
|
+
// private in interface
|
|
136
|
+
setNode(level, index, value) {
|
|
137
|
+
this.store.setNode(index, level, value.toBigInt());
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Sets the value of a leaf node at a given index to a given value.
|
|
141
|
+
* @param index Position of the leaf node.
|
|
142
|
+
* @param leaf New value.
|
|
143
|
+
*/
|
|
144
|
+
setLeaf(index, leaf) {
|
|
145
|
+
this.assertIndexRange(index);
|
|
146
|
+
this.setNode(0, index, leaf);
|
|
147
|
+
let currentIndex = index;
|
|
148
|
+
for (let level = 1; level < AbstractRollupMerkleTree.HEIGHT; level += 1) {
|
|
149
|
+
currentIndex /= 2n;
|
|
150
|
+
const left = this.getNode(level - 1, currentIndex * 2n);
|
|
151
|
+
const right = this.getNode(level - 1, currentIndex * 2n + 1n);
|
|
152
|
+
this.setNode(level, currentIndex, Poseidon.hash([left, right]));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Returns the witness (also known as
|
|
157
|
+
* [Merkle Proof or Merkle Witness](https://computersciencewiki.org/index.php/Merkle_proof))
|
|
158
|
+
* for the leaf at the given index.
|
|
159
|
+
* @param index Position of the leaf node.
|
|
160
|
+
* @returns The witness that belongs to the leaf.
|
|
161
|
+
*/
|
|
162
|
+
getWitness(index) {
|
|
163
|
+
this.assertIndexRange(index);
|
|
164
|
+
const path = [];
|
|
165
|
+
const isLefts = [];
|
|
166
|
+
let currentIndex = index;
|
|
167
|
+
for (let level = 0; level < AbstractRollupMerkleTree.HEIGHT - 1; level += 1) {
|
|
168
|
+
const isLeft = currentIndex % 2n === 0n;
|
|
169
|
+
const sibling = this.getNode(level, isLeft ? currentIndex + 1n : currentIndex - 1n);
|
|
170
|
+
isLefts.push(Bool(isLeft));
|
|
171
|
+
path.push(sibling);
|
|
172
|
+
currentIndex /= 2n;
|
|
173
|
+
}
|
|
174
|
+
return new RollupMerkleWitness({
|
|
175
|
+
isLeft: isLefts,
|
|
176
|
+
path,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
// TODO: should this take an optional offset? should it fail if the array is too long?
|
|
180
|
+
/**
|
|
181
|
+
* Fills all leaves of the tree.
|
|
182
|
+
* @param leaves Values to fill the leaves with.
|
|
183
|
+
*/
|
|
184
|
+
fill(leaves) {
|
|
185
|
+
leaves.forEach((value, index) => {
|
|
186
|
+
this.setLeaf(BigInt(index), value);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns the amount of leaf nodes.
|
|
191
|
+
* @returns Amount of leaf nodes.
|
|
192
|
+
*/
|
|
193
|
+
get leafCount() {
|
|
194
|
+
return AbstractRollupMerkleTree.leafCount;
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
_a.HEIGHT = height,
|
|
198
|
+
_a.EMPTY_ROOT = new _a(new InMemoryMerkleTreeStorage())
|
|
199
|
+
.getRoot()
|
|
200
|
+
.toBigInt(),
|
|
201
|
+
_a.WITNESS = RollupMerkleWitness,
|
|
202
|
+
_a;
|
|
203
|
+
}
|
|
204
|
+
export class RollupMerkleTree extends createMerkleTree(256) {
|
|
205
|
+
}
|
|
206
|
+
export class RollupMerkleTreeWitness extends RollupMerkleTree.WITNESS {
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* More efficient version of `maybeSwapBad` which
|
|
210
|
+
* reuses an intermediate variable
|
|
211
|
+
*/
|
|
212
|
+
function maybeSwap(b, x, y) {
|
|
213
|
+
const m = b.toField().mul(x.sub(y)); // b*(x - y)
|
|
214
|
+
const x1 = y.add(m); // y + b*(x - y)
|
|
215
|
+
const y2 = x.sub(m); // x - b*(x - y) = x + b*(y - x)
|
|
216
|
+
return [x1, y2];
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=RollupMerkleTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RollupMerkleTree.js","sourceRoot":"","sources":["../../src/trees/RollupMerkleTree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAIjC,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE,MAAM,cAAe,SAAQ,MAAM,CAAC;IAClC,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;IAC9B,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;CAChC,CAAC;CAAG;AAqFL;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAc;;IAC7C;;;OAGG;IACH,MAAM,mBACJ,SAAQ,MAAM,CAAC;QACb,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;QACvC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC;KACzC,CAAC;QAKK,MAAM;YACX,OAAO,mBA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C,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY;IACjD,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,gBAAgB;IACrC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,gCAAgC;IACrD,OAAO,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MerkleTreeStore } from "./MerkleTreeStore";
|
|
2
|
+
import { InMemoryMerkleTreeStorage } from "./InMemoryMerkleTreeStorage";
|
|
3
|
+
/**
|
|
4
|
+
* A MemoryMerkleTreeStore that, if falls back to a parent store if it
|
|
5
|
+
* has no data
|
|
6
|
+
*/
|
|
7
|
+
export declare class VirtualMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
8
|
+
private readonly parent;
|
|
9
|
+
constructor(parent: MerkleTreeStore);
|
|
10
|
+
getNode(key: bigint, level: number): bigint | undefined;
|
|
11
|
+
setNode(key: bigint, level: number, value: bigint): void;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=VirtualMerkleTreeStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VirtualMerkleTreeStore.d.ts","sourceRoot":"","sources":["../../src/trees/VirtualMerkleTreeStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,yBAAyB;IAChD,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,eAAe;IAIpD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAIvD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;CAGhE"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { InMemoryMerkleTreeStorage } from "./InMemoryMerkleTreeStorage";
|
|
2
|
+
/**
|
|
3
|
+
* A MemoryMerkleTreeStore that, if falls back to a parent store if it
|
|
4
|
+
* has no data
|
|
5
|
+
*/
|
|
6
|
+
export class VirtualMerkleTreeStore extends InMemoryMerkleTreeStorage {
|
|
7
|
+
constructor(parent) {
|
|
8
|
+
super();
|
|
9
|
+
this.parent = parent;
|
|
10
|
+
}
|
|
11
|
+
getNode(key, level) {
|
|
12
|
+
return super.getNode(key, level) ?? this.parent.getNode(key, level);
|
|
13
|
+
}
|
|
14
|
+
setNode(key, level, value) {
|
|
15
|
+
super.setNode(key, level, value);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=VirtualMerkleTreeStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VirtualMerkleTreeStore.js","sourceRoot":"","sources":["../../src/trees/VirtualMerkleTreeStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAExE;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,yBAAyB;IACnE,YAAoC,MAAuB;QACzD,KAAK,EAAE,CAAC;QAD0B,WAAM,GAAN,MAAM,CAAiB;IAE3D,CAAC;IAEM,OAAO,CAAC,GAAW,EAAE,KAAa;QACvC,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACtE,CAAC;IAEM,OAAO,CAAC,GAAW,EAAE,KAAa,EAAE,KAAa;QACtD,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IACnC,CAAC;CACF"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { DynamicProof, Proof, ProofBase, PublicKey } from "o1js";
|
|
2
|
+
export type TypedClass<Class> = new (...args: any[]) => Class;
|
|
3
|
+
export type UnTypedClass = new (...args: any[]) => unknown;
|
|
4
|
+
/**
|
|
5
|
+
* Using simple `keyof Target` would result into the key
|
|
6
|
+
* being `string | number | symbol`, but we want just a `string`
|
|
7
|
+
*/
|
|
8
|
+
export type StringKeyOf<Target extends object> = Extract<keyof Target, string> & string;
|
|
9
|
+
/**
|
|
10
|
+
* Utility type to infer element type from an array type
|
|
11
|
+
*/
|
|
12
|
+
export type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
13
|
+
/**
|
|
14
|
+
* Transforms X | Y => X & Y
|
|
15
|
+
*/
|
|
16
|
+
export type UnionToIntersection<Union> = (Union extends any ? (x: Union) => void : never) extends (x: infer Intersection) => void ? Intersection : never;
|
|
17
|
+
export type MergeObjects<Input extends Record<string, unknown>> = UnionToIntersection<Input[keyof Input]>;
|
|
18
|
+
export type OmitKeys<Record, Keys> = {
|
|
19
|
+
[Key in keyof Record as Key extends Keys ? never : Key]: Record[Key];
|
|
20
|
+
};
|
|
21
|
+
export declare const EMPTY_PUBLICKEY_X: import("o1js/dist/node/lib/provable/field").Field;
|
|
22
|
+
export declare const EMPTY_PUBLICKEY: PublicKey;
|
|
23
|
+
export type OverwriteObjectType<Base, New> = {
|
|
24
|
+
[Key in keyof Base]: Key extends keyof New ? New[Key] : Base[Key];
|
|
25
|
+
} & New;
|
|
26
|
+
export type InferProofBase<ProofType extends Proof<any, any> | DynamicProof<any, any>> = ProofType extends Proof<infer PI, infer PO> ? ProofBase<PI, PO> : ProofType extends DynamicProof<infer PI, infer PO> ? ProofBase<PI, PO> : undefined;
|
|
27
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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MAAM,MAAM,mBAAmB,CAAC,IAAI,EAAE,GAAG,IAAI;KAC1C,GAAG,IAAI,MAAM,IAAI,GAAG,GAAG,SAAS,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC;CAClE,GAAG,GAAG,CAAC;AAER,MAAM,MAAM,cAAc,CACxB,SAAS,SAAS,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,IAE1D,SAAS,SAAS,KAAK,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,GACvC,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC,GACjB,SAAS,SAAS,YAAY,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,GAChD,SAAS,CAAC,EAAE,EAAE,EAAE,CAAC,GACjB,SAAS,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// allows to reference interfaces as 'classes' rather than instances
|
|
2
|
+
import { Bool, Field, PublicKey } from "o1js";
|
|
3
|
+
// Because Publickey.empty() is not usable in combination with real
|
|
4
|
+
// cryptographic operations because it's group evaluation isn't defined in Fp,
|
|
5
|
+
// we use some other arbitrary point which we treat as "empty" in our circuits
|
|
6
|
+
// other arbitrary point
|
|
7
|
+
export const EMPTY_PUBLICKEY_X = Field(4600);
|
|
8
|
+
export const EMPTY_PUBLICKEY = PublicKey.fromObject({
|
|
9
|
+
x: EMPTY_PUBLICKEY_X,
|
|
10
|
+
isOdd: Bool(true),
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,OAAO,EAAE,IAAI,EAAgB,KAAK,EAAoB,SAAS,EAAE,MAAM,MAAM,CAAC;AAmC9E,mEAAmE;AACnE,+EAA+E;AAC/E,8EAA8E;AAC9E,wBAAwB;AACxB,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;AAC7C,MAAM,CAAC,MAAM,eAAe,GAAG,SAAS,CAAC,UAAU,CAAC;IAClD,CAAC,EAAE,iBAAiB;IACpB,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC;CAClB,CAAC,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Field, FlexibleProvablePure, DynamicProof, Proof } from "o1js";
|
|
2
|
+
import { TypedClass } from "./types";
|
|
3
|
+
export declare function requireTrue(condition: boolean, errorOrFunction: Error | (() => Error)): void;
|
|
4
|
+
/**
|
|
5
|
+
* Utility function to split an array of type T into a record <K, T[]> based on a
|
|
6
|
+
* function T => K that determines the key of each record
|
|
7
|
+
*/
|
|
8
|
+
export declare function splitArray<T, K extends string | number>(arr: T[], split: (t: T) => K): Record<K, T[] | undefined>;
|
|
9
|
+
export declare function range(startOrEnd: number, endOrNothing?: number | undefined): number[];
|
|
10
|
+
export declare function reduceSequential<T, U>(array: T[], callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => Promise<U>, initialValue: U): Promise<U>;
|
|
11
|
+
export declare function mapSequential<T, R>(array: T[], f: (element: T, index: number, array: T[]) => Promise<R>): Promise<R[]>;
|
|
12
|
+
/**
|
|
13
|
+
* Computes a dummy value for the given value type.
|
|
14
|
+
*
|
|
15
|
+
* @param valueType - Value type to generate the dummy value for
|
|
16
|
+
* @returns Dummy value for the given value type
|
|
17
|
+
*/
|
|
18
|
+
export declare function dummyValue<Value>(valueType: FlexibleProvablePure<Value>): Value;
|
|
19
|
+
export declare function noop(): void;
|
|
20
|
+
export interface ToFieldable {
|
|
21
|
+
toFields: () => Field[];
|
|
22
|
+
}
|
|
23
|
+
export interface ToFieldableStatic {
|
|
24
|
+
toFields: (value: unknown) => Field[];
|
|
25
|
+
}
|
|
26
|
+
export interface ToJSONableStatic {
|
|
27
|
+
toJSON: (value: unknown) => any;
|
|
28
|
+
}
|
|
29
|
+
export type ProofTypes = typeof Proof<unknown, unknown> | typeof DynamicProof<unknown, unknown>;
|
|
30
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
31
|
+
export declare function filterNonNull<Type>(value: Type | null): value is Type;
|
|
32
|
+
export declare function filterNonUndefined<Type>(value: Type | undefined): value is Type;
|
|
33
|
+
export declare function prefixToField(prefix: string): Field;
|
|
34
|
+
export declare function hashWithPrefix(prefix: string, input: Field[]): import("o1js/dist/node/lib/provable/field").Field;
|
|
35
|
+
export declare function expectDefined<T>(value: T | undefined): asserts value is T;
|
|
36
|
+
type NonMethodKeys<Type> = {
|
|
37
|
+
[Key in keyof Type]: Type[Key] extends Function ? never : Key;
|
|
38
|
+
}[keyof Type];
|
|
39
|
+
export type NonMethods<Type> = Pick<Type, NonMethodKeys<Type>>;
|
|
40
|
+
export declare const MAX_FIELD: import("o1js/dist/node/lib/provable/field").Field;
|
|
41
|
+
/**
|
|
42
|
+
* Returns a boolean indicating whether a given class is a subclass of another class,
|
|
43
|
+
* indicated by the name parameter.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isSubtypeOfName(clas: TypedClass<unknown>, name: string): boolean;
|
|
46
|
+
export declare function safeParseJson<T>(json: string): T;
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,oBAAoB,EAEpB,YAAY,EACZ,KAAK,EACN,MAAM,MAAM,CAAC;AAEd,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,wBAAgB,WAAW,CACzB,SAAS,EAAE,OAAO,EAClB,eAAe,EAAE,KAAK,GAAG,CAAC,MAAM,KAAK,CAAC,GACrC,IAAI,CAMN;AAED;;;GAG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idon, } from "o1js";
|
|
2
|
+
export function requireTrue(condition, errorOrFunction) {
|
|
3
|
+
if (!condition) {
|
|
4
|
+
throw typeof errorOrFunction === "function"
|
|
5
|
+
? errorOrFunction()
|
|
6
|
+
: errorOrFunction;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Utility function to split an array of type T into a record <K, T[]> based on a
|
|
11
|
+
* function T => K that determines the key of each record
|
|
12
|
+
*/
|
|
13
|
+
export function splitArray(arr, split) {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
15
|
+
const record = {};
|
|
16
|
+
arr.forEach((element) => {
|
|
17
|
+
const k = split(element);
|
|
18
|
+
if (record[k] !== undefined) {
|
|
19
|
+
record[k].push(element);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
record[k] = [element];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return record;
|
|
26
|
+
}
|
|
27
|
+
export function range(startOrEnd, endOrNothing) {
|
|
28
|
+
let end = endOrNothing;
|
|
29
|
+
let start = startOrEnd;
|
|
30
|
+
if (end === undefined) {
|
|
31
|
+
end = startOrEnd;
|
|
32
|
+
start = 0;
|
|
33
|
+
}
|
|
34
|
+
return Array.from({ length: end - start }, (ignored, index) => index + start);
|
|
35
|
+
}
|
|
36
|
+
export function reduceSequential(array, callbackfn, initialValue) {
|
|
37
|
+
return array.reduce(async (previousPromise, current, index, arr) => {
|
|
38
|
+
const previous = await previousPromise;
|
|
39
|
+
return await callbackfn(previous, current, index, arr);
|
|
40
|
+
}, Promise.resolve(initialValue));
|
|
41
|
+
}
|
|
42
|
+
export function mapSequential(array, f) {
|
|
43
|
+
return array.reduce(async (r, element, index, a) => {
|
|
44
|
+
const ret = await r;
|
|
45
|
+
const next = await f(element, index, a);
|
|
46
|
+
ret.push(next);
|
|
47
|
+
return ret;
|
|
48
|
+
}, Promise.resolve([]));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Computes a dummy value for the given value type.
|
|
52
|
+
*
|
|
53
|
+
* @param valueType - Value type to generate the dummy value for
|
|
54
|
+
* @returns Dummy value for the given value type
|
|
55
|
+
*/
|
|
56
|
+
export function dummyValue(valueType) {
|
|
57
|
+
const length = valueType.sizeInFields();
|
|
58
|
+
const fields = Array.from({ length }, () => Field(0));
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
60
|
+
return valueType.fromFields(fields);
|
|
61
|
+
}
|
|
62
|
+
export function noop() { }
|
|
63
|
+
export async function sleep(ms) {
|
|
64
|
+
await new Promise((resolve) => {
|
|
65
|
+
setTimeout(resolve, ms);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export function filterNonNull(value) {
|
|
69
|
+
return value !== null;
|
|
70
|
+
}
|
|
71
|
+
export function filterNonUndefined(value) {
|
|
72
|
+
return value !== undefined;
|
|
73
|
+
}
|
|
74
|
+
const encoder = new TextEncoder();
|
|
75
|
+
// Copied from o1js binable.ts:317
|
|
76
|
+
export function prefixToField(prefix) {
|
|
77
|
+
const fieldSize = Field.sizeInBytes;
|
|
78
|
+
if (prefix.length >= fieldSize)
|
|
79
|
+
throw Error("prefix too long");
|
|
80
|
+
const stringBytes = [...encoder.encode(prefix)];
|
|
81
|
+
return Field.fromBytes(stringBytes.concat(Array(fieldSize - stringBytes.length).fill(0)));
|
|
82
|
+
}
|
|
83
|
+
export function hashWithPrefix(prefix, input) {
|
|
84
|
+
const salt = Poseidon.update([Field(0), Field(0), Field(0)], [prefixToField(prefix)]);
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
86
|
+
return Poseidon.update(salt, input)[0];
|
|
87
|
+
}
|
|
88
|
+
// end copy
|
|
89
|
+
export function expectDefined(value) {
|
|
90
|
+
expect(value).toBeDefined();
|
|
91
|
+
}
|
|
92
|
+
export const MAX_FIELD = Field(Field.ORDER - 1n);
|
|
93
|
+
/**
|
|
94
|
+
* Returns a boolean indicating whether a given class is a subclass of another class,
|
|
95
|
+
* indicated by the name parameter.
|
|
96
|
+
*/
|
|
97
|
+
// TODO Change to class reference based comparisons
|
|
98
|
+
export function isSubtypeOfName(clas, name) {
|
|
99
|
+
if (clas === undefined || clas === null) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (clas.name === name) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
106
|
+
return isSubtypeOfName(Object.getPrototypeOf(clas), name);
|
|
107
|
+
}
|
|
108
|
+
// TODO Eventually, replace this by a schema validation library
|
|
109
|
+
export function safeParseJson(json) {
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
111
|
+
return JSON.parse(json);
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EAEL,QAAQ,GAGT,MAAM,MAAM,CAAC;AAId,MAAM,UAAU,WAAW,CACzB,SAAkB,EAClB,eAAsC;IAEtC,IAAI,CAAC,SAAS,EAAE;QACd,MAAM,OAAO,eAAe,KAAK,UAAU;YACzC,CAAC,CAAC,eAAe,EAAE;YACnB,CAAC,CAAC,eAAe,CAAC;KACrB;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CACxB,GAAQ,EACR,KAAkB;IAElB,yEAAyE;IACzE,MAAM,MAAM,GAAG,EAAyB,CAAC;IACzC,GAAG,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QACtB,MAAM,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;QACzB,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;YAC3B,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC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