@hinkal/common 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/package.json +14 -0
- package/src/EtherumNetwork.d.ts +16 -0
- package/src/EtherumNetwork.js +6 -0
- package/src/EtherumNetwork.js.map +1 -0
- package/src/crypto/index.d.ts +1 -0
- package/src/crypto/index.js +2 -0
- package/src/crypto/index.js.map +1 -0
- package/src/crypto/poseidon.d.ts +7 -0
- package/src/crypto/poseidon.js +21 -0
- package/src/crypto/poseidon.js.map +1 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +8 -0
- package/src/index.js.map +1 -0
- package/src/merkle-tree/MerkleTree.d.ts +74 -0
- package/src/merkle-tree/MerkleTree.js +156 -0
- package/src/merkle-tree/MerkleTree.js.map +1 -0
- package/src/merkle-tree/MerkleTreeIncompleteError.d.ts +3 -0
- package/src/merkle-tree/MerkleTreeIncompleteError.js +7 -0
- package/src/merkle-tree/MerkleTreeIncompleteError.js.map +1 -0
- package/src/merkle-tree/index.d.ts +2 -0
- package/src/merkle-tree/index.js +3 -0
- package/src/merkle-tree/index.js.map +1 -0
- package/src/requireEnv.d.ts +1 -0
- package/src/requireEnv.js +11 -0
- package/src/requireEnv.js.map +1 -0
- package/src/snapshot/AbstractAccessTokenSnapshotService.d.ts +26 -0
- package/src/snapshot/AbstractAccessTokenSnapshotService.js +77 -0
- package/src/snapshot/AbstractAccessTokenSnapshotService.js.map +1 -0
- package/src/snapshot/AbstractCommitmentsSnapshotService.d.ts +29 -0
- package/src/snapshot/AbstractCommitmentsSnapshotService.js +77 -0
- package/src/snapshot/AbstractCommitmentsSnapshotService.js.map +1 -0
- package/src/snapshot/AbstractNullifierSnapshotService.d.ts +21 -0
- package/src/snapshot/AbstractNullifierSnapshotService.js +52 -0
- package/src/snapshot/AbstractNullifierSnapshotService.js.map +1 -0
- package/src/snapshot/AbstractSnapshotService.d.ts +40 -0
- package/src/snapshot/AbstractSnapshotService.js +97 -0
- package/src/snapshot/AbstractSnapshotService.js.map +1 -0
- package/src/snapshot/index.d.ts +4 -0
- package/src/snapshot/index.js +5 -0
- package/src/snapshot/index.js.map +1 -0
- package/src/types/big-intable.types.d.ts +2 -0
- package/src/types/big-intable.types.js +2 -0
- package/src/types/big-intable.types.js.map +1 -0
- package/src/types/commitments.types.d.ts +14 -0
- package/src/types/commitments.types.js +2 -0
- package/src/types/commitments.types.js.map +1 -0
- package/src/types/index.d.ts +2 -0
- package/src/types/index.js +3 -0
- package/src/types/index.js.map +1 -0
- package/src/utils/bigInt.utils.d.ts +3 -0
- package/src/utils/bigInt.utils.js +15 -0
- package/src/utils/bigInt.utils.js.map +1 -0
- package/src/utils/index.d.ts +2 -0
- package/src/utils/index.js +3 -0
- package/src/utils/index.js.map +1 -0
- package/src/utils/to-bigInt.d.ts +2 -0
- package/src/utils/to-bigInt.js +7 -0
- package/src/utils/to-bigInt.js.map +1 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hinkal/common",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"circomlibjs": "0.1.7",
|
|
7
|
+
"ethers": "5.7.2"
|
|
8
|
+
},
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"tslib": "2.5.0"
|
|
11
|
+
},
|
|
12
|
+
"main": "./src/index.js",
|
|
13
|
+
"types": "./src/index.d.ts"
|
|
14
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ContractInterface } from 'ethers';
|
|
2
|
+
export interface ContractData {
|
|
3
|
+
hinkalAddress: string;
|
|
4
|
+
hinkalABI: ContractInterface;
|
|
5
|
+
accessTokenAddress: string;
|
|
6
|
+
accessTokenABI: ContractInterface;
|
|
7
|
+
}
|
|
8
|
+
export interface EthereumNetwork {
|
|
9
|
+
name: string;
|
|
10
|
+
chainId: number;
|
|
11
|
+
rpcUrl: string;
|
|
12
|
+
logoPath?: string;
|
|
13
|
+
supported?: boolean;
|
|
14
|
+
contractData?: ContractData;
|
|
15
|
+
}
|
|
16
|
+
export declare const emptyNetwork: EthereumNetwork;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EtherumNetwork.js","sourceRoot":"","sources":["../../../../../libs/shared/common/src/EtherumNetwork.ts"],"names":[],"mappings":"AAiBA,MAAM,CAAC,MAAM,YAAY,GAAoB;IAC3C,IAAI,EAAE,EAAE;IACR,OAAO,EAAE,CAAC;IACV,MAAM,EAAE,EAAE;CACX,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './poseidon';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/crypto/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const poseidonFunction: (...args: any[]) => bigint;
|
|
2
|
+
/**
|
|
3
|
+
* hashing algorithm implementation
|
|
4
|
+
* @param args rest parameter to take unlimited number of arguments
|
|
5
|
+
* @returns poseidon hash in base 16
|
|
6
|
+
*/
|
|
7
|
+
export declare const poseidonHash: (...args: unknown[]) => string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import { buildPoseidon } from 'circomlibjs';
|
|
3
|
+
import { BigNumber } from 'ethers';
|
|
4
|
+
import { toBigInt } from '../utils';
|
|
5
|
+
const poseidon = await buildPoseidon();
|
|
6
|
+
export const poseidonFunction = (...args) => toBigInt(poseidon.F.toString(poseidon(args)));
|
|
7
|
+
/**
|
|
8
|
+
* hashing algorithm implementation
|
|
9
|
+
* @param args rest parameter to take unlimited number of arguments
|
|
10
|
+
* @returns poseidon hash in base 16
|
|
11
|
+
*/
|
|
12
|
+
// export const poseidonHash = (...args: unknown[]) => {
|
|
13
|
+
// return "0x" + BigInt(poseidon.F.toString(poseidon([...args]))).toString(16);
|
|
14
|
+
// };
|
|
15
|
+
// poseidon([...args]); returns Uint8Array(32)
|
|
16
|
+
// poseidon.F.toString(poseidon([...args])); converts array to decimal string
|
|
17
|
+
// BigInt(poseidon.F.toString(poseidon([...args]))); converts decimal string to BigInt
|
|
18
|
+
// "0x" BigInt(poseidon.F.toString(poseidon([...args]))).toString(16); gives hex
|
|
19
|
+
// conversion from BigInt was giving wrong number
|
|
20
|
+
export const poseidonHash = (...args) => BigNumber.from(poseidon.F.toString(poseidon([...args]))).toHexString();
|
|
21
|
+
//# sourceMappingURL=poseidon.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poseidon.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/crypto/poseidon.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,MAAM,QAAQ,GAAG,MAAM,aAAa,EAAE,CAAC;AAEvC,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAElG;;;;GAIG;AACH,wDAAwD;AACxD,iFAAiF;AACjF,KAAK;AAEL,+CAA+C;AAC/C,8EAA8E;AAC9E,uFAAuF;AACvF,gFAAgF;AAEhF,iDAAiD;AAEjD,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE,CACjD,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC"}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/shared/common/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export type MerkleTreeJson = {
|
|
2
|
+
tree: Record<string, string>;
|
|
3
|
+
count: string;
|
|
4
|
+
index: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Merkle tree implementation that allows the user to insert in specific places in the tree
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare class MerkleTree<T> {
|
|
11
|
+
private hashFunction;
|
|
12
|
+
private levels;
|
|
13
|
+
private defaultNodeValue;
|
|
14
|
+
/**
|
|
15
|
+
* the map where the items are stored
|
|
16
|
+
*/
|
|
17
|
+
private readonly tree;
|
|
18
|
+
/**
|
|
19
|
+
* amount of elements inserted inside the bottom row of the tree
|
|
20
|
+
*/
|
|
21
|
+
private count;
|
|
22
|
+
/**
|
|
23
|
+
* the maximum index inserted into the tree
|
|
24
|
+
* used together with count to make sure that the merkle tree is complete.
|
|
25
|
+
* meaning that there aren't any missing elements between getStartIndex() and index
|
|
26
|
+
*/
|
|
27
|
+
private index;
|
|
28
|
+
static createWithData<T>(tree: Map<bigint, T>, index: bigint, count: bigint, hashFunction: (a: T, b: T) => T, levels: bigint, defaultNodeValue: T): MerkleTree<T>;
|
|
29
|
+
/**
|
|
30
|
+
* @param hashFunction the hashFunction used to fill the upper layers of the merkle tree
|
|
31
|
+
* @param levels the amount of layers in the merkle tree
|
|
32
|
+
* @param defaultNodeValue the default value of an empty node in the merkle tree
|
|
33
|
+
*/
|
|
34
|
+
static create<T>(hashFunction: (a: T, b: T) => T, levels: bigint, defaultNodeValue: T): MerkleTree<T>;
|
|
35
|
+
private constructor();
|
|
36
|
+
/**
|
|
37
|
+
* get starting nodeIndex from which inserts are allowed
|
|
38
|
+
*/
|
|
39
|
+
getStartIndex(): bigint;
|
|
40
|
+
/**
|
|
41
|
+
* implementation of logarithm2 function from merkle contract
|
|
42
|
+
*/
|
|
43
|
+
logarithm2(a: bigint): bigint;
|
|
44
|
+
bigIntMax(a: bigint, b: bigint): bigint;
|
|
45
|
+
/**
|
|
46
|
+
* insert value into the merkle tree
|
|
47
|
+
* @param value the value to insert
|
|
48
|
+
* @param nodeIndex the index to insert the value, you can get starting index from getStartIndex()
|
|
49
|
+
* @return true - if added, false - if already exists
|
|
50
|
+
* @throws will throw RangeError if nodeIndex is less than getStartIndex()
|
|
51
|
+
*/
|
|
52
|
+
insert(value: T, nodeIndex: bigint): boolean;
|
|
53
|
+
private completenessCheck;
|
|
54
|
+
/**
|
|
55
|
+
* get root hash of the merkle tree
|
|
56
|
+
* @returns the hash, if the merkle tree is empty, will return
|
|
57
|
+
* @throws will throw MerkleTreeIncompleteError if there are missing elements in the tree
|
|
58
|
+
*/
|
|
59
|
+
getRootHash(): T | undefined;
|
|
60
|
+
getMerkleData(): Map<bigint, T>;
|
|
61
|
+
getSiblingIndex(index: bigint): bigint;
|
|
62
|
+
/**
|
|
63
|
+
* get sibling hashes needed by main.circom
|
|
64
|
+
* @throws will throw MerkleTreeIncompleteError if there are missing elements in the tree
|
|
65
|
+
*/
|
|
66
|
+
getSiblingHashesForVerification(item: T): bigint[] | T[];
|
|
67
|
+
/**
|
|
68
|
+
* get item's sibling hashes side
|
|
69
|
+
* @returns sibling's indexes
|
|
70
|
+
* @throws will throw MerkleTreeIncompleteError if there are missing elements in the tree
|
|
71
|
+
*/
|
|
72
|
+
getSiblingSides(item: T): bigint[];
|
|
73
|
+
toJSON(): MerkleTreeJson;
|
|
74
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { MerkleTreeIncompleteError } from './MerkleTreeIncompleteError';
|
|
2
|
+
/**
|
|
3
|
+
* Merkle tree implementation that allows the user to insert in specific places in the tree
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export class MerkleTree {
|
|
7
|
+
static createWithData(tree, index, count, hashFunction, levels, defaultNodeValue) {
|
|
8
|
+
const merkleTree = new MerkleTree(hashFunction, levels, defaultNodeValue, tree, index, count);
|
|
9
|
+
merkleTree.completenessCheck();
|
|
10
|
+
return merkleTree;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @param hashFunction the hashFunction used to fill the upper layers of the merkle tree
|
|
14
|
+
* @param levels the amount of layers in the merkle tree
|
|
15
|
+
* @param defaultNodeValue the default value of an empty node in the merkle tree
|
|
16
|
+
*/
|
|
17
|
+
static create(hashFunction, levels, defaultNodeValue) {
|
|
18
|
+
return new MerkleTree(hashFunction, levels, defaultNodeValue);
|
|
19
|
+
}
|
|
20
|
+
constructor(hashFunction, levels, defaultNodeValue, tree, index, count) {
|
|
21
|
+
this.hashFunction = hashFunction;
|
|
22
|
+
this.levels = levels;
|
|
23
|
+
this.defaultNodeValue = defaultNodeValue;
|
|
24
|
+
this.tree = tree ?? new Map();
|
|
25
|
+
this.count = count ?? 0n;
|
|
26
|
+
this.index = index ?? 2n ** (levels - 1n);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* get starting nodeIndex from which inserts are allowed
|
|
30
|
+
*/
|
|
31
|
+
getStartIndex() {
|
|
32
|
+
return 2n ** (this.levels - 1n);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* implementation of logarithm2 function from merkle contract
|
|
36
|
+
*/
|
|
37
|
+
logarithm2(a) {
|
|
38
|
+
let i = 0n;
|
|
39
|
+
while (2n ** i < a)
|
|
40
|
+
i += 1n;
|
|
41
|
+
return i;
|
|
42
|
+
}
|
|
43
|
+
bigIntMax(a, b) {
|
|
44
|
+
return a > b ? a : b;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* insert value into the merkle tree
|
|
48
|
+
* @param value the value to insert
|
|
49
|
+
* @param nodeIndex the index to insert the value, you can get starting index from getStartIndex()
|
|
50
|
+
* @return true - if added, false - if already exists
|
|
51
|
+
* @throws will throw RangeError if nodeIndex is less than getStartIndex()
|
|
52
|
+
*/
|
|
53
|
+
insert(value, nodeIndex) {
|
|
54
|
+
if (nodeIndex < this.getStartIndex()) {
|
|
55
|
+
throw new RangeError();
|
|
56
|
+
}
|
|
57
|
+
if (this.tree.has(nodeIndex)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
this.count += 1n;
|
|
61
|
+
this.tree.set(nodeIndex, value);
|
|
62
|
+
this.index = this.bigIntMax(nodeIndex + 1n, this.index); // if nodeIndex = index => increment index
|
|
63
|
+
const fullCount = this.index - this.getStartIndex();
|
|
64
|
+
const twoPower = this.logarithm2(fullCount);
|
|
65
|
+
let currentNodeIndex = nodeIndex;
|
|
66
|
+
for (let i = 1n; i <= twoPower; i += 1n) {
|
|
67
|
+
currentNodeIndex /= 2n;
|
|
68
|
+
const result = this.hashFunction(this.tree.get(currentNodeIndex * 2n) || this.defaultNodeValue, this.tree.get(currentNodeIndex * 2n + 1n) || this.defaultNodeValue);
|
|
69
|
+
this.tree.set(currentNodeIndex, result);
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
completenessCheck() {
|
|
74
|
+
if (this.count !== this.index - this.getStartIndex()) {
|
|
75
|
+
throw new MerkleTreeIncompleteError();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* get root hash of the merkle tree
|
|
80
|
+
* @returns the hash, if the merkle tree is empty, will return
|
|
81
|
+
* @throws will throw MerkleTreeIncompleteError if there are missing elements in the tree
|
|
82
|
+
*/
|
|
83
|
+
getRootHash() {
|
|
84
|
+
this.completenessCheck();
|
|
85
|
+
for (let i = 1n; i < 2n ** this.levels; i *= 2n) {
|
|
86
|
+
if (this.tree.get(i)) {
|
|
87
|
+
return this.tree.get(i);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return this.defaultNodeValue;
|
|
91
|
+
}
|
|
92
|
+
getMerkleData() {
|
|
93
|
+
this.completenessCheck();
|
|
94
|
+
return new Map(this.tree);
|
|
95
|
+
}
|
|
96
|
+
getSiblingIndex(index) {
|
|
97
|
+
if (index === 1n)
|
|
98
|
+
return 1n;
|
|
99
|
+
return index % 2n === 1n ? index - 1n : index + 1n;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* get sibling hashes needed by main.circom
|
|
103
|
+
* @throws will throw MerkleTreeIncompleteError if there are missing elements in the tree
|
|
104
|
+
*/
|
|
105
|
+
getSiblingHashesForVerification(item) {
|
|
106
|
+
this.completenessCheck();
|
|
107
|
+
let index;
|
|
108
|
+
for (let i = this.getStartIndex(); i < this.getStartIndex() + this.index; i += 1n) {
|
|
109
|
+
if (this.tree.get(i) === item) {
|
|
110
|
+
index = i;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (index === undefined)
|
|
115
|
+
return new Array(Number(this.levels)).fill(0n);
|
|
116
|
+
const hashes = [];
|
|
117
|
+
while (index !== 0n) {
|
|
118
|
+
hashes.push(this.tree.get(this.getSiblingIndex(index)) || this.defaultNodeValue);
|
|
119
|
+
index /= 2n;
|
|
120
|
+
}
|
|
121
|
+
return hashes;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* get item's sibling hashes side
|
|
125
|
+
* @returns sibling's indexes
|
|
126
|
+
* @throws will throw MerkleTreeIncompleteError if there are missing elements in the tree
|
|
127
|
+
*/
|
|
128
|
+
getSiblingSides(item) {
|
|
129
|
+
this.completenessCheck();
|
|
130
|
+
let index;
|
|
131
|
+
for (let i = this.getStartIndex(); i < this.getStartIndex() + this.index; i += 1n) {
|
|
132
|
+
if (this.tree.get(i) === item) {
|
|
133
|
+
index = i;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (index === undefined)
|
|
138
|
+
return new Array(Number(this.levels)).fill(0n);
|
|
139
|
+
const siblingSides = [];
|
|
140
|
+
while (index !== 0n) {
|
|
141
|
+
const value = index % 2n === 0n ? 0n : 1n; // left = 0, right = 1
|
|
142
|
+
siblingSides.push(value);
|
|
143
|
+
index /= 2n;
|
|
144
|
+
}
|
|
145
|
+
return siblingSides;
|
|
146
|
+
}
|
|
147
|
+
toJSON() {
|
|
148
|
+
const { tree, count, index } = this;
|
|
149
|
+
return {
|
|
150
|
+
tree: Object.fromEntries(tree),
|
|
151
|
+
count: count.toString(),
|
|
152
|
+
index: index.toString(),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=MerkleTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MerkleTree.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/merkle-tree/MerkleTree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AAQxE;;;GAGG;AACH,MAAM,OAAO,UAAU;IAkBd,MAAM,CAAC,cAAc,CAC1B,IAAoB,EACpB,KAAa,EACb,KAAa,EACb,YAA+B,EAC/B,MAAc,EACd,gBAAmB;QAEnB,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,YAAY,EAAE,MAAM,EAAE,gBAAgB,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;QAC9F,UAAU,CAAC,iBAAiB,EAAE,CAAC;QAC/B,OAAO,UAAU,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,MAAM,CAAI,YAA+B,EAAE,MAAc,EAAE,gBAAmB;QAC1F,OAAO,IAAI,UAAU,CAAC,YAAY,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kleTreeIncompleteError.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAClD;QACE,KAAK,CAAC,2BAA2B,CAAC,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IAC1C,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/merkle-tree/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,6BAA6B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const requireEnv: (environmentVariableName: string) => string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const requireEnv = (environmentVariableName) => {
|
|
2
|
+
const envVar = process.env[environmentVariableName];
|
|
3
|
+
if (envVar === undefined) {
|
|
4
|
+
console.log(`Error: ${environmentVariableName} environment variable not set`);
|
|
5
|
+
// TODO: Do not exit here
|
|
6
|
+
process.exitCode = 1;
|
|
7
|
+
process.exit();
|
|
8
|
+
}
|
|
9
|
+
return envVar;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=requireEnv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requireEnv.js","sourceRoot":"","sources":["../../../../../libs/shared/common/src/requireEnv.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,uBAA+B,EAAU,EAAE;IACpE,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IACpD,IAAI,MAAM,KAAK,SAAS,EAAE;QACxB,OAAO,CAAC,GAAG,CAAC,UAAU,uBAAuB,+BAA+B,CAAC,CAAC;QAC9E,yBAAyB;QACzB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;QACrB,OAAO,CAAC,IAAI,EAAE,CAAC;KAChB;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { MerkleTree, MerkleTreeJson } from '../merkle-tree';
|
|
3
|
+
import { AccessTokenEvent } from '../types';
|
|
4
|
+
import { AbstractSnapshotService, Snapshot } from './AbstractSnapshotService';
|
|
5
|
+
export type AccessTokenSnapshotPayload = {
|
|
6
|
+
readonly merkleTree: MerkleTree<bigint>;
|
|
7
|
+
readonly senderAddresses: Set<string>;
|
|
8
|
+
};
|
|
9
|
+
export type AccessTokenSerializedSnapshot = {
|
|
10
|
+
latestBlockNumber?: number;
|
|
11
|
+
merkleTree?: MerkleTreeJson;
|
|
12
|
+
senderAddresses?: string[];
|
|
13
|
+
};
|
|
14
|
+
export declare abstract class AbstractAccessTokenSnapshotService extends AbstractSnapshotService<AccessTokenSnapshotPayload, AccessTokenEvent<bigint>, AccessTokenSerializedSnapshot, AccessTokenEvent> {
|
|
15
|
+
constructor(contract: ethers.Contract, initialBlockNumber: number);
|
|
16
|
+
private _merkleTree;
|
|
17
|
+
private _senderAddresses;
|
|
18
|
+
get merkleTree(): MerkleTree<bigint>;
|
|
19
|
+
get senderAddresses(): Set<string>;
|
|
20
|
+
protected serializeSnapshot(snapshot: Snapshot<AccessTokenSnapshotPayload>): AccessTokenSerializedSnapshot;
|
|
21
|
+
protected deserializeSnapshot(serializedSnapshot: AccessTokenSerializedSnapshot): Snapshot<AccessTokenSnapshotPayload>;
|
|
22
|
+
protected addEvent(event: AccessTokenEvent<bigint>, _blockNumber: number): boolean;
|
|
23
|
+
protected mapEvent(event: AccessTokenEvent): AccessTokenEvent<bigint>;
|
|
24
|
+
protected getSnapshotPayload(): AccessTokenSnapshotPayload;
|
|
25
|
+
protected populateSnapshot({ payload: { merkleTree, senderAddresses }, }: Snapshot<AccessTokenSnapshotPayload>): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { poseidonFunction } from '../crypto';
|
|
2
|
+
import { MerkleTree } from '../merkle-tree';
|
|
3
|
+
import { toBigInt } from '../utils';
|
|
4
|
+
import { AbstractSnapshotService } from './AbstractSnapshotService';
|
|
5
|
+
export class AbstractAccessTokenSnapshotService extends AbstractSnapshotService {
|
|
6
|
+
constructor(contract, initialBlockNumber) {
|
|
7
|
+
super(contract, {
|
|
8
|
+
name: 'NewAccessKeyAdded',
|
|
9
|
+
args: ['accessKey', 'index', 'senderAddress'],
|
|
10
|
+
}, initialBlockNumber);
|
|
11
|
+
}
|
|
12
|
+
get merkleTree() {
|
|
13
|
+
this.requireReady();
|
|
14
|
+
return this._merkleTree;
|
|
15
|
+
}
|
|
16
|
+
get senderAddresses() {
|
|
17
|
+
this.requireReady();
|
|
18
|
+
return this._senderAddresses;
|
|
19
|
+
}
|
|
20
|
+
serializeSnapshot(snapshot) {
|
|
21
|
+
return {
|
|
22
|
+
merkleTree: snapshot.payload.merkleTree.toJSON(),
|
|
23
|
+
senderAddresses: Array.from(snapshot.payload.senderAddresses),
|
|
24
|
+
latestBlockNumber: snapshot.latestBlockNumber,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
deserializeSnapshot(serializedSnapshot) {
|
|
28
|
+
let merkleTree;
|
|
29
|
+
if (serializedSnapshot.latestBlockNumber && serializedSnapshot.merkleTree) {
|
|
30
|
+
const tree = new Map(Object.entries(serializedSnapshot.merkleTree.tree).map(([key, value]) => [
|
|
31
|
+
BigInt(key),
|
|
32
|
+
BigInt(value),
|
|
33
|
+
]));
|
|
34
|
+
merkleTree = MerkleTree.createWithData(tree, toBigInt(serializedSnapshot.merkleTree.index), toBigInt(serializedSnapshot.merkleTree.count), poseidonFunction, 25n, 0n);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
merkleTree = MerkleTree.create(poseidonFunction, 25n, 0n);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
latestBlockNumber: serializedSnapshot.latestBlockNumber ?? 0,
|
|
41
|
+
payload: {
|
|
42
|
+
merkleTree,
|
|
43
|
+
senderAddresses: new Set(serializedSnapshot.senderAddresses ?? []),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
addEvent(event, _blockNumber) {
|
|
48
|
+
try {
|
|
49
|
+
this._merkleTree.insert(event.accessKey, event.index);
|
|
50
|
+
this._senderAddresses.add(event.senderAddress);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
console.error(err);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
mapEvent(event) {
|
|
59
|
+
const { accessKey, index, senderAddress } = event;
|
|
60
|
+
return {
|
|
61
|
+
accessKey: toBigInt(accessKey),
|
|
62
|
+
index: toBigInt(index),
|
|
63
|
+
senderAddress,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
getSnapshotPayload() {
|
|
67
|
+
return {
|
|
68
|
+
merkleTree: this._merkleTree,
|
|
69
|
+
senderAddresses: this._senderAddresses,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
populateSnapshot({ payload: { merkleTree, senderAddresses }, }) {
|
|
73
|
+
this._merkleTree = merkleTree;
|
|
74
|
+
this._senderAddresses = senderAddresses;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=AbstractAccessTokenSnapshotService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractAccessTokenSnapshotService.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/snapshot/AbstractAccessTokenSnapshotService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAkB,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,uBAAuB,EAAY,MAAM,2BAA2B,CAAC;AAa9E,MAAM,OAAgB,kCAAmC,SA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AC;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { MerkleTree, MerkleTreeJson } from '../merkle-tree';
|
|
3
|
+
import { CommitmentEvent } from '../types';
|
|
4
|
+
import { AbstractSnapshotService, Snapshot } from './AbstractSnapshotService';
|
|
5
|
+
export type CommitmentsSnapshotPayload = {
|
|
6
|
+
readonly merkleTree: MerkleTree<bigint>;
|
|
7
|
+
readonly encryptedOutputs: Set<string>;
|
|
8
|
+
};
|
|
9
|
+
export type CommitmentsSerializedSnapshot = {
|
|
10
|
+
latestBlockNumber?: number;
|
|
11
|
+
merkleTree?: MerkleTreeJson;
|
|
12
|
+
encryptedOutputs?: string[];
|
|
13
|
+
};
|
|
14
|
+
export declare abstract class AbstractCommitmentsSnapshotService extends AbstractSnapshotService<CommitmentsSnapshotPayload, CommitmentEvent<bigint>, CommitmentsSerializedSnapshot, CommitmentEvent> {
|
|
15
|
+
constructor(contract: ethers.Contract, initialBlockNumber: number);
|
|
16
|
+
private _merkleTree;
|
|
17
|
+
private _encryptedOutputs;
|
|
18
|
+
get merkleTree(): MerkleTree<bigint>;
|
|
19
|
+
get encryptedOutputs(): Set<string>;
|
|
20
|
+
protected serializeSnapshot(snapshot: Snapshot<CommitmentsSnapshotPayload>): CommitmentsSerializedSnapshot;
|
|
21
|
+
protected deserializeSnapshot(serializedSnapshot: CommitmentsSerializedSnapshot): Snapshot<CommitmentsSnapshotPayload>;
|
|
22
|
+
protected addEvent(event: CommitmentEvent<bigint>, _blockNumber: number): boolean;
|
|
23
|
+
protected mapEvent(event: CommitmentEvent): CommitmentEvent<bigint>;
|
|
24
|
+
protected getSnapshotPayload(): {
|
|
25
|
+
merkleTree: MerkleTree<bigint>;
|
|
26
|
+
encryptedOutputs: Set<string>;
|
|
27
|
+
};
|
|
28
|
+
protected populateSnapshot({ payload: { merkleTree, encryptedOutputs }, }: Snapshot<CommitmentsSnapshotPayload>): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { poseidonFunction } from '../crypto';
|
|
2
|
+
import { MerkleTree } from '../merkle-tree';
|
|
3
|
+
import { toBigInt } from '../utils';
|
|
4
|
+
import { AbstractSnapshotService } from './AbstractSnapshotService';
|
|
5
|
+
export class AbstractCommitmentsSnapshotService extends AbstractSnapshotService {
|
|
6
|
+
constructor(contract, initialBlockNumber) {
|
|
7
|
+
super(contract, {
|
|
8
|
+
name: 'NewCommitment',
|
|
9
|
+
args: ['commitment', 'index', 'encryptedOutput'],
|
|
10
|
+
}, initialBlockNumber);
|
|
11
|
+
}
|
|
12
|
+
get merkleTree() {
|
|
13
|
+
this.requireReady();
|
|
14
|
+
return this._merkleTree;
|
|
15
|
+
}
|
|
16
|
+
get encryptedOutputs() {
|
|
17
|
+
this.requireReady();
|
|
18
|
+
return this._encryptedOutputs;
|
|
19
|
+
}
|
|
20
|
+
serializeSnapshot(snapshot) {
|
|
21
|
+
return {
|
|
22
|
+
merkleTree: snapshot.payload.merkleTree.toJSON(),
|
|
23
|
+
latestBlockNumber: snapshot.latestBlockNumber,
|
|
24
|
+
encryptedOutputs: Array.from(snapshot.payload.encryptedOutputs),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
deserializeSnapshot(serializedSnapshot) {
|
|
28
|
+
let merkleTree;
|
|
29
|
+
if (serializedSnapshot.latestBlockNumber && serializedSnapshot.merkleTree) {
|
|
30
|
+
const tree = new Map(Object.entries(serializedSnapshot.merkleTree.tree).map(([key, value]) => [
|
|
31
|
+
BigInt(key),
|
|
32
|
+
BigInt(value),
|
|
33
|
+
]));
|
|
34
|
+
merkleTree = MerkleTree.createWithData(tree, toBigInt(serializedSnapshot.merkleTree.index), toBigInt(serializedSnapshot.merkleTree.count), poseidonFunction, 25n, 0n);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
merkleTree = MerkleTree.create(poseidonFunction, 25n, 0n);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
latestBlockNumber: serializedSnapshot.latestBlockNumber ?? 0,
|
|
41
|
+
payload: {
|
|
42
|
+
merkleTree,
|
|
43
|
+
encryptedOutputs: new Set(serializedSnapshot.encryptedOutputs || []),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
addEvent(event, _blockNumber) {
|
|
48
|
+
try {
|
|
49
|
+
this._merkleTree.insert(event.commitment, event.index);
|
|
50
|
+
this._encryptedOutputs.add(event.encryptedOutput);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
console.error(err);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
mapEvent(event) {
|
|
59
|
+
const { commitment, index, encryptedOutput } = event;
|
|
60
|
+
return {
|
|
61
|
+
commitment: toBigInt(commitment),
|
|
62
|
+
index: toBigInt(index),
|
|
63
|
+
encryptedOutput,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
getSnapshotPayload() {
|
|
67
|
+
return {
|
|
68
|
+
merkleTree: this._merkleTree,
|
|
69
|
+
encryptedOutputs: this._encryptedOutputs,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
populateSnapshot({ payload: { merkleTree, encryptedOutputs }, }) {
|
|
73
|
+
this._merkleTree = merkleTree;
|
|
74
|
+
this._encryptedOutputs = encryptedOutputs;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=AbstractCommitmentsSnapshotService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractCommitmentsSnapshotService.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/snapshot/AbstractCommitmentsSnapshotService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAkB,MAAM,gBAAgB,CAAC;AAE5D,OAAO,EAAE,QAAQ,EAAE,M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AgB,EAAE,GACJ;QACrC,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,iBAAiB,GAAG,gBAAgB,CAAC;IAC5C,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BigNumber, ethers } from 'ethers';
|
|
2
|
+
import { NullifierEvent } from '../types';
|
|
3
|
+
import { AbstractSnapshotService, Snapshot } from './AbstractSnapshotService';
|
|
4
|
+
export type NullifierSnapshotPayload = {
|
|
5
|
+
readonly nullifiers: Set<string>;
|
|
6
|
+
};
|
|
7
|
+
export type NullifierSerializedSnapshot = {
|
|
8
|
+
latestBlockNumber?: number;
|
|
9
|
+
nullifiers?: string[];
|
|
10
|
+
};
|
|
11
|
+
export declare abstract class AbstractNullifierSnapshotService extends AbstractSnapshotService<NullifierSnapshotPayload, NullifierEvent<string>, NullifierSerializedSnapshot, NullifierEvent<BigNumber>> {
|
|
12
|
+
constructor(contract: ethers.Contract, initialBlockNumber: number);
|
|
13
|
+
private _nullifiers;
|
|
14
|
+
get nullifiers(): Set<string>;
|
|
15
|
+
protected serializeSnapshot(snapshot: Snapshot<NullifierSnapshotPayload>): NullifierSerializedSnapshot;
|
|
16
|
+
protected deserializeSnapshot(serializedSnapshot: NullifierSerializedSnapshot): Snapshot<NullifierSnapshotPayload>;
|
|
17
|
+
protected addEvent(event: NullifierEvent<string>, _blockNumber: number): boolean;
|
|
18
|
+
protected mapEvent(event: NullifierEvent<BigNumber>): NullifierEvent<string>;
|
|
19
|
+
protected getSnapshotPayload(): NullifierSnapshotPayload;
|
|
20
|
+
protected populateSnapshot({ payload: { nullifiers } }: Snapshot<NullifierSnapshotPayload>): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { AbstractSnapshotService } from './AbstractSnapshotService';
|
|
2
|
+
export class AbstractNullifierSnapshotService extends AbstractSnapshotService {
|
|
3
|
+
constructor(contract, initialBlockNumber) {
|
|
4
|
+
super(contract, {
|
|
5
|
+
name: 'Nullified',
|
|
6
|
+
args: ['nullifier'],
|
|
7
|
+
}, initialBlockNumber);
|
|
8
|
+
}
|
|
9
|
+
get nullifiers() {
|
|
10
|
+
this.requireReady();
|
|
11
|
+
return this._nullifiers;
|
|
12
|
+
}
|
|
13
|
+
serializeSnapshot(snapshot) {
|
|
14
|
+
return {
|
|
15
|
+
nullifiers: Array.from(snapshot.payload.nullifiers).map((value) => value.toString()),
|
|
16
|
+
latestBlockNumber: snapshot.latestBlockNumber,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
deserializeSnapshot(serializedSnapshot) {
|
|
20
|
+
return {
|
|
21
|
+
latestBlockNumber: serializedSnapshot.latestBlockNumber ?? 0,
|
|
22
|
+
payload: {
|
|
23
|
+
nullifiers: new Set(serializedSnapshot.nullifiers ?? []),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
addEvent(event, _blockNumber) {
|
|
28
|
+
try {
|
|
29
|
+
this._nullifiers.add(event.nullifier);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
console.error(err);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
mapEvent(event) {
|
|
38
|
+
const { nullifier } = event;
|
|
39
|
+
return {
|
|
40
|
+
nullifier: nullifier.toHexString(),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
getSnapshotPayload() {
|
|
44
|
+
return {
|
|
45
|
+
nullifiers: this._nullifiers,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
populateSnapshot({ payload: { nullifiers } }) {
|
|
49
|
+
this._nullifiers = nullifiers;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=AbstractNullifierSnapshotService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractNullifierSnapshotService.js","sourceRoot":"","sources":["../../../../../../libs/shared/common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AAC;QAC5B,OAAO;YACL,SAAS,EAAE,SAAS,CAAC,WAAW,EAAE;SACnC,CAAC;IACJ,CAAC;IAEkB,kBAAkB;QACnC,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,WAAW;SAC7B,CAAC;IACJ,CAAC;IAEkB,gBAAgB,CAAC,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,EAAsC;QACjG,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;IAChC,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
export interface Snapshot<SnapshotPayloadType> {
|
|
3
|
+
readonly latestBlockNumber: number;
|
|
4
|
+
readonly payload: SnapshotPayloadType;
|
|
5
|
+
}
|
|
6
|
+
export type SnapshotReadyListener<SnapshotPayloadType> = (snapshot: Snapshot<SnapshotPayloadType>) => unknown;
|
|
7
|
+
export type NewEventListener<E> = (event: E) => unknown;
|
|
8
|
+
export interface EventConfig {
|
|
9
|
+
name: string;
|
|
10
|
+
args: string[];
|
|
11
|
+
}
|
|
12
|
+
export declare abstract class AbstractSnapshotService<SnapshotPayloadType, EventType, SerializedSnapshotType = unknown, SerializedEventType = unknown> {
|
|
13
|
+
protected readonly contract: ethers.Contract;
|
|
14
|
+
private readonly eventConfig;
|
|
15
|
+
private _latestBlockNumber;
|
|
16
|
+
private isReady;
|
|
17
|
+
private _onSnapshotReady?;
|
|
18
|
+
private _onNewEvent?;
|
|
19
|
+
constructor(contract: ethers.Contract, eventConfig: EventConfig, initialBlockNumber: number);
|
|
20
|
+
get latestBlockNumber(): number;
|
|
21
|
+
set onSnapshotReady(value: SnapshotReadyListener<SnapshotPayloadType>);
|
|
22
|
+
set onNewEvent(value: NewEventListener<EventType>);
|
|
23
|
+
init(): Promise<void>;
|
|
24
|
+
protected requireReady(): void;
|
|
25
|
+
private emitSnapshotReady;
|
|
26
|
+
private emitNewEvent;
|
|
27
|
+
private retrieveEvents;
|
|
28
|
+
private loadSnapshot;
|
|
29
|
+
protected saveSnapshot(): Promise<unknown>;
|
|
30
|
+
protected abstract addEvent(event: EventType, blockNumber: number): boolean;
|
|
31
|
+
protected abstract getSnapshotPayload(): SnapshotPayloadType;
|
|
32
|
+
protected abstract populateSnapshot(snapshot: Snapshot<SnapshotPayloadType>): unknown;
|
|
33
|
+
protected abstract serializeSnapshot(snapshot: Snapshot<SnapshotPayloadType>): SerializedSnapshotType;
|
|
34
|
+
protected abstract deserializeSnapshot(serializedSnapshot: SerializedSnapshotType): Snapshot<SnapshotPayloadType>;
|
|
35
|
+
protected abstract mapEvent(event: SerializedEventType): EventType;
|
|
36
|
+
protected abstract fetchSnapshot(): Promise<SerializedSnapshotType>;
|
|
37
|
+
protected abstract persistSnapshot(serializedSnapshot: SerializedSnapshotType): Promise<unknown>;
|
|
38
|
+
private listenEvents;
|
|
39
|
+
private handleEvent;
|
|
40
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export class AbstractSnapshotService {
|
|
2
|
+
constructor(contract, eventConfig, initialBlockNumber) {
|
|
3
|
+
this.isReady = false;
|
|
4
|
+
this.handleEvent = async (eventName, ...args) => {
|
|
5
|
+
const { args: eventArgsConfig } = this.eventConfig;
|
|
6
|
+
const { blockNumber } = args[args.length - 1];
|
|
7
|
+
const eventFields = args.slice(0, eventArgsConfig.length);
|
|
8
|
+
const serializedEvent = eventArgsConfig.reduce((result, fieldName, idx) => {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
// eslint-disable-next-line no-param-reassign
|
|
12
|
+
result[fieldName] = eventFields[idx];
|
|
13
|
+
return result;
|
|
14
|
+
}, {});
|
|
15
|
+
this.requireReady();
|
|
16
|
+
if (blockNumber) {
|
|
17
|
+
const event = this.mapEvent(serializedEvent);
|
|
18
|
+
const success = this.addEvent(event, blockNumber);
|
|
19
|
+
if (success) {
|
|
20
|
+
this._latestBlockNumber = blockNumber;
|
|
21
|
+
await this.saveSnapshot();
|
|
22
|
+
this.emitNewEvent(event);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
await this.retrieveEvents(this._latestBlockNumber + 1);
|
|
26
|
+
};
|
|
27
|
+
this.contract = contract;
|
|
28
|
+
this.eventConfig = eventConfig;
|
|
29
|
+
this._latestBlockNumber = initialBlockNumber;
|
|
30
|
+
}
|
|
31
|
+
get latestBlockNumber() {
|
|
32
|
+
return this._latestBlockNumber;
|
|
33
|
+
}
|
|
34
|
+
set onSnapshotReady(value) {
|
|
35
|
+
this._onSnapshotReady = value;
|
|
36
|
+
}
|
|
37
|
+
set onNewEvent(value) {
|
|
38
|
+
this._onNewEvent = value;
|
|
39
|
+
}
|
|
40
|
+
async init() {
|
|
41
|
+
if (this.isReady) {
|
|
42
|
+
throw new Error('Already initialized');
|
|
43
|
+
}
|
|
44
|
+
await this.loadSnapshot();
|
|
45
|
+
this.isReady = true;
|
|
46
|
+
await this.retrieveEvents(this._latestBlockNumber + 1);
|
|
47
|
+
this.emitSnapshotReady(this.getSnapshotPayload());
|
|
48
|
+
this.listenEvents();
|
|
49
|
+
}
|
|
50
|
+
requireReady() {
|
|
51
|
+
if (!this.isReady) {
|
|
52
|
+
throw new Error('Not ready');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
emitSnapshotReady(snapshotPayload) {
|
|
56
|
+
this._onSnapshotReady?.({
|
|
57
|
+
latestBlockNumber: this._latestBlockNumber,
|
|
58
|
+
payload: snapshotPayload,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
emitNewEvent(event) {
|
|
62
|
+
this._onNewEvent?.(event);
|
|
63
|
+
}
|
|
64
|
+
async retrieveEvents(fromBlockNumber) {
|
|
65
|
+
this.requireReady();
|
|
66
|
+
const events = await this.contract.queryFilter(this.contract.filters[this.eventConfig.name](), fromBlockNumber);
|
|
67
|
+
if (events.length > 0) {
|
|
68
|
+
events.forEach((event) => {
|
|
69
|
+
if (!event.args) {
|
|
70
|
+
throw new Error('Wrong event structure');
|
|
71
|
+
}
|
|
72
|
+
const { args } = event;
|
|
73
|
+
const success = this.addEvent(this.mapEvent(args), event.blockNumber);
|
|
74
|
+
if (!success) {
|
|
75
|
+
throw new Error('Failed to retrieve events');
|
|
76
|
+
}
|
|
77
|
+
this._latestBlockNumber = event.blockNumber;
|
|
78
|
+
});
|
|
79
|
+
await this.saveSnapshot();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async loadSnapshot() {
|
|
83
|
+
const snapshot = this.deserializeSnapshot(await this.fetchSnapshot());
|
|
84
|
+
await this.populateSnapshot(snapshot);
|
|
85
|
+
this._latestBlockNumber = snapshot.latestBlockNumber;
|
|
86
|
+
}
|
|
87
|
+
saveSnapshot() {
|
|
88
|
+
return this.persistSnapshot(this.serializeSnapshot({
|
|
89
|
+
latestBlockNumber: this._latestBlockNumber,
|
|
90
|
+
payload: this.getSnapshotPayload(),
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
listenEvents() {
|
|
94
|
+
this.contract.on(this.eventConfig.name, (...args) => this.handleEvent(this.eventConfig.name, ...args));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=AbstractSnapshotService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractSnapshotService.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/snapshot/AbstractSnapshotService.ts"],"names":[],"mappings":"AAeA,MAAM,OAAgB,uBAAuB;IAkB3C,YAAY,QAAyB,EAAE,WAAwB,EAAE,kBAA0B;QANnF,YAAO,GAAG,KAAK,CAAC;QAqGhB,gBAAW,GAAG,KAAK,EAAE,SAAiB,EAAE,GAAG,IAAe,EAAE,EAAE;YACpE,MAAM,EAAE,IAAI,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC,WAAW,CAAC;YACnD,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,C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sCAAsC,CAAC;AACrD,cAAc,sCAAsC,CAAC;AACrD,cAAc,oCAAoC,CAAC;AACnD,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"big-intable.types.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/types/big-intable.types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BigIntable } from './big-intable.types';
|
|
2
|
+
export interface CommitmentEvent<BN = BigIntable> {
|
|
3
|
+
commitment: BN;
|
|
4
|
+
index: BN;
|
|
5
|
+
encryptedOutput: string;
|
|
6
|
+
}
|
|
7
|
+
export interface AccessTokenEvent<BN = BigIntable> {
|
|
8
|
+
accessKey: BN;
|
|
9
|
+
index: BN;
|
|
10
|
+
senderAddress: string;
|
|
11
|
+
}
|
|
12
|
+
export interface NullifierEvent<T> {
|
|
13
|
+
nullifier: T;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commitments.types.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/types/commitments.types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function bigintApplySugar() {
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
// To support serialization through JSON.stringify. Do not call in libraries!
|
|
5
|
+
BigInt.prototype.toJSON = function toJSON() {
|
|
6
|
+
return this.toString();
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export function bigintMax(...values) {
|
|
10
|
+
return values.reduce((max, value) => (value > max ? value : max));
|
|
11
|
+
}
|
|
12
|
+
export function bigintMin(...values) {
|
|
13
|
+
return values.reduce((min, value) => (value < min ? value : min));
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=bigInt.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bigInt.utils.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/utils/bigInt.utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,gBAAgB;IAC9B,6DAA6D;IAC7D,aAAa;IACb,6EAA6E;IAC7E,MAAM,CAAC,SAAS,CAAC,MAAM,GAAG,SAAS,MAAM;QACvC,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,GAAG,MAAgB;IAC3C,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACpE,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,GAAG,MAAgB;IAC3C,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-bigInt.js","sourceRoot":"","sources":["../../../../../../libs/shared/common/src/utils/to-bigInt.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,QAAQ,CAAC,KAAiB;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,UAAU,IAAI,KAAK,EAAE;QACpD,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;KACzB;IACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC"}
|