@@ -4,7 +4,7 @@ const debug = require('debug')
44const EventEmitter = require ( 'events' )
55const errcode = require ( 'err-code' )
66
7- const PeerInfo = require ( 'peer-info ' )
7+ const PeerId = require ( 'peer-id ' )
88const MulticodecTopology = require ( 'libp2p-interfaces/src/topology/multicodec-topology' )
99
1010const message = require ( './message' )
@@ -42,7 +42,7 @@ class PubsubBaseProtocol extends EventEmitter {
4242 * @param {Object } props
4343 * @param {String } props.debugName log namespace
4444 * @param {Array<string>|string } props.multicodecs protocol identificers to connect
45- * @param {PeerInfo } props.peerInfo peer's peerInfo
45+ * @param {PeerId } props.peerId peer's peerId
4646 * @param {Object } props.registrar registrar for libp2p protocols
4747 * @param {function } props.registrar.handle
4848 * @param {function } props.registrar.register
@@ -54,7 +54,7 @@ class PubsubBaseProtocol extends EventEmitter {
5454 constructor ( {
5555 debugName,
5656 multicodecs,
57- peerInfo ,
57+ peerId ,
5858 registrar,
5959 signMessages = true ,
6060 strictSigning = true
@@ -67,8 +67,8 @@ class PubsubBaseProtocol extends EventEmitter {
6767 throw new Error ( 'multicodecs are required' )
6868 }
6969
70- if ( ! PeerInfo . isPeerInfo ( peerInfo ) ) {
71- throw new Error ( 'peer info must be an instance of `peer-info `' )
70+ if ( ! PeerId . isPeerId ( peerId ) ) {
71+ throw new Error ( 'peerId must be an instance of `peer-id `' )
7272 }
7373
7474 validateRegistrar ( registrar )
@@ -79,11 +79,12 @@ class PubsubBaseProtocol extends EventEmitter {
7979 this . log . err = debug ( `${ debugName } :error` )
8080
8181 this . multicodecs = utils . ensureArray ( multicodecs )
82- this . peerInfo = peerInfo
8382 this . registrar = registrar
8483
8584 this . started = false
8685
86+ this . peerId = peerId
87+
8788 /**
8889 * Map of topics to which peers are subscribed to
8990 *
@@ -99,9 +100,7 @@ class PubsubBaseProtocol extends EventEmitter {
99100 this . peers = new Map ( )
100101
101102 // Message signing
102- if ( signMessages ) {
103- this . peerId = this . peerInfo . id
104- }
103+ this . signMessages = signMessages
105104
106105 /**
107106 * If message signing should be required for incoming messages
@@ -170,13 +169,14 @@ class PubsubBaseProtocol extends EventEmitter {
170169 * @param {DuplexStream } props.strean
171170 * @param {Connection } props.connection connection
172171 */
173- async _onIncomingStream ( { protocol, stream, connection } ) {
174- const peerInfo = await PeerInfo . create ( connection . remotePeer )
175- peerInfo . protocols . add ( protocol )
172+ _onIncomingStream ( { protocol, stream, connection } ) {
173+ const peerId = connection . remotePeer
174+ const idB58Str = peerId . toB58String ( )
176175
177- const idB58Str = peerInfo . id . toB58String ( )
178-
179- const peer = this . _addPeer ( new Peer ( peerInfo ) )
176+ const peer = this . _addPeer ( new Peer ( {
177+ id : peerId ,
178+ protocols : [ protocol ]
179+ } ) )
180180
181181 peer . attachConnection ( stream )
182182 this . _processMessages ( idB58Str , stream , peer )
@@ -185,14 +185,18 @@ class PubsubBaseProtocol extends EventEmitter {
185185 /**
186186 * Registrar notifies a connection successfully with pubsub protocol.
187187 * @private
188- * @param {PeerInfo } peerInfo remote peer info
188+ * @param {PeerId } peerId remote peer-id
189189 * @param {Connection } conn connection to the peer
190190 */
191- async _onPeerConnected ( peerInfo , conn ) {
192- const idB58Str = peerInfo . id . toB58String ( )
191+ async _onPeerConnected ( peerId , conn ) {
192+ const idB58Str = peerId . toB58String ( )
193193 this . log ( 'connected' , idB58Str )
194194
195- const peer = this . _addPeer ( new Peer ( peerInfo ) )
195+ const peer = this . _addPeer ( new Peer ( {
196+ id : peerId ,
197+ protocols : this . multicodecs
198+ } ) )
199+
196200 try {
197201 const { stream } = await conn . newStream ( this . multicodecs )
198202 peer . attachConnection ( stream )
@@ -205,11 +209,11 @@ class PubsubBaseProtocol extends EventEmitter {
205209 /**
206210 * Registrar notifies a closing connection with pubsub protocol.
207211 * @private
208- * @param {PeerInfo } peerInfo peer info
212+ * @param {PeerId } peerId peerId
209213 * @param {Error } err error for connection end
210214 */
211- _onPeerDisconnected ( peerInfo , err ) {
212- const idB58Str = peerInfo . id . toB58String ( )
215+ _onPeerDisconnected ( peerId , err ) {
216+ const idB58Str = peerId . toB58String ( )
213217 const peer = this . peers . get ( idB58Str )
214218
215219 this . log ( 'connection ended' , idB58Str , err ? err . message : '' )
@@ -219,11 +223,11 @@ class PubsubBaseProtocol extends EventEmitter {
219223 /**
220224 * Add a new connected peer to the peers map.
221225 * @private
222- * @param {PeerInfo } peer peer info
223- * @returns {PeerInfo }
226+ * @param {Peer } peer internal peer
227+ * @returns {Peer }
224228 */
225229 _addPeer ( peer ) {
226- const id = peer . info . id . toB58String ( )
230+ const id = peer . id . toB58String ( )
227231 let existing = this . peers . get ( id )
228232
229233 if ( ! existing ) {
@@ -242,11 +246,11 @@ class PubsubBaseProtocol extends EventEmitter {
242246 * Remove a peer from the peers map.
243247 * @private
244248 * @param {Peer } peer peer state
245- * @returns {PeerInfo }
249+ * @returns {Peer }
246250 */
247251 _removePeer ( peer ) {
248252 if ( ! peer ) return
249- const id = peer . info . id . toB58String ( )
253+ const id = peer . id . toB58String ( )
250254
251255 this . log ( 'remove' , id , peer . _references )
252256
@@ -287,7 +291,7 @@ class PubsubBaseProtocol extends EventEmitter {
287291 */
288292 _buildMessage ( message ) {
289293 const msg = utils . normalizeOutRpcMessage ( message )
290- if ( this . peerId ) {
294+ if ( this . signMessages ) {
291295 return signMessage ( this . peerId , msg )
292296 } else {
293297 return message
@@ -310,7 +314,7 @@ class PubsubBaseProtocol extends EventEmitter {
310314
311315 return Array . from ( this . peers . values ( ) )
312316 . filter ( ( peer ) => peer . topics . has ( topic ) )
313- . map ( ( peer ) => peer . info . id . toB58String ( ) )
317+ . map ( ( peer ) => peer . id . toB58String ( ) )
314318 }
315319
316320 /**
0 commit comments