-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.mjs
58 lines (52 loc) · 1.75 KB
/
init.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
52
53
54
55
56
57
58
import {
promises as fsp
} from 'fs';
import path from 'path'
import { zencode_exec } from "zenroom"
import dotenv from "dotenv";
dotenv.config();
const PRIVATE_ZENCODE_DIR = process.env.PRIVATE_ZENCODE_DIR;
const ZENCODE_DIR = process.env.ZENCODE_DIR || "contracts";
/*
* Run a zenroom script (outside restroom mw)
*/
const zen = async (zencode, keys, data) => {
const params = {};
if (keys !== undefined && keys !== null) {
params.keys = typeof keys === 'string' ? keys : JSON.stringify(keys);
}
if (data !== undefined && data !== null) {
params.data = typeof data === 'string' ? data : JSON.stringify(data);
}
try {
return await zencode_exec(zencode, params);
} catch (e) {
console.log("Error from zencode_exec: ", e);
}
}
// generate private keys
const generatePrivateKeysScript = await fsp.readFile(path.join(PRIVATE_ZENCODE_DIR, "consensus-generate-all-private-keys.zen"), 'utf8')
let keyring = {}
const keys = await zen(generatePrivateKeysScript, null, null);
if (!keys) {
console.error("Error in generate private keys");
process.exit(-1)
}
Object.assign(keyring, JSON.parse(keys.result))
try {
await fsp.unlink(path.join(ZENCODE_DIR, "zenswarm-oracle-generate-all-public-keys.keys"))
} catch(e) {}
await fsp.writeFile(
path.join(ZENCODE_DIR, "zenswarm-oracle-generate-all-public-keys.keys"),
keys.result, {mode: 0o600})
// Delete keyring if it exists (writing a file that exist
// doesn't change the permissions)
try {
await fsp.unlink(path.join(ZENCODE_DIR, "keyring.json"))
} catch(e) {}
await fsp.writeFile(
path.join(ZENCODE_DIR, "keyring.json"),
JSON.stringify(keyring), {mode: 0o600})
try {
await fsp.unlink(path.join(ZENCODE_DIR, "identity.json"))
} catch(e) {}