Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/session/nodeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ import { Multiaddr, isMultiaddr } from "@multiformats/multiaddr";
import { peerIdFromString } from "@libp2p/peer-id";
import { createKeypairFromPeerId, IKeypair } from "../keypair/index.js";
import { ENR, NodeId, v4 } from "../enr/index.js";
import { NodeAddressString } from "./types.js";

/** A representation of an unsigned contactable node. */
export interface INodeAddress {
/** The destination socket address. */
socketAddr: Multiaddr;
/** The destination Node Id. */
nodeId: NodeId;
/** internal string to track sessions */
cachedStr?: NodeAddressString;
}

export function nodeAddressToString(nodeAddr: INodeAddress): string {
export function nodeAddressToString(nodeAddr: INodeAddress): NodeAddressString {
// Since the Discv5 service allows a Multiaddr in outbound message handlers and requires a multiaddr input to
// have a peer ID specified, we remove any p2p portions of the multiaddr when generating the nodeAddressString
// since only the UDP socket addr is included in the session cache key (e.g. /ip4/127.0.0.1/udp/9000/p2p/Qm...)
const normalizedAddr = nodeAddr.socketAddr.decapsulateCode(421);
return nodeAddr.nodeId + ":" + Buffer.from(normalizedAddr.bytes).toString("hex");
if (!nodeAddr.cachedStr) {
const normalizedAddr = nodeAddr.socketAddr.decapsulateCode(421);
nodeAddr.cachedStr = nodeAddr.nodeId + ":" + Buffer.from(normalizedAddr.bytes).toString("hex");
}

return nodeAddr.cachedStr;
}

/**
Expand Down