2022-07-24 03:16:34 +00:00
|
|
|
//const require = createRequire(import.meta.url);
|
|
|
|
//import { createRequire } from "module";
|
2022-07-19 22:24:53 +00:00
|
|
|
|
2022-08-03 06:02:40 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import DHT from "@hyperswarm/dht";
|
2022-09-21 12:59:22 +00:00
|
|
|
import config from "../config.js";
|
|
|
|
import { errorExit } from "../lib/error.js";
|
2022-09-21 11:13:48 +00:00
|
|
|
import {
|
|
|
|
deriveMyskyRootKeypair,
|
|
|
|
seedPhraseToSeed,
|
|
|
|
validSeedPhrase,
|
|
|
|
} from "libskynet";
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
let node: {
|
2022-07-19 22:31:15 +00:00
|
|
|
ready: () => any;
|
|
|
|
createServer: () => any;
|
|
|
|
defaultKeyPair: any;
|
|
|
|
on: any;
|
2022-07-19 22:24:53 +00:00
|
|
|
};
|
2022-08-05 03:33:16 +00:00
|
|
|
let server: any;
|
2022-06-27 17:53:00 +00:00
|
|
|
|
|
|
|
async function start() {
|
2022-07-22 23:55:09 +00:00
|
|
|
const seed = config.str("seed");
|
2022-07-05 19:02:07 +00:00
|
|
|
|
2022-07-22 23:51:11 +00:00
|
|
|
let err = validSeedPhrase(seed);
|
2022-07-19 22:31:15 +00:00
|
|
|
if (err !== null) {
|
|
|
|
errorExit("RELAY_SEED is invalid. Aborting.");
|
|
|
|
}
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
const keyPair = deriveMyskyRootKeypair(seedPhraseToSeed(seed)[0]);
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
node = new DHT({ keyPair });
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
await node.ready();
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
server = node.createServer();
|
|
|
|
await server.listen(keyPair);
|
2022-07-19 22:24:53 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
return node;
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
export async function get(
|
2022-07-19 22:31:15 +00:00
|
|
|
ret: "server" | "dht" = "dht"
|
2022-07-19 22:24:53 +00:00
|
|
|
): Promise<typeof server | typeof node> {
|
2022-07-19 22:31:15 +00:00
|
|
|
if (!node) {
|
|
|
|
await start();
|
|
|
|
}
|
2022-07-19 22:24:53 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
if (ret == "server") {
|
|
|
|
return server;
|
|
|
|
}
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:31:15 +00:00
|
|
|
return node;
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|