relay/src/modules/swarm.ts

44 lines
962 B
TypeScript
Raw Normal View History

//const require = createRequire(import.meta.url);
//import { createRequire } from "module";
// @ts-ignore
import Hyperswarm from "hyperswarm";
// @ts-ignore
import DHT from "@hyperswarm/dht";
2022-06-27 17:53:00 +00:00
// @ts-ignore
import sodium from "sodium-universal";
import b4a from "b4a";
2022-12-19 16:42:51 +00:00
import { log } from "../log.js";
import { getKeyPair } from "../lib/seed.js";
const LUMEWEB = b4a.from("lumeweb");
export const LUMEWEB_TOPIC_HASH = b4a.allocUnsafe(32);
sodium.crypto_generichash(LUMEWEB_TOPIC_HASH, LUMEWEB);
export type SecretStream = any;
let node: Hyperswarm;
2022-06-27 17:53:00 +00:00
export async function start() {
const keyPair = getKeyPair();
2022-06-27 17:53:00 +00:00
node = new Hyperswarm({ keyPair, dht: new DHT({ keyPair }) });
2022-06-27 17:53:00 +00:00
// @ts-ignore
await node.dht.ready();
await node.listen();
node.join(LUMEWEB_TOPIC_HASH);
2022-12-06 22:03:30 +00:00
log.info(
"Relay Identity is",
b4a.from(node.dht.defaultKeyPair.publicKey).toString("hex")
);
2022-07-19 22:31:15 +00:00
return node;
2022-06-27 17:53:00 +00:00
}
export function get(): Hyperswarm {
2022-07-19 22:31:15 +00:00
return node;
2022-06-27 17:53:00 +00:00
}