Skip to content

Commit 763dc01

Browse files
authored
feat: add mapping function to multiaddr of peerid (#1306)
* feat: add mapping function to multiaddr of peerid * remove unused
1 parent b7d4b67 commit 763dc01

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/core/src/lib/waku.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { Stream } from "@libp2p/interface-connection";
22
import type { Libp2p } from "@libp2p/interface-libp2p";
33
import type { PeerId } from "@libp2p/interface-peer-id";
4-
import type { Multiaddr } from "@multiformats/multiaddr";
4+
import { isPeerId } from "@libp2p/interface-peer-id";
5+
import type { Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
6+
import { multiaddr } from "@multiformats/multiaddr";
57
import type {
68
IFilter,
79
ILightPush,
@@ -104,10 +106,11 @@ export class WakuNode implements Waku {
104106
* @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
105107
*/
106108
async dial(
107-
peer: PeerId | Multiaddr,
109+
peer: PeerId | MultiaddrInput,
108110
protocols?: Protocols[]
109111
): Promise<Stream> {
110112
const _protocols = protocols ?? [];
113+
const peerId = mapToPeerIdOrMultiaddr(peer);
111114

112115
if (typeof protocols === "undefined") {
113116
this.relay && _protocols.push(Protocols.Relay);
@@ -156,9 +159,9 @@ export class WakuNode implements Waku {
156159
}
157160
}
158161

159-
log(`Dialing to ${peer.toString()} with protocols ${_protocols}`);
162+
log(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
160163

161-
return this.libp2p.dialProtocol(peer, codecs);
164+
return this.libp2p.dialProtocol(peerId, codecs);
162165
}
163166

164167
async start(): Promise<void> {
@@ -189,3 +192,9 @@ export class WakuNode implements Waku {
189192
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
190193
}
191194
}
195+
196+
function mapToPeerIdOrMultiaddr(
197+
peerId: PeerId | MultiaddrInput
198+
): PeerId | Multiaddr {
199+
return isPeerId(peerId) ? peerId : multiaddr(peerId);
200+
}

0 commit comments

Comments
 (0)