feat: add mapping function to multiaddr of peerid (#1306)

* feat: add mapping function to multiaddr of peerid

* remove unused
This commit is contained in:
Sasha 2023-04-19 20:03:27 +02:00 committed by GitHub
parent b7d4b675bd
commit 763dc0125d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 4 deletions

View File

@ -1,7 +1,9 @@
import type { Stream } from "@libp2p/interface-connection"; import type { Stream } from "@libp2p/interface-connection";
import type { Libp2p } from "@libp2p/interface-libp2p"; import type { Libp2p } from "@libp2p/interface-libp2p";
import type { PeerId } from "@libp2p/interface-peer-id"; import type { PeerId } from "@libp2p/interface-peer-id";
import type { Multiaddr } from "@multiformats/multiaddr"; import { isPeerId } from "@libp2p/interface-peer-id";
import type { Multiaddr, MultiaddrInput } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import type { import type {
IFilter, IFilter,
ILightPush, ILightPush,
@ -104,10 +106,11 @@ export class WakuNode implements Waku {
* @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols * @param protocols Waku protocols we expect from the peer; Defaults to mounted protocols
*/ */
async dial( async dial(
peer: PeerId | Multiaddr, peer: PeerId | MultiaddrInput,
protocols?: Protocols[] protocols?: Protocols[]
): Promise<Stream> { ): Promise<Stream> {
const _protocols = protocols ?? []; const _protocols = protocols ?? [];
const peerId = mapToPeerIdOrMultiaddr(peer);
if (typeof protocols === "undefined") { if (typeof protocols === "undefined") {
this.relay && _protocols.push(Protocols.Relay); this.relay && _protocols.push(Protocols.Relay);
@ -156,9 +159,9 @@ export class WakuNode implements Waku {
} }
} }
log(`Dialing to ${peer.toString()} with protocols ${_protocols}`); log(`Dialing to ${peerId.toString()} with protocols ${_protocols}`);
return this.libp2p.dialProtocol(peer, codecs); return this.libp2p.dialProtocol(peerId, codecs);
} }
async start(): Promise<void> { async start(): Promise<void> {
@ -189,3 +192,9 @@ export class WakuNode implements Waku {
return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString(); return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString();
} }
} }
function mapToPeerIdOrMultiaddr(
peerId: PeerId | MultiaddrInput
): PeerId | Multiaddr {
return isPeerId(peerId) ? peerId : multiaddr(peerId);
}