From cd7b12e8b3864c8660ad25b0e7d43a1b5862b8cf Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 4 Dec 2022 12:04:18 -0500 Subject: [PATCH] *add get_peers and get_direct_peers api methods to the core rpc plugin --- src/modules/plugins/rpc.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/modules/plugins/rpc.ts b/src/modules/plugins/rpc.ts index 1df1014..90fcc06 100644 --- a/src/modules/plugins/rpc.ts +++ b/src/modules/plugins/rpc.ts @@ -9,7 +9,7 @@ import { RPCResponse, } from "@lumeweb/relay-types"; import { getRpcByPeer } from "../rpc"; -import { get as getSwarm } from "../swarm"; +import { get as getSwarm, LUMEWEB_TOPIC_HASH } from "../swarm"; import b4a from "b4a"; async function broadcastRequest( @@ -124,6 +124,21 @@ const plugin: Plugin = { return result; }, }); + api.registerMethod("get_peers", { + cacheable: false, + async handler(): Promise { + return [...getRpcServer().cache.dhtCache.online]; + }, + }); + api.registerMethod("get_direct_peers", { + cacheable: false, + async handler(): Promise { + const topic = LUMEWEB_TOPIC_HASH.toString("hex"); + return [...getRpcServer().cache.swarm.peers.values()] + .filter((item: any) => [...item._seenTopics.keys()].includes(topic)) + .map((item: any) => item.publicKey.toString("hex")); + }, + }); }, };