2022-07-19 22:24:53 +00:00
|
|
|
import {createRequire} from "module";
|
|
|
|
|
2022-06-27 17:53:00 +00:00
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
const DHT = require("@hyperswarm/dht");
|
2022-07-19 22:24:53 +00:00
|
|
|
import {errorExit} from "./util.js";
|
2022-06-27 17:53:00 +00:00
|
|
|
import {
|
2022-07-19 22:24:53 +00:00
|
|
|
deriveMyskyRootKeypair,
|
|
|
|
ed25519Keypair,
|
|
|
|
seedPhraseToSeed,
|
|
|
|
validSeedPhrase,
|
2022-06-27 17:53:00 +00:00
|
|
|
} from "libskynet";
|
2022-07-05 19:02:07 +00:00
|
|
|
import config from "./config.js";
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
let node: {
|
|
|
|
ready: () => any;
|
|
|
|
createServer: () => any;
|
|
|
|
defaultKeyPair: any;
|
|
|
|
on: any;
|
|
|
|
};
|
2022-06-27 17:53:00 +00:00
|
|
|
let server: {
|
2022-07-19 22:24:53 +00:00
|
|
|
listen: (arg0: ed25519Keypair) => any;
|
|
|
|
on: any;
|
2022-06-27 17:53:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function start() {
|
2022-07-19 22:24:53 +00:00
|
|
|
const seed = config.str("relay-seed");
|
2022-07-05 19:02:07 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
let [, err] = validSeedPhrase(seed);
|
|
|
|
if (err !== null) {
|
|
|
|
errorExit("RELAY_SEED is invalid. Aborting.");
|
|
|
|
}
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
const keyPair = deriveMyskyRootKeypair(seedPhraseToSeed(seed)[0]);
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
node = new DHT({keyPair});
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
await node.ready();
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
server = node.createServer();
|
|
|
|
await server.listen(keyPair);
|
|
|
|
|
|
|
|
return node;
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
export async function get(
|
|
|
|
ret: "server" | "dht" = "dht"
|
|
|
|
): Promise<typeof server | typeof node> {
|
|
|
|
if (!node) {
|
|
|
|
await start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == "server") {
|
|
|
|
return server;
|
|
|
|
}
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
return node;
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|