@@ -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,13 @@ 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
82
+ this . signMessages = peerId
83
83
this . registrar = registrar
84
84
85
85
this . started = false
86
86
87
+ this . peerId = peerId
88
+
87
89
/**
88
90
* Map of topics to which peers are subscribed to
89
91
*
@@ -99,9 +101,7 @@ class PubsubBaseProtocol extends EventEmitter {
99
101
this . peers = new Map ( )
100
102
101
103
// Message signing
102
- if ( signMessages ) {
103
- this . peerId = this . peerInfo . id
104
- }
104
+ this . signMessages = signMessages
105
105
106
106
/**
107
107
* If message signing should be required for incoming messages
@@ -170,13 +170,11 @@ class PubsubBaseProtocol extends EventEmitter {
170
170
* @param {DuplexStream } props.strean
171
171
* @param {Connection } props.connection connection
172
172
*/
173
- async _onIncomingStream ( { protocol, stream, connection } ) {
174
- const peerInfo = await PeerInfo . create ( connection . remotePeer )
175
- peerInfo . protocols . add ( protocol )
176
-
177
- const idB58Str = peerInfo . id . toB58String ( )
173
+ _onIncomingStream ( { protocol, stream, connection } ) {
174
+ const peerId = connection . remotePeer
175
+ const idB58Str = peerId . toB58String ( )
178
176
179
- const peer = this . _addPeer ( new Peer ( peerInfo ) )
177
+ const peer = this . _addPeer ( new Peer ( peerId , [ protocol ] ) )
180
178
181
179
peer . attachConnection ( stream )
182
180
this . _processMessages ( idB58Str , stream , peer )
@@ -185,14 +183,14 @@ class PubsubBaseProtocol extends EventEmitter {
185
183
/**
186
184
* Registrar notifies a connection successfully with pubsub protocol.
187
185
* @private
188
- * @param {PeerInfo } peerInfo remote peer info
186
+ * @param {PeerId } peerId remote peer-id
189
187
* @param {Connection } conn connection to the peer
190
188
*/
191
- async _onPeerConnected ( peerInfo , conn ) {
192
- const idB58Str = peerInfo . id . toB58String ( )
189
+ async _onPeerConnected ( peerId , conn ) {
190
+ const idB58Str = peerId . toB58String ( )
193
191
this . log ( 'connected' , idB58Str )
194
192
195
- const peer = this . _addPeer ( new Peer ( peerInfo ) )
193
+ const peer = this . _addPeer ( new Peer ( peerId , this . multicodecs ) )
196
194
try {
197
195
const { stream } = await conn . newStream ( this . multicodecs )
198
196
peer . attachConnection ( stream )
@@ -205,11 +203,11 @@ class PubsubBaseProtocol extends EventEmitter {
205
203
/**
206
204
* Registrar notifies a closing connection with pubsub protocol.
207
205
* @private
208
- * @param {PeerInfo } peerInfo peer info
206
+ * @param {PeerId } peerId peerId
209
207
* @param {Error } err error for connection end
210
208
*/
211
- _onPeerDisconnected ( peerInfo , err ) {
212
- const idB58Str = peerInfo . id . toB58String ( )
209
+ _onPeerDisconnected ( peerId , err ) {
210
+ const idB58Str = peerId . toB58String ( )
213
211
const peer = this . peers . get ( idB58Str )
214
212
215
213
this . log ( 'connection ended' , idB58Str , err ? err . message : '' )
@@ -219,11 +217,11 @@ class PubsubBaseProtocol extends EventEmitter {
219
217
/**
220
218
* Add a new connected peer to the peers map.
221
219
* @private
222
- * @param {PeerInfo } peer peer info
223
- * @returns {PeerInfo }
220
+ * @param {Peer } peer internal peer
221
+ * @returns {Peer }
224
222
*/
225
223
_addPeer ( peer ) {
226
- const id = peer . info . id . toB58String ( )
224
+ const id = peer . id . toB58String ( )
227
225
let existing = this . peers . get ( id )
228
226
229
227
if ( ! existing ) {
@@ -242,11 +240,11 @@ class PubsubBaseProtocol extends EventEmitter {
242
240
* Remove a peer from the peers map.
243
241
* @private
244
242
* @param {Peer } peer peer state
245
- * @returns {PeerInfo }
243
+ * @returns {Peer }
246
244
*/
247
245
_removePeer ( peer ) {
248
246
if ( ! peer ) return
249
- const id = peer . info . id . toB58String ( )
247
+ const id = peer . id . toB58String ( )
250
248
251
249
this . log ( 'remove' , id , peer . _references )
252
250
@@ -287,7 +285,7 @@ class PubsubBaseProtocol extends EventEmitter {
287
285
*/
288
286
_buildMessage ( message ) {
289
287
const msg = utils . normalizeOutRpcMessage ( message )
290
- if ( this . peerId ) {
288
+ if ( this . signMessages ) {
291
289
return signMessage ( this . peerId , msg )
292
290
} else {
293
291
return message
@@ -310,7 +308,7 @@ class PubsubBaseProtocol extends EventEmitter {
310
308
311
309
return Array . from ( this . peers . values ( ) )
312
310
. filter ( ( peer ) => peer . topics . has ( topic ) )
313
- . map ( ( peer ) => peer . info . id . toB58String ( ) )
311
+ . map ( ( peer ) => peer . id . toB58String ( ) )
314
312
}
315
313
316
314
/**
0 commit comments