@silvana-one/mina-utils 1.0.41 → 1.1.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.
@@ -126,7 +126,7 @@ export async function sendTx(params: {
126
126
  )}`
127
127
  );
128
128
  }
129
- if (chain !== "local") {
129
+ if (chain !== "mina:local") {
130
130
  // we still wait for the tx to be included in the block by checking the nonce
131
131
  // even in the case of tx NOT included
132
132
  // because the tx might still be included in the block in the case of devnet instability
@@ -4,5 +4,4 @@ export * from "./fetch.js";
4
4
  export * from "./fields.js";
5
5
  export * from "./fee.js";
6
6
  export * from "./mina.js";
7
- export * from "./indexed-map.js";
8
7
  export * from "./utils.js";
@@ -1,32 +0,0 @@
1
- import { Experimental } from "o1js";
2
- declare const IndexedMerkleMap: typeof import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMap;
3
- type IndexedMerkleMap = Experimental.IndexedMerkleMap;
4
- export interface IndexedMapSerialized {
5
- height: number;
6
- root: string;
7
- length: string;
8
- nodes: string;
9
- sortedLeaves: string;
10
- }
11
- export declare function loadIndexedMerkleMap(params: {
12
- url: string;
13
- type: ReturnType<typeof IndexedMerkleMap>;
14
- timeout?: number;
15
- attempts?: number;
16
- }): Promise<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase>;
17
- export declare function saveIndexedMerkleMap(params: {
18
- map: IndexedMerkleMap;
19
- name?: string;
20
- keyvalues?: {
21
- key: string;
22
- value: string;
23
- }[];
24
- auth: string;
25
- }): Promise<string | undefined>;
26
- export declare function serializeIndexedMap(map: IndexedMerkleMap): IndexedMapSerialized;
27
- export declare function deserializeIndexedMerkleMap(params: {
28
- serializedIndexedMap: IndexedMapSerialized;
29
- type?: ReturnType<typeof IndexedMerkleMap>;
30
- }): InstanceType<ReturnType<typeof IndexedMerkleMap>> | undefined;
31
- export declare function parseIndexedMapSerialized(serializedMap: string): IndexedMapSerialized;
32
- export {};
@@ -1,124 +0,0 @@
1
- import { Experimental, Field } from "o1js";
2
- import { bigintToBase64, bigintFromBase64 } from "./base64.js";
3
- import { sleep } from "./utils.js";
4
- import { pinJSON } from "../storage/pinata.js";
5
- const { IndexedMerkleMap } = Experimental;
6
- export async function loadIndexedMerkleMap(params) {
7
- const { url, type, timeout = 60000, attempts = 5 } = params;
8
- let attempt = 0;
9
- const start = Date.now();
10
- let response = await fetch(url);
11
- while (!response.ok && attempt < attempts && Date.now() - start < timeout) {
12
- attempt++;
13
- await sleep(5000 * attempt); // handle rate limiting
14
- response = await fetch(url);
15
- }
16
- if (!response.ok) {
17
- throw new Error("Failed to fetch IndexedMerkleMap");
18
- }
19
- const json = await response.json();
20
- const serializedIndexedMap = json.map;
21
- if (!serializedIndexedMap)
22
- throw new Error("wrong IndexedMerkleMap json format");
23
- const map = deserializeIndexedMerkleMapInternal({
24
- serializedIndexedMap,
25
- type,
26
- });
27
- if (!map) {
28
- throw new Error("Failed to deserialize whitelist");
29
- }
30
- return map;
31
- }
32
- export async function saveIndexedMerkleMap(params) {
33
- const { map, name = "indexed-map", keyvalues, auth } = params;
34
- const serialized = serializeIndexedMap(map);
35
- const ipfsHash = await pinJSON({
36
- data: { map: serialized },
37
- name,
38
- keyvalues,
39
- auth,
40
- });
41
- return ipfsHash;
42
- }
43
- export function serializeIndexedMap(map) {
44
- return {
45
- height: map.height,
46
- root: map.root.toJSON(),
47
- length: map.length.toJSON(),
48
- nodes: JSON.stringify(map.data.get().nodes, (_, v) => typeof v === "bigint" ? "n" + bigintToBase64(v) : v),
49
- sortedLeaves: JSON.stringify(map.data
50
- .get()
51
- .sortedLeaves.map((v) => [
52
- bigintToBase64(v.key),
53
- bigintToBase64(v.nextKey),
54
- bigintToBase64(v.value),
55
- bigintToBase64(BigInt(v.index)),
56
- ])),
57
- };
58
- }
59
- export function deserializeIndexedMerkleMap(params) {
60
- try {
61
- const { serializedIndexedMap, type } = params;
62
- return deserializeIndexedMerkleMapInternal({
63
- serializedIndexedMap,
64
- type: type ?? IndexedMerkleMap(serializedIndexedMap.height),
65
- });
66
- }
67
- catch (error) {
68
- console.error("Error deserializing map:", error?.message ?? error);
69
- return undefined;
70
- }
71
- }
72
- export function parseIndexedMapSerialized(serializedMap) {
73
- const json = JSON.parse(serializedMap);
74
- if (json.height === undefined ||
75
- json.root === undefined ||
76
- json.length === undefined ||
77
- json.nodes === undefined ||
78
- json.sortedLeaves === undefined)
79
- throw new Error("wrong IndexedMerkleMap json format");
80
- if (typeof json.height !== "number")
81
- throw new Error("wrong IndexedMerkleMap height format");
82
- if (typeof json.root !== "string")
83
- throw new Error("wrong IndexedMerkleMap root format");
84
- if (typeof json.length !== "string")
85
- throw new Error("wrong IndexedMerkleMap length format");
86
- if (typeof json.nodes !== "string")
87
- throw new Error("wrong IndexedMerkleMap nodes format");
88
- if (typeof json.sortedLeaves !== "string")
89
- throw new Error("wrong IndexedMerkleMap sortedLeaves format");
90
- return json;
91
- }
92
- function deserializeIndexedMerkleMapInternal(params) {
93
- const { serializedIndexedMap, type } = params;
94
- const map = new type();
95
- if (serializedIndexedMap.height !== map.height) {
96
- throw new Error("wrong IndexedMap height");
97
- }
98
- const nodes = JSON.parse(serializedIndexedMap.nodes, (_, v) => {
99
- // Check if the value is a string that represents a BigInt
100
- if (typeof v === "string" && v[0] === "n") {
101
- // Remove the first 'n' and convert the string to a BigInt
102
- return bigintFromBase64(v.slice(1));
103
- }
104
- return v;
105
- });
106
- const sortedLeaves = JSON.parse(serializedIndexedMap.sortedLeaves).map((row) => {
107
- return {
108
- key: bigintFromBase64(row[0]),
109
- nextKey: bigintFromBase64(row[1]),
110
- value: bigintFromBase64(row[2]),
111
- index: Number(bigintFromBase64(row[3])),
112
- };
113
- });
114
- map.root = Field.fromJSON(serializedIndexedMap.root);
115
- map.length = Field.fromJSON(serializedIndexedMap.length);
116
- map.data.updateAsProver(() => {
117
- return {
118
- nodes: nodes.map((row) => [...row]),
119
- sortedLeaves: [...sortedLeaves],
120
- };
121
- });
122
- return map;
123
- }
124
- //# sourceMappingURL=indexed-map.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"indexed-map.js","sourceRoot":"","sources":["../../../src/utils/indexed-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,MAAM,EAAE,gBAAgB,EAAE,GAAG,YAAY,CAAC;AAW1C,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,MAK1C;IACC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,GAAG,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC;IAC5D,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACzB,IAAI,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,OAAO,GAAG,QAAQ,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,OAAO,EAAE,CAAC;QAC1E,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,uBAAuB;QACpD,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IACD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IACtD,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACnC,MAAM,oBAAoB,GACxB,IACD,CAAC,GAAG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,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC/B,KAAK,EAAE,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;SACxC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;IACrD,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;IACzD,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE;QAC3B,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;YACxC,YAAY,EAAE,CAAC,GAAG,YAAY,CAAC;SAChC,CAAC;IACJ,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -1,32 +0,0 @@
1
- import { Experimental } from "o1js";
2
- declare const IndexedMerkleMap: typeof import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMap;
3
- type IndexedMerkleMap = Experimental.IndexedMerkleMap;
4
- export interface IndexedMapSerialized {
5
- height: number;
6
- root: string;
7
- length: string;
8
- nodes: string;
9
- sortedLeaves: string;
10
- }
11
- export declare function loadIndexedMerkleMap(params: {
12
- url: string;
13
- type: ReturnType<typeof IndexedMerkleMap>;
14
- timeout?: number;
15
- attempts?: number;
16
- }): Promise<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase>;
17
- export declare function saveIndexedMerkleMap(params: {
18
- map: IndexedMerkleMap;
19
- name?: string;
20
- keyvalues?: {
21
- key: string;
22
- value: string;
23
- }[];
24
- auth: string;
25
- }): Promise<string | undefined>;
26
- export declare function serializeIndexedMap(map: IndexedMerkleMap): IndexedMapSerialized;
27
- export declare function deserializeIndexedMerkleMap(params: {
28
- serializedIndexedMap: IndexedMapSerialized;
29
- type?: ReturnType<typeof IndexedMerkleMap>;
30
- }): InstanceType<ReturnType<typeof IndexedMerkleMap>> | undefined;
31
- export declare function parseIndexedMapSerialized(serializedMap: string): IndexedMapSerialized;
32
- export {};
@@ -1,124 +0,0 @@
1
- import { Experimental, Field } from "o1js";
2
- import { bigintToBase64, bigintFromBase64 } from "./base64.js";
3
- import { sleep } from "./utils.js";
4
- import { pinJSON } from "../storage/pinata.js";
5
- const { IndexedMerkleMap } = Experimental;
6
- export async function loadIndexedMerkleMap(params) {
7
- const { url, type, timeout = 60000, attempts = 5 } = params;
8
- let attempt = 0;
9
- const start = Date.now();
10
- let response = await fetch(url);
11
- while (!response.ok && attempt < attempts && Date.now() - start < timeout) {
12
- attempt++;
13
- await sleep(5000 * attempt); // handle rate limiting
14
- response = await fetch(url);
15
- }
16
- if (!response.ok) {
17
- throw new Error("Failed to fetch IndexedMerkleMap");
18
- }
19
- const json = await response.json();
20
- const serializedIndexedMap = json.map;
21
- if (!serializedIndexedMap)
22
- throw new Error("wrong IndexedMerkleMap json format");
23
- const map = deserializeIndexedMerkleMapInternal({
24
- serializedIndexedMap,
25
- type,
26
- });
27
- if (!map) {
28
- throw new Error("Failed to deserialize whitelist");
29
- }
30
- return map;
31
- }
32
- export async function saveIndexedMerkleMap(params) {
33
- const { map, name = "indexed-map", keyvalues, auth } = params;
34
- const serialized = serializeIndexedMap(map);
35
- const ipfsHash = await pinJSON({
36
- data: { map: serialized },
37
- name,
38
- keyvalues,
39
- auth,
40
- });
41
- return ipfsHash;
42
- }
43
- export function serializeIndexedMap(map) {
44
- return {
45
- height: map.height,
46
- root: map.root.toJSON(),
47
- length: map.length.toJSON(),
48
- nodes: JSON.stringify(map.data.get().nodes, (_, v) => typeof v === "bigint" ? "n" + bigintToBase64(v) : v),
49
- sortedLeaves: JSON.stringify(map.data
50
- .get()
51
- .sortedLeaves.map((v) => [
52
- bigintToBase64(v.key),
53
- bigintToBase64(v.nextKey),
54
- bigintToBase64(v.value),
55
- bigintToBase64(BigInt(v.index)),
56
- ])),
57
- };
58
- }
59
- export function deserializeIndexedMerkleMap(params) {
60
- try {
61
- const { serializedIndexedMap, type } = params;
62
- return deserializeIndexedMerkleMapInternal({
63
- serializedIndexedMap,
64
- type: type ?? IndexedMerkleMap(serializedIndexedMap.height),
65
- });
66
- }
67
- catch (error) {
68
- console.error("Error deserializing map:", error?.message ?? error);
69
- return undefined;
70
- }
71
- }
72
- export function parseIndexedMapSerialized(serializedMap) {
73
- const json = JSON.parse(serializedMap);
74
- if (json.height === undefined ||
75
- json.root === undefined ||
76
- json.length === undefined ||
77
- json.nodes === undefined ||
78
- json.sortedLeaves === undefined)
79
- throw new Error("wrong IndexedMerkleMap json format");
80
- if (typeof json.height !== "number")
81
- throw new Error("wrong IndexedMerkleMap height format");
82
- if (typeof json.root !== "string")
83
- throw new Error("wrong IndexedMerkleMap root format");
84
- if (typeof json.length !== "string")
85
- throw new Error("wrong IndexedMerkleMap length format");
86
- if (typeof json.nodes !== "string")
87
- throw new Error("wrong IndexedMerkleMap nodes format");
88
- if (typeof json.sortedLeaves !== "string")
89
- throw new Error("wrong IndexedMerkleMap sortedLeaves format");
90
- return json;
91
- }
92
- function deserializeIndexedMerkleMapInternal(params) {
93
- const { serializedIndexedMap, type } = params;
94
- const map = new type();
95
- if (serializedIndexedMap.height !== map.height) {
96
- throw new Error("wrong IndexedMap height");
97
- }
98
- const nodes = JSON.parse(serializedIndexedMap.nodes, (_, v) => {
99
- // Check if the value is a string that represents a BigInt
100
- if (typeof v === "string" && v[0] === "n") {
101
- // Remove the first 'n' and convert the string to a BigInt
102
- return bigintFromBase64(v.slice(1));
103
- }
104
- return v;
105
- });
106
- const sortedLeaves = JSON.parse(serializedIndexedMap.sortedLeaves).map((row) => {
107
- return {
108
- key: bigintFromBase64(row[0]),
109
- nextKey: bigintFromBase64(row[1]),
110
- value: bigintFromBase64(row[2]),
111
- index: Number(bigintFromBase64(row[3])),
112
- };
113
- });
114
- map.root = Field.fromJSON(serializedIndexedMap.root);
115
- map.length = Field.fromJSON(serializedIndexedMap.length);
116
- map.data.updateAsProver(() => {
117
- return {
118
- nodes: nodes.map((row) => [...row]),
119
- sortedLeaves: [...sortedLeaves],
120
- };
121
- });
122
- return map;
123
- }
124
- //# sourceMappingURL=indexed-map.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"indexed-map.js","sourceRoot":"","sources":["../../../src/utils/indexed-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,MAAM,EAAE,gBAAgB,EAAE,GAAG,YAAY,CAAC;AAW1C,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,MAK1C;IACC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,GAAG,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC;IAC5D,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACzB,IAAI,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,OAAO,GAAG,QAAQ,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,OAAO,EAAE,CAAC;QAC1E,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,uBAAuB;QACpD,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IACD,IAAI,CAAC,QA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AC,CAAC,GAAG,CACpE,CAAC,GAAQ,EAAE,EAAE;QACX,OAAO;YACL,GAAG,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC7B,OAAO,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACjC,KAAK,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAC/B,KAAK,EAAE,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;SACxC,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;IACrD,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;IACzD,GAAG,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE;QAC3B,OAAO;YACL,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC;YACxC,YAAY,EAAE,CAAC,GAAG,YAAY,CAAC;SAChC,CAAC;IACJ,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -1,170 +0,0 @@
1
- import { Experimental, Field } from "o1js";
2
- import { bigintToBase64, bigintFromBase64 } from "./base64.js";
3
- import { sleep } from "./utils.js";
4
- import { pinJSON } from "../storage/pinata.js";
5
-
6
- const { IndexedMerkleMap } = Experimental;
7
- type IndexedMerkleMap = Experimental.IndexedMerkleMap;
8
-
9
- export interface IndexedMapSerialized {
10
- height: number;
11
- root: string;
12
- length: string;
13
- nodes: string;
14
- sortedLeaves: string;
15
- }
16
-
17
- export async function loadIndexedMerkleMap(params: {
18
- url: string;
19
- type: ReturnType<typeof IndexedMerkleMap>;
20
- timeout?: number;
21
- attempts?: number;
22
- }) {
23
- const { url, type, timeout = 60000, attempts = 5 } = params;
24
- let attempt = 0;
25
- const start = Date.now();
26
- let response = await fetch(url);
27
- while (!response.ok && attempt < attempts && Date.now() - start < timeout) {
28
- attempt++;
29
- await sleep(5000 * attempt); // handle rate limiting
30
- response = await fetch(url);
31
- }
32
- if (!response.ok) {
33
- throw new Error("Failed to fetch IndexedMerkleMap");
34
- }
35
-
36
- const json = await response.json();
37
- const serializedIndexedMap = (
38
- json as unknown as { map: IndexedMapSerialized }
39
- ).map;
40
- if (!serializedIndexedMap)
41
- throw new Error("wrong IndexedMerkleMap json format");
42
- const map = deserializeIndexedMerkleMapInternal({
43
- serializedIndexedMap,
44
- type,
45
- });
46
- if (!map) {
47
- throw new Error("Failed to deserialize whitelist");
48
- }
49
- return map;
50
- }
51
-
52
- export async function saveIndexedMerkleMap(params: {
53
- map: IndexedMerkleMap;
54
- name?: string;
55
- keyvalues?: { key: string; value: string }[];
56
- auth: string;
57
- }): Promise<string | undefined> {
58
- const { map, name = "indexed-map", keyvalues, auth } = params;
59
- const serialized = serializeIndexedMap(map);
60
- const ipfsHash = await pinJSON({
61
- data: { map: serialized },
62
- name,
63
- keyvalues,
64
- auth,
65
- });
66
- return ipfsHash;
67
- }
68
-
69
- export function serializeIndexedMap(
70
- map: IndexedMerkleMap
71
- ): IndexedMapSerialized {
72
- return {
73
- height: map.height,
74
- root: map.root.toJSON(),
75
- length: map.length.toJSON(),
76
- nodes: JSON.stringify(map.data.get().nodes, (_, v) =>
77
- typeof v === "bigint" ? "n" + bigintToBase64(v) : v
78
- ),
79
- sortedLeaves: JSON.stringify(
80
- map.data
81
- .get()
82
- .sortedLeaves.map((v) => [
83
- bigintToBase64(v.key),
84
- bigintToBase64(v.nextKey),
85
- bigintToBase64(v.value),
86
- bigintToBase64(BigInt(v.index)),
87
- ])
88
- ),
89
- };
90
- }
91
-
92
- export function deserializeIndexedMerkleMap(params: {
93
- serializedIndexedMap: IndexedMapSerialized;
94
- type?: ReturnType<typeof IndexedMerkleMap>;
95
- }): InstanceType<ReturnType<typeof IndexedMerkleMap>> | undefined {
96
- try {
97
- const { serializedIndexedMap, type } = params;
98
- return deserializeIndexedMerkleMapInternal({
99
- serializedIndexedMap,
100
- type: type ?? IndexedMerkleMap(serializedIndexedMap.height),
101
- });
102
- } catch (error: any) {
103
- console.error("Error deserializing map:", error?.message ?? error);
104
- return undefined;
105
- }
106
- }
107
-
108
- export function parseIndexedMapSerialized(
109
- serializedMap: string
110
- ): IndexedMapSerialized {
111
- const json = JSON.parse(serializedMap);
112
- if (
113
- json.height === undefined ||
114
- json.root === undefined ||
115
- json.length === undefined ||
116
- json.nodes === undefined ||
117
- json.sortedLeaves === undefined
118
- )
119
- throw new Error("wrong IndexedMerkleMap json format");
120
- if (typeof json.height !== "number")
121
- throw new Error("wrong IndexedMerkleMap height format");
122
- if (typeof json.root !== "string")
123
- throw new Error("wrong IndexedMerkleMap root format");
124
- if (typeof json.length !== "string")
125
- throw new Error("wrong IndexedMerkleMap length format");
126
- if (typeof json.nodes !== "string")
127
- throw new Error("wrong IndexedMerkleMap nodes format");
128
- if (typeof json.sortedLeaves !== "string")
129
- throw new Error("wrong IndexedMerkleMap sortedLeaves format");
130
- return json;
131
- }
132
-
133
- function deserializeIndexedMerkleMapInternal(params: {
134
- serializedIndexedMap: IndexedMapSerialized;
135
- type: ReturnType<typeof IndexedMerkleMap>;
136
- }): InstanceType<ReturnType<typeof IndexedMerkleMap>> {
137
- const { serializedIndexedMap, type } = params;
138
- const map = new type();
139
- if (serializedIndexedMap.height !== map.height) {
140
- throw new Error("wrong IndexedMap height");
141
- }
142
- const nodes = JSON.parse(serializedIndexedMap.nodes, (_, v) => {
143
- // Check if the value is a string that represents a BigInt
144
- if (typeof v === "string" && v[0] === "n") {
145
- // Remove the first 'n' and convert the string to a BigInt
146
- return bigintFromBase64(v.slice(1));
147
- }
148
- return v;
149
- });
150
- const sortedLeaves = JSON.parse(serializedIndexedMap.sortedLeaves).map(
151
- (row: any) => {
152
- return {
153
- key: bigintFromBase64(row[0]),
154
- nextKey: bigintFromBase64(row[1]),
155
- value: bigintFromBase64(row[2]),
156
- index: Number(bigintFromBase64(row[3])),
157
- };
158
- }
159
- );
160
-
161
- map.root = Field.fromJSON(serializedIndexedMap.root);
162
- map.length = Field.fromJSON(serializedIndexedMap.length);
163
- map.data.updateAsProver(() => {
164
- return {
165
- nodes: nodes.map((row: any) => [...row]),
166
- sortedLeaves: [...sortedLeaves],
167
- };
168
- });
169
- return map;
170
- }