@silvana-one/storage 0.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.
Files changed (110) hide show
  1. package/README.md +1 -0
  2. package/dist/node/base64/bigint.d.ts +6 -0
  3. package/dist/node/base64/bigint.js +83 -0
  4. package/dist/node/base64/bigint.js.map +1 -0
  5. package/dist/node/base64/field.d.ts +5 -0
  6. package/dist/node/base64/field.js +29 -0
  7. package/dist/node/base64/field.js.map +1 -0
  8. package/dist/node/base64/index.d.ts +2 -0
  9. package/dist/node/base64/index.js +3 -0
  10. package/dist/node/base64/index.js.map +1 -0
  11. package/dist/node/index.cjs +735 -0
  12. package/dist/node/index.d.ts +5 -0
  13. package/dist/node/index.js +6 -0
  14. package/dist/node/index.js.map +1 -0
  15. package/dist/node/indexed-map/index.d.ts +1 -0
  16. package/dist/node/indexed-map/index.js +2 -0
  17. package/dist/node/indexed-map/index.js.map +1 -0
  18. package/dist/node/indexed-map/indexed-map.d.ts +39 -0
  19. package/dist/node/indexed-map/indexed-map.js +125 -0
  20. package/dist/node/indexed-map/indexed-map.js.map +1 -0
  21. package/dist/node/storage/index.d.ts +3 -0
  22. package/dist/node/storage/index.js +4 -0
  23. package/dist/node/storage/index.js.map +1 -0
  24. package/dist/node/storage/ipfs.d.ts +5 -0
  25. package/dist/node/storage/ipfs.js +16 -0
  26. package/dist/node/storage/ipfs.js.map +1 -0
  27. package/dist/node/storage/pinata.d.ts +6 -0
  28. package/dist/node/storage/pinata.js +40 -0
  29. package/dist/node/storage/pinata.js.map +1 -0
  30. package/dist/node/storage/storage.d.ts +78 -0
  31. package/dist/node/storage/storage.js +60 -0
  32. package/dist/node/storage/storage.js.map +1 -0
  33. package/dist/node/util/index.d.ts +1 -0
  34. package/dist/node/util/index.js +2 -0
  35. package/dist/node/util/index.js.map +1 -0
  36. package/dist/node/util/sleep.d.ts +1 -0
  37. package/dist/node/util/sleep.js +4 -0
  38. package/dist/node/util/sleep.js.map +1 -0
  39. package/dist/node/whitelist/index.d.ts +2 -0
  40. package/dist/node/whitelist/index.js +3 -0
  41. package/dist/node/whitelist/index.js.map +1 -0
  42. package/dist/node/whitelist/offchain-map.d.ts +352 -0
  43. package/dist/node/whitelist/offchain-map.js +247 -0
  44. package/dist/node/whitelist/offchain-map.js.map +1 -0
  45. package/dist/node/whitelist/whitelist.d.ts +122 -0
  46. package/dist/node/whitelist/whitelist.js +98 -0
  47. package/dist/node/whitelist/whitelist.js.map +1 -0
  48. package/dist/tsconfig.tsbuildinfo +1 -0
  49. package/dist/tsconfig.web.tsbuildinfo +1 -0
  50. package/dist/web/base64/bigint.d.ts +6 -0
  51. package/dist/web/base64/bigint.js +83 -0
  52. package/dist/web/base64/bigint.js.map +1 -0
  53. package/dist/web/base64/field.d.ts +5 -0
  54. package/dist/web/base64/field.js +29 -0
  55. package/dist/web/base64/field.js.map +1 -0
  56. package/dist/web/base64/index.d.ts +2 -0
  57. package/dist/web/base64/index.js +3 -0
  58. package/dist/web/base64/index.js.map +1 -0
  59. package/dist/web/index.d.ts +5 -0
  60. package/dist/web/index.js +6 -0
  61. package/dist/web/index.js.map +1 -0
  62. package/dist/web/indexed-map/index.d.ts +1 -0
  63. package/dist/web/indexed-map/index.js +2 -0
  64. package/dist/web/indexed-map/index.js.map +1 -0
  65. package/dist/web/indexed-map/indexed-map.d.ts +39 -0
  66. package/dist/web/indexed-map/indexed-map.js +125 -0
  67. package/dist/web/indexed-map/indexed-map.js.map +1 -0
  68. package/dist/web/storage/index.d.ts +3 -0
  69. package/dist/web/storage/index.js +4 -0
  70. package/dist/web/storage/index.js.map +1 -0
  71. package/dist/web/storage/ipfs.d.ts +5 -0
  72. package/dist/web/storage/ipfs.js +16 -0
  73. package/dist/web/storage/ipfs.js.map +1 -0
  74. package/dist/web/storage/pinata.d.ts +6 -0
  75. package/dist/web/storage/pinata.js +40 -0
  76. package/dist/web/storage/pinata.js.map +1 -0
  77. package/dist/web/storage/storage.d.ts +78 -0
  78. package/dist/web/storage/storage.js +60 -0
  79. package/dist/web/storage/storage.js.map +1 -0
  80. package/dist/web/util/index.d.ts +1 -0
  81. package/dist/web/util/index.js +2 -0
  82. package/dist/web/util/index.js.map +1 -0
  83. package/dist/web/util/sleep.d.ts +1 -0
  84. package/dist/web/util/sleep.js +4 -0
  85. package/dist/web/util/sleep.js.map +1 -0
  86. package/dist/web/whitelist/index.d.ts +2 -0
  87. package/dist/web/whitelist/index.js +3 -0
  88. package/dist/web/whitelist/index.js.map +1 -0
  89. package/dist/web/whitelist/offchain-map.d.ts +352 -0
  90. package/dist/web/whitelist/offchain-map.js +247 -0
  91. package/dist/web/whitelist/offchain-map.js.map +1 -0
  92. package/dist/web/whitelist/whitelist.d.ts +122 -0
  93. package/dist/web/whitelist/whitelist.js +98 -0
  94. package/dist/web/whitelist/whitelist.js.map +1 -0
  95. package/package.json +60 -0
  96. package/src/base64/bigint.ts +87 -0
  97. package/src/base64/field.ts +34 -0
  98. package/src/base64/index.ts +2 -0
  99. package/src/index.ts +5 -0
  100. package/src/indexed-map/index.ts +1 -0
  101. package/src/indexed-map/indexed-map.ts +189 -0
  102. package/src/storage/index.ts +3 -0
  103. package/src/storage/ipfs.ts +20 -0
  104. package/src/storage/pinata.ts +56 -0
  105. package/src/storage/storage.ts +65 -0
  106. package/src/util/index.ts +1 -0
  107. package/src/util/sleep.ts +3 -0
  108. package/src/whitelist/index.ts +2 -0
  109. package/src/whitelist/offchain-map.ts +341 -0
  110. package/src/whitelist/whitelist.ts +139 -0
@@ -0,0 +1,352 @@
1
+ import { Field, Option, Bool } from "o1js";
2
+ import { IndexedMapSerialized, IndexedMapSerializedJson } from "../indexed-map/indexed-map.js";
3
+ import { Storage, IpfsHash } from "../storage/storage.js";
4
+ export interface OffchainMapSerialized extends IndexedMapSerializedJson {
5
+ [key: string]: {
6
+ map: IndexedMapSerialized;
7
+ list: {
8
+ key: string;
9
+ value?: string;
10
+ }[];
11
+ data?: object;
12
+ };
13
+ }
14
+ declare const OffchainMap_base: typeof import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase;
15
+ /** Represents the Offchain Map using an Indexed Merkle Map. */
16
+ export declare class OffchainMap extends OffchainMap_base {
17
+ }
18
+ declare const OffchainMapOption_base: import("node_modules/o1js/dist/node/lib/provable/types/provable-intf.js").Provable<Option<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase, {
19
+ root: bigint;
20
+ length: bigint;
21
+ data: {
22
+ nodes: (bigint | undefined)[][];
23
+ sortedLeaves: {
24
+ readonly value: bigint;
25
+ readonly key: bigint;
26
+ readonly nextKey: bigint;
27
+ readonly index: number;
28
+ }[];
29
+ };
30
+ }>, {
31
+ root: bigint;
32
+ length: bigint;
33
+ data: {
34
+ nodes: (bigint | undefined)[][];
35
+ sortedLeaves: {
36
+ readonly value: bigint;
37
+ readonly key: bigint;
38
+ readonly nextKey: bigint;
39
+ readonly index: number;
40
+ }[];
41
+ };
42
+ } | undefined> & (new (option: {
43
+ isSome: Bool;
44
+ value: import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase;
45
+ }) => Option<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase, {
46
+ root: bigint;
47
+ length: bigint;
48
+ data: {
49
+ nodes: (bigint | undefined)[][];
50
+ sortedLeaves: {
51
+ readonly value: bigint;
52
+ readonly key: bigint;
53
+ readonly nextKey: bigint;
54
+ readonly index: number;
55
+ }[];
56
+ };
57
+ }>) & {
58
+ fromValue(value: import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase | {
59
+ root: bigint;
60
+ length: bigint;
61
+ data: {
62
+ nodes: (bigint | undefined)[][];
63
+ sortedLeaves: {
64
+ readonly value: bigint;
65
+ readonly key: bigint;
66
+ readonly nextKey: bigint;
67
+ readonly index: number;
68
+ }[];
69
+ };
70
+ } | {
71
+ isSome: boolean | Bool;
72
+ value: import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase | {
73
+ root: bigint;
74
+ length: bigint;
75
+ data: {
76
+ nodes: (bigint | undefined)[][];
77
+ sortedLeaves: {
78
+ readonly value: bigint;
79
+ readonly key: bigint;
80
+ readonly nextKey: bigint;
81
+ readonly index: number;
82
+ }[];
83
+ };
84
+ };
85
+ } | undefined): Option<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase, {
86
+ root: bigint;
87
+ length: bigint;
88
+ data: {
89
+ nodes: (bigint | undefined)[][];
90
+ sortedLeaves: {
91
+ readonly value: bigint;
92
+ readonly key: bigint;
93
+ readonly nextKey: bigint;
94
+ readonly index: number;
95
+ }[];
96
+ };
97
+ }>;
98
+ from(value?: import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase | {
99
+ root: bigint;
100
+ length: bigint;
101
+ data: {
102
+ nodes: (bigint | undefined)[][];
103
+ sortedLeaves: {
104
+ readonly value: bigint;
105
+ readonly key: bigint;
106
+ readonly nextKey: bigint;
107
+ readonly index: number;
108
+ }[];
109
+ };
110
+ } | undefined): Option<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase, {
111
+ root: bigint;
112
+ length: bigint;
113
+ data: {
114
+ nodes: (bigint | undefined)[][];
115
+ sortedLeaves: {
116
+ readonly value: bigint;
117
+ readonly key: bigint;
118
+ readonly nextKey: bigint;
119
+ readonly index: number;
120
+ }[];
121
+ };
122
+ }>;
123
+ none(): Option<import("node_modules/o1js/dist/node/lib/provable/merkle-tree-indexed.js").IndexedMerkleMapBase, {
124
+ root: bigint;
125
+ length: bigint;
126
+ data: {
127
+ nodes: (bigint | undefined)[][];
128
+ sortedLeaves: {
129
+ readonly value: bigint;
130
+ readonly key: bigint;
131
+ readonly nextKey: bigint;
132
+ readonly index: number;
133
+ }[];
134
+ };
135
+ }>;
136
+ };
137
+ export declare class OffchainMapOption extends OffchainMapOption_base {
138
+ }
139
+ declare const FieldOption_base: Omit<import("node_modules/o1js/dist/node/lib/provable/types/provable-intf.js").Provable<Option<import("node_modules/o1js/dist/node/lib/provable/field.js").Field, bigint>, bigint | undefined>, "fromFields"> & {
140
+ fromFields: (fields: import("node_modules/o1js/dist/node/lib/provable/field.js").Field[]) => Option<import("node_modules/o1js/dist/node/lib/provable/field.js").Field, bigint>;
141
+ } & (new (option: {
142
+ isSome: Bool;
143
+ value: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
144
+ }) => Option<import("node_modules/o1js/dist/node/lib/provable/field.js").Field, bigint>) & {
145
+ fromValue(value: bigint | import("node_modules/o1js/dist/node/lib/provable/field.js").Field | {
146
+ isSome: boolean | Bool;
147
+ value: bigint | import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
148
+ } | undefined): Option<import("node_modules/o1js/dist/node/lib/provable/field.js").Field, bigint>;
149
+ from(value?: bigint | import("node_modules/o1js/dist/node/lib/provable/field.js").Field | undefined): Option<import("node_modules/o1js/dist/node/lib/provable/field.js").Field, bigint>;
150
+ none(): Option<import("node_modules/o1js/dist/node/lib/provable/field.js").Field, bigint>;
151
+ };
152
+ export declare class FieldOption extends FieldOption_base {
153
+ }
154
+ export interface OffChainMapEntry {
155
+ key: Field | bigint | number | string;
156
+ value?: Field | bigint | number | string;
157
+ }
158
+ /**
159
+ * IpfsHash is used when the map is already pinned to IPFS, it should be string with IPFS hash
160
+ */
161
+ export type OffChainMapEntries = OffChainMapEntry[] | IpfsHash;
162
+ declare const OffChainListBase_base: (new (value: {
163
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
164
+ }) => {
165
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
166
+ }) & {
167
+ _isStruct: true;
168
+ } & Omit<import("node_modules/o1js/dist/node/lib/provable/types/provable-intf.js").Provable<{
169
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
170
+ }, {
171
+ root: bigint;
172
+ }>, "fromFields"> & {
173
+ fromFields: (fields: import("node_modules/o1js/dist/node/lib/provable/field.js").Field[]) => {
174
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
175
+ };
176
+ } & {
177
+ fromValue: (value: {
178
+ root: string | number | bigint | import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
179
+ }) => {
180
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
181
+ };
182
+ toInput: (x: {
183
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
184
+ }) => {
185
+ fields?: Field[] | undefined;
186
+ packed?: [Field, number][] | undefined;
187
+ };
188
+ toJSON: (x: {
189
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
190
+ }) => {
191
+ root: string;
192
+ };
193
+ fromJSON: (x: {
194
+ root: string;
195
+ }) => {
196
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
197
+ };
198
+ empty: () => {
199
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
200
+ };
201
+ };
202
+ /**
203
+ * Base class for offchain lists and maps that does not have storage
204
+ */
205
+ export declare class OffChainListBase extends OffChainListBase_base {
206
+ isNone(): Bool;
207
+ isSome(): Bool;
208
+ load(storage: Storage, name?: string): Promise<OffchainMapOption>;
209
+ /**
210
+ * The function fetches a whitelisted amount associated with a given key using a map and returns it
211
+ * as a FieldOption.
212
+ * @param {Field} key - The `key` parameter is of type `Field`,
213
+ * which represents a field element in the context of a cryptographic system.
214
+ * @returns The `getValue` function returns a `Promise` that resolves to a `FieldOption`
215
+ * object. This object contains a `value` property representing the amount retrieved from a map based
216
+ * on the provided key. The `isSome` property indicates whether the value is present or not.
217
+ * The value is not present if the list is NOT empty and the key is NOT in the map.
218
+ * The value is present if the list IS empty or the key IS in the map.
219
+ * The value is present and equals to Field(0) if the list IS empty.
220
+ */
221
+ getValue(key: Field, storage: Storage, name?: string): Promise<FieldOption>;
222
+ static empty(): OffChainListBase;
223
+ /**
224
+ * Creates a new OffchainListBase
225
+ * @param params - The parameters for creating the list.
226
+ * @param params.list - The list of entries to be added to the map.
227
+ * @param params.data - The JSON data that should be added to the IPFS storage that represent the initial data
228
+ * @returns A new `OffChainList` instance.
229
+ */
230
+ static create(params: {
231
+ list: OffChainMapEntry[] | {
232
+ key: string;
233
+ value?: number;
234
+ }[];
235
+ name?: string;
236
+ data?: object;
237
+ }): Promise<{
238
+ listBase: OffChainListBase;
239
+ json: OffchainMapSerialized;
240
+ }>;
241
+ toString(): string;
242
+ static fromString(str: string): OffChainListBase;
243
+ }
244
+ declare const OffChainList_base: (new (value: {
245
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
246
+ storage: Storage;
247
+ }) => {
248
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
249
+ storage: Storage;
250
+ }) & {
251
+ _isStruct: true;
252
+ } & Omit<import("node_modules/o1js/dist/node/lib/provable/types/provable-intf.js").Provable<{
253
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
254
+ storage: Storage;
255
+ }, {
256
+ root: bigint;
257
+ storage: {
258
+ url: bigint[];
259
+ };
260
+ }>, "fromFields"> & {
261
+ fromFields: (fields: import("node_modules/o1js/dist/node/lib/provable/field.js").Field[]) => {
262
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
263
+ storage: Storage;
264
+ };
265
+ } & {
266
+ fromValue: (value: {
267
+ root: string | number | bigint | import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
268
+ storage: Storage | {
269
+ url: import("node_modules/o1js/dist/node/lib/provable/field.js").Field[] | bigint[];
270
+ };
271
+ }) => {
272
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
273
+ storage: Storage;
274
+ };
275
+ toInput: (x: {
276
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
277
+ storage: Storage;
278
+ }) => {
279
+ fields?: Field[] | undefined;
280
+ packed?: [Field, number][] | undefined;
281
+ };
282
+ toJSON: (x: {
283
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
284
+ storage: Storage;
285
+ }) => {
286
+ root: string;
287
+ storage: {
288
+ url: string[];
289
+ };
290
+ };
291
+ fromJSON: (x: {
292
+ root: string;
293
+ storage: {
294
+ url: string[];
295
+ };
296
+ }) => {
297
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
298
+ storage: Storage;
299
+ };
300
+ empty: () => {
301
+ root: import("node_modules/o1js/dist/node/lib/provable/field.js").Field;
302
+ storage: Storage;
303
+ };
304
+ };
305
+ export declare class OffChainList extends OffChainList_base {
306
+ isNone(): Bool;
307
+ isSome(): Bool;
308
+ load(name?: string | undefined): Promise<OffchainMapOption>;
309
+ /**
310
+ * The function fetches a whitelisted amount associated with a given key using a map and returns it
311
+ * as a FieldOption.
312
+ * @param {Field} key - The `key` parameter is of type `Field`,
313
+ * which represents a field element in the context of a cryptographic system.
314
+ * @returns The `getValue` function returns a `Promise` that resolves to a `FieldOption`
315
+ * object. This object contains a `value` property representing the amount retrieved from a map based
316
+ * on the provided key. The `isSome` property indicates whether the value is present or not.
317
+ * The value is not present if the list is NOT empty and the key is NOT in the map.
318
+ * The value is present if the list IS empty or the key IS in the map.
319
+ * The value is present and equals to Field(0) if the list IS empty.
320
+ */
321
+ getValue(key: Field, name?: string | undefined): Promise<FieldOption>;
322
+ static empty(): OffChainList;
323
+ /**
324
+ * Creates a new OffchainList
325
+ * and pins it to IPFS.
326
+ * @param params - The parameters for creating the list.
327
+ * @param params.list - The list of entries to be added to the map.
328
+ * @param params.data - The JSON data that should be added to the IPFS storage that represent the initial data
329
+ * @returns A new `OffChainList` instance.
330
+ */
331
+ static create(params: {
332
+ list: OffChainMapEntry[] | {
333
+ key: string;
334
+ value?: number;
335
+ }[];
336
+ data?: object;
337
+ name?: string;
338
+ filename?: string;
339
+ keyvalues?: object;
340
+ timeout?: number;
341
+ attempts?: number;
342
+ auth?: string;
343
+ pin?: boolean;
344
+ json?: OffchainMapSerialized;
345
+ }): Promise<{
346
+ list: OffChainList;
347
+ json: OffchainMapSerialized;
348
+ }>;
349
+ toString(): string;
350
+ static fromString(str: string): OffChainList;
351
+ }
352
+ export {};
@@ -0,0 +1,247 @@
1
+ import { Experimental, Struct, Field, Option, Provable } from "o1js";
2
+ import { serializeIndexedMap, loadIndexedMerkleMap, } from "../indexed-map/indexed-map.js";
3
+ import { sleep } from "../util/sleep.js";
4
+ import { Storage } from "../storage/storage.js";
5
+ import { createIpfsURL } from "../storage/ipfs.js";
6
+ import { pinJSON } from "../storage/pinata.js";
7
+ const { IndexedMerkleMap } = Experimental;
8
+ const OFFCHAIN_MAP_HEIGHT = 20;
9
+ /** Represents the Offchain Map using an Indexed Merkle Map. */
10
+ export class OffchainMap extends IndexedMerkleMap(OFFCHAIN_MAP_HEIGHT) {
11
+ }
12
+ export class OffchainMapOption extends Option(OffchainMap) {
13
+ }
14
+ export class FieldOption extends Option(Field) {
15
+ }
16
+ /**
17
+ * Base class for offchain lists and maps that does not have storage
18
+ */
19
+ export class OffChainListBase extends Struct({
20
+ /** The root hash of the Merkle tree representing the whitelist. */
21
+ root: Field,
22
+ }) {
23
+ isNone() {
24
+ return this.root.equals(Field(0));
25
+ }
26
+ isSome() {
27
+ return this.isNone().not();
28
+ }
29
+ async load(storage, name = "offchain-map") {
30
+ const isNone = this.isNone();
31
+ const map = await Provable.witnessAsync(OffchainMapOption, async () => {
32
+ if (isNone.toBoolean())
33
+ return OffchainMapOption.none();
34
+ else
35
+ return OffchainMapOption.fromValue(await loadIndexedMerkleMap({
36
+ url: createIpfsURL({ hash: storage.toString() }),
37
+ type: OffchainMap,
38
+ name,
39
+ }));
40
+ });
41
+ isNone.assertEquals(map.isSome.not());
42
+ const root = Provable.if(map.isSome, map.orElse(new OffchainMap()).root, Field(0));
43
+ root.assertEquals(this.root);
44
+ return map;
45
+ }
46
+ /**
47
+ * The function fetches a whitelisted amount associated with a given key using a map and returns it
48
+ * as a FieldOption.
49
+ * @param {Field} key - The `key` parameter is of type `Field`,
50
+ * which represents a field element in the context of a cryptographic system.
51
+ * @returns The `getValue` function returns a `Promise` that resolves to a `FieldOption`
52
+ * object. This object contains a `value` property representing the amount retrieved from a map based
53
+ * on the provided key. The `isSome` property indicates whether the value is present or not.
54
+ * The value is not present if the list is NOT empty and the key is NOT in the map.
55
+ * The value is present if the list IS empty or the key IS in the map.
56
+ * The value is present and equals to Field(0) if the list IS empty.
57
+ */
58
+ async getValue(key, storage, name = "offchain-map") {
59
+ const map = await this.load(storage, name);
60
+ const value = map.orElse(new OffchainMap()).getOption(key);
61
+ const valueField = value.orElse(Field(0));
62
+ return new FieldOption({
63
+ value: valueField,
64
+ isSome: value.isSome.or(this.isNone()),
65
+ });
66
+ }
67
+ static empty() {
68
+ return new OffChainListBase({
69
+ root: Field(0),
70
+ });
71
+ }
72
+ /**
73
+ * Creates a new OffchainListBase
74
+ * @param params - The parameters for creating the list.
75
+ * @param params.list - The list of entries to be added to the map.
76
+ * @param params.data - The JSON data that should be added to the IPFS storage that represent the initial data
77
+ * @returns A new `OffChainList` instance.
78
+ */
79
+ static async create(params) {
80
+ function toField(value) {
81
+ if (!value)
82
+ return Field(0);
83
+ if (typeof value === "string")
84
+ return Field.fromJSON(value);
85
+ if (typeof value === "bigint" || typeof value === "number")
86
+ return Field(value);
87
+ return value;
88
+ }
89
+ const list = params.list.map((item) => ({
90
+ key: toField(item.key),
91
+ value: toField(item.value),
92
+ }));
93
+ const map = new OffchainMap();
94
+ for (const item of list) {
95
+ map.insert(item.key, item.value);
96
+ }
97
+ const serializedMap = serializeIndexedMap(map);
98
+ const json = {
99
+ [params.name ?? "offchain-map"]: {
100
+ map: serializedMap,
101
+ list: list.map((item) => ({
102
+ key: item.key.toJSON(),
103
+ value: item.value?.toJSON(),
104
+ })),
105
+ data: params.data,
106
+ },
107
+ };
108
+ return {
109
+ listBase: new OffChainListBase({
110
+ root: map.root,
111
+ }),
112
+ json,
113
+ };
114
+ }
115
+ toString() {
116
+ return JSON.stringify({ root: this.root.toJSON() }, null, 2);
117
+ }
118
+ static fromString(str) {
119
+ const json = JSON.parse(str);
120
+ return new OffChainListBase({
121
+ root: Field.fromJSON(json.root),
122
+ });
123
+ }
124
+ }
125
+ export class OffChainList extends Struct({
126
+ /** The root hash of the Merkle tree representing the whitelist. */
127
+ root: Field,
128
+ /** Off-chain storage information, typically an IPFS hash pointing to the whitelist data. */
129
+ storage: Storage,
130
+ }) {
131
+ isNone() {
132
+ return this.root.equals(Field(0)).or(this.storage.isEmpty());
133
+ }
134
+ isSome() {
135
+ return this.isNone().not();
136
+ }
137
+ async load(name = "offchain-map") {
138
+ const isNone = this.isNone();
139
+ const map = await Provable.witnessAsync(OffchainMapOption, async () => {
140
+ if (isNone.toBoolean())
141
+ return OffchainMapOption.none();
142
+ else
143
+ return OffchainMapOption.fromValue(await loadIndexedMerkleMap({
144
+ url: createIpfsURL({ hash: this.storage.toString() }),
145
+ type: OffchainMap,
146
+ name,
147
+ }));
148
+ });
149
+ isNone.assertEquals(map.isSome.not());
150
+ const root = Provable.if(map.isSome, map.orElse(new OffchainMap()).root, Field(0));
151
+ root.assertEquals(this.root);
152
+ return map;
153
+ }
154
+ /**
155
+ * The function fetches a whitelisted amount associated with a given key using a map and returns it
156
+ * as a FieldOption.
157
+ * @param {Field} key - The `key` parameter is of type `Field`,
158
+ * which represents a field element in the context of a cryptographic system.
159
+ * @returns The `getValue` function returns a `Promise` that resolves to a `FieldOption`
160
+ * object. This object contains a `value` property representing the amount retrieved from a map based
161
+ * on the provided key. The `isSome` property indicates whether the value is present or not.
162
+ * The value is not present if the list is NOT empty and the key is NOT in the map.
163
+ * The value is present if the list IS empty or the key IS in the map.
164
+ * The value is present and equals to Field(0) if the list IS empty.
165
+ */
166
+ async getValue(key, name = "offchain-map") {
167
+ const map = await this.load(name);
168
+ const value = map.orElse(new OffchainMap()).getOption(key);
169
+ const valueField = value.orElse(Field(0));
170
+ return new FieldOption({
171
+ value: valueField,
172
+ isSome: value.isSome.or(this.isNone()),
173
+ });
174
+ }
175
+ static empty() {
176
+ return new OffChainList({
177
+ root: Field(0),
178
+ storage: Storage.empty(),
179
+ });
180
+ }
181
+ /**
182
+ * Creates a new OffchainList
183
+ * and pins it to IPFS.
184
+ * @param params - The parameters for creating the list.
185
+ * @param params.list - The list of entries to be added to the map.
186
+ * @param params.data - The JSON data that should be added to the IPFS storage that represent the initial data
187
+ * @returns A new `OffChainList` instance.
188
+ */
189
+ static async create(params) {
190
+ const { name = "offchain-map", filename = "offchain-list.json", keyvalues, timeout = 60 * 1000, attempts = 5, auth, pin = true, json: initialJson = {}, } = params;
191
+ const { listBase, json: newJson } = await OffChainListBase.create({
192
+ list: params.list,
193
+ data: params.data,
194
+ name,
195
+ });
196
+ const json = { ...initialJson, ...newJson };
197
+ if (process.env.DEBUG === "true")
198
+ console.log("OffChainList.create:", { json, name, keyvalues });
199
+ if (pin) {
200
+ let attempt = 0;
201
+ const start = Date.now();
202
+ let hash = await pinJSON({
203
+ data: json,
204
+ name: filename,
205
+ keyvalues,
206
+ auth,
207
+ });
208
+ while (!hash && attempt < attempts && Date.now() - start < timeout) {
209
+ attempt++;
210
+ await sleep(5000 * attempt); // handle rate-limits
211
+ hash = await pinJSON({
212
+ data: json,
213
+ name,
214
+ keyvalues,
215
+ auth,
216
+ });
217
+ }
218
+ if (!hash)
219
+ throw new Error("Failed to pin OffchainMap");
220
+ return {
221
+ list: new OffChainList({
222
+ root: listBase.root,
223
+ storage: Storage.fromString(hash),
224
+ }),
225
+ json,
226
+ };
227
+ }
228
+ return {
229
+ list: new OffChainList({
230
+ root: listBase.root,
231
+ storage: Storage.empty(),
232
+ }),
233
+ json,
234
+ };
235
+ }
236
+ toString() {
237
+ return JSON.stringify({ root: this.root.toJSON(), storage: this.storage.toString() }, null, 2);
238
+ }
239
+ static fromString(str) {
240
+ const json = JSON.parse(str);
241
+ return new OffChainList({
242
+ root: Field.fromJSON(json.root),
243
+ storage: Storage.fromString(json.storage),
244
+ });
245
+ }
246
+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