zkcloudworker 0.17.6 → 0.17.7

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.
@@ -3,6 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createIpfsURL = createIpfsURL;
4
4
  function createIpfsURL(params) {
5
5
  let { hash, gateway, apiToken } = params;
6
+ gateway ??=
7
+ process.env.PINATA_IPFS_GATEWAY ??
8
+ process.env.NEXT_PUBLIC_PINATA_IPFS_GATEWAY ??
9
+ process.env.REACT_APP_PINATA_IPFS_GATEWAY;
10
+ apiToken ??=
11
+ process.env.PINATA_GATEWAY_TOKEN ??
12
+ process.env.NEXT_PUBLIC_PINATA_GATEWAY_TOKEN ??
13
+ process.env.REACT_APP_PINATA_GATEWAY_TOKEN;
6
14
  if (!gateway) {
7
15
  gateway = "https://gateway.pinata.cloud/ipfs/";
8
16
  }
@@ -1,6 +1,6 @@
1
1
  export declare function pinJSON(params: {
2
2
  data: string | object;
3
- name: string;
3
+ name?: string;
4
4
  keyvalues?: object;
5
- auth: string;
5
+ auth?: string;
6
6
  }): Promise<string | undefined>;
@@ -2,7 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pinJSON = pinJSON;
4
4
  async function pinJSON(params) {
5
- const { data, name, keyvalues, auth } = params;
5
+ const { data, name = "data.json", keyvalues = [{ key: "library", value: "zkcloudworker" }], } = params;
6
+ const auth = params.auth ??
7
+ process.env.PINATA_JWT ??
8
+ process.env.NEXT_PUBLIC_PINATA_JWT ??
9
+ process.env.REACT_APP_PINATA_JWT;
6
10
  try {
7
11
  const pinataData = {
8
12
  pinataOptions: {
@@ -2,3 +2,4 @@ export * from "./FungibleToken";
2
2
  export * from "./FungibleTokenAdmin";
3
3
  export * from "./api";
4
4
  export * from "./vk";
5
+ export * from "./whitelist";
@@ -18,3 +18,4 @@ __exportStar(require("./FungibleToken"), exports);
18
18
  __exportStar(require("./FungibleTokenAdmin"), exports);
19
19
  __exportStar(require("./api"), exports);
20
20
  __exportStar(require("./vk"), exports);
21
+ __exportStar(require("./whitelist"), exports);
@@ -0,0 +1,305 @@
1
+ import { Field, Option, PublicKey, UInt64, Bool } from "o1js";
2
+ import { Storage } from "../..";
3
+ declare const WhitelistMap_base: typeof import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase;
4
+ /** Represents the whitelist using an Indexed Merkle Map. */
5
+ export declare class WhitelistMap extends WhitelistMap_base {
6
+ }
7
+ declare const WhitelistMapOption_base: import("o1js/dist/node/lib/provable/types/provable-intf").Provable<Option<import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase, {
8
+ root: bigint;
9
+ length: bigint;
10
+ data: {
11
+ nodes: (bigint | undefined)[][];
12
+ sortedLeaves: {
13
+ readonly value: bigint;
14
+ readonly key: bigint;
15
+ readonly nextKey: bigint;
16
+ readonly index: number;
17
+ }[];
18
+ };
19
+ }>, {
20
+ root: bigint;
21
+ length: bigint;
22
+ data: {
23
+ nodes: (bigint | undefined)[][];
24
+ sortedLeaves: {
25
+ readonly value: bigint;
26
+ readonly key: bigint;
27
+ readonly nextKey: bigint;
28
+ readonly index: number;
29
+ }[];
30
+ };
31
+ } | undefined> & (new (option: {
32
+ isSome: Bool;
33
+ value: import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase;
34
+ }) => Option<import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase, {
35
+ root: bigint;
36
+ length: bigint;
37
+ data: {
38
+ nodes: (bigint | undefined)[][];
39
+ sortedLeaves: {
40
+ readonly value: bigint;
41
+ readonly key: bigint;
42
+ readonly nextKey: bigint;
43
+ readonly index: number;
44
+ }[];
45
+ };
46
+ }>) & {
47
+ fromValue(value: import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase | {
48
+ root: bigint;
49
+ length: bigint;
50
+ data: {
51
+ nodes: (bigint | undefined)[][];
52
+ sortedLeaves: {
53
+ readonly value: bigint;
54
+ readonly key: bigint;
55
+ readonly nextKey: bigint;
56
+ readonly index: number;
57
+ }[];
58
+ };
59
+ } | {
60
+ isSome: boolean | Bool;
61
+ value: import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase | {
62
+ root: bigint;
63
+ length: bigint;
64
+ data: {
65
+ nodes: (bigint | undefined)[][];
66
+ sortedLeaves: {
67
+ readonly value: bigint;
68
+ readonly key: bigint;
69
+ readonly nextKey: bigint;
70
+ readonly index: number;
71
+ }[];
72
+ };
73
+ };
74
+ } | undefined): Option<import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase, {
75
+ root: bigint;
76
+ length: bigint;
77
+ data: {
78
+ nodes: (bigint | undefined)[][];
79
+ sortedLeaves: {
80
+ readonly value: bigint;
81
+ readonly key: bigint;
82
+ readonly nextKey: bigint;
83
+ readonly index: number;
84
+ }[];
85
+ };
86
+ }>;
87
+ from(value?: import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase | {
88
+ root: bigint;
89
+ length: bigint;
90
+ data: {
91
+ nodes: (bigint | undefined)[][];
92
+ sortedLeaves: {
93
+ readonly value: bigint;
94
+ readonly key: bigint;
95
+ readonly nextKey: bigint;
96
+ readonly index: number;
97
+ }[];
98
+ };
99
+ } | undefined): Option<import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase, {
100
+ root: bigint;
101
+ length: bigint;
102
+ data: {
103
+ nodes: (bigint | undefined)[][];
104
+ sortedLeaves: {
105
+ readonly value: bigint;
106
+ readonly key: bigint;
107
+ readonly nextKey: bigint;
108
+ readonly index: number;
109
+ }[];
110
+ };
111
+ }>;
112
+ none(): Option<import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase, {
113
+ root: bigint;
114
+ length: bigint;
115
+ data: {
116
+ nodes: (bigint | undefined)[][];
117
+ sortedLeaves: {
118
+ readonly value: bigint;
119
+ readonly key: bigint;
120
+ readonly nextKey: bigint;
121
+ readonly index: number;
122
+ }[];
123
+ };
124
+ }>;
125
+ };
126
+ export declare class WhitelistMapOption extends WhitelistMapOption_base {
127
+ }
128
+ declare const UInt64Option_base: Omit<import("o1js/dist/node/lib/provable/types/provable-intf").Provable<Option<UInt64, bigint>, bigint | undefined>, "fromFields"> & {
129
+ fromFields: (fields: import("o1js/dist/node/lib/provable/field").Field[]) => Option<UInt64, bigint>;
130
+ } & (new (option: {
131
+ isSome: Bool;
132
+ value: UInt64;
133
+ }) => Option<UInt64, bigint>) & {
134
+ fromValue(value: bigint | UInt64 | {
135
+ isSome: boolean | Bool;
136
+ value: bigint | UInt64;
137
+ } | undefined): Option<UInt64, bigint>;
138
+ from(value?: bigint | UInt64 | undefined): Option<UInt64, bigint>;
139
+ none(): Option<UInt64, bigint>;
140
+ };
141
+ export declare class UInt64Option extends UInt64Option_base {
142
+ }
143
+ declare const WhitelistedAddress_base: (new (value: {
144
+ address: PublicKey;
145
+ amount: UInt64;
146
+ }) => {
147
+ address: PublicKey;
148
+ amount: UInt64;
149
+ }) & {
150
+ _isStruct: true;
151
+ } & Omit<import("o1js/dist/node/lib/provable/types/provable-intf").Provable<{
152
+ address: PublicKey;
153
+ amount: UInt64;
154
+ }, {
155
+ address: {
156
+ x: bigint;
157
+ isOdd: boolean;
158
+ };
159
+ amount: bigint;
160
+ }>, "fromFields"> & {
161
+ fromFields: (fields: import("o1js/dist/node/lib/provable/field").Field[]) => {
162
+ address: PublicKey;
163
+ amount: UInt64;
164
+ };
165
+ } & {
166
+ fromValue: (value: {
167
+ address: PublicKey | {
168
+ x: Field | bigint;
169
+ isOdd: Bool | boolean;
170
+ };
171
+ amount: bigint | UInt64;
172
+ }) => {
173
+ address: PublicKey;
174
+ amount: UInt64;
175
+ };
176
+ toInput: (x: {
177
+ address: PublicKey;
178
+ amount: UInt64;
179
+ }) => {
180
+ fields?: Field[] | undefined;
181
+ packed?: [Field, number][] | undefined;
182
+ };
183
+ toJSON: (x: {
184
+ address: PublicKey;
185
+ amount: UInt64;
186
+ }) => {
187
+ address: string;
188
+ amount: string;
189
+ };
190
+ fromJSON: (x: {
191
+ address: string;
192
+ amount: string;
193
+ }) => {
194
+ address: PublicKey;
195
+ amount: UInt64;
196
+ };
197
+ empty: () => {
198
+ address: PublicKey;
199
+ amount: UInt64;
200
+ };
201
+ };
202
+ export declare class WhitelistedAddress extends WhitelistedAddress_base {
203
+ }
204
+ declare const Whitelist_base: (new (value: {
205
+ root: import("o1js/dist/node/lib/provable/field").Field;
206
+ storage: Storage;
207
+ }) => {
208
+ root: import("o1js/dist/node/lib/provable/field").Field;
209
+ storage: Storage;
210
+ }) & {
211
+ _isStruct: true;
212
+ } & Omit<import("o1js/dist/node/lib/provable/types/provable-intf").Provable<{
213
+ root: import("o1js/dist/node/lib/provable/field").Field;
214
+ storage: Storage;
215
+ }, {
216
+ root: bigint;
217
+ storage: {
218
+ url: bigint[];
219
+ };
220
+ }>, "fromFields"> & {
221
+ fromFields: (fields: import("o1js/dist/node/lib/provable/field").Field[]) => {
222
+ root: import("o1js/dist/node/lib/provable/field").Field;
223
+ storage: Storage;
224
+ };
225
+ } & {
226
+ fromValue: (value: {
227
+ root: string | number | bigint | import("o1js/dist/node/lib/provable/field").Field;
228
+ storage: Storage | {
229
+ url: import("o1js/dist/node/lib/provable/field").Field[] | bigint[];
230
+ };
231
+ }) => {
232
+ root: import("o1js/dist/node/lib/provable/field").Field;
233
+ storage: Storage;
234
+ };
235
+ toInput: (x: {
236
+ root: import("o1js/dist/node/lib/provable/field").Field;
237
+ storage: Storage;
238
+ }) => {
239
+ fields?: Field[] | undefined;
240
+ packed?: [Field, number][] | undefined;
241
+ };
242
+ toJSON: (x: {
243
+ root: import("o1js/dist/node/lib/provable/field").Field;
244
+ storage: Storage;
245
+ }) => {
246
+ root: string;
247
+ storage: {
248
+ url: string[];
249
+ };
250
+ };
251
+ fromJSON: (x: {
252
+ root: string;
253
+ storage: {
254
+ url: string[];
255
+ };
256
+ }) => {
257
+ root: import("o1js/dist/node/lib/provable/field").Field;
258
+ storage: Storage;
259
+ };
260
+ empty: () => {
261
+ root: import("o1js/dist/node/lib/provable/field").Field;
262
+ storage: Storage;
263
+ };
264
+ };
265
+ export declare class Whitelist extends Whitelist_base {
266
+ isNone(): Bool;
267
+ isSome(): Bool;
268
+ load(): Promise<WhitelistMapOption>;
269
+ /**
270
+ * The function fetches a whitelisted amount associated with a given address using a map and returns it
271
+ * as a UInt64Option.
272
+ * @param {PublicKey} address - The `address` parameter is of type `PublicKey`, which represents a
273
+ * public key used in cryptography for various purposes such as encryption, digital signatures, and
274
+ * authentication. In the context of the `fetchWhitelistedAmount` function, the `address` parameter is
275
+ * used to retrieve a whitelisted amount
276
+ * @returns The `fetchWhitelistedAmount` function returns a `Promise` that resolves to a `UInt64Option`
277
+ * object. This object contains a `value` property representing the amount retrieved from a map based
278
+ * on the provided address. The `isSome` property indicates whether the value is present or not.
279
+ * The value is not present if the whitelist is NOT empty and the address is NOT whitelisted.
280
+ * The value is present if the whitelist is NOT empty or the address IS whitelisted.
281
+ * The value is present and equals to UInt64.MAXINT() if the whitelist IS empty.
282
+ */
283
+ getWhitelistedAmount(address: PublicKey): Promise<UInt64Option>;
284
+ static empty(): Whitelist;
285
+ /**
286
+ * Creates a new whitelist and pins it to IPFS.
287
+ * @param params - The parameters for creating the whitelist.
288
+ * @returns A new `Whitelist` instance.
289
+ */
290
+ static create(params: {
291
+ list: WhitelistedAddress[] | {
292
+ address: string;
293
+ amount?: number;
294
+ }[];
295
+ name?: string;
296
+ keyvalues?: {
297
+ key: string;
298
+ value: string;
299
+ }[];
300
+ timeout?: number;
301
+ attempts?: number;
302
+ auth?: string;
303
+ }): Promise<Whitelist>;
304
+ }
305
+ export {};
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Whitelist = exports.WhitelistedAddress = exports.UInt64Option = exports.WhitelistMapOption = exports.WhitelistMap = void 0;
4
+ const o1js_1 = require("o1js");
5
+ const __1 = require("../..");
6
+ const { IndexedMerkleMap } = o1js_1.Experimental;
7
+ const WHITELIST_HEIGHT = 20;
8
+ /** Represents the whitelist using an Indexed Merkle Map. */
9
+ class WhitelistMap extends IndexedMerkleMap(WHITELIST_HEIGHT) {
10
+ }
11
+ exports.WhitelistMap = WhitelistMap;
12
+ class WhitelistMapOption extends (0, o1js_1.Option)(WhitelistMap) {
13
+ }
14
+ exports.WhitelistMapOption = WhitelistMapOption;
15
+ class UInt64Option extends (0, o1js_1.Option)(o1js_1.UInt64) {
16
+ }
17
+ exports.UInt64Option = UInt64Option;
18
+ class WhitelistedAddress extends (0, o1js_1.Struct)({
19
+ address: o1js_1.PublicKey,
20
+ amount: o1js_1.UInt64, // Maximum permitted amount of the transaction
21
+ }) {
22
+ }
23
+ exports.WhitelistedAddress = WhitelistedAddress;
24
+ class Whitelist extends (0, o1js_1.Struct)({
25
+ /** The root hash of the Merkle tree representing the whitelist. */
26
+ root: o1js_1.Field,
27
+ /** Off-chain storage information, typically an IPFS hash pointing to the whitelist data. */
28
+ storage: __1.Storage,
29
+ }) {
30
+ isNone() {
31
+ return this.root
32
+ .equals((0, o1js_1.Field)(0))
33
+ .or(__1.Storage.equals(this.storage, __1.Storage.empty()));
34
+ }
35
+ isSome() {
36
+ return this.isNone().not();
37
+ }
38
+ async load() {
39
+ const isNone = this.isNone();
40
+ const map = await o1js_1.Provable.witnessAsync(WhitelistMapOption, async () => {
41
+ if (isNone.toBoolean())
42
+ return WhitelistMapOption.none();
43
+ else
44
+ return WhitelistMapOption.fromValue(await (0, __1.loadIndexedMerkleMap)({
45
+ url: (0, __1.createIpfsURL)({ hash: this.storage.toString() }),
46
+ type: WhitelistMap,
47
+ }));
48
+ });
49
+ isNone.assertEquals(map.isSome.not());
50
+ const root = o1js_1.Provable.if(map.isSome, map.orElse(new WhitelistMap()).root, (0, o1js_1.Field)(0));
51
+ root.equals(this.root);
52
+ return map;
53
+ }
54
+ /**
55
+ * The function fetches a whitelisted amount associated with a given address using a map and returns it
56
+ * as a UInt64Option.
57
+ * @param {PublicKey} address - The `address` parameter is of type `PublicKey`, which represents a
58
+ * public key used in cryptography for various purposes such as encryption, digital signatures, and
59
+ * authentication. In the context of the `fetchWhitelistedAmount` function, the `address` parameter is
60
+ * used to retrieve a whitelisted amount
61
+ * @returns The `fetchWhitelistedAmount` function returns a `Promise` that resolves to a `UInt64Option`
62
+ * object. This object contains a `value` property representing the amount retrieved from a map based
63
+ * on the provided address. The `isSome` property indicates whether the value is present or not.
64
+ * The value is not present if the whitelist is NOT empty and the address is NOT whitelisted.
65
+ * The value is present if the whitelist is NOT empty or the address IS whitelisted.
66
+ * The value is present and equals to UInt64.MAXINT() if the whitelist IS empty.
67
+ */
68
+ async getWhitelistedAmount(address) {
69
+ const map = await this.load();
70
+ const key = o1js_1.Poseidon.hashPacked(o1js_1.PublicKey, address);
71
+ const value = map.orElse(new WhitelistMap()).getOption(key);
72
+ const valueField = value.orElse(o1js_1.UInt64.MAXINT().value);
73
+ valueField.assertLessThanOrEqual(o1js_1.UInt64.MAXINT().value);
74
+ const amount = o1js_1.UInt64.Unsafe.fromField(valueField);
75
+ return new UInt64Option({
76
+ value: amount,
77
+ isSome: value.isSome.or(this.isNone()),
78
+ });
79
+ }
80
+ static empty() {
81
+ return new Whitelist({
82
+ root: (0, o1js_1.Field)(0),
83
+ storage: __1.Storage.empty(),
84
+ });
85
+ }
86
+ /**
87
+ * Creates a new whitelist and pins it to IPFS.
88
+ * @param params - The parameters for creating the whitelist.
89
+ * @returns A new `Whitelist` instance.
90
+ */
91
+ static async create(params) {
92
+ const { name = "whitelist.json", keyvalues = [{ key: "library", value: "zkcloudworker" }], timeout = 60 * 1000, attempts = 5, auth, } = params;
93
+ const list = typeof params.list[0].address === "string"
94
+ ? params.list.map((item) => new WhitelistedAddress({
95
+ address: o1js_1.PublicKey.fromBase58(item.address),
96
+ amount: item.amount
97
+ ? o1js_1.UInt64.from(item.amount)
98
+ : o1js_1.UInt64.MAXINT(),
99
+ }))
100
+ : params.list;
101
+ const map = new WhitelistMap();
102
+ for (const item of list) {
103
+ map.insert(o1js_1.Poseidon.hashPacked(o1js_1.PublicKey, item.address), item.amount.toBigInt());
104
+ }
105
+ const serializedMap = (0, __1.serializeIndexedMap)(map);
106
+ const json = {
107
+ map: serializedMap,
108
+ whitelist: list.map((item) => ({
109
+ address: item.address.toBase58(),
110
+ amount: Number(item.amount.toBigInt()),
111
+ })),
112
+ };
113
+ let attempt = 0;
114
+ const start = Date.now();
115
+ let hash = await (0, __1.pinJSON)({
116
+ data: json,
117
+ name,
118
+ keyvalues,
119
+ auth,
120
+ });
121
+ while (!hash && attempt < attempts && Date.now() - start < timeout) {
122
+ attempt++;
123
+ await (0, __1.sleep)(5000 * attempt); // handle rate-limits
124
+ hash = await (0, __1.pinJSON)({
125
+ data: json,
126
+ name,
127
+ keyvalues,
128
+ auth,
129
+ });
130
+ }
131
+ if (!hash)
132
+ throw new Error("Failed to pin whitelist");
133
+ return new Whitelist({
134
+ root: map.root,
135
+ storage: __1.Storage.fromString(hash),
136
+ });
137
+ }
138
+ }
139
+ exports.Whitelist = Whitelist;
@@ -11,6 +11,8 @@ export interface IndexedMapSerialized {
11
11
  export declare function loadIndexedMerkleMap(params: {
12
12
  url: string;
13
13
  type: ReturnType<typeof IndexedMerkleMap>;
14
+ timeout?: number;
15
+ attempts?: number;
14
16
  }): Promise<import("o1js/dist/node/lib/provable/merkle-tree-indexed").IndexedMerkleMapBase>;
15
17
  export declare function saveIndexedMerkleMap(params: {
16
18
  map: IndexedMerkleMap;
@@ -10,12 +10,22 @@ const cloud_1 = require("../../cloud");
10
10
  const pinata_1 = require("../storage/pinata");
11
11
  const { IndexedMerkleMap } = o1js_1.Experimental;
12
12
  async function loadIndexedMerkleMap(params) {
13
- const { url, type } = params;
14
- const response = await fetch(url);
13
+ const { url, type, timeout = 60000, attempts = 5 } = params;
14
+ let attempt = 0;
15
+ const start = Date.now();
16
+ let response = await fetch(url);
17
+ while (!response.ok && attempt < attempts && Date.now() - start < timeout) {
18
+ attempt++;
19
+ await (0, cloud_1.sleep)(5000 * attempt); // handle rate limiting
20
+ response = await fetch(url);
21
+ }
15
22
  if (!response.ok) {
16
23
  throw new Error("Failed to fetch IndexedMerkleMap");
17
24
  }
18
- const serializedIndexedMap = (await response.json());
25
+ const json = await response.json();
26
+ const serializedIndexedMap = json.map;
27
+ if (!serializedIndexedMap)
28
+ throw new Error("wrong IndexedMerkleMap json format");
19
29
  const map = deserializeIndexedMerkleMapInternal({
20
30
  serializedIndexedMap,
21
31
  type,
@@ -29,7 +39,7 @@ async function saveIndexedMerkleMap(params) {
29
39
  const { map, name = "indexed-map", keyvalues, auth } = params;
30
40
  const serialized = serializeIndexedMap(map);
31
41
  const ipfsHash = await (0, pinata_1.pinJSON)({
32
- data: serialized,
42
+ data: { map: serialized },
33
43
  name,
34
44
  keyvalues,
35
45
  auth,