*Add new core dht plugin that allows joining a DHT topic and getting the peers of a given topic

This commit is contained in:
Derrick Hammer 2023-01-05 20:51:43 -05:00
parent ba86b3587b
commit 523f2c04f0
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 29 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import type { Logger } from "pino";
import { getHDKey, getSeed } from "../lib/seed.js";
import pluginRpc from "./plugins/rpc";
import pluginCore from "./plugins/core";
import pluginDht from "./plugins/dht";
import type Config from "@lumeweb/cfg";
import EventEmitter2 from "eventemitter2";
import log from "../log.js";
@ -229,6 +230,7 @@ export async function loadPlugins() {
apiManager.loadPluginInstance(pluginCore);
apiManager.loadPluginInstance(pluginRpc);
apiManager.loadPluginInstance(pluginDht);
for (const plugin of [...new Set(config.array("plugins", []))] as []) {
await apiManager.loadPlugin(plugin);

View File

@ -0,0 +1,27 @@
import { Plugin, PluginAPI } from "@lumeweb/relay-types";
import { getRpcServer } from "../rpc/server";
import b4a from "b4a";
const plugin: Plugin = {
name: "dht",
async plugin(api: PluginAPI): Promise<void> {
api.registerMethod("join_topic", {
cacheable: false,
async handler(topic: string): Promise<void> {
if (!api.swarm._discovery.has(topic)) {
api.swarm.join(topic);
}
},
});
api.registerMethod("get_topic_peers", {
cacheable: false,
async handler(topic: string): Promise<string[]> {
return [...api.swarm.peers.values()]
.filter((peerInfo) => peerInfo._seenTopics.has(topic))
.map((peerInfo) => b4a.from(peerInfo.publicKey).toString());
},
});
},
};
export default plugin;