diff --git a/packages/interfaces/src/waku.ts b/packages/interfaces/src/waku.ts index 3602046f01..ca49e489f8 100644 --- a/packages/interfaces/src/waku.ts +++ b/packages/interfaces/src/waku.ts @@ -20,6 +20,26 @@ export interface IWaku { health: IHealthManager; connectionManager: IConnectionManager; + /** + * Returns a unique identifier for a node on the network. + * + * @example + * ```typescript + * console.log(waku.peerId); // 12D3KooWNmk9yXHfHJ4rUduRqD1TCTHkNFMPF9WP2dqWpZDL4aUb + * ``` + */ + peerId: PeerId; + + /** + * Returns a list of supported protocols. + * + * @example + * ```typescript + * console.log(waku.protocols); // ['/ipfs/id/1.0.0', '/ipfs/ping/1.0.0', '/vac/waku/filter-push/2.0.0-beta1', '/vac/waku/metadata/1.0.0'] + * ``` + */ + protocols: string[]; + /** * Dials to the provided peer * diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 319b23fad6..414abefbea 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -128,12 +128,20 @@ export class WakuNode implements IWaku { ); } + public get peerId(): PeerId { + return this.libp2p.peerId; + } + + public get protocols(): string[] { + return this.libp2p.getProtocols(); + } + public async dial( peer: PeerId | MultiaddrInput, protocols?: Protocols[] ): Promise { const _protocols = protocols ?? []; - const peerId = mapToPeerIdOrMultiaddr(peer); + const peerId = this.mapToPeerIdOrMultiaddr(peer); if (typeof protocols === "undefined") { this.relay && _protocols.push(Protocols.Relay); @@ -212,23 +220,9 @@ export class WakuNode implements IWaku { return this.connectionManager.isConnected(); } - /** - * Return the local multiaddr with peer id on which libp2p is listening. - * - * @throws if libp2p is not listening on localhost. - */ - public getLocalMultiaddrWithID(): string { - const localMultiaddr = this.libp2p - .getMultiaddrs() - .find((addr) => addr.toString().match(/127\.0\.0\.1/)); - if (!localMultiaddr || localMultiaddr.toString() === "") { - throw "Not listening on localhost"; - } - return localMultiaddr + "/p2p/" + this.libp2p.peerId.toString(); + private mapToPeerIdOrMultiaddr( + peerId: PeerId | MultiaddrInput + ): PeerId | Multiaddr { + return isPeerId(peerId) ? peerId : multiaddr(peerId); } } -function mapToPeerIdOrMultiaddr( - peerId: PeerId | MultiaddrInput -): PeerId | Multiaddr { - return isPeerId(peerId) ? peerId : multiaddr(peerId); -}