From 6c9b4465f7505e2c019f6d513f80ef6320ae018d Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 8 Oct 2024 01:59:49 +0200 Subject: [PATCH 1/3] chore: expose peerId and protocols from WakuNode --- packages/interfaces/src/waku.ts | 20 ++++++++++++++++++++ packages/sdk/src/waku/waku.ts | 8 ++++++++ 2 files changed, 28 insertions(+) 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..df49b3b3ec 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -128,6 +128,14 @@ 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[] From 7c5fba2463baf461c5c8be5518b050166b8726a0 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 8 Oct 2024 02:00:26 +0200 Subject: [PATCH 2/3] remove unused method --- packages/sdk/src/waku/waku.ts | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index df49b3b3ec..44172392da 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -219,21 +219,6 @@ export class WakuNode implements IWaku { public isConnected(): boolean { 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(); - } } function mapToPeerIdOrMultiaddr( peerId: PeerId | MultiaddrInput From affb13132b5f6dabe16bd9590726d9c8fe7534d6 Mon Sep 17 00:00:00 2001 From: Sasha Date: Tue, 8 Oct 2024 02:01:21 +0200 Subject: [PATCH 3/3] move to private method --- packages/sdk/src/waku/waku.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/sdk/src/waku/waku.ts b/packages/sdk/src/waku/waku.ts index 44172392da..414abefbea 100644 --- a/packages/sdk/src/waku/waku.ts +++ b/packages/sdk/src/waku/waku.ts @@ -141,7 +141,7 @@ export class WakuNode implements IWaku { 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); @@ -219,9 +219,10 @@ export class WakuNode implements IWaku { public isConnected(): boolean { return this.connectionManager.isConnected(); } -} -function mapToPeerIdOrMultiaddr( - peerId: PeerId | MultiaddrInput -): PeerId | Multiaddr { - return isPeerId(peerId) ? peerId : multiaddr(peerId); + + private mapToPeerIdOrMultiaddr( + peerId: PeerId | MultiaddrInput + ): PeerId | Multiaddr { + return isPeerId(peerId) ? peerId : multiaddr(peerId); + } }