Skip to content

Commit

Permalink
feat: expose peerId and protocols from WakuNode (#2166)
Browse files Browse the repository at this point in the history
* chore: expose peerId and protocols from WakuNode

* remove unused method

* move to private method
  • Loading branch information
weboko authored Oct 8, 2024
1 parent 8937a56 commit 027a1d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
20 changes: 20 additions & 0 deletions packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
32 changes: 13 additions & 19 deletions packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stream> {
const _protocols = protocols ?? [];
const peerId = mapToPeerIdOrMultiaddr(peer);
const peerId = this.mapToPeerIdOrMultiaddr(peer);

if (typeof protocols === "undefined") {
this.relay && _protocols.push(Protocols.Relay);
Expand Down Expand Up @@ -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);
}

0 comments on commit 027a1d1

Please sign in to comment.