zkcloudworker 0.5.2 → 0.5.3

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 (48) hide show
  1. package/lib/ts/src/index.d.ts +3 -2
  2. package/lib/ts/src/index.js +3 -2
  3. package/lib/ts/src/utils/base64.d.ts +5 -0
  4. package/lib/ts/src/utils/base64.js +90 -0
  5. package/lib/ts/src/{fetch.js → utils/fetch.js} +24 -6
  6. package/lib/ts/src/utils/fields.d.ts +3 -0
  7. package/lib/ts/src/utils/fields.js +33 -0
  8. package/lib/ts/tsconfig.tsbuildinfo +1 -1
  9. package/lib/web/src/api/api.js +263 -286
  10. package/lib/web/src/api/api.js.map +1 -1
  11. package/lib/web/src/cloud/cloud.js +12 -25
  12. package/lib/web/src/cloud/cloud.js.map +1 -1
  13. package/lib/web/src/cloud/files.js +59 -68
  14. package/lib/web/src/cloud/files.js.map +1 -1
  15. package/lib/web/src/cloud/local.js +221 -259
  16. package/lib/web/src/cloud/local.js.map +1 -1
  17. package/lib/web/src/encryption/encryption.js +2 -5
  18. package/lib/web/src/encryption/encryption.js.map +1 -1
  19. package/lib/web/src/encryption/messages.js +24 -29
  20. package/lib/web/src/encryption/messages.js.map +1 -1
  21. package/lib/web/src/encryption/nats-client.js +67 -83
  22. package/lib/web/src/encryption/nats-client.js.map +1 -1
  23. package/lib/web/src/fee.js +3 -6
  24. package/lib/web/src/fee.js.map +1 -1
  25. package/lib/web/src/index.d.ts +3 -2
  26. package/lib/web/src/index.js +3 -2
  27. package/lib/web/src/index.js.map +1 -1
  28. package/lib/web/src/mina.js +60 -67
  29. package/lib/web/src/mina.js.map +1 -1
  30. package/lib/web/src/utils/base64.d.ts +5 -0
  31. package/lib/web/src/utils/base64.js +84 -0
  32. package/lib/web/src/utils/base64.js.map +1 -0
  33. package/lib/web/src/utils/fetch.js +79 -0
  34. package/lib/web/src/utils/fetch.js.map +1 -0
  35. package/lib/web/src/utils/fields.d.ts +3 -0
  36. package/lib/web/src/utils/fields.js +29 -0
  37. package/lib/web/src/utils/fields.js.map +1 -0
  38. package/lib/web/tsconfig.web.tsbuildinfo +1 -1
  39. package/package.json +1 -1
  40. package/lib/ts/src/api/client-api.d.ts +0 -147
  41. package/lib/ts/src/api/client-api.js +0 -122
  42. package/lib/web/src/api/client-api.d.ts +0 -147
  43. package/lib/web/src/api/client-api.js +0 -134
  44. package/lib/web/src/api/client-api.js.map +0 -1
  45. package/lib/web/src/fetch.js +0 -68
  46. package/lib/web/src/fetch.js.map +0 -1
  47. /package/lib/ts/src/{fetch.d.ts → utils/fetch.d.ts} +0 -0
  48. /package/lib/web/src/{fetch.d.ts → utils/fetch.d.ts} +0 -0
@@ -1,4 +1,3 @@
1
- import { __awaiter } from "tslib";
2
1
  import axios from "axios";
3
2
  import chalk from "chalk";
4
3
  import { sleep } from "../mina";
@@ -19,7 +18,7 @@ export class zkCloudWorkerClient {
19
18
  const { jwt, zkcloudworker, chain, webhook } = params;
20
19
  this.jwt = jwt;
21
20
  this.endpoint = ZKCLOUDWORKER_API;
22
- this.chain = chain !== null && chain !== void 0 ? chain : "devnet";
21
+ this.chain = chain ?? "devnet";
23
22
  this.webhook = webhook;
24
23
  if (jwt === "local") {
25
24
  if (zkcloudworker === undefined)
@@ -43,21 +42,19 @@ export class zkCloudWorkerClient {
43
42
  * The developers repo should provide a BackupPlugin with the name task
44
43
  * that can be called with the given parameters
45
44
  */
46
- recursiveProof(data) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- const result = yield this.apiHub("recursiveProof", data);
49
- if (result.data === "error")
50
- return {
51
- success: false,
52
- error: result.error,
53
- };
54
- else
55
- return {
56
- success: result.success,
57
- jobId: result.data,
58
- error: result.error,
59
- };
60
- });
45
+ async recursiveProof(data) {
46
+ const result = await this.apiHub("recursiveProof", data);
47
+ if (result.data === "error")
48
+ return {
49
+ success: false,
50
+ error: result.error,
51
+ };
52
+ else
53
+ return {
54
+ success: result.success,
55
+ jobId: result.data,
56
+ error: result.error,
57
+ };
61
58
  }
62
59
  /**
63
60
  * Starts a new job for the function call using serverless api call
@@ -75,34 +72,31 @@ export class zkCloudWorkerClient {
75
72
  * @returns { success: boolean, error?: string, jobId?: string }
76
73
  * where jonId is the jobId of the job
77
74
  */
78
- execute(data) {
79
- return __awaiter(this, void 0, void 0, function* () {
80
- var _a, _b, _c, _d, _e;
81
- const result = yield this.apiHub("execute", data);
82
- if (result.data === "error" ||
83
- (typeof result.data === "string" && result.data.startsWith("error")))
84
- return {
85
- success: false,
86
- error: result.error,
87
- };
88
- else if (result.success === false || ((_a = result.data) === null || _a === void 0 ? void 0 : _a.success) === false)
89
- return {
90
- success: false,
91
- error: (_d = (_b = result.error) !== null && _b !== void 0 ? _b : (_c = result.data) === null || _c === void 0 ? void 0 : _c.error) !== null && _d !== void 0 ? _d : "execute call failed",
92
- };
93
- else if (result.success === true && ((_e = result.data) === null || _e === void 0 ? void 0 : _e.success) === true)
94
- return {
95
- success: result.success,
96
- jobId: data.mode === "sync" ? undefined : result.data.jobId,
97
- result: data.mode === "sync" ? result.data : undefined,
98
- error: result.error,
99
- };
100
- else
101
- return {
102
- success: false,
103
- error: "execute call error",
104
- };
105
- });
75
+ async execute(data) {
76
+ const result = await this.apiHub("execute", data);
77
+ if (result.data === "error" ||
78
+ (typeof result.data === "string" && result.data.startsWith("error")))
79
+ return {
80
+ success: false,
81
+ error: result.error,
82
+ };
83
+ else if (result.success === false || result.data?.success === false)
84
+ return {
85
+ success: false,
86
+ error: result.error ?? result.data?.error ?? "execute call failed",
87
+ };
88
+ else if (result.success === true && result.data?.success === true)
89
+ return {
90
+ success: result.success,
91
+ jobId: data.mode === "sync" ? undefined : result.data.jobId,
92
+ result: data.mode === "sync" ? result.data : undefined,
93
+ error: result.error,
94
+ };
95
+ else
96
+ return {
97
+ success: false,
98
+ error: "execute call error",
99
+ };
106
100
  }
107
101
  /**
108
102
  * Starts a new job for the function call using serverless api call
@@ -116,22 +110,20 @@ export class zkCloudWorkerClient {
116
110
  * @returns { success: boolean, error?: string, jobId?: string }
117
111
  * where jonId is the jobId of the job
118
112
  */
119
- sendTransactions(data) {
120
- return __awaiter(this, void 0, void 0, function* () {
121
- const result = yield this.apiHub("sendTransactions", data);
122
- if (result.data === "error")
123
- // TODO: check if this is correct in AWS code
124
- return {
125
- success: false,
126
- error: result.error,
127
- };
128
- else
129
- return {
130
- success: result.success,
131
- txId: result.data,
132
- error: result.error,
133
- };
134
- });
113
+ async sendTransactions(data) {
114
+ const result = await this.apiHub("sendTransactions", data);
115
+ if (result.data === "error")
116
+ // TODO: check if this is correct in AWS code
117
+ return {
118
+ success: false,
119
+ error: result.error,
120
+ };
121
+ else
122
+ return {
123
+ success: result.success,
124
+ txId: result.data,
125
+ error: result.error,
126
+ };
135
127
  }
136
128
  /**
137
129
  * Gets the result of the job using serverless api call
@@ -145,22 +137,20 @@ export class zkCloudWorkerClient {
145
137
  * if the job is finished, the result will be set and error will be undefined
146
138
  * if the job is not found, the result will be undefined and error will be set
147
139
  */
148
- jobResult(data) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- const result = yield this.apiHub("jobResult", data);
151
- if (this.isError(result.data))
152
- return {
153
- success: false,
154
- error: result.error,
155
- result: result.data,
156
- };
157
- else
158
- return {
159
- success: result.success,
160
- error: result.error,
161
- result: result.data,
162
- };
163
- });
140
+ async jobResult(data) {
141
+ const result = await this.apiHub("jobResult", data);
142
+ if (this.isError(result.data))
143
+ return {
144
+ success: false,
145
+ error: result.error,
146
+ result: result.data,
147
+ };
148
+ else
149
+ return {
150
+ success: result.success,
151
+ error: result.error,
152
+ result: result.data,
153
+ };
164
154
  }
165
155
  /**
166
156
  * Gets the result of the job using serverless api call
@@ -173,73 +163,66 @@ export class zkCloudWorkerClient {
173
163
  * if the job is finished, the result will be set and error will be undefined
174
164
  * if the job is not found, the result will be undefined and error will be set
175
165
  */
176
- deploy(data) {
177
- return __awaiter(this, void 0, void 0, function* () {
178
- var _a, _b;
179
- // TODO: encrypt env.json
180
- const { repo, developer, packageManager } = data;
181
- const result = yield this.apiHub("deploy", {
182
- developer,
183
- repo,
184
- args: packageManager,
185
- });
186
- if (result.data === "error" ||
187
- (typeof result.data === "string" && result.data.startsWith("error")))
188
- return {
189
- success: false,
190
- error: result.error,
191
- };
192
- else
193
- return {
194
- success: result.success && ((_a = result.data) === null || _a === void 0 ? void 0 : _a.success),
195
- jobId: (_b = result.data) === null || _b === void 0 ? void 0 : _b.jobId,
196
- error: result.error,
197
- };
166
+ async deploy(data) {
167
+ // TODO: encrypt env.json
168
+ const { repo, developer, packageManager } = data;
169
+ const result = await this.apiHub("deploy", {
170
+ developer,
171
+ repo,
172
+ args: packageManager,
198
173
  });
174
+ if (result.data === "error" ||
175
+ (typeof result.data === "string" && result.data.startsWith("error")))
176
+ return {
177
+ success: false,
178
+ error: result.error,
179
+ };
180
+ else
181
+ return {
182
+ success: result.success && result.data?.success,
183
+ jobId: result.data?.jobId,
184
+ error: result.error,
185
+ };
199
186
  }
200
187
  /**
201
188
  * Gets the billing report for the jobs sent using JWT
202
189
  * @returns { success: boolean, error?: string, result?: any }
203
190
  * where result is the billing report
204
191
  */
205
- queryBilling() {
206
- return __awaiter(this, void 0, void 0, function* () {
207
- const result = yield this.apiHub("queryBilling", {});
208
- if (this.isError(result.data))
209
- return {
210
- success: false,
211
- error: result.error,
212
- result: result.data,
213
- };
214
- else
215
- return {
216
- success: result.success,
217
- error: result.error,
218
- result: result.data,
219
- };
220
- });
192
+ async queryBilling() {
193
+ const result = await this.apiHub("queryBilling", {});
194
+ if (this.isError(result.data))
195
+ return {
196
+ success: false,
197
+ error: result.error,
198
+ result: result.data,
199
+ };
200
+ else
201
+ return {
202
+ success: result.success,
203
+ error: result.error,
204
+ result: result.data,
205
+ };
221
206
  }
222
207
  /**
223
208
  * Gets the remaining balance
224
209
  * @returns { success: boolean, error?: string, result?: any }
225
210
  * where result is the billing report
226
211
  */
227
- getBalance() {
228
- return __awaiter(this, void 0, void 0, function* () {
229
- const result = yield this.apiHub("getBalance", {});
230
- if (this.isError(result.data))
231
- return {
232
- success: false,
233
- error: result.error,
234
- result: result.data,
235
- };
236
- else
237
- return {
238
- success: result.success,
239
- error: result.error,
240
- result: result.data,
241
- };
242
- });
212
+ async getBalance() {
213
+ const result = await this.apiHub("getBalance", {});
214
+ if (this.isError(result.data))
215
+ return {
216
+ success: false,
217
+ error: result.error,
218
+ result: result.data,
219
+ };
220
+ else
221
+ return {
222
+ success: result.success,
223
+ error: result.error,
224
+ result: result.data,
225
+ };
243
226
  }
244
227
  /**
245
228
  * Waits for the job to finish
@@ -252,194 +235,188 @@ export class zkCloudWorkerClient {
252
235
  * @returns { success: boolean, error?: string, result?: any }
253
236
  * where result is the result of the job
254
237
  */
255
- waitForJobResult(data) {
256
- return __awaiter(this, void 0, void 0, function* () {
257
- var _a, _b, _c, _d, _e, _f, _g, _h;
258
- const maxAttempts = (_a = data === null || data === void 0 ? void 0 : data.maxAttempts) !== null && _a !== void 0 ? _a : 360; // 1 hour
259
- const interval = (_b = data === null || data === void 0 ? void 0 : data.interval) !== null && _b !== void 0 ? _b : 10000;
260
- const maxErrors = (_c = data === null || data === void 0 ? void 0 : data.maxErrors) !== null && _c !== void 0 ? _c : 10;
261
- const errorDelay = 30000; // 30 seconds
262
- const printedLogs = [];
263
- const printLogs = (_d = data.printLogs) !== null && _d !== void 0 ? _d : true;
264
- function isAllLogsFetched() {
265
- if (printLogs === false)
266
- return true;
267
- // search for "Billed Duration" in the logs and return true if found
268
- return printedLogs.some((log) => log.includes("Billed Duration"));
269
- }
270
- function print(logs) {
271
- logs.forEach((log) => {
272
- if (printedLogs.includes(log) === false) {
273
- printedLogs.push(log);
274
- if (printLogs) {
275
- // replace all occurrences of "error" with red color
276
- const text = log.replace(/error/gi, (matched) => chalk.red(matched));
277
- console.log(text);
278
- }
238
+ async waitForJobResult(data) {
239
+ const maxAttempts = data?.maxAttempts ?? 360; // 1 hour
240
+ const interval = data?.interval ?? 10000;
241
+ const maxErrors = data?.maxErrors ?? 10;
242
+ const errorDelay = 30000; // 30 seconds
243
+ const printedLogs = [];
244
+ const printLogs = data.printLogs ?? true;
245
+ function isAllLogsFetched() {
246
+ if (printLogs === false)
247
+ return true;
248
+ // search for "Billed Duration" in the logs and return true if found
249
+ return printedLogs.some((log) => log.includes("Billed Duration"));
250
+ }
251
+ function print(logs) {
252
+ logs.forEach((log) => {
253
+ if (printedLogs.includes(log) === false) {
254
+ printedLogs.push(log);
255
+ if (printLogs) {
256
+ // replace all occurrences of "error" with red color
257
+ const text = log.replace(/error/gi, (matched) => chalk.red(matched));
258
+ console.log(text);
279
259
  }
280
- });
260
+ }
261
+ });
262
+ }
263
+ let attempts = 0;
264
+ let errors = 0;
265
+ while (attempts < maxAttempts) {
266
+ const result = await this.apiHub("jobResult", {
267
+ jobId: data.jobId,
268
+ includeLogs: printLogs,
269
+ });
270
+ if (printLogs === true &&
271
+ result?.data?.logs !== undefined &&
272
+ result?.data?.logs !== null &&
273
+ Array.isArray(result.data.logs) === true)
274
+ print(result.data.logs);
275
+ if (result.success === false) {
276
+ errors++;
277
+ if (errors > maxErrors) {
278
+ return {
279
+ success: false,
280
+ error: "Too many network errors",
281
+ result: undefined,
282
+ };
283
+ }
284
+ await sleep(errorDelay * errors);
281
285
  }
282
- let attempts = 0;
283
- let errors = 0;
284
- while (attempts < maxAttempts) {
285
- const result = yield this.apiHub("jobResult", {
286
- jobId: data.jobId,
287
- includeLogs: printLogs,
288
- });
289
- if (printLogs === true &&
290
- ((_e = result === null || result === void 0 ? void 0 : result.data) === null || _e === void 0 ? void 0 : _e.logs) !== undefined &&
291
- ((_f = result === null || result === void 0 ? void 0 : result.data) === null || _f === void 0 ? void 0 : _f.logs) !== null &&
292
- Array.isArray(result.data.logs) === true)
293
- print(result.data.logs);
294
- if (result.success === false) {
295
- errors++;
296
- if (errors > maxErrors) {
297
- return {
298
- success: false,
299
- error: "Too many network errors",
300
- result: undefined,
301
- };
302
- }
303
- yield sleep(errorDelay * errors);
286
+ else {
287
+ if (this.isError(result.data))
288
+ return {
289
+ success: false,
290
+ error: result.error,
291
+ result: result.data,
292
+ };
293
+ else if (result.data?.result !== undefined && isAllLogsFetched()) {
294
+ return {
295
+ success: result.success,
296
+ error: result.error,
297
+ result: result.data,
298
+ };
304
299
  }
305
- else {
306
- if (this.isError(result.data))
307
- return {
308
- success: false,
309
- error: result.error,
310
- result: result.data,
311
- };
312
- else if (((_g = result.data) === null || _g === void 0 ? void 0 : _g.result) !== undefined && isAllLogsFetched()) {
313
- return {
314
- success: result.success,
315
- error: result.error,
316
- result: result.data,
317
- };
318
- }
319
- else if (((_h = result.data) === null || _h === void 0 ? void 0 : _h.jobStatus) === "failed" && isAllLogsFetched()) {
320
- return {
321
- success: false,
322
- error: "Job failed",
323
- result: result.data,
324
- };
325
- }
326
- yield sleep(interval);
300
+ else if (result.data?.jobStatus === "failed" && isAllLogsFetched()) {
301
+ return {
302
+ success: false,
303
+ error: "Job failed",
304
+ result: result.data,
305
+ };
327
306
  }
328
- attempts++;
307
+ await sleep(interval);
329
308
  }
330
- return {
331
- success: false,
332
- error: "Timeout",
333
- result: undefined,
334
- };
335
- });
309
+ attempts++;
310
+ }
311
+ return {
312
+ success: false,
313
+ error: "Timeout",
314
+ result: undefined,
315
+ };
336
316
  }
337
317
  /**
338
318
  * Calls the serverless API
339
319
  * @param command the command of the API
340
320
  * @param data the data of the API
341
321
  * */
342
- apiHub(command,
322
+ async apiHub(command,
343
323
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
344
324
  data
345
325
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
346
326
  ) {
347
- return __awaiter(this, void 0, void 0, function* () {
348
- var _a;
349
- if (this.jwt === "local") {
350
- if (this.localWorker === undefined)
351
- throw new Error("localWorker is undefined");
352
- switch (command) {
353
- case "recursiveProof": {
354
- const jobId = yield LocalCloud.run({
355
- command: "recursiveProof",
356
- data,
357
- chain: this.chain,
358
- localWorker: this.localWorker,
359
- });
327
+ if (this.jwt === "local") {
328
+ if (this.localWorker === undefined)
329
+ throw new Error("localWorker is undefined");
330
+ switch (command) {
331
+ case "recursiveProof": {
332
+ const jobId = await LocalCloud.run({
333
+ command: "recursiveProof",
334
+ data,
335
+ chain: this.chain,
336
+ localWorker: this.localWorker,
337
+ });
338
+ return {
339
+ success: true,
340
+ data: jobId,
341
+ };
342
+ }
343
+ case "execute": {
344
+ const jobId = await LocalCloud.run({
345
+ command: "execute",
346
+ data,
347
+ chain: this.chain,
348
+ localWorker: this.localWorker,
349
+ });
350
+ if (data.mode === "sync")
351
+ return { success: true, data: LocalStorage.jobs[jobId].result };
352
+ else
360
353
  return {
361
354
  success: true,
362
355
  data: jobId,
363
356
  };
364
- }
365
- case "execute": {
366
- const jobId = yield LocalCloud.run({
367
- command: "execute",
368
- data,
369
- chain: this.chain,
370
- localWorker: this.localWorker,
371
- });
372
- if (data.mode === "sync")
373
- return { success: true, data: LocalStorage.jobs[jobId].result };
374
- else
375
- return {
376
- success: true,
377
- data: jobId,
378
- };
379
- }
380
- case "jobResult": {
381
- const job = LocalStorage.jobs[data.jobId];
382
- if (job === undefined) {
383
- return {
384
- success: false,
385
- error: "local job not found",
386
- };
387
- }
388
- else {
389
- return {
390
- success: true,
391
- data: job,
392
- };
393
- }
394
- }
395
- case "sendTransactions": {
357
+ }
358
+ case "jobResult": {
359
+ const job = LocalStorage.jobs[data.jobId];
360
+ if (job === undefined) {
396
361
  return {
397
- success: true,
398
- data: yield LocalCloud.addTransactions(data.transactions),
362
+ success: false,
363
+ error: "local job not found",
399
364
  };
400
365
  }
401
- case "deploy":
402
- return {
403
- success: true,
404
- data: "local_deploy",
405
- };
406
- case "queryBilling":
366
+ else {
407
367
  return {
408
368
  success: true,
409
- data: "local_queryBilling",
410
- };
411
- default:
412
- return {
413
- success: false,
414
- error: "local_error",
369
+ data: job,
415
370
  };
371
+ }
416
372
  }
417
- }
418
- else {
419
- const apiData = {
420
- auth: ZKCLOUDWORKER_AUTH,
421
- command: command,
422
- jwtToken: this.jwt,
423
- data: data,
424
- chain: this.chain,
425
- webhook: this.webhook, // TODO: implement webhook code on AWS
426
- };
427
- try {
428
- const response = yield axios.post(this.endpoint, apiData);
429
- return { success: true, data: response.data };
430
- }
431
- catch (error) {
432
- console.error("apiHub error:", (_a = error.message) !== null && _a !== void 0 ? _a : error);
433
- return { success: false, error: error };
373
+ case "sendTransactions": {
374
+ return {
375
+ success: true,
376
+ data: await LocalCloud.addTransactions(data.transactions),
377
+ };
434
378
  }
379
+ case "deploy":
380
+ return {
381
+ success: true,
382
+ data: "local_deploy",
383
+ };
384
+ case "queryBilling":
385
+ return {
386
+ success: true,
387
+ data: "local_queryBilling",
388
+ };
389
+ default:
390
+ return {
391
+ success: false,
392
+ error: "local_error",
393
+ };
435
394
  }
436
- });
395
+ }
396
+ else {
397
+ const apiData = {
398
+ auth: ZKCLOUDWORKER_AUTH,
399
+ command: command,
400
+ jwtToken: this.jwt,
401
+ data: data,
402
+ chain: this.chain,
403
+ webhook: this.webhook, // TODO: implement webhook code on AWS
404
+ };
405
+ try {
406
+ const response = await axios.post(this.endpoint, apiData);
407
+ return { success: true, data: response.data };
408
+ }
409
+ catch (error) {
410
+ console.error("apiHub error:", error.message ?? error);
411
+ return { success: false, error: error };
412
+ }
413
+ }
437
414
  }
438
415
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
439
416
  isError(data) {
440
417
  if (data === "error")
441
418
  return true;
442
- if ((data === null || data === void 0 ? void 0 : data.jobStatus) === "failed")
419
+ if (data?.jobStatus === "failed")
443
420
  return true;
444
421
  if (typeof data === "string" && data.toLowerCase().startsWith("error"))
445
422
  return true;