relay/src/dht.ts

42 lines
829 B
TypeScript
Raw Normal View History

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-05 19:02:07 +00:00
import config from "./config.js";
2022-06-27 17:53:00 +00:00
let server: {
listen: (arg0: ed25519Keypair) => void;
ready: () => any;
};
async function start() {
2022-07-05 19:02:07 +00:00
const seed = config.str("relay-seed");
let [, err] = validSeedPhrase(seed);
2022-06-27 17:53:00 +00:00
if (err !== null) {
errorExit("RELAY_SEED is invalid. Aborting.");
}
2022-07-05 19:02:07 +00:00
const keyPair = deriveMyskyRootKeypair(seedPhraseToSeed(seed)[0]);
2022-06-27 17:53:00 +00:00
const node = new DHT({ keyPair });
await node.ready();
return (server = node);
}
export async function get() {
if (!server) {
return start();
}
return server;
}