@@ -4,7 +4,7 @@ const debug = require('debug')
4
4
const EventEmitter = require ( 'events' )
5
5
const errcode = require ( 'err-code' )
6
6
7
- const PeerInfo = require ( 'peer-info ' )
7
+ const PeerId = require ( 'peer-id ' )
8
8
const MulticodecTopology = require ( 'libp2p-interfaces/src/topology/multicodec-topology' )
9
9
10
10
const message = require ( './message' )
@@ -42,7 +42,7 @@ class PubsubBaseProtocol extends EventEmitter {
42
42
* @param {Object } props
43
43
* @param {String } props.debugName log namespace
44
44
* @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
46
46
* @param {Object } props.registrar registrar for libp2p protocols
47
47
* @param {function } props.registrar.handle
48
48
* @param {function } props.registrar.register
@@ -54,7 +54,7 @@ class PubsubBaseProtocol extends EventEmitter {
54
54
constructor ( {
55
55
debugName,
56
56
multicodecs,
57
- peerInfo ,
57
+ peerId ,
58
58
registrar,
59
59
signMessages = true ,
60
60
strictSigning = true
@@ -67,8 +67,8 @@ class PubsubBaseProtocol extends EventEmitter {
67
67
throw new Error ( 'multicodecs are required' )
68
68
}
69
69
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 `' )
72
72
}
73
73
74
74
validateRegistrar ( registrar )
@@ -79,11 +79,12 @@ class PubsubBaseProtocol extends EventEmitter {
79
79
this . log . err = debug ( `${ debugName } :error` )
80
80
81
81
this . multicodecs = utils . ensureArray ( multicodecs )
82
- this . peerInfo = peerInfo
83
82
this . registrar = registrar
84
83
85
84
this . started = false
86
85
86
+ this . peerId = peerId
87
+
87
88
/**
88
89
* Map of topics to which peers are subscribed to
89
90
*
@@ -99,9 +100,7 @@ class PubsubBaseProtocol extends EventEmitter {
99
100
this . peers = new Map ( )
100
101
101
102
// Message signing
102
- if ( signMessages ) {
103
- this . peerId = this . peerInfo . id
104
- }
103
+ this . signMessages = signMessages
105
104
106
105
/**
107
106
* If message signing should be required for incoming messages
@@ -170,13 +169,14 @@ class PubsubBaseProtocol extends EventEmitter {
170
169
* @param {DuplexStream } props.strean
171
170
* @param {Connection } props.connection connection
172
171
*/
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 ( )
176
175
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
+ } ) )
180
180
181
181
peer . attachConnection ( stream )
182
182
this . _processMessages ( idB58Str , stream , peer )
@@ -185,14 +185,18 @@ class PubsubBaseProtocol extends EventEmitter {
185
185
/**
186
186
* Registrar notifies a connection successfully with pubsub protocol.
187
187
* @private
188
- * @param {PeerInfo } peerInfo remote peer info
188
+ * @param {PeerId } peerId remote peer-id
189
189
* @param {Connection } conn connection to the peer
190
190
*/
191
- async _onPeerConnected ( peerInfo , conn ) {
192
- const idB58Str = peerInfo . id . toB58String ( )
191
+ async _onPeerConnected ( peerId , conn ) {
192
+ const idB58Str = peerId . toB58String ( )
193
193
this . log ( 'connected' , idB58Str )
194
194
195
- const peer = this . _addPeer ( new Peer ( peerInfo ) )
195
+ const peer = this . _addPeer ( new Peer ( {
196
+ id : peerId ,
197
+ protocols : this . multicodecs
198
+ } ) )
199
+
196
200
try {
197
201
const { stream } = await conn . newStream ( this . multicodecs )
198
202
peer . attachConnection ( stream )
@@ -205,11 +209,11 @@ class PubsubBaseProtocol extends EventEmitter {
205
209
/**
206
210
* Registrar notifies a closing connection with pubsub protocol.
207
211
* @private
208
- * @param {PeerInfo } peerInfo peer info
212
+ * @param {PeerId } peerId peerId
209
213
* @param {Error } err error for connection end
210
214
*/
211
- _onPeerDisconnected ( peerInfo , err ) {
212
- const idB58Str = peerInfo . id . toB58String ( )
215
+ _onPeerDisconnected ( peerId , err ) {
216
+ const idB58Str = peerId . toB58String ( )
213
217
const peer = this . peers . get ( idB58Str )
214
218
215
219
this . log ( 'connection ended' , idB58Str , err ? err . message : '' )
@@ -219,11 +223,11 @@ class PubsubBaseProtocol extends EventEmitter {
219
223
/**
220
224
* Add a new connected peer to the peers map.
221
225
* @private
222
- * @param {PeerInfo } peer peer info
223
- * @returns {PeerInfo }
226
+ * @param {Peer } peer internal peer
227
+ * @returns {Peer }
224
228
*/
225
229
_addPeer ( peer ) {
226
- const id = peer . info . id . toB58String ( )
230
+ const id = peer . id . toB58String ( )
227
231
let existing = this . peers . get ( id )
228
232
229
233
if ( ! existing ) {
@@ -242,11 +246,11 @@ class PubsubBaseProtocol extends EventEmitter {
242
246
* Remove a peer from the peers map.
243
247
* @private
244
248
* @param {Peer } peer peer state
245
- * @returns {PeerInfo }
249
+ * @returns {Peer }
246
250
*/
247
251
_removePeer ( peer ) {
248
252
if ( ! peer ) return
249
- const id = peer . info . id . toB58String ( )
253
+ const id = peer . id . toB58String ( )
250
254
251
255
this . log ( 'remove' , id , peer . _references )
252
256
@@ -287,7 +291,7 @@ class PubsubBaseProtocol extends EventEmitter {
287
291
*/
288
292
_buildMessage ( message ) {
289
293
const msg = utils . normalizeOutRpcMessage ( message )
290
- if ( this . peerId ) {
294
+ if ( this . signMessages ) {
291
295
return signMessage ( this . peerId , msg )
292
296
} else {
293
297
return message
@@ -310,7 +314,7 @@ class PubsubBaseProtocol extends EventEmitter {
310
314
311
315
return Array . from ( this . peers . values ( ) )
312
316
. filter ( ( peer ) => peer . topics . has ( topic ) )
313
- . map ( ( peer ) => peer . info . id . toB58String ( ) )
317
+ . map ( ( peer ) => peer . id . toB58String ( ) )
314
318
}
315
319
316
320
/**
0 commit comments