zkcloudworker 0.1.18 → 0.1.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. package/LICENSE +201 -0
  2. package/dist/README.md +79 -0
  3. package/dist/package.json +49 -0
  4. package/dist/yarn.lock +94 -0
  5. package/jest-config.ts +32 -0
  6. package/package.json +50 -40
  7. package/src/api/api.ts +250 -0
  8. package/src/api/client-api.ts +186 -0
  9. package/src/cloud/cloud.ts +22 -0
  10. package/src/cloud/local.ts +34 -0
  11. package/src/config.ts +9 -0
  12. package/src/custom/backend.ts +30 -0
  13. package/src/fee.ts +7 -0
  14. package/src/index.ts +38 -0
  15. package/src/mina.ts +230 -0
  16. package/src/networks.ts +88 -0
  17. package/tsconfig.json +41 -0
  18. package/tsconfig.web.json +29 -0
  19. /package/{lib → dist/lib}/ts/src/api/api.d.ts +0 -0
  20. /package/{lib → dist/lib}/ts/src/api/api.js +0 -0
  21. /package/{lib → dist/lib}/ts/src/api/client-api.d.ts +0 -0
  22. /package/{lib → dist/lib}/ts/src/api/client-api.js +0 -0
  23. /package/{lib → dist/lib}/ts/src/cloud/cloud.d.ts +0 -0
  24. /package/{lib → dist/lib}/ts/src/cloud/cloud.js +0 -0
  25. /package/{lib → dist/lib}/ts/src/cloud/local.d.ts +0 -0
  26. /package/{lib → dist/lib}/ts/src/cloud/local.js +0 -0
  27. /package/{lib → dist/lib}/ts/src/config.d.ts +0 -0
  28. /package/{lib → dist/lib}/ts/src/config.js +0 -0
  29. /package/{lib → dist/lib}/ts/src/custom/backend.d.ts +0 -0
  30. /package/{lib → dist/lib}/ts/src/custom/backend.js +0 -0
  31. /package/{lib → dist/lib}/ts/src/fee.d.ts +0 -0
  32. /package/{lib → dist/lib}/ts/src/fee.js +0 -0
  33. /package/{lib → dist/lib}/ts/src/index.d.ts +0 -0
  34. /package/{lib → dist/lib}/ts/src/index.js +0 -0
  35. /package/{lib → dist/lib}/ts/src/mina.d.ts +0 -0
  36. /package/{lib → dist/lib}/ts/src/mina.js +0 -0
  37. /package/{lib → dist/lib}/ts/src/networks.d.ts +0 -0
  38. /package/{lib → dist/lib}/ts/src/networks.js +0 -0
  39. /package/{lib → dist/lib}/ts/tsconfig.tsbuildinfo +0 -0
  40. /package/{lib → dist/lib}/web/src/api/api.d.ts +0 -0
  41. /package/{lib → dist/lib}/web/src/api/api.js +0 -0
  42. /package/{lib → dist/lib}/web/src/api/api.js.map +0 -0
  43. /package/{lib → dist/lib}/web/src/api/client-api.d.ts +0 -0
  44. /package/{lib → dist/lib}/web/src/api/client-api.js +0 -0
  45. /package/{lib → dist/lib}/web/src/api/client-api.js.map +0 -0
  46. /package/{lib → dist/lib}/web/src/cloud/cloud.d.ts +0 -0
  47. /package/{lib → dist/lib}/web/src/cloud/cloud.js +0 -0
  48. /package/{lib → dist/lib}/web/src/cloud/cloud.js.map +0 -0
  49. /package/{lib → dist/lib}/web/src/cloud/local.d.ts +0 -0
  50. /package/{lib → dist/lib}/web/src/cloud/local.js +0 -0
  51. /package/{lib → dist/lib}/web/src/cloud/local.js.map +0 -0
  52. /package/{lib → dist/lib}/web/src/config.d.ts +0 -0
  53. /package/{lib → dist/lib}/web/src/config.js +0 -0
  54. /package/{lib → dist/lib}/web/src/config.js.map +0 -0
  55. /package/{lib → dist/lib}/web/src/custom/backend.d.ts +0 -0
  56. /package/{lib → dist/lib}/web/src/custom/backend.js +0 -0
  57. /package/{lib → dist/lib}/web/src/custom/backend.js.map +0 -0
  58. /package/{lib → dist/lib}/web/src/fee.d.ts +0 -0
  59. /package/{lib → dist/lib}/web/src/fee.js +0 -0
  60. /package/{lib → dist/lib}/web/src/fee.js.map +0 -0
  61. /package/{lib → dist/lib}/web/src/index.d.ts +0 -0
  62. /package/{lib → dist/lib}/web/src/index.js +0 -0
  63. /package/{lib → dist/lib}/web/src/index.js.map +0 -0
  64. /package/{lib → dist/lib}/web/src/mina.d.ts +0 -0
  65. /package/{lib → dist/lib}/web/src/mina.js +0 -0
  66. /package/{lib → dist/lib}/web/src/mina.js.map +0 -0
  67. /package/{lib → dist/lib}/web/src/networks.d.ts +0 -0
  68. /package/{lib → dist/lib}/web/src/networks.js +0 -0
  69. /package/{lib → dist/lib}/web/src/networks.js.map +0 -0
  70. /package/{lib → dist/lib}/web/tsconfig.web.tsbuildinfo +0 -0
package/src/mina.ts ADDED
@@ -0,0 +1,230 @@
1
+ export {
2
+ initBlockchain,
3
+ Memory,
4
+ makeString,
5
+ sleep,
6
+ accountBalance,
7
+ accountBalanceMina,
8
+ formatTime,
9
+ MinaNetworkInstance,
10
+ currentNetwork,
11
+ getNetworkIdHash,
12
+ };
13
+
14
+ import {
15
+ Mina,
16
+ PublicKey,
17
+ PrivateKey,
18
+ UInt64,
19
+ fetchAccount,
20
+ Field,
21
+ CircuitString,
22
+ } from "o1js";
23
+ import { networks, blockchain, MinaNetwork, Local } from "./networks";
24
+
25
+ interface MinaNetworkInstance {
26
+ keys: {
27
+ publicKey: PublicKey;
28
+ privateKey: PrivateKey;
29
+ }[];
30
+ network: MinaNetwork;
31
+ networkIdHash: Field;
32
+ }
33
+
34
+ let currentNetwork: MinaNetworkInstance | undefined = undefined;
35
+
36
+ function getNetworkIdHash(): Field {
37
+ if (currentNetwork === undefined) {
38
+ throw new Error("Network is not initialized");
39
+ }
40
+ return currentNetwork.networkIdHash;
41
+ }
42
+
43
+ /*function getNetworkIdHash(params: {
44
+ chainId?: blockchain;
45
+ verbose?: boolean;
46
+ }): Field {
47
+ const { chainId, verbose } = params;
48
+ if (chainId !== undefined) {
49
+ if (verbose) console.log(`Chain ID: ${chainId}`);
50
+ return CircuitString.fromString(chainId).hash();
51
+ }
52
+ const networkId = Mina.getNetworkId();
53
+ if (verbose) console.log(`Network ID: ${networkId}`);
54
+ if (networkId === "testnet")
55
+ throw new Error(
56
+ "Network ID is not set, please call initBlockchain() first"
57
+ );
58
+
59
+ if (networkId === "mainnet")
60
+ return CircuitString.fromString("mainnet").hash();
61
+ else {
62
+ if (
63
+ networkId.custom === undefined ||
64
+ typeof networkId.custom !== "string"
65
+ ) {
66
+ throw new Error(
67
+ "Network ID is not set, please call initBlockchain() first"
68
+ );
69
+ }
70
+ return CircuitString.fromString(networkId.custom).hash();
71
+ }
72
+ }
73
+ */
74
+
75
+ function initBlockchain(
76
+ instance: blockchain,
77
+ deployersNumber: number = 0
78
+ ): MinaNetworkInstance {
79
+ if (instance === "mainnet") {
80
+ throw new Error("Mainnet is not supported yet by zkApps");
81
+ }
82
+
83
+ if (instance === "local") {
84
+ const local = Mina.LocalBlockchain({
85
+ proofsEnabled: true,
86
+ });
87
+ Mina.setActiveInstance(local);
88
+ currentNetwork = {
89
+ keys: local.testAccounts,
90
+ network: Local,
91
+ networkIdHash: CircuitString.fromString("local").hash(),
92
+ };
93
+ return currentNetwork;
94
+ }
95
+
96
+ const network = networks.find((n) => n.chainId === instance);
97
+ if (network === undefined) {
98
+ throw new Error("Unknown network");
99
+ }
100
+
101
+ const networkInstance = Mina.Network({
102
+ mina: network.mina,
103
+ archive: network.archive,
104
+ lightnetAccountManager: network.accountManager,
105
+ });
106
+ Mina.setActiveInstance(networkInstance);
107
+
108
+ const keys: {
109
+ publicKey: PublicKey;
110
+ privateKey: PrivateKey;
111
+ }[] = [];
112
+
113
+ if (deployersNumber > 0) {
114
+ if (instance === "lighnet") {
115
+ throw new Error(
116
+ "Use await Lightnet.acquireKeyPair() to get keys for Lightnet"
117
+ );
118
+ } else {
119
+ const deployers = process.env.DEPLOYERS;
120
+ if (
121
+ deployers === undefined ||
122
+ Array.isArray(deployers) === false ||
123
+ deployers.length < deployersNumber
124
+ )
125
+ throw new Error("Deployers are not set");
126
+ for (let i = 0; i < deployersNumber; i++) {
127
+ const privateKey = PrivateKey.fromBase58(deployers[i]);
128
+ const publicKey = privateKey.toPublicKey();
129
+ keys.push({ publicKey, privateKey });
130
+ }
131
+ }
132
+ }
133
+
134
+ currentNetwork = {
135
+ keys,
136
+ network,
137
+ networkIdHash: CircuitString.fromString(instance).hash(),
138
+ };
139
+ return currentNetwork;
140
+ }
141
+
142
+ async function accountBalance(address: PublicKey): Promise<UInt64> {
143
+ await fetchAccount({ publicKey: address });
144
+ if (Mina.hasAccount(address)) return Mina.getBalance(address);
145
+ else return UInt64.from(0);
146
+ }
147
+
148
+ async function accountBalanceMina(address: PublicKey): Promise<number> {
149
+ return Number((await accountBalance(address)).toBigInt()) / 1e9;
150
+ }
151
+
152
+ function sleep(ms: number) {
153
+ return new Promise((resolve) => setTimeout(resolve, ms));
154
+ }
155
+
156
+ function makeString(length: number): string {
157
+ // eslint-disable-next-line @typescript-eslint/no-inferrable-types
158
+ let outString: string = ``;
159
+ // eslint-disable-next-line @typescript-eslint/no-inferrable-types
160
+ const inOptions: string = `abcdefghijklmnopqrstuvwxyz0123456789`;
161
+
162
+ for (let i = 0; i < length; i++) {
163
+ outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));
164
+ }
165
+
166
+ return outString;
167
+ }
168
+
169
+ function formatTime(ms: number): string {
170
+ if (ms === undefined) return "";
171
+ if (ms < 1000) return ms.toString() + " ms";
172
+ if (ms < 60 * 1000)
173
+ return parseInt((ms / 1000).toString()).toString() + " sec";
174
+ if (ms < 60 * 60 * 1000) {
175
+ const minutes = parseInt((ms / 1000 / 60).toString());
176
+ const seconds = parseInt(((ms - minutes * 60 * 1000) / 1000).toString());
177
+ return minutes.toString() + " min " + seconds.toString() + " sec";
178
+ } else {
179
+ const hours = parseInt((ms / 1000 / 60 / 60).toString());
180
+ const minutes = parseInt(
181
+ ((ms - hours * 60 * 60 * 1000) / 1000 / 60).toString()
182
+ );
183
+ return hours.toString() + " h " + minutes.toString() + " min";
184
+ }
185
+ }
186
+
187
+ class Memory {
188
+ // eslint-disable-next-line @typescript-eslint/no-inferrable-types
189
+ static rss: number = 0;
190
+ constructor() {
191
+ Memory.rss = 0;
192
+ }
193
+
194
+ // eslint-disable-next-line @typescript-eslint/no-inferrable-types
195
+ public static info(description: string = ``, fullInfo: boolean = false) {
196
+ const memoryData = process.memoryUsage();
197
+ const formatMemoryUsage = (data: number) =>
198
+ `${Math.round(data / 1024 / 1024)} MB`;
199
+ const oldRSS = Memory.rss;
200
+ Memory.rss = Math.round(memoryData.rss / 1024 / 1024);
201
+
202
+ const memoryUsage = fullInfo
203
+ ? {
204
+ step: `${description}:`,
205
+ rssDelta: `${(oldRSS === 0
206
+ ? 0
207
+ : Memory.rss - oldRSS
208
+ ).toString()} MB -> Resident Set Size memory change`,
209
+ rss: `${formatMemoryUsage(
210
+ memoryData.rss
211
+ )} -> Resident Set Size - total memory allocated`,
212
+ heapTotal: `${formatMemoryUsage(
213
+ memoryData.heapTotal
214
+ )} -> total size of the allocated heap`,
215
+ heapUsed: `${formatMemoryUsage(
216
+ memoryData.heapUsed
217
+ )} -> actual memory used during the execution`,
218
+ external: `${formatMemoryUsage(
219
+ memoryData.external
220
+ )} -> V8 external memory`,
221
+ }
222
+ : `RSS memory ${description}: ${formatMemoryUsage(memoryData.rss)}${
223
+ oldRSS === 0
224
+ ? ``
225
+ : `, changed by ` + (Memory.rss - oldRSS).toString() + ` MB`
226
+ }`;
227
+
228
+ console.log(memoryUsage);
229
+ }
230
+ }
@@ -0,0 +1,88 @@
1
+ export {
2
+ blockchain,
3
+ MinaNetwork,
4
+ networks,
5
+ Mainnet,
6
+ Berkeley,
7
+ Zeko,
8
+ TestWorld2,
9
+ Lightnet,
10
+ Local,
11
+ };
12
+
13
+ type blockchain =
14
+ | "local"
15
+ | "berkeley"
16
+ | "lighnet"
17
+ | "mainnet"
18
+ | "testworld2"
19
+ | "zeko";
20
+
21
+ interface MinaNetwork {
22
+ mina: string[];
23
+ archive: string[];
24
+ chainId: blockchain;
25
+ name?: string;
26
+ accountManager?: string;
27
+ explorerAccountUrl?: string;
28
+ explorerTransactionUrl?: string;
29
+ }
30
+
31
+ const Mainnet: MinaNetwork = {
32
+ mina: [],
33
+ archive: [],
34
+ chainId: "mainnet",
35
+ };
36
+
37
+ const Local: MinaNetwork = {
38
+ mina: [],
39
+ archive: [],
40
+ chainId: "local",
41
+ };
42
+
43
+ const Berkeley: MinaNetwork = {
44
+ mina: [
45
+ "https://api.minascan.io/node/berkeley/v1/graphql",
46
+ "https://proxy.berkeley.minaexplorer.com/graphql",
47
+ ],
48
+ archive: [
49
+ "https://api.minascan.io/archive/berkeley/v1/graphql",
50
+ "https://archive.berkeley.minaexplorer.com",
51
+ ],
52
+ explorerAccountUrl: "https://minascan.io/berkeley/account/",
53
+ explorerTransactionUrl: "https://minascan.io/berkeley/tx/",
54
+ chainId: "berkeley",
55
+ name: "Berkeley",
56
+ };
57
+
58
+ const Zeko: MinaNetwork = {
59
+ mina: ["http://sequencer-zeko-dev.dcspark.io/graphql"],
60
+ archive: [],
61
+ chainId: "zeko",
62
+ };
63
+
64
+ const TestWorld2: MinaNetwork = {
65
+ mina: ["https://api.minascan.io/node/testworld/v1/graphql"],
66
+ archive: ["https://archive.testworld.minaexplorer.com"],
67
+ explorerAccountUrl: "https://minascan.io/testworld/account/",
68
+ explorerTransactionUrl: "https://minascan.io/testworld/tx/",
69
+ chainId: "testworld2",
70
+ name: "TestWorld2",
71
+ };
72
+
73
+ const Lightnet: MinaNetwork = {
74
+ mina: ["http://localhost:8080/graphql"],
75
+ archive: ["http://localhost:8282"],
76
+ accountManager: "http://localhost:8181",
77
+ chainId: "lighnet",
78
+ name: "Lightnet",
79
+ };
80
+
81
+ const networks: MinaNetwork[] = [
82
+ Mainnet,
83
+ Local,
84
+ Berkeley,
85
+ Zeko,
86
+ TestWorld2,
87
+ Lightnet,
88
+ ];
package/tsconfig.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2021" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
4
+ "lib": ["es2021"],
5
+ "module": "CommonJS" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
6
+ "outDir": "./dist/lib/ts" /* Redirect output structure to the directory. */,
7
+ "strict": true /* Enable all strict type-checking options. */,
8
+ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
9
+ "skipLibCheck": true /* Skip type checking of declaration files. */,
10
+ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
11
+ "composite": true,
12
+ "experimentalDecorators": true,
13
+ "emitDecoratorMetadata": true,
14
+ "strictPropertyInitialization": false,
15
+ "resolveJsonModule": true
16
+ },
17
+ "typedocOptions": {
18
+ "entryPoints": ["src/index.ts"],
19
+ "out": "docs",
20
+ "plugin": [],
21
+ "excludeExternals": true,
22
+ "sourceLinkTemplate": "https://github.com/zkcloudworker/zkcloudworker-lib/blob/master/{path}#L{line}"
23
+ },
24
+ "include": ["src/**/*"],
25
+ "exclude": [
26
+ "dist/**",
27
+ "tests/**",
28
+ "docs/**",
29
+ "experimental/**",
30
+ "mycache/**",
31
+ "nftcache/**",
32
+ "experimental/**",
33
+ "node_modules",
34
+ "deprecated/**",
35
+ "images/**"
36
+ ],
37
+ "ts-node": {
38
+ "esm": true, // «———— enabling ESM for ts-node
39
+ "experimentalSpecifierResolution": "node"
40
+ }
41
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "outDir": "./dist/lib/web",
5
+ "lib": [
6
+ "ES2021",
7
+ "DOM"
8
+ ],
9
+ "moduleResolution": "node",
10
+ "strict": true /* Enable all strict type-checking options. */,
11
+ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
12
+ "allowSyntheticDefaultImports": true,
13
+ "skipLibCheck": true /* Skip type checking of declaration files. */,
14
+ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
15
+ "composite": true,
16
+ "resolveJsonModule": true,
17
+ "experimentalDecorators": true,
18
+ "emitDecoratorMetadata": true,
19
+ "strictPropertyInitialization": false,
20
+ "useDefineForClassFields": false,
21
+ "importHelpers": true,
22
+ "noEmitOnError": false,
23
+ "allowJs": true,
24
+ "sourceMap": true
25
+ },
26
+ "include": [
27
+ "src/**/*"
28
+ ]
29
+ }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes