*Refactor getRpcByPeer to operate in Buffers

This commit is contained in:
Derrick Hammer 2022-11-28 00:14:11 -05:00
parent 91642ea729
commit 2f9a0c7356
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 4 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import { errorExit } from "../lib/error.js";
import stringify from "json-stable-stringify";
import { getRpcServer, RPC_PROTOCOL_SYMBOL } from "./rpc/server.js";
import { get as getSwarm, SecretStream } from "./swarm.js";
import b4a from "b4a";
export async function start() {
if (!config.str("pocket-app-id") || !config.str("pocket-app-key")) {
@ -18,8 +19,9 @@ export async function start() {
);
}
export async function getRpcByPeer(peer: string) {
export async function getRpcByPeer(peer: Buffer | string) {
const swarm = getSwarm();
peer = b4a.from(peer) as Buffer;
if (swarm._allConnections.has(peer)) {
return swarm._allConnections.get(peer)[RPC_PROTOCOL_SYMBOL];
@ -28,7 +30,7 @@ export async function getRpcByPeer(peer: string) {
return new Promise((resolve) => {
const listener = () => {};
swarm.on("connection", (peer: any, info: any) => {
if (info.publicKey.toString("hex") !== peer) {
if (info.publicKey.toString("hex") !== peer.toString("hex")) {
return;
}
swarm.removeListener("connection", listener);