diff --git a/packages/core/src/lib/connection_manager.ts b/packages/core/src/lib/connection_manager.ts index 75f88f9e15..722a7eaf7d 100644 --- a/packages/core/src/lib/connection_manager.ts +++ b/packages/core/src/lib/connection_manager.ts @@ -1,6 +1,6 @@ import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface"; -import { TypedEventEmitter } from "@libp2p/interface"; -import { multiaddr } from "@multiformats/multiaddr"; +import { isPeerId, TypedEventEmitter } from "@libp2p/interface"; +import { multiaddr, MultiaddrInput } from "@multiformats/multiaddr"; import { ConnectionManagerOptions, DiscoveryTrigger, @@ -12,7 +12,6 @@ import { IPeersByDiscoveryEvents, IRelay, KeepAliveOptions, - MultiaddrStr, PeersByDiscoveryResult, PubsubTopic, ShardInfo @@ -222,27 +221,10 @@ export class ConnectionManager } public async dialPeer( - peer: PeerId | MultiaddrStr, + peer: PeerId | MultiaddrInput, protocols?: string[] ): Promise { - let peerId: PeerId; - if (typeof peer === "string") { - const ma = multiaddr(peer); - const peerIdStr = ma.getPeerId(); - if (!peerIdStr) { - throw new Error("Failed to dial multiaddr: missing peer ID"); - } - const conn = this.libp2p - .getConnections() - .find((conn) => conn.remotePeer.toString() === peerIdStr); - if (conn) { - peerId = conn.remotePeer; - } else { - throw new Error("Failed to dial multiaddr: no connection found"); - } - } else { - peerId = peer; - } + const peerId = this.getPeerIdFromPeerOrMultiaddr(peer); this.currentActiveParallelDialCount += 1; let dialAttempt = 0; @@ -331,6 +313,34 @@ export class ConnectionManager } } + /** + * Internal utility to extract a PeerId from either a PeerId object or multiaddr input. + * This is used internally by the connection manager to handle different peer input formats. + * @internal + * @param peer - The PeerId or MultiaddrInput to extract the PeerId from + * @returns The extracted PeerId + */ + private getPeerIdFromPeerOrMultiaddr(peer: PeerId | MultiaddrInput): PeerId { + if (isPeerId(peer)) { + return peer; + } else { + // peer is of MultiaddrInput type + const ma = multiaddr(peer); + const peerIdStr = ma.getPeerId(); + if (!peerIdStr) { + throw new Error("Failed to dial multiaddr: missing peer ID"); + } + const conn = this.libp2p + .getConnections() + .find((conn) => conn.remotePeer.toString() === peerIdStr); + if (conn) { + return conn.remotePeer; + } else { + throw new Error("Failed to dial multiaddr: no connection found"); + } + } + } + private async attemptDnsDiscovery(): Promise { if (this.libp2p.getConnections().length > 0) return; if ((await this.libp2p.peerStore.all()).length > 0) return; diff --git a/packages/interfaces/src/libp2p.ts b/packages/interfaces/src/libp2p.ts index 401ead09f5..53bb106308 100644 --- a/packages/interfaces/src/libp2p.ts +++ b/packages/interfaces/src/libp2p.ts @@ -36,5 +36,3 @@ export type CreateLibp2pOptions = Libp2pOptions & { */ filterMultiaddrs?: boolean; }; - -export type MultiaddrStr = string; diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index 4f3edf4f86..79a057e0f2 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -1,4 +1,4 @@ -import type { PeerId, Stream } from "@libp2p/interface"; +import type { PeerId } from "@libp2p/interface"; import type { MultiaddrInput } from "@multiformats/multiaddr"; import { IConnectionManager } from "./connection_manager.js"; @@ -55,7 +55,7 @@ export interface IWaku { * waku.isConnected() === true; * ``` */ - dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise; + dial(peer: PeerId | MultiaddrInput, protocols?: Protocols[]): Promise; /** * Starts all services and components related to functionality of Waku node. diff --git a/packages/sdk/src/create/libp2p.ts b/packages/sdk/src/create/libp2p.ts index e979487f9b..e474f88332 100644 --- a/packages/sdk/src/create/libp2p.ts +++ b/packages/sdk/src/create/libp2p.ts @@ -36,7 +36,8 @@ export async function defaultLibp2p( options?: Partial, userAgent?: string ): Promise { - if (!options?.hideWebSocketInfo && process?.env?.NODE_ENV !== "test") { + const isTestEnv = process?.env?.NODE_ENV === "test" ?? false; + if (!options?.hideWebSocketInfo && !isTestEnv) { /* eslint-disable no-console */ console.info( "%cIgnore WebSocket connection failures", diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 48d9b71d39..8992912a33 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -1,4 +1,3 @@ -import type { Stream } from "@libp2p/interface"; import { isPeerId, PeerId } from "@libp2p/interface"; import { multiaddr, Multiaddr, MultiaddrInput } from "@multiformats/multiaddr"; import { ConnectionManager, getHealthManager, StoreCodec } from "@waku/core"; @@ -153,9 +152,8 @@ export class WakuNode implements IWaku { public async dial( peer: PeerId | MultiaddrInput, protocols?: Protocols[] - ): Promise { + ): Promise { const _protocols = protocols ?? []; - const peerId = this.mapToPeerIdOrMultiaddr(peer); if (typeof protocols === "undefined") { this.relay && _protocols.push(Protocols.Relay); @@ -204,9 +202,9 @@ export class WakuNode implements IWaku { } } + const peerId = this.mapToPeerIdOrMultiaddr(peer); log.info(`Dialing to ${peerId.toString()} with protocols ${_protocols}`); - - return this.libp2p.dialProtocol(peerId, codecs); + return await this.connectionManager.dialPeer(peer, codecs); } public async start(): Promise {