@silvana-one/mina-prover 0.2.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.
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/dist/node/api/api.d.ts +211 -0
- package/dist/node/api/api.js +472 -0
- package/dist/node/api/api.js.map +1 -0
- package/dist/node/index.cjs +1161 -0
- package/dist/node/index.d.ts +4 -0
- package/dist/node/index.js +5 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/local/local.d.ts +292 -0
- package/dist/node/local/local.js +528 -0
- package/dist/node/local/local.js.map +1 -0
- package/dist/node/tokens/index.d.ts +2 -0
- package/dist/node/tokens/index.js +3 -0
- package/dist/node/tokens/index.js.map +1 -0
- package/dist/node/tokens/nft.d.ts +29 -0
- package/dist/node/tokens/nft.js +107 -0
- package/dist/node/tokens/nft.js.map +1 -0
- package/dist/node/tokens/token.d.ts +29 -0
- package/dist/node/tokens/token.js +99 -0
- package/dist/node/tokens/token.js.map +1 -0
- package/dist/node/verification/index.d.ts +1 -0
- package/dist/node/verification/index.js +2 -0
- package/dist/node/verification/index.js.map +1 -0
- package/dist/node/verification/verification.d.ts +21 -0
- package/dist/node/verification/verification.js +2 -0
- package/dist/node/verification/verification.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/tsconfig.web.tsbuildinfo +1 -0
- package/dist/web/api/api.d.ts +211 -0
- package/dist/web/api/api.js +472 -0
- package/dist/web/api/api.js.map +1 -0
- package/dist/web/index.d.ts +4 -0
- package/dist/web/index.js +5 -0
- package/dist/web/index.js.map +1 -0
- package/dist/web/local/local.d.ts +292 -0
- package/dist/web/local/local.js +528 -0
- package/dist/web/local/local.js.map +1 -0
- package/dist/web/tokens/index.d.ts +2 -0
- package/dist/web/tokens/index.js +3 -0
- package/dist/web/tokens/index.js.map +1 -0
- package/dist/web/tokens/nft.d.ts +29 -0
- package/dist/web/tokens/nft.js +107 -0
- package/dist/web/tokens/nft.js.map +1 -0
- package/dist/web/tokens/token.d.ts +29 -0
- package/dist/web/tokens/token.js +99 -0
- package/dist/web/tokens/token.js.map +1 -0
- package/dist/web/verification/index.d.ts +1 -0
- package/dist/web/verification/index.js +2 -0
- package/dist/web/verification/index.js.map +1 -0
- package/dist/web/verification/verification.d.ts +21 -0
- package/dist/web/verification/verification.js +2 -0
- package/dist/web/verification/verification.js.map +1 -0
- package/package.json +68 -0
- package/src/api/api.ts +613 -0
- package/src/index.ts +4 -0
- package/src/local/local.ts +651 -0
- package/src/tokens/index.ts +2 -0
- package/src/tokens/nft.ts +147 -0
- package/src/tokens/token.ts +140 -0
- package/src/verification/index.ts +1 -0
- package/src/verification/verification.ts +23 -0
|
@@ -0,0 +1,1161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// dist/node/index.js
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
LocalCloud: () => LocalCloud,
|
|
34
|
+
LocalStorage: () => LocalStorage,
|
|
35
|
+
NftAPI: () => NftAPI,
|
|
36
|
+
TokenAPI: () => TokenAPI,
|
|
37
|
+
zkCloudWorkerClient: () => zkCloudWorkerClient
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// dist/node/api/api.js
|
|
42
|
+
var import_chalk = __toESM(require("chalk"), 1);
|
|
43
|
+
var import_mina_utils2 = require("@silvana-one/mina-utils");
|
|
44
|
+
|
|
45
|
+
// dist/node/local/local.js
|
|
46
|
+
var import_prover = require("@silvana-one/prover");
|
|
47
|
+
var import_mina_utils = require("@silvana-one/mina-utils");
|
|
48
|
+
var LocalCloud = class _LocalCloud extends import_prover.Cloud {
|
|
49
|
+
/**
|
|
50
|
+
* Constructor for LocalCloud
|
|
51
|
+
* @param params the parameters to create the LocalCloud
|
|
52
|
+
* @param params.job the job data
|
|
53
|
+
* @param params.chain the blockchain to execute the job on, can be any blockchain, not only local
|
|
54
|
+
* @param params.cache the cache folder
|
|
55
|
+
* @param params.stepId the step id
|
|
56
|
+
* @param params.localWorker the worker to execute the tasks
|
|
57
|
+
*/
|
|
58
|
+
constructor(params) {
|
|
59
|
+
const { job, chain, cache, stepId, localWorker } = params;
|
|
60
|
+
const { id, jobId, developer, repo, task, userId, args, metadata, taskId } = job;
|
|
61
|
+
super({
|
|
62
|
+
id,
|
|
63
|
+
jobId,
|
|
64
|
+
stepId: stepId ?? "stepId",
|
|
65
|
+
taskId: taskId ?? "taskId",
|
|
66
|
+
cache: cache ?? "./cache",
|
|
67
|
+
developer,
|
|
68
|
+
repo,
|
|
69
|
+
task,
|
|
70
|
+
userId,
|
|
71
|
+
args,
|
|
72
|
+
metadata,
|
|
73
|
+
isLocalCloud: true,
|
|
74
|
+
chain
|
|
75
|
+
});
|
|
76
|
+
this.localWorker = localWorker;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Provides the deployer key pair for testing and development
|
|
80
|
+
* @returns the deployer key pair
|
|
81
|
+
*/
|
|
82
|
+
async getDeployer() {
|
|
83
|
+
const privateKey = process.env.DEPLOYER_PRIVATE_KEY;
|
|
84
|
+
const publicKey = process.env.DEPLOYER_PUBLIC_KEY;
|
|
85
|
+
try {
|
|
86
|
+
return privateKey === void 0 || publicKey === void 0 ? void 0 : {
|
|
87
|
+
privateKey,
|
|
88
|
+
publicKey
|
|
89
|
+
};
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error(`getDeployer: error getting deployer key pair: ${error}`, error);
|
|
92
|
+
return void 0;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Releases the deployer key pair
|
|
97
|
+
*/
|
|
98
|
+
async releaseDeployer(params) {
|
|
99
|
+
console.log("LocalCloud: releaseDeployer", params);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Gets the data by key
|
|
103
|
+
* @param key the key to get the data
|
|
104
|
+
* @returns the data
|
|
105
|
+
*/
|
|
106
|
+
async getDataByKey(key) {
|
|
107
|
+
const value = LocalStorage.data[key];
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Saves the data by key
|
|
112
|
+
* @param key the key to save the data
|
|
113
|
+
* @param value the value to save
|
|
114
|
+
*/
|
|
115
|
+
async saveDataByKey(key, value) {
|
|
116
|
+
if (value !== void 0)
|
|
117
|
+
LocalStorage.data[key] = value;
|
|
118
|
+
else
|
|
119
|
+
delete LocalStorage.data[key];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Saves the file
|
|
123
|
+
* @param filename the filename to save
|
|
124
|
+
* @param value the value to save
|
|
125
|
+
*/
|
|
126
|
+
async saveFile(filename, value) {
|
|
127
|
+
LocalStorage.files[filename] = value;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Loads the file
|
|
131
|
+
* @param filename
|
|
132
|
+
* @returns the file data
|
|
133
|
+
*/
|
|
134
|
+
async loadFile(filename) {
|
|
135
|
+
return LocalStorage.files[filename];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Encrypts the data
|
|
139
|
+
* @param params
|
|
140
|
+
* @param params.data the data
|
|
141
|
+
* @param params.context the context
|
|
142
|
+
* @param params.keyId the key id, optional
|
|
143
|
+
* @returns encrypted data
|
|
144
|
+
*/
|
|
145
|
+
async encrypt(params) {
|
|
146
|
+
return JSON.stringify(params);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Decrypts the data
|
|
150
|
+
* @param params
|
|
151
|
+
* @param params.data the data
|
|
152
|
+
* @param params.context the context
|
|
153
|
+
* @param params.keyId the key id, optional
|
|
154
|
+
* @returns
|
|
155
|
+
*/
|
|
156
|
+
async decrypt(params) {
|
|
157
|
+
const { data, context, keyId } = JSON.parse(params.data);
|
|
158
|
+
if (context !== params.context) {
|
|
159
|
+
console.error("decrypt: context mismatch");
|
|
160
|
+
return void 0;
|
|
161
|
+
}
|
|
162
|
+
if (keyId !== params.keyId) {
|
|
163
|
+
console.error("decrypt: keyId mismatch");
|
|
164
|
+
return void 0;
|
|
165
|
+
}
|
|
166
|
+
return data;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Generates an id for local cloud
|
|
170
|
+
* @returns generated unique id
|
|
171
|
+
*/
|
|
172
|
+
static generateId(tx = void 0) {
|
|
173
|
+
return Date.now() + "." + (0, import_mina_utils.makeString)(32);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Send transactions to the local cloud
|
|
177
|
+
* @param transactions the transactions to add
|
|
178
|
+
* @returns the transaction ids
|
|
179
|
+
*/
|
|
180
|
+
async sendTransactions(transactions) {
|
|
181
|
+
return await _LocalCloud.addTransactions(transactions);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Adds transactions to the local cloud
|
|
185
|
+
* @param transactions the transactions to add
|
|
186
|
+
* @returns the transaction ids
|
|
187
|
+
*/
|
|
188
|
+
static async addTransactions(transactions) {
|
|
189
|
+
const timeReceived = Date.now();
|
|
190
|
+
const txs = [];
|
|
191
|
+
transactions.forEach((tx) => {
|
|
192
|
+
if (typeof tx === "string") {
|
|
193
|
+
const txId = _LocalCloud.generateId(JSON.stringify({ tx, time: timeReceived }));
|
|
194
|
+
const transaction = {
|
|
195
|
+
txId,
|
|
196
|
+
transaction: tx,
|
|
197
|
+
timeReceived,
|
|
198
|
+
status: "accepted"
|
|
199
|
+
};
|
|
200
|
+
LocalStorage.transactions[txId] = transaction;
|
|
201
|
+
txs.push(transaction);
|
|
202
|
+
} else {
|
|
203
|
+
LocalStorage.transactions[tx.txId] = tx;
|
|
204
|
+
txs.push(tx);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
return txs;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Deletes a transaction from the local cloud
|
|
211
|
+
* @param txId the transaction id to delete
|
|
212
|
+
*/
|
|
213
|
+
async deleteTransaction(txId) {
|
|
214
|
+
if (LocalStorage.transactions[txId] === void 0)
|
|
215
|
+
throw new Error(`deleteTransaction: Transaction ${txId} not found`);
|
|
216
|
+
delete LocalStorage.transactions[txId];
|
|
217
|
+
}
|
|
218
|
+
async getTransactions() {
|
|
219
|
+
const txs = Object.keys(LocalStorage.transactions).map((txId) => {
|
|
220
|
+
return LocalStorage.transactions[txId];
|
|
221
|
+
});
|
|
222
|
+
return txs;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Publish the transaction metadata in human-readable format
|
|
226
|
+
* @param params
|
|
227
|
+
* @param params.txId the transaction id
|
|
228
|
+
* @param params.metadata the metadata
|
|
229
|
+
*/
|
|
230
|
+
async publishTransactionMetadata(params) {
|
|
231
|
+
console.log("publishTransactionMetadata:", params);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Runs the worker in the local cloud
|
|
235
|
+
* @param params the parameters to run the worker
|
|
236
|
+
* @param params.command the command to run
|
|
237
|
+
* @param params.data the data to use
|
|
238
|
+
* @param params.chain the blockchain to execute the job on
|
|
239
|
+
* @param params.localWorker the worker to execute the tasks
|
|
240
|
+
* @returns the job id
|
|
241
|
+
*/
|
|
242
|
+
static async run(params) {
|
|
243
|
+
const { command, data, chain, localWorker } = params;
|
|
244
|
+
const { developer, repo, transactions, task, userId, args, metadata } = data;
|
|
245
|
+
const timeCreated = Date.now();
|
|
246
|
+
const jobId = _LocalCloud.generateId();
|
|
247
|
+
const job = {
|
|
248
|
+
id: "local",
|
|
249
|
+
jobId,
|
|
250
|
+
developer,
|
|
251
|
+
repo,
|
|
252
|
+
task,
|
|
253
|
+
userId,
|
|
254
|
+
args,
|
|
255
|
+
metadata,
|
|
256
|
+
txNumber: command === "recursiveProof" ? transactions.length : 1,
|
|
257
|
+
timeCreated,
|
|
258
|
+
timeStarted: timeCreated,
|
|
259
|
+
chain
|
|
260
|
+
};
|
|
261
|
+
const cloud = new _LocalCloud({
|
|
262
|
+
job,
|
|
263
|
+
chain,
|
|
264
|
+
localWorker
|
|
265
|
+
});
|
|
266
|
+
const worker = await localWorker(cloud);
|
|
267
|
+
if (worker === void 0)
|
|
268
|
+
throw new Error("worker is undefined");
|
|
269
|
+
const result = command === "recursiveProof" ? await _LocalCloud.sequencer({
|
|
270
|
+
worker,
|
|
271
|
+
data
|
|
272
|
+
}) : command === "execute" ? await worker.execute(transactions) : void 0;
|
|
273
|
+
const timeFinished = Date.now();
|
|
274
|
+
if (result !== void 0) {
|
|
275
|
+
LocalStorage.jobEvents[jobId] = {
|
|
276
|
+
jobId,
|
|
277
|
+
jobStatus: "finished",
|
|
278
|
+
eventTime: timeFinished,
|
|
279
|
+
result
|
|
280
|
+
};
|
|
281
|
+
job.timeFinished = timeFinished;
|
|
282
|
+
job.jobStatus = "finished";
|
|
283
|
+
job.result = result;
|
|
284
|
+
} else {
|
|
285
|
+
LocalStorage.jobEvents[jobId] = {
|
|
286
|
+
jobId,
|
|
287
|
+
jobStatus: "failed",
|
|
288
|
+
eventTime: timeFinished
|
|
289
|
+
};
|
|
290
|
+
job.timeFailed = timeFinished;
|
|
291
|
+
job.jobStatus = "failed";
|
|
292
|
+
}
|
|
293
|
+
job.billedDuration = timeFinished - timeCreated;
|
|
294
|
+
LocalStorage.jobs[jobId] = job;
|
|
295
|
+
return jobId;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Runs the recursive proof in the local cloud
|
|
299
|
+
* @param data the data to use
|
|
300
|
+
* @param data.transactions the transactions to process
|
|
301
|
+
* @param data.task the task to execute
|
|
302
|
+
* @param data.userId the user id
|
|
303
|
+
* @param data.args the arguments for the job
|
|
304
|
+
* @param data.metadata the metadata for the job
|
|
305
|
+
* @returns the job id
|
|
306
|
+
*/
|
|
307
|
+
async recursiveProof(data) {
|
|
308
|
+
return await _LocalCloud.run({
|
|
309
|
+
command: "recursiveProof",
|
|
310
|
+
data: {
|
|
311
|
+
developer: this.developer,
|
|
312
|
+
repo: this.repo,
|
|
313
|
+
transactions: data.transactions,
|
|
314
|
+
task: data.task ?? "recursiveProof",
|
|
315
|
+
userId: data.userId,
|
|
316
|
+
args: data.args,
|
|
317
|
+
metadata: data.metadata
|
|
318
|
+
},
|
|
319
|
+
chain: this.chain,
|
|
320
|
+
localWorker: this.localWorker
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Executes the task in the local cloud
|
|
325
|
+
* @param data the data to use
|
|
326
|
+
* @param data.transactions the transactions to process
|
|
327
|
+
* @param data.task the task to execute
|
|
328
|
+
* @param data.userId the user id
|
|
329
|
+
* @param data.args the arguments for the job
|
|
330
|
+
* @param data.metadata the metadata for the job
|
|
331
|
+
* @returns the job id
|
|
332
|
+
*/
|
|
333
|
+
async execute(data) {
|
|
334
|
+
return await _LocalCloud.run({
|
|
335
|
+
command: "execute",
|
|
336
|
+
data: {
|
|
337
|
+
developer: this.developer,
|
|
338
|
+
repo: this.repo,
|
|
339
|
+
transactions: data.transactions,
|
|
340
|
+
task: data.task,
|
|
341
|
+
userId: data.userId,
|
|
342
|
+
args: data.args,
|
|
343
|
+
metadata: data.metadata
|
|
344
|
+
},
|
|
345
|
+
chain: this.chain,
|
|
346
|
+
localWorker: this.localWorker
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Gets the job result
|
|
351
|
+
* @param jobId the job id
|
|
352
|
+
* @returns the job data
|
|
353
|
+
*/
|
|
354
|
+
async jobResult(jobId) {
|
|
355
|
+
return LocalStorage.jobs[jobId];
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Adds a task to the local cloud
|
|
359
|
+
* @param data the data to use
|
|
360
|
+
* @param data.task the task to execute
|
|
361
|
+
* @param data.startTime the start time for the task
|
|
362
|
+
* @param data.userId the user id
|
|
363
|
+
* @param data.args the arguments for the job
|
|
364
|
+
* @param data.metadata the metadata for the job
|
|
365
|
+
* @returns the task id
|
|
366
|
+
*/
|
|
367
|
+
async addTask(data) {
|
|
368
|
+
const taskId = _LocalCloud.generateId();
|
|
369
|
+
LocalStorage.tasks[taskId] = {
|
|
370
|
+
...data,
|
|
371
|
+
id: "local",
|
|
372
|
+
taskId,
|
|
373
|
+
timeCreated: Date.now(),
|
|
374
|
+
developer: this.developer,
|
|
375
|
+
repo: this.repo,
|
|
376
|
+
chain: this.chain
|
|
377
|
+
};
|
|
378
|
+
return taskId;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Deletes a task from the local cloud
|
|
382
|
+
* @param taskId the task id to delete
|
|
383
|
+
*/
|
|
384
|
+
async deleteTask(taskId) {
|
|
385
|
+
if (LocalStorage.tasks[taskId] === void 0)
|
|
386
|
+
throw new Error(`deleteTask: Task ${taskId} not found`);
|
|
387
|
+
delete LocalStorage.tasks[taskId];
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Processes the tasks in the local cloud
|
|
391
|
+
*/
|
|
392
|
+
async processTasks() {
|
|
393
|
+
await _LocalCloud.processLocalTasks({
|
|
394
|
+
developer: this.developer,
|
|
395
|
+
repo: this.repo,
|
|
396
|
+
localWorker: this.localWorker,
|
|
397
|
+
chain: this.chain
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Processes the local tasks
|
|
402
|
+
* @param params the parameters to process the local tasks
|
|
403
|
+
* @param params.developer the developer of the repo
|
|
404
|
+
* @param params.repo the repo
|
|
405
|
+
* @param params.localWorker the worker to execute the tasks
|
|
406
|
+
* @param params.chain the blockchain to execute the job on
|
|
407
|
+
*/
|
|
408
|
+
static async processLocalTasks(params) {
|
|
409
|
+
const { developer, repo, localWorker, chain } = params;
|
|
410
|
+
for (const taskId in LocalStorage.tasks) {
|
|
411
|
+
const data = LocalStorage.tasks[taskId];
|
|
412
|
+
const jobId = _LocalCloud.generateId();
|
|
413
|
+
const timeCreated = Date.now();
|
|
414
|
+
if (data.startTime !== void 0 && data.startTime < timeCreated)
|
|
415
|
+
continue;
|
|
416
|
+
const job = {
|
|
417
|
+
id: "local",
|
|
418
|
+
jobId,
|
|
419
|
+
taskId,
|
|
420
|
+
developer,
|
|
421
|
+
repo,
|
|
422
|
+
task: data.task,
|
|
423
|
+
userId: data.userId,
|
|
424
|
+
args: data.args,
|
|
425
|
+
metadata: data.metadata,
|
|
426
|
+
txNumber: 1,
|
|
427
|
+
timeCreated
|
|
428
|
+
};
|
|
429
|
+
const cloud = new _LocalCloud({
|
|
430
|
+
job,
|
|
431
|
+
chain,
|
|
432
|
+
localWorker
|
|
433
|
+
});
|
|
434
|
+
const worker = await localWorker(cloud);
|
|
435
|
+
const result = await worker.task();
|
|
436
|
+
const timeFinished = Date.now();
|
|
437
|
+
if (result !== void 0) {
|
|
438
|
+
LocalStorage.jobEvents[jobId] = {
|
|
439
|
+
jobId,
|
|
440
|
+
jobStatus: "finished",
|
|
441
|
+
eventTime: timeFinished,
|
|
442
|
+
result
|
|
443
|
+
};
|
|
444
|
+
job.timeFinished = timeFinished;
|
|
445
|
+
} else {
|
|
446
|
+
LocalStorage.jobEvents[jobId] = {
|
|
447
|
+
jobId,
|
|
448
|
+
jobStatus: "failed",
|
|
449
|
+
eventTime: timeFinished
|
|
450
|
+
};
|
|
451
|
+
job.timeFailed = timeFinished;
|
|
452
|
+
}
|
|
453
|
+
job.billedDuration = timeFinished - timeCreated;
|
|
454
|
+
LocalStorage.jobs[jobId] = job;
|
|
455
|
+
}
|
|
456
|
+
let count = 0;
|
|
457
|
+
for (const task in LocalStorage.tasks)
|
|
458
|
+
count++;
|
|
459
|
+
return count;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Runs the sequencer in the local cloud
|
|
463
|
+
* @param params the parameters to run the sequencer
|
|
464
|
+
* @param params.worker the worker to execute the tasks
|
|
465
|
+
* @param params.data the data to use
|
|
466
|
+
* @returns the proof
|
|
467
|
+
*/
|
|
468
|
+
static async sequencer(params) {
|
|
469
|
+
const { worker, data } = params;
|
|
470
|
+
const { transactions } = data;
|
|
471
|
+
if (transactions.length === 0)
|
|
472
|
+
throw new Error("No transactions to process");
|
|
473
|
+
const proofs = [];
|
|
474
|
+
for (const transaction of transactions) {
|
|
475
|
+
const result = await worker.create(transaction);
|
|
476
|
+
if (result === void 0)
|
|
477
|
+
throw new Error("Failed to create proof");
|
|
478
|
+
proofs.push(result);
|
|
479
|
+
}
|
|
480
|
+
let proof = proofs[0];
|
|
481
|
+
for (let i = 1; i < proofs.length; i++) {
|
|
482
|
+
const result = await worker.merge(proof, proofs[i]);
|
|
483
|
+
if (result === void 0)
|
|
484
|
+
throw new Error("Failed to merge proofs");
|
|
485
|
+
proof = result;
|
|
486
|
+
}
|
|
487
|
+
return proof;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* forces the worker to restart
|
|
491
|
+
*/
|
|
492
|
+
async forceWorkerRestart() {
|
|
493
|
+
throw new Error("forceWorkerRestart called in LocalCloud");
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
var _LocalStorage = class _LocalStorage {
|
|
497
|
+
/**
|
|
498
|
+
* Saves the data.
|
|
499
|
+
* @param name The name to save the data under.
|
|
500
|
+
* @throws Error Method not implemented to keep web compatibility.
|
|
501
|
+
*/
|
|
502
|
+
static async saveData(name) {
|
|
503
|
+
throw new Error("Method not implemented to keep web compatibility.");
|
|
504
|
+
const data = {
|
|
505
|
+
jobs: _LocalStorage.jobs,
|
|
506
|
+
data: _LocalStorage.data,
|
|
507
|
+
transactions: _LocalStorage.transactions,
|
|
508
|
+
tasks: _LocalStorage.tasks
|
|
509
|
+
};
|
|
510
|
+
const filename = name + ".cloud";
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Loads the data.
|
|
514
|
+
* @param name The name to load the data from.
|
|
515
|
+
* @throws Error Method not implemented to keep web compatibility.
|
|
516
|
+
*/
|
|
517
|
+
static async loadData(name) {
|
|
518
|
+
throw new Error("Method not implemented to keep web compatibility.");
|
|
519
|
+
const filename = name + ".cloud";
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
_LocalStorage.jobs = {};
|
|
523
|
+
_LocalStorage.jobEvents = {};
|
|
524
|
+
_LocalStorage.data = {};
|
|
525
|
+
_LocalStorage.files = {};
|
|
526
|
+
_LocalStorage.transactions = {};
|
|
527
|
+
_LocalStorage.tasks = {};
|
|
528
|
+
var LocalStorage = _LocalStorage;
|
|
529
|
+
|
|
530
|
+
// dist/node/api/api.js
|
|
531
|
+
var import_prover2 = require("@silvana-one/prover");
|
|
532
|
+
var { ZKCLOUDWORKER_AUTH, ZKCLOUDWORKER_API } = import_prover2.config;
|
|
533
|
+
var zkCloudWorkerClient = class {
|
|
534
|
+
/**
|
|
535
|
+
* Constructor for the API class
|
|
536
|
+
* @param params the parameters for the API class
|
|
537
|
+
* @param params.jwt The jwt token for authentication, get it at https://t.me/minanft_bot?start=auth
|
|
538
|
+
* @param params.zkcloudworker The local worker for the serverless api to test the code locally
|
|
539
|
+
* @param params.chain The blockchain network to use
|
|
540
|
+
* @param params.webhook The webhook for the serverless api to get the results
|
|
541
|
+
*/
|
|
542
|
+
constructor(params) {
|
|
543
|
+
const { jwt, zkcloudworker, webhook } = params;
|
|
544
|
+
this.jwt = jwt;
|
|
545
|
+
const chain = params.chain ?? "devnet";
|
|
546
|
+
this.chain = chain;
|
|
547
|
+
this.endpoint = chain === "devnet" || chain === "zeko" || chain === "mainnet" ? ZKCLOUDWORKER_API + chain : void 0;
|
|
548
|
+
this.webhook = webhook;
|
|
549
|
+
if (jwt === "local") {
|
|
550
|
+
if (zkcloudworker === void 0)
|
|
551
|
+
throw new Error("worker is required for local mode");
|
|
552
|
+
this.localWorker = zkcloudworker;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Starts a new job for the proof calculation using serverless api call
|
|
557
|
+
* @param data the data for the proof call
|
|
558
|
+
* @param data.developer the developer
|
|
559
|
+
* @param data.repo the repo to use
|
|
560
|
+
* @param data.transactions the transactions
|
|
561
|
+
* @param data.task the task of the job
|
|
562
|
+
* @param data.userId the userId of the job
|
|
563
|
+
* @param data.args the arguments of the job, should be serialized JSON or string
|
|
564
|
+
* @param data.metadata the metadata of the job, should be serialized JSON or string
|
|
565
|
+
* @param data.webhook the webhook for the job
|
|
566
|
+
* @returns { success: boolean, error?: string, jobId?: string }
|
|
567
|
+
* where jonId is the jobId of the job
|
|
568
|
+
*
|
|
569
|
+
* The developers repo should provide a zkcloudworker function
|
|
570
|
+
* that can be called with the given parameters, see the examples
|
|
571
|
+
*/
|
|
572
|
+
async recursiveProof(data) {
|
|
573
|
+
const result = await this.apiHub("recursiveProof", data);
|
|
574
|
+
if (result.data === "error" || typeof result.data === "string" && result.data.startsWith("error"))
|
|
575
|
+
return {
|
|
576
|
+
success: false,
|
|
577
|
+
error: result.error
|
|
578
|
+
};
|
|
579
|
+
else if (result.success === false || result.data?.success === false)
|
|
580
|
+
return {
|
|
581
|
+
success: false,
|
|
582
|
+
error: result.error ?? result.data?.error ?? "recursiveProof call failed"
|
|
583
|
+
};
|
|
584
|
+
else if (result.success === true && result.data?.success === true && result.data?.jobId !== void 0)
|
|
585
|
+
return {
|
|
586
|
+
success: result.success,
|
|
587
|
+
jobId: result.data.jobId,
|
|
588
|
+
error: result.error
|
|
589
|
+
};
|
|
590
|
+
else
|
|
591
|
+
return {
|
|
592
|
+
success: false,
|
|
593
|
+
error: "recursiveProof call error"
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Starts a new job for the function call using serverless api call
|
|
598
|
+
* @param data the data for the proof call
|
|
599
|
+
* @param data.developer the developer
|
|
600
|
+
* @param data.repo the repo to use
|
|
601
|
+
* @param data.transactions the transactions
|
|
602
|
+
* @param data.task the task of the job
|
|
603
|
+
* @param data.userId the userId of the job
|
|
604
|
+
* @param data.args the arguments of the job
|
|
605
|
+
* @param data.metadata the metadata of the job
|
|
606
|
+
* @param data.mode the mode of the job execution: "sync" will not create a job, it will execute the function synchronously within 30 seconds and with the memory limit of 256 MB
|
|
607
|
+
* @returns { success: boolean, error?: string, jobId?: string, result?: any }
|
|
608
|
+
* where jonId is the jobId of the job (for async calls), result is the result of the job (for sync calls)
|
|
609
|
+
*/
|
|
610
|
+
async execute(data) {
|
|
611
|
+
const result = await this.apiHub("execute", data);
|
|
612
|
+
if (result.data === "error" || typeof result.data === "string" && result.data.startsWith("error"))
|
|
613
|
+
return {
|
|
614
|
+
success: false,
|
|
615
|
+
error: result.error
|
|
616
|
+
};
|
|
617
|
+
else if (result.success === false || result.data?.success === false)
|
|
618
|
+
return {
|
|
619
|
+
success: false,
|
|
620
|
+
error: result.error ?? result.data?.error ?? "execute call failed"
|
|
621
|
+
};
|
|
622
|
+
else if (result.success === true && data.mode === "sync" && result.data !== void 0)
|
|
623
|
+
return {
|
|
624
|
+
success: result.success,
|
|
625
|
+
jobId: void 0,
|
|
626
|
+
result: result.data,
|
|
627
|
+
error: result.error
|
|
628
|
+
};
|
|
629
|
+
else if (result.success === true && data.mode !== "sync" && result.data?.success === true && result.data?.jobId !== void 0)
|
|
630
|
+
return {
|
|
631
|
+
success: result.success,
|
|
632
|
+
jobId: result.data.jobId,
|
|
633
|
+
result: void 0,
|
|
634
|
+
error: result.error
|
|
635
|
+
};
|
|
636
|
+
else
|
|
637
|
+
return {
|
|
638
|
+
success: false,
|
|
639
|
+
error: "execute call error"
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Sends transactions to the blockchain using serverless api call
|
|
644
|
+
* @param data the data for the proof call
|
|
645
|
+
* @param data.developer the developer
|
|
646
|
+
* @param data.repo the repo to use
|
|
647
|
+
* @param data.transactions the transactions
|
|
648
|
+
* @returns { success: boolean, error?: string, txId?: string[] }
|
|
649
|
+
* where txId is the transaction id of the transaction, in the sequence of the input transactions
|
|
650
|
+
*/
|
|
651
|
+
async sendTransactions(data) {
|
|
652
|
+
const result = await this.apiHub("sendTransactions", data);
|
|
653
|
+
if (result.data === "error")
|
|
654
|
+
return {
|
|
655
|
+
success: false,
|
|
656
|
+
error: result.error
|
|
657
|
+
};
|
|
658
|
+
else
|
|
659
|
+
return {
|
|
660
|
+
success: result.success,
|
|
661
|
+
txId: result.data,
|
|
662
|
+
error: result.error
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Gets the result of the job using serverless api call
|
|
667
|
+
* @param data the data for the jobResult call
|
|
668
|
+
* @param data.jobId the jobId of the job
|
|
669
|
+
* @param data.includeLogs include logs in the result, default is false
|
|
670
|
+
* @returns { success: boolean, error?: string, result?: any }
|
|
671
|
+
* where result is the result of the job
|
|
672
|
+
* if the job is not finished yet, the result will be undefined
|
|
673
|
+
* if the job failed, the result will be undefined and error will be set
|
|
674
|
+
* if the job is finished, the result will be set and error will be undefined
|
|
675
|
+
* if the job is not found, the result will be undefined and error will be set
|
|
676
|
+
*/
|
|
677
|
+
async jobResult(data) {
|
|
678
|
+
const result = await this.apiHub("jobResult", data);
|
|
679
|
+
if (this.isError(result.data))
|
|
680
|
+
return {
|
|
681
|
+
success: false,
|
|
682
|
+
error: result.error,
|
|
683
|
+
result: result.data
|
|
684
|
+
};
|
|
685
|
+
else
|
|
686
|
+
return {
|
|
687
|
+
success: result.success,
|
|
688
|
+
error: result.error,
|
|
689
|
+
result: result.success ? result.data : result.data
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Deploys the code to the cloud using serverless api call
|
|
694
|
+
* @param data the data for the deploy call
|
|
695
|
+
* @param data.repo the repo to use
|
|
696
|
+
* @param data.developer the developer
|
|
697
|
+
* @param data.packageManager the package manager to use
|
|
698
|
+
* @returns { success: boolean, error?: string, jobId?: string}
|
|
699
|
+
* where jobId is the jobId of the job
|
|
700
|
+
*/
|
|
701
|
+
async deploy(data) {
|
|
702
|
+
const { repo, developer, packageManager } = data;
|
|
703
|
+
const result = await this.apiHub("deploy", {
|
|
704
|
+
developer,
|
|
705
|
+
repo,
|
|
706
|
+
args: packageManager
|
|
707
|
+
});
|
|
708
|
+
if (result.data === "error" || typeof result.data === "string" && result.data.startsWith("error"))
|
|
709
|
+
return {
|
|
710
|
+
success: false,
|
|
711
|
+
error: result.error
|
|
712
|
+
};
|
|
713
|
+
else
|
|
714
|
+
return {
|
|
715
|
+
success: result.success && result.data?.success,
|
|
716
|
+
jobId: result.data?.jobId,
|
|
717
|
+
error: result.error
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Gets the billing report for the jobs sent using JWT
|
|
722
|
+
* @returns { success: boolean, error?: string, result?: any }
|
|
723
|
+
* where result is the billing report
|
|
724
|
+
*/
|
|
725
|
+
async queryBilling() {
|
|
726
|
+
const result = await this.apiHub("queryBilling", {});
|
|
727
|
+
if (this.isError(result.data))
|
|
728
|
+
return {
|
|
729
|
+
success: false,
|
|
730
|
+
error: result.error,
|
|
731
|
+
result: result.data
|
|
732
|
+
};
|
|
733
|
+
else
|
|
734
|
+
return {
|
|
735
|
+
success: result.success,
|
|
736
|
+
error: result.error,
|
|
737
|
+
result: result.data
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Gets the remaining balance
|
|
742
|
+
* @returns { success: boolean, error?: string, result?: any }
|
|
743
|
+
* where result is the balance
|
|
744
|
+
*/
|
|
745
|
+
async getBalance() {
|
|
746
|
+
const result = await this.apiHub("getBalance", {});
|
|
747
|
+
if (this.isError(result.data))
|
|
748
|
+
return {
|
|
749
|
+
success: false,
|
|
750
|
+
error: result.error,
|
|
751
|
+
result: result.data
|
|
752
|
+
};
|
|
753
|
+
else
|
|
754
|
+
return {
|
|
755
|
+
success: result.success,
|
|
756
|
+
error: result.error,
|
|
757
|
+
result: result.data
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Waits for the job to finish
|
|
762
|
+
* @param data the data for the waitForJobResult call
|
|
763
|
+
* @param data.jobId the jobId of the job
|
|
764
|
+
* @param data.maxAttempts the maximum number of attempts, default is 360 (2 hours)
|
|
765
|
+
* @param data.interval the interval between attempts, default is 20000 (20 seconds)
|
|
766
|
+
* @param data.maxErrors the maximum number of network errors, default is 10
|
|
767
|
+
* @param data.printLogs print logs, default is true
|
|
768
|
+
* @returns { success: boolean, error?: string, result?: any }
|
|
769
|
+
* where result is the result of the job
|
|
770
|
+
*/
|
|
771
|
+
async waitForJobResult(data) {
|
|
772
|
+
if (this.jwt === "local")
|
|
773
|
+
return this.jobResult({ jobId: data.jobId });
|
|
774
|
+
const maxAttempts = data?.maxAttempts ?? 360;
|
|
775
|
+
const interval = data?.interval ?? 1e4;
|
|
776
|
+
const maxErrors = data?.maxErrors ?? 10;
|
|
777
|
+
const errorDelay = 3e4;
|
|
778
|
+
const printedLogs = [];
|
|
779
|
+
const printLogs = data.printLogs ?? true;
|
|
780
|
+
function print(logs) {
|
|
781
|
+
logs.forEach((log) => {
|
|
782
|
+
if (printedLogs.includes(log) === false) {
|
|
783
|
+
printedLogs.push(log);
|
|
784
|
+
if (printLogs) {
|
|
785
|
+
const text = log.replace(/error/gi, (matched) => import_chalk.default.red(matched));
|
|
786
|
+
console.log(text);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
let attempts = 0;
|
|
792
|
+
let errors = 0;
|
|
793
|
+
while (attempts < maxAttempts) {
|
|
794
|
+
const result = await this.apiHub("jobResult", {
|
|
795
|
+
jobId: data.jobId,
|
|
796
|
+
includeLogs: printLogs
|
|
797
|
+
});
|
|
798
|
+
const isAllLogsFetched = result?.data?.isFullLog === true || printLogs === false;
|
|
799
|
+
if (printLogs === true && result?.data?.logs !== void 0 && result?.data?.logs !== null && Array.isArray(result.data.logs) === true)
|
|
800
|
+
print(result.data.logs);
|
|
801
|
+
if (result.success === false) {
|
|
802
|
+
errors++;
|
|
803
|
+
if (errors > maxErrors) {
|
|
804
|
+
return {
|
|
805
|
+
success: false,
|
|
806
|
+
error: "Too many network errors",
|
|
807
|
+
result: void 0
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
await (0, import_mina_utils2.sleep)(errorDelay * errors);
|
|
811
|
+
} else {
|
|
812
|
+
if (this.isError(result.data) && isAllLogsFetched)
|
|
813
|
+
return {
|
|
814
|
+
success: false,
|
|
815
|
+
error: result.error,
|
|
816
|
+
result: result.data
|
|
817
|
+
};
|
|
818
|
+
else if (result.data?.result !== void 0 && isAllLogsFetched) {
|
|
819
|
+
return {
|
|
820
|
+
success: result.success,
|
|
821
|
+
error: result.error,
|
|
822
|
+
result: result.data
|
|
823
|
+
};
|
|
824
|
+
} else if (result.data?.jobStatus === "failed" && isAllLogsFetched) {
|
|
825
|
+
return {
|
|
826
|
+
success: false,
|
|
827
|
+
error: "Job failed",
|
|
828
|
+
result: result.data
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
await (0, import_mina_utils2.sleep)(interval);
|
|
832
|
+
}
|
|
833
|
+
attempts++;
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
success: false,
|
|
837
|
+
error: "Timeout",
|
|
838
|
+
result: void 0
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Calls the serverless API
|
|
843
|
+
* @param command the command of the API
|
|
844
|
+
* @param data the data of the API
|
|
845
|
+
* */
|
|
846
|
+
async apiHub(command, data) {
|
|
847
|
+
if (this.jwt === "local") {
|
|
848
|
+
if (this.localWorker === void 0)
|
|
849
|
+
throw new Error("localWorker is undefined");
|
|
850
|
+
switch (command) {
|
|
851
|
+
case "recursiveProof": {
|
|
852
|
+
const jobId = await LocalCloud.run({
|
|
853
|
+
command: "recursiveProof",
|
|
854
|
+
data,
|
|
855
|
+
chain: this.chain,
|
|
856
|
+
localWorker: this.localWorker
|
|
857
|
+
});
|
|
858
|
+
return {
|
|
859
|
+
success: true,
|
|
860
|
+
data: { success: true, jobId }
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
case "execute": {
|
|
864
|
+
const jobId = await LocalCloud.run({
|
|
865
|
+
command: "execute",
|
|
866
|
+
data,
|
|
867
|
+
chain: this.chain,
|
|
868
|
+
localWorker: this.localWorker
|
|
869
|
+
});
|
|
870
|
+
if (data.mode === "sync")
|
|
871
|
+
return {
|
|
872
|
+
success: true,
|
|
873
|
+
data: LocalStorage.jobEvents[jobId].result
|
|
874
|
+
};
|
|
875
|
+
else
|
|
876
|
+
return {
|
|
877
|
+
success: true,
|
|
878
|
+
data: { success: true, jobId }
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
case "jobResult": {
|
|
882
|
+
const job = LocalStorage.jobs[data.jobId];
|
|
883
|
+
if (job === void 0) {
|
|
884
|
+
return {
|
|
885
|
+
success: false,
|
|
886
|
+
error: "local job not found"
|
|
887
|
+
};
|
|
888
|
+
} else {
|
|
889
|
+
return {
|
|
890
|
+
success: true,
|
|
891
|
+
data: job
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
case "sendTransactions": {
|
|
896
|
+
return {
|
|
897
|
+
success: true,
|
|
898
|
+
data: await LocalCloud.addTransactions(data.transactions)
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
case "deploy":
|
|
902
|
+
return {
|
|
903
|
+
success: true,
|
|
904
|
+
data: "local_deploy"
|
|
905
|
+
};
|
|
906
|
+
case "queryBilling":
|
|
907
|
+
return {
|
|
908
|
+
success: true,
|
|
909
|
+
data: "local_queryBilling"
|
|
910
|
+
};
|
|
911
|
+
default:
|
|
912
|
+
return {
|
|
913
|
+
success: false,
|
|
914
|
+
error: "local_error"
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
} else {
|
|
918
|
+
if (this.endpoint === void 0)
|
|
919
|
+
throw new Error("zkCloudWorker supports only mainnet, devnet and zeko chains in the cloud.");
|
|
920
|
+
const apiData = {
|
|
921
|
+
auth: ZKCLOUDWORKER_AUTH,
|
|
922
|
+
command,
|
|
923
|
+
jwtToken: this.jwt,
|
|
924
|
+
data,
|
|
925
|
+
chain: this.chain,
|
|
926
|
+
webhook: this.webhook
|
|
927
|
+
// TODO: implement webhook code on AWS
|
|
928
|
+
};
|
|
929
|
+
try {
|
|
930
|
+
const response = await fetch(this.endpoint, {
|
|
931
|
+
method: "POST",
|
|
932
|
+
headers: {
|
|
933
|
+
"Content-Type": "application/json"
|
|
934
|
+
},
|
|
935
|
+
body: JSON.stringify(apiData)
|
|
936
|
+
});
|
|
937
|
+
if (!response.ok) {
|
|
938
|
+
return {
|
|
939
|
+
success: false,
|
|
940
|
+
error: `fetch error: ${response.statusText} status: ${response.status}`
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
const data2 = await response.json();
|
|
944
|
+
return { success: true, data: data2 };
|
|
945
|
+
} catch (error) {
|
|
946
|
+
console.error("apiHub error:", error.message ?? error);
|
|
947
|
+
return { success: false, error };
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
952
|
+
isError(data) {
|
|
953
|
+
if (data === "error")
|
|
954
|
+
return true;
|
|
955
|
+
if (data?.jobStatus === "failed")
|
|
956
|
+
return true;
|
|
957
|
+
if (typeof data === "string" && data.toLowerCase().startsWith("error"))
|
|
958
|
+
return true;
|
|
959
|
+
if (data !== void 0 && data.error !== void 0)
|
|
960
|
+
return true;
|
|
961
|
+
return false;
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
|
|
965
|
+
// dist/node/tokens/token.js
|
|
966
|
+
var TokenAPI = class {
|
|
967
|
+
constructor(params) {
|
|
968
|
+
const { jwt, zkcloudworker, chain } = params;
|
|
969
|
+
if (jwt === void 0)
|
|
970
|
+
throw new Error("jwt is undefined");
|
|
971
|
+
this.client = new zkCloudWorkerClient({
|
|
972
|
+
jwt,
|
|
973
|
+
chain,
|
|
974
|
+
zkcloudworker
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
async proveTransaction(params) {
|
|
978
|
+
return this.proveTransactions([params]);
|
|
979
|
+
}
|
|
980
|
+
async proveTransactions(params) {
|
|
981
|
+
const transactions = [];
|
|
982
|
+
for (const tx of params) {
|
|
983
|
+
const transaction = JSON.stringify(tx, null, 2);
|
|
984
|
+
transactions.push(transaction);
|
|
985
|
+
}
|
|
986
|
+
const { request, symbol } = params[0];
|
|
987
|
+
const { txType } = request;
|
|
988
|
+
const answer = await this.client.execute({
|
|
989
|
+
developer: "DFST",
|
|
990
|
+
repo: "token-launchpad",
|
|
991
|
+
transactions,
|
|
992
|
+
task: "prove",
|
|
993
|
+
args: JSON.stringify({ tokenAddress: params[0].request.tokenAddress }),
|
|
994
|
+
metadata: `${params.length > 1 ? "airdrop" : txType.replace(/^token:/, "")} token${symbol ? ` ${symbol}` : ""}${params.length > 1 ? ` (${params.length} txs)` : ""}`
|
|
995
|
+
});
|
|
996
|
+
const jobId = answer.jobId;
|
|
997
|
+
if (jobId === void 0)
|
|
998
|
+
console.error("Job ID is undefined", { answer, txType, symbol });
|
|
999
|
+
return jobId;
|
|
1000
|
+
}
|
|
1001
|
+
// Warning: this function will block the thread until the job is done and will print logs to the console
|
|
1002
|
+
// Do not use it in "use server" functions, use getResults instead
|
|
1003
|
+
async waitForJobResults(params) {
|
|
1004
|
+
const deployResult = await this.client.waitForJobResult(params);
|
|
1005
|
+
console.log("waitForJobResult result:", deployResult?.result?.result?.slice(0, 50));
|
|
1006
|
+
return deployResult?.result?.result ?? "error";
|
|
1007
|
+
}
|
|
1008
|
+
async getResults(jobId) {
|
|
1009
|
+
try {
|
|
1010
|
+
const callResult = await this.client.jobResult({ jobId });
|
|
1011
|
+
const jobStatus = typeof callResult?.result === "string" ? void 0 : callResult?.result?.jobStatus;
|
|
1012
|
+
if (!callResult.success) {
|
|
1013
|
+
return {
|
|
1014
|
+
success: false,
|
|
1015
|
+
error: callResult?.error,
|
|
1016
|
+
jobStatus
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
const jobResult = callResult.result?.result;
|
|
1020
|
+
if (callResult.error)
|
|
1021
|
+
return {
|
|
1022
|
+
success: false,
|
|
1023
|
+
error: callResult.error,
|
|
1024
|
+
jobStatus
|
|
1025
|
+
};
|
|
1026
|
+
if (!jobResult)
|
|
1027
|
+
return { success: true, jobStatus };
|
|
1028
|
+
if (jobResult.toLowerCase().startsWith("error"))
|
|
1029
|
+
return {
|
|
1030
|
+
success: false,
|
|
1031
|
+
error: jobResult,
|
|
1032
|
+
jobStatus
|
|
1033
|
+
};
|
|
1034
|
+
try {
|
|
1035
|
+
const { proofs } = JSON.parse(jobResult);
|
|
1036
|
+
const results = [];
|
|
1037
|
+
for (const proof of proofs) {
|
|
1038
|
+
const { success, tx, hash, error } = JSON.parse(proof);
|
|
1039
|
+
results.push({ success, tx, hash, error });
|
|
1040
|
+
}
|
|
1041
|
+
return { success: true, results, jobStatus };
|
|
1042
|
+
} catch (e) {
|
|
1043
|
+
return {
|
|
1044
|
+
success: false,
|
|
1045
|
+
error: `Error parsing job result: ${jobResult} ${e?.message ?? ""}`,
|
|
1046
|
+
jobStatus
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
} catch (e) {
|
|
1050
|
+
return {
|
|
1051
|
+
success: false,
|
|
1052
|
+
error: `Error getting job result: ${e?.message ?? ""}`,
|
|
1053
|
+
jobStatus: void 0
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
|
|
1059
|
+
// dist/node/tokens/nft.js
|
|
1060
|
+
var NftAPI = class {
|
|
1061
|
+
constructor(params) {
|
|
1062
|
+
const { jwt, zkcloudworker, chain } = params;
|
|
1063
|
+
if (jwt === void 0)
|
|
1064
|
+
throw new Error("jwt is undefined");
|
|
1065
|
+
this.client = new zkCloudWorkerClient({
|
|
1066
|
+
jwt,
|
|
1067
|
+
chain,
|
|
1068
|
+
zkcloudworker
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
async proveTransaction(params) {
|
|
1072
|
+
return this.proveTransactions([params]);
|
|
1073
|
+
}
|
|
1074
|
+
async proveTransactions(params, name) {
|
|
1075
|
+
const transactions = [];
|
|
1076
|
+
for (const tx of params) {
|
|
1077
|
+
const transaction = JSON.stringify(tx, null, 2);
|
|
1078
|
+
transactions.push(transaction);
|
|
1079
|
+
}
|
|
1080
|
+
const { request, symbol } = params[0];
|
|
1081
|
+
const { txType } = request;
|
|
1082
|
+
const answer = await this.client.execute({
|
|
1083
|
+
developer: "DFST",
|
|
1084
|
+
repo: "nft-agent",
|
|
1085
|
+
transactions,
|
|
1086
|
+
task: "prove",
|
|
1087
|
+
args: JSON.stringify({
|
|
1088
|
+
collectionAddress: params[0].request.collectionAddress
|
|
1089
|
+
}),
|
|
1090
|
+
metadata: `${params.length > 1 ? "mint" : txType.replace(/^nft:/, "")} ${symbol ? ` ${symbol}` : ""} ${params.length > 1 ? `(${params.length} txs)` : txType === "nft:launch" ? "Collection " + request.collectionName : txType === "nft:mint" ? request.nftMintParams.name : name ?? ""}`
|
|
1091
|
+
});
|
|
1092
|
+
const jobId = answer.jobId;
|
|
1093
|
+
if (jobId === void 0)
|
|
1094
|
+
console.error("Job ID is undefined", { answer, txType, symbol });
|
|
1095
|
+
return jobId;
|
|
1096
|
+
}
|
|
1097
|
+
// Warning: this function will block the thread until the job is done and will print logs to the console
|
|
1098
|
+
// Do not use it in "use server" functions, use getResults instead
|
|
1099
|
+
async waitForJobResults(params) {
|
|
1100
|
+
const deployResult = await this.client.waitForJobResult(params);
|
|
1101
|
+
console.log("waitForJobResult result:", deployResult?.result?.result?.slice(0, 50));
|
|
1102
|
+
return deployResult?.result?.result ?? "error";
|
|
1103
|
+
}
|
|
1104
|
+
async getResults(jobId) {
|
|
1105
|
+
try {
|
|
1106
|
+
const callResult = await this.client.jobResult({ jobId });
|
|
1107
|
+
const jobStatus = typeof callResult?.result === "string" ? void 0 : callResult?.result?.jobStatus;
|
|
1108
|
+
if (!callResult.success) {
|
|
1109
|
+
return {
|
|
1110
|
+
success: false,
|
|
1111
|
+
error: callResult?.error,
|
|
1112
|
+
jobStatus
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
const jobResult = callResult.result?.result;
|
|
1116
|
+
if (callResult.error)
|
|
1117
|
+
return {
|
|
1118
|
+
success: false,
|
|
1119
|
+
error: callResult.error,
|
|
1120
|
+
jobStatus
|
|
1121
|
+
};
|
|
1122
|
+
if (!jobResult)
|
|
1123
|
+
return { success: true, jobStatus };
|
|
1124
|
+
if (jobResult.toLowerCase().startsWith("error"))
|
|
1125
|
+
return {
|
|
1126
|
+
success: false,
|
|
1127
|
+
error: jobResult,
|
|
1128
|
+
jobStatus
|
|
1129
|
+
};
|
|
1130
|
+
try {
|
|
1131
|
+
const { proofs } = JSON.parse(jobResult);
|
|
1132
|
+
const results = [];
|
|
1133
|
+
for (const proof of proofs) {
|
|
1134
|
+
const { success, tx, hash, error } = JSON.parse(proof);
|
|
1135
|
+
results.push({ success, tx, hash, error });
|
|
1136
|
+
}
|
|
1137
|
+
return { success: true, results, jobStatus };
|
|
1138
|
+
} catch (e) {
|
|
1139
|
+
return {
|
|
1140
|
+
success: false,
|
|
1141
|
+
error: `Error parsing job result: ${jobResult} ${e?.message ?? ""}`,
|
|
1142
|
+
jobStatus
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
} catch (e) {
|
|
1146
|
+
return {
|
|
1147
|
+
success: false,
|
|
1148
|
+
error: `Error getting job result: ${e?.message ?? ""}`,
|
|
1149
|
+
jobStatus: void 0
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1155
|
+
0 && (module.exports = {
|
|
1156
|
+
LocalCloud,
|
|
1157
|
+
LocalStorage,
|
|
1158
|
+
NftAPI,
|
|
1159
|
+
TokenAPI,
|
|
1160
|
+
zkCloudWorkerClient
|
|
1161
|
+
});
|