2022-06-27 17:53:00 +00:00
|
|
|
import { createRequire } from "module";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
const DHT = require("@hyperswarm/dht");
|
|
|
|
import { errorExit } from "./util.js";
|
|
|
|
import {
|
|
|
|
deriveMyskyRootKeypair,
|
|
|
|
ed25519Keypair,
|
|
|
|
seedPhraseToSeed,
|
|
|
|
validSeedPhrase,
|
|
|
|
} from "libskynet";
|
2022-07-04 21:22:48 +00:00
|
|
|
import { RELAY_SEED } from "./constant_vars";
|
2022-06-27 17:53:00 +00:00
|
|
|
|
|
|
|
let server: {
|
|
|
|
listen: (arg0: ed25519Keypair) => void;
|
|
|
|
ready: () => any;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function start() {
|
|
|
|
let [, err] = validSeedPhrase(RELAY_SEED as string);
|
|
|
|
if (err !== null) {
|
|
|
|
errorExit("RELAY_SEED is invalid. Aborting.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyPair = deriveMyskyRootKeypair(
|
|
|
|
seedPhraseToSeed(RELAY_SEED as string)[0]
|
|
|
|
);
|
|
|
|
|
|
|
|
const node = new DHT({ keyPair });
|
|
|
|
|
|
|
|
await node.ready();
|
|
|
|
|
|
|
|
return (server = node);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function get() {
|
|
|
|
if (!server) {
|
|
|
|
return start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|