zkcloudworker 0.5.2 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/ts/src/api/api.js +1 -1
- package/lib/ts/src/index.d.ts +3 -2
- package/lib/ts/src/index.js +3 -2
- package/lib/ts/src/utils/base64.d.ts +5 -0
- package/lib/ts/src/utils/base64.js +90 -0
- package/lib/ts/src/{fetch.js → utils/fetch.js} +24 -6
- package/lib/ts/src/utils/fields.d.ts +3 -0
- package/lib/ts/src/utils/fields.js +33 -0
- package/lib/ts/tsconfig.tsbuildinfo +1 -1
- package/lib/web/src/api/api.js +264 -287
- package/lib/web/src/api/api.js.map +1 -1
- package/lib/web/src/cloud/cloud.js +12 -25
- package/lib/web/src/cloud/cloud.js.map +1 -1
- package/lib/web/src/cloud/files.js +59 -68
- package/lib/web/src/cloud/files.js.map +1 -1
- package/lib/web/src/cloud/local.js +221 -259
- package/lib/web/src/cloud/local.js.map +1 -1
- package/lib/web/src/encryption/encryption.js +2 -5
- package/lib/web/src/encryption/encryption.js.map +1 -1
- package/lib/web/src/encryption/messages.js +24 -29
- package/lib/web/src/encryption/messages.js.map +1 -1
- package/lib/web/src/encryption/nats-client.js +67 -83
- package/lib/web/src/encryption/nats-client.js.map +1 -1
- package/lib/web/src/fee.js +3 -6
- package/lib/web/src/fee.js.map +1 -1
- package/lib/web/src/index.d.ts +3 -2
- package/lib/web/src/index.js +3 -2
- package/lib/web/src/index.js.map +1 -1
- package/lib/web/src/mina.js +60 -67
- package/lib/web/src/mina.js.map +1 -1
- package/lib/web/src/utils/base64.d.ts +5 -0
- package/lib/web/src/utils/base64.js +84 -0
- package/lib/web/src/utils/base64.js.map +1 -0
- package/lib/web/src/utils/fetch.js +79 -0
- package/lib/web/src/utils/fetch.js.map +1 -0
- package/lib/web/src/utils/fields.d.ts +3 -0
- package/lib/web/src/utils/fields.js +29 -0
- package/lib/web/src/utils/fields.js.map +1 -0
- package/lib/web/tsconfig.web.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/lib/ts/src/api/client-api.d.ts +0 -147
- package/lib/ts/src/api/client-api.js +0 -122
- package/lib/web/src/api/client-api.d.ts +0 -147
- package/lib/web/src/api/client-api.js +0 -134
- package/lib/web/src/api/client-api.js.map +0 -1
- package/lib/web/src/fetch.js +0 -68
- package/lib/web/src/fetch.js.map +0 -1
- /package/lib/ts/src/{fetch.d.ts → utils/fetch.d.ts} +0 -0
- /package/lib/web/src/{fetch.d.ts → utils/fetch.d.ts} +0 -0
@@ -1,36 +1,31 @@
|
|
1
|
-
import { __awaiter } from "tslib";
|
2
1
|
import { connect, JSONCodec } from "nats";
|
3
2
|
import config from "../config";
|
4
3
|
const { NATS_SERVER } = config;
|
5
4
|
const codec = JSONCodec();
|
6
|
-
export function postReadyMessage(clientAddress, workerAddress) {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
return response;
|
19
|
-
});
|
5
|
+
export async function postReadyMessage(clientAddress, workerAddress) {
|
6
|
+
// connect to the NATS server and send a 'ready' request
|
7
|
+
const nc = await connect({ servers: NATS_SERVER });
|
8
|
+
const msg = await nc.request(`zkcw:${clientAddress}`, codec.encode({
|
9
|
+
post: "ready",
|
10
|
+
params: { key: workerAddress },
|
11
|
+
}));
|
12
|
+
const response = codec.decode(msg.data);
|
13
|
+
console.log("Response: ", response);
|
14
|
+
// disconnect and clean all pending
|
15
|
+
await nc.drain();
|
16
|
+
return response;
|
20
17
|
}
|
21
|
-
export function postDoneMessage(clientAddress, encrypted) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
return response;
|
34
|
-
});
|
18
|
+
export async function postDoneMessage(clientAddress, encrypted) {
|
19
|
+
// connect to the NATS server and send a 'ready' request
|
20
|
+
const nc = await connect({ servers: NATS_SERVER });
|
21
|
+
const msg = await nc.request(`zkcw:${clientAddress}`, codec.encode({
|
22
|
+
post: "done",
|
23
|
+
params: { result: encrypted },
|
24
|
+
}));
|
25
|
+
const response = codec.decode(msg.data);
|
26
|
+
console.log("Response: ", response);
|
27
|
+
// disconnect and clean all pending
|
28
|
+
await nc.drain();
|
29
|
+
return response;
|
35
30
|
}
|
36
31
|
//# sourceMappingURL=messages.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../../../../src/encryption/messages.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../../../../src/encryption/messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,MAAM,MAAM,WAAW,CAAC;AAE/B,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC;AAE/B,MAAM,KAAK,GAAG,SAAS,EAAE,CAAC;AAE1B,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,aAAqB,EACrB,aAAqB;IAErB,wDAAwD;IACxD,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC;IACnD,MAAM,GAAG,GAAQ,MAAM,EAAE,CAAC,OAAO,CAC/B,QAAQ,aAAa,EAAE,EACvB,KAAK,CAAC,MAAM,CAAC;QACX,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,EAAE,GAAG,EAAE,aAAa,EAAE;KAC/B,CAAC,CACH,CAAC;IACF,MAAM,QAAQ,GAAQ,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC7C,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAEpC,mCAAmC;IACnC,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC;IAEjB,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,aAAqB,EACrB,SAAiB;IAEjB,wDAAwD;IACxD,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC;IACnD,MAAM,GAAG,GAAQ,MAAM,EAAE,CAAC,OAAO,CAC/B,QAAQ,aAAa,EAAE,EACvB,KAAK,CAAC,MAAM,CAAC;QACX,IAAI,EAAE,MAAM;QACZ,MAAM,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE;KAC9B,CAAC,CACH,CAAC;IACF,MAAM,QAAQ,GAAQ,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC7C,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;IAEpC,mCAAmC;IACnC,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC;IAEjB,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
@@ -1,95 +1,79 @@
|
|
1
|
-
import { __asyncValues, __awaiter } from "tslib";
|
2
1
|
import { connect, JSONCodec } from "nats";
|
3
2
|
import { initializeBindings } from "o1js";
|
4
3
|
import { CipherText } from "./encryption";
|
5
4
|
import config from "../config";
|
6
5
|
const { NATS_SERVER } = config;
|
7
|
-
export function listen(subject) {
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
yield initializeBindings();
|
6
|
+
export async function listen(subject) {
|
7
|
+
// Create a JSON codec for encoding and decoding messages
|
8
|
+
const codec = JSONCodec();
|
9
|
+
const connection = await connect({ servers: NATS_SERVER });
|
10
|
+
// Subscribe to the subject
|
11
|
+
const subscription = connection.subscribe(subject);
|
12
|
+
console.log(`Subscribed to subject ${subject}`);
|
13
|
+
// Process messages received on the subscribed subject
|
14
|
+
(async () => {
|
15
|
+
// Error decoding message: Error: Could not encrypt message={}
|
16
|
+
// Error: Poseidon.Sponge(): bindings are not initialized, try calling `await initializeBindings()` first.
|
17
|
+
// This shouldn't have happened and indicates an internal bug.
|
18
|
+
await initializeBindings();
|
19
|
+
for await (const msg of subscription) {
|
22
20
|
try {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
command: "execute",
|
52
|
-
encrypted: encryptedPayload,
|
53
|
-
},
|
54
|
-
error: undefined,
|
55
|
-
}));
|
56
|
-
}
|
57
|
-
break;
|
58
|
-
case "done":
|
59
|
-
{
|
60
|
-
let result = params.result || "";
|
61
|
-
console.log("Received 'done' message from worker");
|
62
|
-
msg.respond(codec.encode({
|
63
|
-
success: true,
|
64
|
-
data: { status: "closed" },
|
65
|
-
error: undefined,
|
66
|
-
}));
|
67
|
-
// we want to insure that messages that are in flight
|
68
|
-
// get processed, so we are going to drain the
|
69
|
-
// connection. Drain is the same as close, but makes
|
70
|
-
// sure that all messages in flight get seen
|
71
|
-
// by the iterator. After calling drain on the connection
|
72
|
-
// the connection closes.
|
73
|
-
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
74
|
-
yield connection.drain();
|
75
|
-
}), 1000);
|
76
|
-
}
|
77
|
-
break;
|
21
|
+
const data = codec.decode(msg.data);
|
22
|
+
//console.log(`Received message on subject ${subject}:`, data);
|
23
|
+
// Perform processing logic here
|
24
|
+
const { post, params } = data;
|
25
|
+
// console.log(`Post: `, post, params);
|
26
|
+
switch (post) {
|
27
|
+
case "ready":
|
28
|
+
{
|
29
|
+
// the workers announces it is ready
|
30
|
+
// and we receive the worker's publicKey
|
31
|
+
let workerKey = params.key || "";
|
32
|
+
console.log("Received 'ready' message from worker");
|
33
|
+
console.log("Worker publicKey: ", workerKey);
|
34
|
+
// we will use its key to encrypt the message
|
35
|
+
const encryptedPayload = CipherText.encrypt(JSON.stringify({
|
36
|
+
value: Math.ceil(Math.random() * 100).toString(),
|
37
|
+
}), workerKey);
|
38
|
+
console.log("Encrypted payload: ", encryptedPayload);
|
39
|
+
// we reply with the command we want the worker to execute
|
40
|
+
// and with the encrypted payload
|
41
|
+
msg.respond(codec.encode({
|
42
|
+
success: true,
|
43
|
+
data: {
|
44
|
+
command: "execute",
|
45
|
+
encrypted: encryptedPayload,
|
46
|
+
},
|
47
|
+
error: undefined,
|
48
|
+
}));
|
78
49
|
}
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
50
|
+
break;
|
51
|
+
case "done":
|
52
|
+
{
|
53
|
+
let result = params.result || "";
|
54
|
+
console.log("Received 'done' message from worker");
|
55
|
+
msg.respond(codec.encode({
|
56
|
+
success: true,
|
57
|
+
data: { status: "closed" },
|
58
|
+
error: undefined,
|
59
|
+
}));
|
60
|
+
// we want to insure that messages that are in flight
|
61
|
+
// get processed, so we are going to drain the
|
62
|
+
// connection. Drain is the same as close, but makes
|
63
|
+
// sure that all messages in flight get seen
|
64
|
+
// by the iterator. After calling drain on the connection
|
65
|
+
// the connection closes.
|
66
|
+
setTimeout(async () => {
|
67
|
+
await connection.drain();
|
68
|
+
}, 1000);
|
69
|
+
}
|
70
|
+
break;
|
83
71
|
}
|
84
72
|
}
|
85
|
-
catch (
|
86
|
-
|
87
|
-
try {
|
88
|
-
if (!_d && !_a && (_b = subscription_1.return)) yield _b.call(subscription_1);
|
89
|
-
}
|
90
|
-
finally { if (e_1) throw e_1.error; }
|
73
|
+
catch (err) {
|
74
|
+
console.error("Error decoding message: ", err);
|
91
75
|
}
|
92
|
-
}
|
93
|
-
});
|
76
|
+
}
|
77
|
+
})();
|
94
78
|
}
|
95
79
|
//# sourceMappingURL=nats-client.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"nats-client.js","sourceRoot":"","sources":["../../../../../src/encryption/nats-client.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"nats-client.js","sourceRoot":"","sources":["../../../../../src/encryption/nats-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,MAAM,MAAM,WAAW,CAAC;AAE/B,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC;AAE/B,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,OAAe;IAC1C,yDAAyD;IACzD,MAAM,KAAK,GAAG,SAAS,EAAE,CAAC;IAE1B,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC;IAE3D,2BAA2B;IAC3B,MAAM,YAAY,GAAG,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACnD,OAAO,CAAC,GAAG,CAAC,yBAAyB,OAAO,EAAE,CAAC,CAAC;IAEhD,sDAAsD;IACtD,CAAC,KAAK,IAAI,EAAE;QACV,+DAA+D;QAC/D,0GAA0G;QAC1G,8DAA8D;QAC9D,MAAM,kBAAkB,EAAE,CAAC;QAE3B,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;YACrC,IAAI,CAAC;gBACH,MAAM,IAAI,GAAQ,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBACzC,+DAA+D;gBAE/D,gCAAgC;gBAChC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;gBAC9B,uCAAuC;gBAEvC,QAAQ,IAAI,EAAE,CAAC;oBACb,KAAK,OAAO;wBACV,CAAC;4BACC,oCAAoC;4BACpC,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AAE,CAAC;4BAC3B,CAAC,EAAE,IAAI,CAAC,CAAC;wBACX,CAAC;wBACD,MAAM;gBACV,CAAC;YACH,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,CAAC,KAAK,CAAC,0BAA0B,EAAE,GAAG,CAAC,CAAC;YACjD,CAAC;QACH,CAAC;IACH,CAAC,CAAC,EAAE,CAAC;AACP,CAAC"}
|
package/lib/web/src/fee.js
CHANGED
@@ -1,10 +1,7 @@
|
|
1
|
-
import { __awaiter } from "tslib";
|
2
1
|
import { UInt64 } from "o1js";
|
3
2
|
import config from "./config";
|
4
|
-
export function fee() {
|
5
|
-
|
6
|
-
|
7
|
-
return UInt64.fromJSON(config.MINAFEE);
|
8
|
-
});
|
3
|
+
export async function fee() {
|
4
|
+
//TODO: update after mainnet launch
|
5
|
+
return UInt64.fromJSON(config.MINAFEE);
|
9
6
|
}
|
10
7
|
//# sourceMappingURL=fee.js.map
|
package/lib/web/src/fee.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fee.js","sourceRoot":"","sources":["../../../../src/fee.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"fee.js","sourceRoot":"","sources":["../../../../src/fee.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,MAAM,MAAM,UAAU,CAAC;AAE9B,MAAM,CAAC,KAAK,UAAU,GAAG;IACvB,mCAAmC;IACnC,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC"}
|
package/lib/web/src/index.d.ts
CHANGED
@@ -5,9 +5,10 @@ export * from "./cloud/job";
|
|
5
5
|
export * from "./cloud/task";
|
6
6
|
export * from "./mina";
|
7
7
|
export * from "./fee";
|
8
|
-
export * from "./fetch";
|
9
8
|
export * from "./networks";
|
10
|
-
export * from "./api/client-api";
|
11
9
|
export * from "./encryption/encryption";
|
12
10
|
export * from "./encryption/nats-client";
|
13
11
|
export * from "./encryption/messages";
|
12
|
+
export * from "./utils/fields";
|
13
|
+
export * from "./utils/base64";
|
14
|
+
export * from "./utils/fetch";
|
package/lib/web/src/index.js
CHANGED
@@ -5,10 +5,11 @@ export * from "./cloud/job";
|
|
5
5
|
export * from "./cloud/task";
|
6
6
|
export * from "./mina";
|
7
7
|
export * from "./fee";
|
8
|
-
export * from "./fetch";
|
9
8
|
export * from "./networks";
|
10
|
-
export * from "./api/client-api";
|
11
9
|
export * from "./encryption/encryption";
|
12
10
|
export * from "./encryption/nats-client";
|
13
11
|
export * from "./encryption/messages";
|
12
|
+
export * from "./utils/fields";
|
13
|
+
export * from "./utils/base64";
|
14
|
+
export * from "./utils/fetch";
|
14
15
|
//# sourceMappingURL=index.js.map
|
package/lib/web/src/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,OAAO,CAAC;AACtB,cAAc,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC"}
|
package/lib/web/src/mina.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import { __awaiter } from "tslib";
|
2
1
|
export { initBlockchain, Memory, makeString, sleep, accountBalance, accountBalanceMina, formatTime, currentNetwork, getNetworkIdHash, getCurrentNetwork, getDeployer, };
|
3
2
|
import { Mina, PrivateKey, UInt64, fetchAccount, Encoding, Poseidon, Lightnet, } from "o1js";
|
4
3
|
import { networks, Local } from "./networks";
|
@@ -23,80 +22,74 @@ function getDeployer() {
|
|
23
22
|
return undefined;
|
24
23
|
return currentNetwork.keys[0];
|
25
24
|
}
|
26
|
-
function initBlockchain(
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
proofsEnabled: true,
|
36
|
-
});
|
37
|
-
Mina.setActiveInstance(local);
|
38
|
-
if (deployersNumber > local.testAccounts.length)
|
39
|
-
throw new Error("Not enough test accounts");
|
40
|
-
currentNetwork = {
|
41
|
-
keys: local.testAccounts,
|
42
|
-
network: Local,
|
43
|
-
networkIdHash,
|
44
|
-
};
|
45
|
-
return currentNetwork;
|
46
|
-
}
|
47
|
-
const network = networks.find((n) => n.chainId === instance);
|
48
|
-
if (network === undefined) {
|
49
|
-
throw new Error("Unknown network");
|
50
|
-
}
|
51
|
-
const networkInstance = Mina.Network({
|
52
|
-
mina: network.mina,
|
53
|
-
archive: network.archive,
|
54
|
-
lightnetAccountManager: network.accountManager,
|
25
|
+
async function initBlockchain(instance, deployersNumber = 0) {
|
26
|
+
if (instance === "mainnet") {
|
27
|
+
throw new Error("Mainnet is not supported yet by zkApps");
|
28
|
+
}
|
29
|
+
const networkIdHash = Poseidon.hash(Encoding.stringToFields(instance));
|
30
|
+
// await used for compatibility with future versions of o1js
|
31
|
+
if (instance === "local") {
|
32
|
+
const local = await Mina.LocalBlockchain({
|
33
|
+
proofsEnabled: true,
|
55
34
|
});
|
56
|
-
Mina.setActiveInstance(
|
57
|
-
|
58
|
-
|
59
|
-
if (instance === "lighnet") {
|
60
|
-
for (let i = 0; i < deployersNumber; i++) {
|
61
|
-
const keyPair = yield Lightnet.acquireKeyPair();
|
62
|
-
const key = Mina.TestPublicKey(keyPair.privateKey);
|
63
|
-
keys.push(key);
|
64
|
-
}
|
65
|
-
}
|
66
|
-
else {
|
67
|
-
const deployers = process.env.DEPLOYERS;
|
68
|
-
if (deployers === undefined ||
|
69
|
-
Array.isArray(deployers) === false ||
|
70
|
-
deployers.length < deployersNumber)
|
71
|
-
throw new Error("Deployers are not set");
|
72
|
-
for (let i = 0; i < deployersNumber; i++) {
|
73
|
-
const privateKey = PrivateKey.fromBase58(deployers[i]);
|
74
|
-
const key = Mina.TestPublicKey(privateKey);
|
75
|
-
keys.push(key);
|
76
|
-
}
|
77
|
-
}
|
78
|
-
}
|
35
|
+
Mina.setActiveInstance(local);
|
36
|
+
if (deployersNumber > local.testAccounts.length)
|
37
|
+
throw new Error("Not enough test accounts");
|
79
38
|
currentNetwork = {
|
80
|
-
keys,
|
81
|
-
network,
|
39
|
+
keys: local.testAccounts,
|
40
|
+
network: Local,
|
82
41
|
networkIdHash,
|
83
42
|
};
|
84
43
|
return currentNetwork;
|
44
|
+
}
|
45
|
+
const network = networks.find((n) => n.chainId === instance);
|
46
|
+
if (network === undefined) {
|
47
|
+
throw new Error("Unknown network");
|
48
|
+
}
|
49
|
+
const networkInstance = Mina.Network({
|
50
|
+
mina: network.mina,
|
51
|
+
archive: network.archive,
|
52
|
+
lightnetAccountManager: network.accountManager,
|
85
53
|
});
|
54
|
+
Mina.setActiveInstance(networkInstance);
|
55
|
+
const keys = [];
|
56
|
+
if (deployersNumber > 0) {
|
57
|
+
if (instance === "lighnet") {
|
58
|
+
for (let i = 0; i < deployersNumber; i++) {
|
59
|
+
const keyPair = await Lightnet.acquireKeyPair();
|
60
|
+
const key = Mina.TestPublicKey(keyPair.privateKey);
|
61
|
+
keys.push(key);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
const deployers = process.env.DEPLOYERS;
|
66
|
+
if (deployers === undefined ||
|
67
|
+
Array.isArray(deployers) === false ||
|
68
|
+
deployers.length < deployersNumber)
|
69
|
+
throw new Error("Deployers are not set");
|
70
|
+
for (let i = 0; i < deployersNumber; i++) {
|
71
|
+
const privateKey = PrivateKey.fromBase58(deployers[i]);
|
72
|
+
const key = Mina.TestPublicKey(privateKey);
|
73
|
+
keys.push(key);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
}
|
77
|
+
currentNetwork = {
|
78
|
+
keys,
|
79
|
+
network,
|
80
|
+
networkIdHash,
|
81
|
+
};
|
82
|
+
return currentNetwork;
|
86
83
|
}
|
87
|
-
function accountBalance(address) {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
return UInt64.from(0);
|
94
|
-
});
|
84
|
+
async function accountBalance(address) {
|
85
|
+
await fetchAccount({ publicKey: address });
|
86
|
+
if (Mina.hasAccount(address))
|
87
|
+
return Mina.getBalance(address);
|
88
|
+
else
|
89
|
+
return UInt64.from(0);
|
95
90
|
}
|
96
|
-
function accountBalanceMina(address) {
|
97
|
-
return
|
98
|
-
return Number((yield accountBalance(address)).toBigInt()) / 1e9;
|
99
|
-
});
|
91
|
+
async function accountBalanceMina(address) {
|
92
|
+
return Number((await accountBalance(address)).toBigInt()) / 1e9;
|
100
93
|
}
|
101
94
|
function sleep(ms) {
|
102
95
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
package/lib/web/src/mina.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mina.js","sourceRoot":"","sources":["../../../../src/mina.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"mina.js","sourceRoot":"","sources":["../../../../src/mina.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,UAAU,EACV,KAAK,EACL,cAAc,EACd,kBAAkB,EAClB,UAAU,EAEV,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACj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CACrB,sCAAsC;gBACvC,QAAQ,EAAE,GAAG,iBAAiB,CAC5B,UAAU,CAAC,QAAQ,CACpB,6CAA6C;gBAC9C,QAAQ,EAAE,GAAG,iBAAiB,CAC5B,UAAU,CAAC,QAAQ,CACpB,wBAAwB;aAC1B;YACH,CAAC,CAAC,cAAc,WAAW,KAAK,iBAAiB,CAAC,UAAU,CAAC,GAAG,CAAC,GAC7D,MAAM,KAAK,CAAC;gBACV,CAAC,CAAC,EAAE;gBACJ,CAAC,CAAC,eAAe,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,CAAC,QAAQ,EAAE,GAAG,KAC3D,EAAE,CAAC;QAEP,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAC3B,CAAC;;AAzCD,kEAAkE;AAC3D,UAAG,GAAW,CAAC,CAAC"}
|
@@ -0,0 +1,5 @@
|
|
1
|
+
import { Field } from "o1js";
|
2
|
+
export declare function fieldToBase64(field: Field): string;
|
3
|
+
export declare function bigintToBase64(value: bigint): string;
|
4
|
+
export declare function fieldFromBase64(str: string): Field;
|
5
|
+
export declare function bigintFromBase64(str: string): bigint;
|
@@ -0,0 +1,84 @@
|
|
1
|
+
import { Field } from "o1js";
|
2
|
+
// URL friendly base64 encoding
|
3
|
+
const TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
4
|
+
export function fieldToBase64(field) {
|
5
|
+
const digits = toBase(field.toBigInt(), 64n);
|
6
|
+
//console.log("digits:", digits);
|
7
|
+
const str = digits.map((x) => TABLE[Number(x)]).join("");
|
8
|
+
//console.log("str:", str);
|
9
|
+
return str;
|
10
|
+
}
|
11
|
+
export function bigintToBase64(value) {
|
12
|
+
const digits = toBase(value, 64n);
|
13
|
+
//console.log("digits:", digits);
|
14
|
+
const str = digits.map((x) => TABLE[Number(x)]).join("");
|
15
|
+
//console.log("str:", str);
|
16
|
+
return str;
|
17
|
+
}
|
18
|
+
export function fieldFromBase64(str) {
|
19
|
+
const base64Digits = str.split("").map((x) => BigInt(TABLE.indexOf(x)));
|
20
|
+
const x = fromBase(base64Digits, 64n);
|
21
|
+
return Field(x);
|
22
|
+
}
|
23
|
+
export function bigintFromBase64(str) {
|
24
|
+
const base64Digits = str.split("").map((x) => BigInt(TABLE.indexOf(x)));
|
25
|
+
const x = fromBase(base64Digits, 64n);
|
26
|
+
return x;
|
27
|
+
}
|
28
|
+
function fromBase(digits, base) {
|
29
|
+
if (base <= 0n)
|
30
|
+
throw Error("fromBase: base must be positive");
|
31
|
+
// compute powers base, base^2, base^4, ..., base^(2^k)
|
32
|
+
// with largest k s.t. n = 2^k < digits.length
|
33
|
+
let basePowers = [];
|
34
|
+
for (let power = base, n = 1; n < digits.length; power **= 2n, n *= 2) {
|
35
|
+
basePowers.push(power);
|
36
|
+
}
|
37
|
+
let k = basePowers.length;
|
38
|
+
// pad digits array with zeros s.t. digits.length === 2^k
|
39
|
+
digits = digits.concat(Array(2 ** k - digits.length).fill(0n));
|
40
|
+
// accumulate [x0, x1, x2, x3, ...] -> [x0 + base*x1, x2 + base*x3, ...] -> [x0 + base*x1 + base^2*(x2 + base*x3), ...] -> ...
|
41
|
+
// until we end up with a single element
|
42
|
+
for (let i = 0; i < k; i++) {
|
43
|
+
let newDigits = Array(digits.length >> 1);
|
44
|
+
let basePower = basePowers[i];
|
45
|
+
for (let j = 0; j < newDigits.length; j++) {
|
46
|
+
newDigits[j] = digits[2 * j] + basePower * digits[2 * j + 1];
|
47
|
+
}
|
48
|
+
digits = newDigits;
|
49
|
+
}
|
50
|
+
console.assert(digits.length === 1);
|
51
|
+
let [digit] = digits;
|
52
|
+
return digit;
|
53
|
+
}
|
54
|
+
function toBase(x, base) {
|
55
|
+
if (base <= 0n)
|
56
|
+
throw Error("toBase: base must be positive");
|
57
|
+
// compute powers base, base^2, base^4, ..., base^(2^k)
|
58
|
+
// with largest k s.t. base^(2^k) < x
|
59
|
+
let basePowers = [];
|
60
|
+
for (let power = base; power <= x; power **= 2n) {
|
61
|
+
basePowers.push(power);
|
62
|
+
}
|
63
|
+
let digits = [x]; // single digit w.r.t base^(2^(k+1))
|
64
|
+
// successively split digits w.r.t. base^(2^j) into digits w.r.t. base^(2^(j-1))
|
65
|
+
// until we arrive at digits w.r.t. base
|
66
|
+
let k = basePowers.length;
|
67
|
+
for (let i = 0; i < k; i++) {
|
68
|
+
let newDigits = Array(2 * digits.length);
|
69
|
+
let basePower = basePowers[k - 1 - i];
|
70
|
+
for (let j = 0; j < digits.length; j++) {
|
71
|
+
let x = digits[j];
|
72
|
+
let high = x / basePower;
|
73
|
+
newDigits[2 * j + 1] = high;
|
74
|
+
newDigits[2 * j] = x - high * basePower;
|
75
|
+
}
|
76
|
+
digits = newDigits;
|
77
|
+
}
|
78
|
+
// pop "leading" zero digits
|
79
|
+
while (digits[digits.length - 1] === 0n) {
|
80
|
+
digits.pop();
|
81
|
+
}
|
82
|
+
return digits;
|
83
|
+
}
|
84
|
+
//# sourceMappingURL=base64.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"base64.js","sourceRoot":"","sources":["../../../../../src/utils/base64.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAE7B,+BAA+B;AAC/B,MAAM,KAAK,GACT,kEAAkE,CAAC;AAErE,MAAM,UAAU,aAAa,CAAC,KAAY;IACxC,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,GAAG,CAAC,CAAC;IAC7C,iCAAiC;IACjC,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACzD,2BAA2B;IAC3B,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,KAAa;IAC1C,M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