From c804216afaa6b6420ddc86f111c1ce8202ba5527 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 26 Apr 2023 10:20:49 +0100 Subject: [PATCH] feat: add getAddresses function to return detailed address info Returning a plain list of multiaddrs isn't enough to know what is going on with the state of a node's addresses. This PR adds a `getAddresses` function that returns detailed info about the addresses - are they being announced, was it an observed address, are we confident that the observed address is publicly routable, etc. --- packages/interface-libp2p/src/index.ts | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/interface-libp2p/src/index.ts b/packages/interface-libp2p/src/index.ts index c62d7466e..f3efe3aa6 100644 --- a/packages/interface-libp2p/src/index.ts +++ b/packages/interface-libp2p/src/index.ts @@ -46,6 +46,34 @@ export interface PeerUpdate { previous?: Peer } +/** + * A address this node knows about + */ +export interface NodeAddress { + /** + * This address as a multiaddr + */ + multiaddr: Multiaddr + + /** + * If `true`, this address is advertised to the network via peer records and the + * Identify protocols + */ + announce?: boolean + + /** + * If `true`, this address was observed by remote peers and communicated back to + * this node via the AutoNAT protocol + */ + observed?: boolean + + /** + * Only set on `observed` addresses - if `true` the node has confidence that this + * address is publicly routable and remote peers can dial it + */ + confidence?: boolean +} + /** * Once you have a libp2p instance, you can listen to several events it emits, * so that you can be notified of relevant network events. @@ -404,6 +432,22 @@ export interface Libp2p extends Startable, EventEmitter { */ getMultiaddrs: () => Multiaddr[] + /** + * Returns a list of address this node is listening on and the status + * of those addresses + * + * @example + * + * ```js + * const listenMa = libp2p.getAddresses() + * // [{ + * // multiaddr: + * // announce: true + * // }] + * ``` + */ + getAddresses: () => NodeAddress[] + /** * Returns a list of supported protocols *