-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
87 lines (71 loc) · 2.7 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import Web3Utils from "./modules/Web3Utils.mjs";
import dotenv from "dotenv";
import Logger from "./modules/Logger.mjs";
import NetworkMap from "./modules/NetworkMap.mjs";
import ProtocolMessages from "./modules/ProtocolMessages.mjs";
import UnencryptedSignedMessage from "./modules/messages/UnencryptedSignedMessage.mjs";
import WebsocketNetwork from "./modules/networkings/websocket/WebsocketNetwork.mjs";
import DHChannel from "./modules/DHChannel.mjs";
import * as fs from "node:fs";
import API from "./modules/API.mjs";
dotenv.config({path: process.argv[2] || '.env'});
Logger.logMessage('StarWave 2 Protocol node');
const nodePrivateKey = process.env.NODE_PRIVATE_KEY;
const nodeAddress = (await Web3Utils.privateKeyToAddress(nodePrivateKey)).toLowerCase();
Logger.logMessage(`Node address: ${nodeAddress}`);
let networkMap = new NetworkMap();
let messagesProcessor = new ProtocolMessages({myAddress: nodeAddress, myPrivateKey: nodePrivateKey, networkMap});
//Create DH Channel
const dhChannel = new DHChannel({
myAddress: nodeAddress,
myPrivateKey: nodePrivateKey,
protocolMessages: messagesProcessor
});
//Add network providers
messagesProcessor.registerNetworkProvider(new WebsocketNetwork({myAddress: nodeAddress, myPrivateKey: nodePrivateKey}));
//Load networks plugins providers
let NETWORK_PLUGINS = process.env.NETWORK_PLUGINS ? process.env.NETWORK_PLUGINS.split(',') : [];
if(process.env.AUTOLOAD_PLUGINS?.toLowerCase() === 'true') {
let networkingPlugins = fs.readdirSync('./plugins/networks');
for (let plugin of networkingPlugins) {
//Check is dir
let stat = fs.statSync(`./plugins/networks/${plugin}`);
if (!stat.isDirectory()) {
continue;
}
NETWORK_PLUGINS.push(`./plugins/networks/${plugin}/index.mjs`);
}
}
for (let plugin of NETWORK_PLUGINS) {
try {
const pluginModule = (await import(plugin)).default;
messagesProcessor.registerNetworkProvider(new pluginModule({
myAddress: nodeAddress,
myPrivateKey: nodePrivateKey,
utils: {
logger: Logger,
dhChannel,
networkMap,
}
}));
} catch (e) {
Logger.error(`Failed to load plugin ${plugin}`, e);
}
}
//Initialize networks, plugins and addons
await messagesProcessor.initNetworks();
Logger.log('Networks initialized');
await dhChannel.init();
Logger.log('DH Channel initialized');
if(process.env.API_ENABLE?.toLowerCase() === 'true') {
const api = new API({
protocolMessages: messagesProcessor,
dhChannel,
networkMap,
nodeAddress: nodeAddress,
utils: {
logger: Logger
}
});
await api.init();
}