This repository was archived by the owner on Apr 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (52 loc) · 1.82 KB
/
Copy pathserver.js
File metadata and controls
60 lines (52 loc) · 1.82 KB
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
59
60
const fs = require('fs')
require("dotenv").config()
const filename = 'nodes.json';
if (!fs.existsSync(filename)) {
fs.writeFileSync(filename, '[]', 'utf-8');
console.log(`File '${filename}' created with an empty array.`);
} else {
console.log(`File '${filename}' already exists.`);
}
const crypto = require("hypercore-crypto");
const startNode = require("./startNode.js")
console.log(process.argv)
const node = require("hyper-ipc-secure")();
const rootKey = { publicKey: Buffer.from(process.argv[2], 'hex') }
let hostKey = crypto.keyPair();
try {
hostKey = JSON.parse(fs.readFileSync('hostKey.json'))
console.log(hostKey)
hostKey.publicKey = Buffer.from(hostKey.publicKey, 'hex')
hostKey.secretKey = Buffer.from(hostKey.secretKey, 'hex')
}catch(e) {
console.error(e)
fs.writeFileSync('hostKey.json', JSON.stringify({publicKey:hostKey.publicKey.toString('hex'), secretKey:hostKey.secretKey.toString('hex')}))
}
console.log('host key')
console.log(hostKey)
node.announce(rootKey.publicKey.toString('hex'), hostKey);
let nodes = [];
const run = async (params) => {
for (let n of params.nodes) {
const sub = node.getSub(hostKey, 'calls');
sub.publicKey=sub.publicKey.toString('hex');
sub.scalar=sub.scalar.toString('hex');
console.log('starting node', n.env)
const newNode = await startNode(sub, n.callKey, "hype", n.name, n.args || [], n.env);
newNode.env = n.env;
console.log('started node')
newNode.callKey = n.callKey;
nodes.push(newNode)
}
return { nodes };
}
run({ nodes: JSON.parse(fs.readFileSync('nodes.json')) })
node.serve(hostKey, "startNode", async (params) => {
console.log('startNode', params)
await run(params)
fs.writeFileSync('nodes.json', JSON.stringify(nodes))
})
console.log(hostKey.publicKey.toString('hex'))
node.serve(hostKey, "getNodes", (input) => {
return { nodes }
})