zkcloudworker 0.1.17 → 0.1.19
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/dist/README.md +79 -0
- package/dist/lib/ts/src/cloud/cloud.d.ts +12 -0
- package/dist/lib/ts/src/cloud/local.d.ts +13 -0
- package/dist/lib/ts/src/cloud/local.js +29 -0
- package/{lib → dist/lib}/ts/src/mina.js +10 -3
- package/dist/lib/ts/tsconfig.tsbuildinfo +1 -0
- package/dist/lib/web/src/cloud/cloud.d.ts +12 -0
- package/{lib → dist/lib}/web/src/cloud/cloud.js.map +1 -1
- package/dist/lib/web/src/cloud/local.d.ts +13 -0
- package/dist/lib/web/src/cloud/local.js +39 -0
- package/dist/lib/web/src/cloud/local.js.map +1 -0
- package/{lib → dist/lib}/web/src/mina.js +10 -3
- package/{lib → dist/lib}/web/src/mina.js.map +1 -1
- package/dist/lib/web/tsconfig.web.tsbuildinfo +1 -0
- package/dist/package.json +49 -0
- package/dist/yarn.lock +94 -0
- package/jest-config.ts +32 -0
- package/package.json +50 -40
- package/src/api/api.ts +250 -0
- package/src/api/client-api.ts +186 -0
- package/src/cloud/cloud.ts +22 -0
- package/src/cloud/local.ts +34 -0
- package/src/config.ts +9 -0
- package/src/custom/backend.ts +30 -0
- package/src/fee.ts +7 -0
- package/src/index.ts +38 -0
- package/src/mina.ts +230 -0
- package/src/networks.ts +88 -0
- package/tsconfig.json +41 -0
- package/tsconfig.web.json +29 -0
- package/lib/ts/src/cloud/cloud.d.ts +0 -7
- package/lib/ts/tsconfig.tsbuildinfo +0 -1
- package/lib/web/src/cloud/cloud.d.ts +0 -7
- package/lib/web/tsconfig.web.tsbuildinfo +0 -1
- /package/{lib → dist/lib}/ts/src/api/api.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/api/api.js +0 -0
- /package/{lib → dist/lib}/ts/src/api/client-api.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/api/client-api.js +0 -0
- /package/{lib → dist/lib}/ts/src/cloud/cloud.js +0 -0
- /package/{lib → dist/lib}/ts/src/config.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/config.js +0 -0
- /package/{lib → dist/lib}/ts/src/custom/backend.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/custom/backend.js +0 -0
- /package/{lib → dist/lib}/ts/src/fee.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/fee.js +0 -0
- /package/{lib → dist/lib}/ts/src/index.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/index.js +0 -0
- /package/{lib → dist/lib}/ts/src/mina.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/networks.d.ts +0 -0
- /package/{lib → dist/lib}/ts/src/networks.js +0 -0
- /package/{lib → dist/lib}/web/src/api/api.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/api/api.js +0 -0
- /package/{lib → dist/lib}/web/src/api/api.js.map +0 -0
- /package/{lib → dist/lib}/web/src/api/client-api.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/api/client-api.js +0 -0
- /package/{lib → dist/lib}/web/src/api/client-api.js.map +0 -0
- /package/{lib → dist/lib}/web/src/cloud/cloud.js +0 -0
- /package/{lib → dist/lib}/web/src/config.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/config.js +0 -0
- /package/{lib → dist/lib}/web/src/config.js.map +0 -0
- /package/{lib → dist/lib}/web/src/custom/backend.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/custom/backend.js +0 -0
- /package/{lib → dist/lib}/web/src/custom/backend.js.map +0 -0
- /package/{lib → dist/lib}/web/src/fee.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/fee.js +0 -0
- /package/{lib → dist/lib}/web/src/fee.js.map +0 -0
- /package/{lib → dist/lib}/web/src/index.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/index.js +0 -0
- /package/{lib → dist/lib}/web/src/index.js.map +0 -0
- /package/{lib → dist/lib}/web/src/mina.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/networks.d.ts +0 -0
- /package/{lib → dist/lib}/web/src/networks.js +0 -0
- /package/{lib → dist/lib}/web/src/networks.js.map +0 -0
@@ -0,0 +1,186 @@
|
|
1
|
+
/**
|
2
|
+
* Client API for calling the zkCloudWorker
|
3
|
+
*/
|
4
|
+
export {
|
5
|
+
TxnPayload,
|
6
|
+
IsError,
|
7
|
+
SerializedTxn,
|
8
|
+
SignedSerializedTxn,
|
9
|
+
TxnResult,
|
10
|
+
JobPayload,
|
11
|
+
JobResult,
|
12
|
+
zkCloudWorkerAPI,
|
13
|
+
};
|
14
|
+
|
15
|
+
interface TxnPayload {
|
16
|
+
data: object;
|
17
|
+
options?: object;
|
18
|
+
}
|
19
|
+
|
20
|
+
interface IsError {
|
21
|
+
code: number;
|
22
|
+
message: string;
|
23
|
+
exception: any;
|
24
|
+
}
|
25
|
+
|
26
|
+
interface SerializedTxn {
|
27
|
+
hash: string | null;
|
28
|
+
transaction: any | null;
|
29
|
+
error: IsError | null;
|
30
|
+
}
|
31
|
+
|
32
|
+
interface SignedSerializedTxn {
|
33
|
+
hash: string | null;
|
34
|
+
transaction: any | null;
|
35
|
+
error: IsError | null;
|
36
|
+
}
|
37
|
+
|
38
|
+
interface TxnResult {
|
39
|
+
hash: string | null;
|
40
|
+
data: any | null;
|
41
|
+
error: IsError | null;
|
42
|
+
}
|
43
|
+
|
44
|
+
interface JobPayload {
|
45
|
+
data: object;
|
46
|
+
options?: object;
|
47
|
+
}
|
48
|
+
|
49
|
+
interface JobResult {
|
50
|
+
data: any | null;
|
51
|
+
error: IsError | null;
|
52
|
+
}
|
53
|
+
|
54
|
+
class zkCloudWorkerAPI {
|
55
|
+
private API_KEY: string = "";
|
56
|
+
|
57
|
+
constructor(apiKey: string) {
|
58
|
+
this.API_KEY = apiKey;
|
59
|
+
}
|
60
|
+
|
61
|
+
/**
|
62
|
+
* prove() sign() and send()
|
63
|
+
*
|
64
|
+
* The called cloud worker is expected to compile the needed Contract,
|
65
|
+
* create the transaction, prove it and send it back serialized.
|
66
|
+
*
|
67
|
+
* Then the serialized transaction can be signed locally using AuroWallet
|
68
|
+
* and finally send it to MINA using the cloud worker.
|
69
|
+
*
|
70
|
+
* IMPORTANT: the transaction fee will be paid by the local sender, using
|
71
|
+
* the Auro Wallet at the moment of signing the serialized transaction.
|
72
|
+
*
|
73
|
+
* We only need the sender public key to create and prove the transaction.
|
74
|
+
* The sender private key NEVER leaves the local wallet.
|
75
|
+
*
|
76
|
+
* Example:
|
77
|
+
* ~~~
|
78
|
+
* let zkWorker = new ZKRunner(API_KEY);
|
79
|
+
*
|
80
|
+
* let serializedTxn = await zkWorker.prove('batch-voting-...', {
|
81
|
+
* data: {
|
82
|
+
* claimUid: '012345...789',
|
83
|
+
* // ...
|
84
|
+
* },
|
85
|
+
* options: {
|
86
|
+
* senderAddress: 'B62...',
|
87
|
+
* fee: MIN_FEE // MAX_FEE | AUTO_FEE | number
|
88
|
+
* }
|
89
|
+
* });
|
90
|
+
* if (serializedTxn.error)
|
91
|
+
* // treat error here
|
92
|
+
*
|
93
|
+
* let signedTxn = await zkWorker.sign(signerAddress, serializedTxn);
|
94
|
+
* if (signedTxn.error)
|
95
|
+
* // treat error here
|
96
|
+
*
|
97
|
+
* let txnResult = await zkWorker.send(signedTxn) ;
|
98
|
+
* if (txnResult.error)
|
99
|
+
* // treat error here
|
100
|
+
* ~~~
|
101
|
+
*/
|
102
|
+
async prove(
|
103
|
+
jobName: string,
|
104
|
+
payload: TxnPayload //: Promise < SerializedTxn > {
|
105
|
+
) {}
|
106
|
+
|
107
|
+
async sign(
|
108
|
+
signerAddress: string,
|
109
|
+
serializedTxn: SerializedTxn //: Promise<SignedSerializedTxn> {
|
110
|
+
) {}
|
111
|
+
|
112
|
+
async send(
|
113
|
+
txn: SignedSerializedTxn //: Promise<TxnResult> {
|
114
|
+
) {}
|
115
|
+
|
116
|
+
/**
|
117
|
+
* proveAndSend()
|
118
|
+
*
|
119
|
+
* The called cloud worker is expected to do all: compile the needed Contract,
|
120
|
+
* create the transaction, prove it, sign it using one of the available
|
121
|
+
* fee payers, and finally send it to MINA.
|
122
|
+
*
|
123
|
+
* IMPORTANT: the transaction fee will be paid by the first fee payer
|
124
|
+
* available from the list of fee payers provided by the ZKRunner service.
|
125
|
+
* Also the fee will be set by the cloud worker using some optimal algorithm
|
126
|
+
* that minimizes fees.
|
127
|
+
*
|
128
|
+
* In this case the sender public key to create and prove the transaction
|
129
|
+
* will be the selected ZKRunner fee payer previously mentioned.
|
130
|
+
*
|
131
|
+
* Example:
|
132
|
+
* ~~~
|
133
|
+
* let zkWorker = new ZKRunner(API_KEY);
|
134
|
+
*
|
135
|
+
* let txnResult = await zkWorker.proveAndSend('batch-voting-...', {
|
136
|
+
* data: {
|
137
|
+
* claimUid: '012345...789',
|
138
|
+
* // ...
|
139
|
+
* }
|
140
|
+
* });
|
141
|
+
*
|
142
|
+
* if (txnResult.error)
|
143
|
+
* // treat error here
|
144
|
+
* ~~~
|
145
|
+
*/
|
146
|
+
async proveAndSend(
|
147
|
+
jobName: string,
|
148
|
+
payload: TxnPayload //: Promise<TxnResult> {
|
149
|
+
) {}
|
150
|
+
|
151
|
+
/**
|
152
|
+
* runJob()
|
153
|
+
*
|
154
|
+
* The called cloud worker can also be used to easily run jobs not related to
|
155
|
+
* a MINA transaction, and will act just like any serverless function.
|
156
|
+
*
|
157
|
+
* This "generic" job can benefit from the easy to use deploy and call service
|
158
|
+
* already implemented for cloud proving without no extra costs.
|
159
|
+
*
|
160
|
+
* IMPORTANT: there will be a small fee that needs to be paid for service usage,
|
161
|
+
* but no MINA fees need to be paid.
|
162
|
+
*
|
163
|
+
* Example:
|
164
|
+
* ~~~
|
165
|
+
* let zkWorker = new ZKRunner(API_KEY);
|
166
|
+
*
|
167
|
+
* let jobResult = await zkWorker.runJob('send-email-to-judges', {
|
168
|
+
* data: {
|
169
|
+
* judges: [
|
170
|
+
* // ...
|
171
|
+
* ],
|
172
|
+
* },
|
173
|
+
* options: {
|
174
|
+
* // ...
|
175
|
+
* }
|
176
|
+
* });
|
177
|
+
*
|
178
|
+
* if (txnResult.error)
|
179
|
+
* // treat error here
|
180
|
+
* ~~~
|
181
|
+
*/
|
182
|
+
async runJob(
|
183
|
+
jobName: string,
|
184
|
+
payload: JobPayload //: Promise<JobResult> {
|
185
|
+
) {}
|
186
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { Cache, PrivateKey } from "o1js";
|
2
|
+
|
3
|
+
export abstract class Cloud {
|
4
|
+
cache: Cache;
|
5
|
+
constructor(cache: Cache) {
|
6
|
+
this.cache = cache;
|
7
|
+
}
|
8
|
+
// TODO: change it to the sign method to protect the private key
|
9
|
+
abstract getDeployer(): Promise<PrivateKey>;
|
10
|
+
abstract log(msg: string): void;
|
11
|
+
|
12
|
+
/* TODO: add more methods
|
13
|
+
-getDataByKey
|
14
|
+
-saveDataByKey
|
15
|
+
-saveFile
|
16
|
+
-loadFile
|
17
|
+
*/
|
18
|
+
abstract getDataByKey(key: string): Promise<string | undefined>;
|
19
|
+
abstract saveDataByKey(key: string, value: string): Promise<void>;
|
20
|
+
abstract saveFile(filename: string, value: Buffer): Promise<void>;
|
21
|
+
abstract loadFile(filename: string): Promise<Buffer | undefined>;
|
22
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { Cache, PrivateKey } from "o1js";
|
2
|
+
import { Cloud } from "./cloud";
|
3
|
+
|
4
|
+
export class LocalCloud extends Cloud {
|
5
|
+
cache: Cache;
|
6
|
+
data: Map<string, string> = new Map<string, string>();
|
7
|
+
|
8
|
+
constructor() {
|
9
|
+
const cache = Cache.FileSystem("./cache");
|
10
|
+
super(cache);
|
11
|
+
}
|
12
|
+
public async getDeployer(): Promise<PrivateKey> {
|
13
|
+
throw new Error("Method not implemented.");
|
14
|
+
}
|
15
|
+
public async log(msg: string): Promise<void> {
|
16
|
+
console.log("LocalCloud:", msg);
|
17
|
+
}
|
18
|
+
|
19
|
+
public async getDataByKey(key: string): Promise<string | undefined> {
|
20
|
+
const value = this.data.get(key);
|
21
|
+
return value;
|
22
|
+
}
|
23
|
+
|
24
|
+
public async saveDataByKey(key: string, value: string): Promise<void> {
|
25
|
+
this.data.set(key, value);
|
26
|
+
}
|
27
|
+
|
28
|
+
public async saveFile(filename: string, value: Buffer): Promise<void> {
|
29
|
+
throw new Error("Method not implemented.");
|
30
|
+
}
|
31
|
+
public async loadFile(filename: string): Promise<Buffer | undefined> {
|
32
|
+
throw new Error("Method not implemented.");
|
33
|
+
}
|
34
|
+
}
|
package/src/config.ts
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
const config = {
|
2
|
+
MINAFEE: "200000000",
|
3
|
+
ZKCLOUDWORKER_AUTH:
|
4
|
+
"M6t4jtbBAFFXhLERHQWyEB9JA9xi4cWqmYduaCXtbrFjb7yaY7TyaXDunKDJNiUTBEcyUomNXJgC",
|
5
|
+
ZKCLOUDWORKER_API:
|
6
|
+
"https://cuq99yahhi.execute-api.eu-west-1.amazonaws.com/dev/zkcloudworker",
|
7
|
+
};
|
8
|
+
|
9
|
+
export default config;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
export { BackendPlugin };
|
2
|
+
import type { Cache } from "o1js";
|
3
|
+
|
4
|
+
abstract class BackendPlugin {
|
5
|
+
name: string;
|
6
|
+
task: string;
|
7
|
+
args: string[];
|
8
|
+
jobId?: string;
|
9
|
+
|
10
|
+
constructor(params: {
|
11
|
+
name: string;
|
12
|
+
task: string;
|
13
|
+
args: string[];
|
14
|
+
jobId?: string;
|
15
|
+
}) {
|
16
|
+
const { name, task, args, jobId } = params;
|
17
|
+
this.name = name;
|
18
|
+
this.task = task;
|
19
|
+
this.args = args;
|
20
|
+
this.jobId = jobId;
|
21
|
+
}
|
22
|
+
|
23
|
+
abstract compile(cache: Cache): Promise<void>;
|
24
|
+
abstract create(transaction: string): Promise<string | undefined>;
|
25
|
+
abstract merge(proof1: string, proof2: string): Promise<string | undefined>;
|
26
|
+
|
27
|
+
abstract send(transaction: string): Promise<string | undefined>;
|
28
|
+
abstract mint(transaction: string): Promise<string | undefined>;
|
29
|
+
abstract verify(proof: string): Promise<string | undefined>;
|
30
|
+
}
|
package/src/fee.ts
ADDED
package/src/index.ts
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
export { zkCloudWorker } from "./api/api";
|
2
|
+
export { Cloud } from "./cloud/cloud";
|
3
|
+
export { LocalCloud } from "./cloud/local";
|
4
|
+
export {
|
5
|
+
initBlockchain,
|
6
|
+
Memory,
|
7
|
+
makeString,
|
8
|
+
sleep,
|
9
|
+
accountBalance,
|
10
|
+
accountBalanceMina,
|
11
|
+
formatTime,
|
12
|
+
MinaNetworkInstance,
|
13
|
+
currentNetwork,
|
14
|
+
getNetworkIdHash,
|
15
|
+
} from "./mina";
|
16
|
+
export { fee } from "./fee";
|
17
|
+
export {
|
18
|
+
blockchain,
|
19
|
+
MinaNetwork,
|
20
|
+
networks,
|
21
|
+
Mainnet,
|
22
|
+
Berkeley,
|
23
|
+
Zeko,
|
24
|
+
TestWorld2,
|
25
|
+
Lightnet,
|
26
|
+
Local,
|
27
|
+
} from "./networks";
|
28
|
+
export {
|
29
|
+
TxnPayload,
|
30
|
+
IsError,
|
31
|
+
SerializedTxn,
|
32
|
+
SignedSerializedTxn,
|
33
|
+
TxnResult,
|
34
|
+
JobPayload,
|
35
|
+
JobResult,
|
36
|
+
zkCloudWorkerAPI,
|
37
|
+
} from "./api/client-api";
|
38
|
+
export { BackendPlugin } from "./custom/backend";
|
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
|
+
}
|
package/src/networks.ts
ADDED
@@ -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
|
+
}
|