diff --git a/doc/API.md b/doc/API.md index bfaa9f778b..f2bb193995 100644 --- a/doc/API.md +++ b/doc/API.md @@ -41,6 +41,8 @@ * [`metrics.forPeer`](#metricsforpeer) * [`metrics.forProtocol`](#metricsforprotocol) * [Events](#events) + * [`libp2p`](#eventslibp2p) + * [`libp2p.peerStore`](#eventslibp2ppeerStore) * [Types](#types) * [`Stats`](#stats) @@ -1099,7 +1101,9 @@ console.log(peerStats.toJSON()) ## Events -Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events. +Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events. + +### libp2p #### An error has occurred @@ -1132,6 +1136,28 @@ This event will be triggered anytime we are disconnected from another peer, rega - `peer`: instance of [`PeerInfo`][peer-info] +### libp2p.peerStore + +#### A new peer is added to the peerStore + +`libp2p.peerStore.on('peer', (peerId) => {})` + +- `peerId`: instance of [`PeerId`][peer-id] + +#### Knownw multiaddrs for a peer change + +`libp2p.peerStore.on('change:multiaddrs', ({ peerId, multiaddrs}) => {})` + +- `peerId`: instance of [`PeerId`][peer-id] +- `multiaddrs`: array of known [`multiaddr`][multiaddr] for a the peer + +#### Knownw protocols for a peer change + +`libp2p.peerStore.on('change:protocols', ({ peerId, protocols}) => {})` + +- `peerId`: instance of [`PeerId`][peer-id] +- `protocols`: array of known supported protocols for the peer (string identifiers) + ## Types ### Stats diff --git a/src/peer-store/address-book.js b/src/peer-store/address-book.js index 3e3df3412d..6ae4623041 100644 --- a/src/peer-store/address-book.js +++ b/src/peer-store/address-book.js @@ -185,12 +185,6 @@ class AddressBook extends Book { return multiaddrInfos } - _setPeerId (peerId) { - if (!this._ps.peerIds.get(peerId)) { - this._ps.peerIds.set(peerId.toString(), peerId) - } - } - /** * Get the known multiaddrs for a given peer. All returned multiaddrs * will include the encapsulated `PeerId` of the peer. diff --git a/src/peer-store/book.js b/src/peer-store/book.js index 32456b7c13..c44b55d5c2 100644 --- a/src/peer-store/book.js +++ b/src/peer-store/book.js @@ -82,6 +82,12 @@ class Book { return true } + + _setPeerId (peerId) { + if (!this._ps.peerIds.get(peerId)) { + this._ps.peerIds.set(peerId.toB58String(), peerId) + } + } } module.exports = Book diff --git a/src/peer-store/index.js b/src/peer-store/index.js index 35d34218dd..589a186dca 100644 --- a/src/peer-store/index.js +++ b/src/peer-store/index.js @@ -120,7 +120,7 @@ class PeerStore extends EventEmitter { throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) } - const id = this.peerIds.get(peerId.toString()) + const id = this.peerIds.get(peerId.toB58String()) const multiaddrInfos = this.addressBook.get(peerId) const protocols = this.protoBook.get(peerId) diff --git a/src/peer-store/proto-book.js b/src/peer-store/proto-book.js index 50e815299b..7f73812524 100644 --- a/src/peer-store/proto-book.js +++ b/src/peer-store/proto-book.js @@ -134,12 +134,6 @@ class ProtoBook extends Book { return this } - - _setPeerId (peerId) { - if (!this._ps.peerIds.get(peerId)) { - this._ps.peerIds.set(peerId.toString(), peerId) - } - } } module.exports = ProtoBook