diff --git a/packages/sdk/src/wait_for_remote_peer.ts b/packages/sdk/src/wait_for_remote_peer.ts index 9126bd7314..a7bae966b4 100644 --- a/packages/sdk/src/wait_for_remote_peer.ts +++ b/packages/sdk/src/wait_for_remote_peer.ts @@ -31,7 +31,8 @@ export async function waitForRemotePeer( protocols?: Protocols[], timeoutMs?: number ): Promise { - protocols = protocols ?? getEnabledProtocols(waku); + // if no protocols or empty array passed - try to derive from mounted + protocols = protocols?.length ? protocols : getEnabledProtocols(waku); const connections = waku.libp2p.getConnections(); if (!waku.isStarted()) { @@ -39,7 +40,7 @@ export async function waitForRemotePeer( } if (connections.length > 0 && !protocols.includes(Protocols.Relay)) { - const success = await waitForMetadata(waku); + const success = await waitForMetadata(waku, protocols); if (success) { return; @@ -135,10 +136,13 @@ async function waitForConnectedPeer( /** * Waits for the metadata from the remote peer. */ -async function waitForMetadata(waku: Waku): Promise { +async function waitForMetadata( + waku: Waku, + protocols: Protocols[] +): Promise { const connectedPeers = waku.libp2p.getPeers(); const metadataService = waku.libp2p.services.metadata; - const enabledCodes = getEnabledCodecs(waku); + const enabledCodes = mapProtocolsToCodecs(protocols); if (!connectedPeers.length || !metadataService) { log.info( @@ -236,19 +240,19 @@ function getEnabledProtocols(waku: Waku): Protocols[] { return protocols; } -function getEnabledCodecs(waku: Waku): Map { +function mapProtocolsToCodecs(protocols: Protocols[]): Map { const codecs: Map = new Map(); - if (waku.filter) { - codecs.set(FilterCodecs.SUBSCRIBE, false); - } - - if (waku.store) { - codecs.set(StoreCodec, false); - } + const protocolToCodec: Record = { + [Protocols.Filter]: FilterCodecs.SUBSCRIBE, + [Protocols.LightPush]: LightPushCodec, + [Protocols.Store]: StoreCodec + }; - if (waku.lightPush) { - codecs.set(LightPushCodec, false); + for (const protocol of protocols) { + if (protocolToCodec[protocol]) { + codecs.set(protocolToCodec[protocol], false); + } } return codecs;