zkcloudworker 0.2.7 → 0.2.8

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.
@@ -1,5 +1,5 @@
1
1
  import { zkCloudWorker, Cloud } from "../cloud/cloud";
2
- import { JobData } from "../cloud/job";
2
+ import { blockchain } from "../networks";
3
3
  /**
4
4
  * API class for interacting with the zkCloudWorker
5
5
  * @property jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
@@ -8,13 +8,17 @@ import { JobData } from "../cloud/job";
8
8
  export declare class zkCloudWorkerClient {
9
9
  readonly jwt: string;
10
10
  readonly endpoint: string;
11
- readonly localJobs: Map<string, JobData>;
11
+ readonly chain: blockchain;
12
12
  readonly localWorker: (cloud: Cloud) => Promise<zkCloudWorker> | undefined;
13
13
  /**
14
14
  * Constructor for the API class
15
15
  * @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
16
16
  */
17
- constructor(jwt: string, zkcloudworker?: ((cloud: Cloud) => Promise<zkCloudWorker>) | undefined);
17
+ constructor(params: {
18
+ jwt: string;
19
+ zkcloudworker?: (cloud: Cloud) => Promise<zkCloudWorker>;
20
+ chain?: blockchain;
21
+ });
18
22
  /**
19
23
  * Starts a new job for the proof calculation using serverless api call
20
24
  * The developer and name should correspond to the BackupPlugin of the API
@@ -19,10 +19,11 @@ class zkCloudWorkerClient {
19
19
  * Constructor for the API class
20
20
  * @param jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
21
21
  */
22
- constructor(jwt, zkcloudworker = undefined) {
23
- this.localJobs = new Map();
22
+ constructor(params) {
23
+ const { jwt, zkcloudworker, chain } = params;
24
24
  this.jwt = jwt;
25
25
  this.endpoint = ZKCLOUDWORKER_API;
26
+ this.chain = chain ?? "berkeley";
26
27
  if (jwt === "local") {
27
28
  if (zkcloudworker === undefined)
28
29
  throw new Error("worker is required for local mode");
@@ -224,6 +225,7 @@ class zkCloudWorkerClient {
224
225
  if (this.jwt === "local") {
225
226
  switch (command) {
226
227
  case "recursiveProof": {
228
+ console.log("calculating recursive proof locally...");
227
229
  const timeCreated = Date.now();
228
230
  const jobId = this.generateJobId();
229
231
  const job = {
@@ -243,7 +245,7 @@ class zkCloudWorkerClient {
243
245
  jobStatus: "started",
244
246
  maxAttempts: 0,
245
247
  };
246
- const cloud = new local_1.LocalCloud({ job });
248
+ const cloud = new local_1.LocalCloud({ job, chain: this.chain });
247
249
  const worker = await this.localWorker(cloud);
248
250
  if (worker === undefined)
249
251
  throw new Error("worker is undefined");
@@ -255,13 +257,14 @@ class zkCloudWorkerClient {
255
257
  job.jobStatus = "finished";
256
258
  job.result = proof;
257
259
  job.maxAttempts = 1;
258
- this.localJobs.set(jobId, job);
260
+ local_1.LocalStorage.jobs[jobId] = job;
259
261
  return {
260
262
  success: true,
261
263
  data: jobId,
262
264
  };
263
265
  }
264
266
  case "execute": {
267
+ console.log("executing locally...");
265
268
  const timeCreated = Date.now();
266
269
  const jobId = this.generateJobId();
267
270
  const job = {
@@ -280,7 +283,7 @@ class zkCloudWorkerClient {
280
283
  jobStatus: "started",
281
284
  maxAttempts: 0,
282
285
  };
283
- const cloud = new local_1.LocalCloud({ job });
286
+ const cloud = new local_1.LocalCloud({ job, chain: this.chain });
284
287
  const worker = await this.localWorker(cloud);
285
288
  if (worker === undefined)
286
289
  throw new Error("worker is undefined");
@@ -289,14 +292,14 @@ class zkCloudWorkerClient {
289
292
  job.jobStatus = "finished";
290
293
  job.result = result;
291
294
  job.maxAttempts = 1;
292
- this.localJobs.set(jobId, job);
295
+ local_1.LocalStorage.jobs[jobId] = job;
293
296
  return {
294
297
  success: true,
295
298
  data: jobId,
296
299
  };
297
300
  }
298
301
  case "jobResult": {
299
- const job = this.localJobs.get(data.jobId);
302
+ const job = local_1.LocalStorage.jobs[data.jobId];
300
303
  if (job === undefined) {
301
304
  return {
302
305
  success: false,
@@ -333,6 +336,7 @@ class zkCloudWorkerClient {
333
336
  command: command,
334
337
  jwtToken: this.jwt,
335
338
  data: data,
339
+ chain: this.chain,
336
340
  };
337
341
  try {
338
342
  const response = await axios_1.default.post(this.endpoint, apiData);
@@ -16,6 +16,7 @@ export declare abstract class Cloud {
16
16
  readonly userId?: string;
17
17
  readonly args?: string;
18
18
  readonly metadata?: string;
19
+ readonly chain: blockchain;
19
20
  readonly isLocalCloud: boolean;
20
21
  constructor(params: {
21
22
  jobId: string;
@@ -28,6 +29,7 @@ export declare abstract class Cloud {
28
29
  args?: string;
29
30
  metadata?: string;
30
31
  isLocalCloud?: boolean;
32
+ chain: blockchain;
31
33
  });
32
34
  abstract getDeployer(): Promise<PrivateKey>;
33
35
  abstract log(msg: string): void;
@@ -37,11 +39,18 @@ export declare abstract class Cloud {
37
39
  abstract loadFile(filename: string): Promise<Buffer | undefined>;
38
40
  abstract loadEnvironment(password: string): Promise<void>;
39
41
  }
42
+ export interface CloudTransaction {
43
+ txId: string;
44
+ transaction: string;
45
+ timeReceived: number;
46
+ }
40
47
  export declare abstract class zkCloudWorker {
41
48
  readonly cloud: Cloud;
42
49
  constructor(cloud: Cloud);
43
- abstract deployedContracts(): Promise<DeployedSmartContract[]>;
44
- abstract create(transaction: string): Promise<string | undefined>;
45
- abstract merge(proof1: string, proof2: string): Promise<string | undefined>;
46
- abstract execute(): Promise<string | undefined>;
50
+ deployedContracts(): Promise<DeployedSmartContract[]>;
51
+ create(transaction: string): Promise<string | undefined>;
52
+ merge(proof1: string, proof2: string): Promise<string | undefined>;
53
+ execute(): Promise<string | undefined>;
54
+ processTransactions(transactions: CloudTransaction[]): Promise<void>;
55
+ task(data: string): Promise<void>;
47
56
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.zkCloudWorker = exports.Cloud = void 0;
4
4
  class Cloud {
5
5
  constructor(params) {
6
- const { jobId, stepId, cache, developer, repo, task, userId, args, metadata, isLocalCloud, } = params;
6
+ const { jobId, stepId, cache, developer, repo, task, userId, args, metadata, isLocalCloud, chain, } = params;
7
7
  this.jobId = jobId;
8
8
  this.stepId = stepId;
9
9
  this.cache = cache;
@@ -14,6 +14,7 @@ class Cloud {
14
14
  this.args = args;
15
15
  this.metadata = metadata;
16
16
  this.isLocalCloud = isLocalCloud ?? false;
17
+ this.chain = chain;
17
18
  }
18
19
  }
19
20
  exports.Cloud = Cloud;
@@ -21,5 +22,24 @@ class zkCloudWorker {
21
22
  constructor(cloud) {
22
23
  this.cloud = cloud;
23
24
  }
25
+ // To verify the SmartContract code
26
+ async deployedContracts() {
27
+ return [];
28
+ }
29
+ // Those methods should be implemented for recursive proofs calculations
30
+ async create(transaction) {
31
+ return undefined;
32
+ }
33
+ async merge(proof1, proof2) {
34
+ return undefined;
35
+ }
36
+ // Those methods should be implemented for anything except for recursive proofs
37
+ async execute() {
38
+ return undefined;
39
+ }
40
+ // process the transactions received by the cloud
41
+ async processTransactions(transactions) { }
42
+ // process the task defined by the developer
43
+ async task(data) { }
24
44
  }
25
45
  exports.zkCloudWorker = zkCloudWorker;
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ export declare function saveFile(params: {
3
+ data: any;
4
+ filename: string;
5
+ }): Promise<string | undefined>;
6
+ export declare function loadFile(filename: string): Promise<any>;
7
+ export declare function saveBinaryFile(params: {
8
+ data: any;
9
+ filename: string;
10
+ }): Promise<string | undefined>;
11
+ export declare function loadBinaryFile(filename: string): Promise<Buffer | undefined>;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.loadBinaryFile = exports.saveBinaryFile = exports.loadFile = exports.saveFile = void 0;
7
+ const promises_1 = __importDefault(require("fs/promises"));
8
+ async function saveFile(params) {
9
+ const { data, filename } = params;
10
+ const folder = "./data/";
11
+ const name = folder + filename + ".json";
12
+ try {
13
+ await promises_1.default.access("./data");
14
+ }
15
+ catch (e) {
16
+ // if not, create it
17
+ await promises_1.default.mkdir("./data");
18
+ }
19
+ try {
20
+ await promises_1.default.writeFile(name, JSON.stringify(data, null, 2));
21
+ return name;
22
+ }
23
+ catch (e) {
24
+ console.error(`Error writing file ${name}`);
25
+ return undefined;
26
+ }
27
+ }
28
+ exports.saveFile = saveFile;
29
+ async function loadFile(filename) {
30
+ const name = "./data/" + filename + ".json";
31
+ try {
32
+ const filedata = await promises_1.default.readFile(name, "utf8");
33
+ const data = JSON.parse(filedata);
34
+ return data;
35
+ }
36
+ catch (e) {
37
+ console.error(`File ${name} does not exist or has wrong format`);
38
+ return undefined;
39
+ }
40
+ }
41
+ exports.loadFile = loadFile;
42
+ async function saveBinaryFile(params) {
43
+ const { data, filename } = params;
44
+ const folder = "./data/";
45
+ const name = folder + filename + ".bin";
46
+ try {
47
+ await promises_1.default.access("./data");
48
+ }
49
+ catch (e) {
50
+ // if not, create it
51
+ await promises_1.default.mkdir("./data");
52
+ }
53
+ try {
54
+ await promises_1.default.writeFile(name, data);
55
+ return name;
56
+ }
57
+ catch (e) {
58
+ console.error(`Error writing file ${name}`);
59
+ return undefined;
60
+ }
61
+ }
62
+ exports.saveBinaryFile = saveBinaryFile;
63
+ async function loadBinaryFile(filename) {
64
+ const name = "./data/" + filename + ".bin";
65
+ try {
66
+ const data = await promises_1.default.readFile(name);
67
+ return data;
68
+ }
69
+ catch (e) {
70
+ console.error(`File ${name} does not exist or has wrong format`);
71
+ return undefined;
72
+ }
73
+ }
74
+ exports.loadBinaryFile = loadBinaryFile;
@@ -2,10 +2,11 @@
2
2
  import { Cache, PrivateKey } from "o1js";
3
3
  import { Cloud, zkCloudWorker } from "./cloud";
4
4
  import { JobData } from "./job";
5
+ import { blockchain } from "../networks";
5
6
  export declare class LocalCloud extends Cloud {
6
- data: Map<string, string>;
7
7
  constructor(params: {
8
8
  job: JobData;
9
+ chain: blockchain;
9
10
  cache?: Cache;
10
11
  stepId?: string;
11
12
  });
@@ -29,3 +30,22 @@ export declare class LocalCloud extends Cloud {
29
30
  };
30
31
  }): Promise<string>;
31
32
  }
33
+ export declare class LocalStorage {
34
+ static jobs: {
35
+ [key: string]: JobData;
36
+ };
37
+ static data: {
38
+ [key: string]: string;
39
+ };
40
+ static transactions: {
41
+ [key: string]: {
42
+ transaction: string;
43
+ timeReceived: number;
44
+ };
45
+ };
46
+ static tasks: {
47
+ [key: string]: string;
48
+ };
49
+ static saveData(name: string): Promise<void>;
50
+ static loadData(name: string): Promise<void>;
51
+ }
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LocalCloud = void 0;
3
+ exports.LocalStorage = exports.LocalCloud = void 0;
4
4
  const o1js_1 = require("o1js");
5
5
  const cloud_1 = require("./cloud");
6
+ const files_1 = require("./files");
6
7
  class LocalCloud extends cloud_1.Cloud {
7
8
  constructor(params) {
8
- const { job, cache, stepId } = params;
9
+ const { job, chain, cache, stepId } = params;
9
10
  const { jobId, developer, repo, task, userId, args, metadata } = job;
10
11
  super({
11
12
  jobId: jobId,
@@ -18,8 +19,8 @@ class LocalCloud extends cloud_1.Cloud {
18
19
  args: args,
19
20
  metadata: metadata,
20
21
  isLocalCloud: true,
22
+ chain,
21
23
  });
22
- this.data = new Map();
23
24
  }
24
25
  async getDeployer() {
25
26
  throw new Error("Method not implemented.");
@@ -28,17 +29,18 @@ class LocalCloud extends cloud_1.Cloud {
28
29
  console.log("LocalCloud:", msg);
29
30
  }
30
31
  async getDataByKey(key) {
31
- const value = this.data.get(key);
32
+ const value = LocalStorage.data[key];
32
33
  return value;
33
34
  }
34
35
  async saveDataByKey(key, value) {
35
- this.data.set(key, value);
36
+ LocalStorage.data[key] = value;
36
37
  }
37
38
  async saveFile(filename, value) {
38
- throw new Error("Method not implemented.");
39
+ await (0, files_1.saveBinaryFile)({ data: value, filename });
39
40
  }
40
41
  async loadFile(filename) {
41
- throw new Error("Method not implemented.");
42
+ const data = await (0, files_1.loadBinaryFile)(filename);
43
+ return data;
42
44
  }
43
45
  async loadEnvironment(password) {
44
46
  throw new Error("Method not implemented.");
@@ -66,3 +68,30 @@ class LocalCloud extends cloud_1.Cloud {
66
68
  }
67
69
  }
68
70
  exports.LocalCloud = LocalCloud;
71
+ class LocalStorage {
72
+ static async saveData(name) {
73
+ const data = {
74
+ jobs: LocalStorage.jobs,
75
+ data: LocalStorage.data,
76
+ transactions: LocalStorage.transactions,
77
+ tasks: LocalStorage.tasks,
78
+ };
79
+ const filename = name + ".cloud";
80
+ await (0, files_1.saveFile)({ data, filename });
81
+ }
82
+ static async loadData(name) {
83
+ const filename = name + ".cloud";
84
+ const data = await (0, files_1.loadFile)(filename);
85
+ if (data === undefined)
86
+ return;
87
+ LocalStorage.jobs = data.jobs;
88
+ LocalStorage.data = data.data;
89
+ LocalStorage.transactions = data.transactions;
90
+ LocalStorage.tasks = data.tasks;
91
+ }
92
+ }
93
+ exports.LocalStorage = LocalStorage;
94
+ LocalStorage.jobs = {};
95
+ LocalStorage.data = {};
96
+ LocalStorage.transactions = {};
97
+ LocalStorage.tasks = {};