@@ -22,9 +22,9 @@ const PeerRecord = require('../record/peer-record')
22
22
23
23
const {
24
24
MULTICODEC_IDENTIFY ,
25
- MULTICODEC_IDENTIFY_LEGACY ,
25
+ MULTICODEC_IDENTIFY_1_0_0 ,
26
26
MULTICODEC_IDENTIFY_PUSH ,
27
- MULTICODEC_IDENTIFY_PUSH_LEGACY ,
27
+ MULTICODEC_IDENTIFY_PUSH_1_0_0 ,
28
28
AGENT_VERSION ,
29
29
PROTOCOL_VERSION
30
30
} = require ( './consts' )
@@ -97,26 +97,12 @@ class IdentifyService {
97
97
push ( connections ) {
98
98
const pushes = connections . map ( async connection => {
99
99
try {
100
- const { protocol, stream } = await connection . newStream ( [ MULTICODEC_IDENTIFY_PUSH , MULTICODEC_IDENTIFY_PUSH_LEGACY ] )
101
-
102
- // Handle Legacy
103
- if ( protocol === MULTICODEC_IDENTIFY_PUSH_LEGACY ) {
104
- return pipe (
105
- [ {
106
- listenAddrs : this . _libp2p . multiaddrs . map ( ( ma ) => ma . buffer ) ,
107
- protocols : Array . from ( this . _protocols . keys ( ) )
108
- } ] ,
109
- pb . encode ( Message ) ,
110
- stream ,
111
- consume
112
- )
113
- }
114
-
115
- const envelope = await this . _getSelfPeerRecord ( )
116
- const signedPeerRecord = envelope . marshal ( )
100
+ const { stream } = await connection . newStream ( [ MULTICODEC_IDENTIFY_PUSH , MULTICODEC_IDENTIFY_PUSH_1_0_0 ] )
101
+ const signedPeerRecord = await this . _getSelfPeerRecord ( )
117
102
118
103
await pipe (
119
104
[ {
105
+ listenAddrs : this . _libp2p . multiaddrs . map ( ( ma ) => ma . buffer ) ,
120
106
signedPeerRecord,
121
107
protocols : Array . from ( this . _protocols . keys ( ) )
122
108
} ] ,
@@ -159,7 +145,7 @@ class IdentifyService {
159
145
* @returns {Promise<void> }
160
146
*/
161
147
async identify ( connection ) {
162
- const { protocol, stream } = await connection . newStream ( [ MULTICODEC_IDENTIFY , MULTICODEC_IDENTIFY_LEGACY ] )
148
+ const { protocol, stream } = await connection . newStream ( [ MULTICODEC_IDENTIFY , MULTICODEC_IDENTIFY_1_0_0 ] )
163
149
const [ data ] = await pipe (
164
150
[ ] ,
165
151
stream ,
@@ -198,7 +184,7 @@ class IdentifyService {
198
184
observedAddr = IdentifyService . getCleanMultiaddr ( observedAddr )
199
185
200
186
// LEGACY: differentiate message with SignedPeerRecord
201
- if ( protocol === MULTICODEC_IDENTIFY_LEGACY ) {
187
+ if ( protocol === MULTICODEC_IDENTIFY_1_0_0 ) {
202
188
// Update peers data in PeerStore
203
189
this . peerStore . addressBook . set ( id , listenAddrs . map ( ( addr ) => multiaddr ( addr ) ) )
204
190
this . peerStore . protoBook . set ( id , protocols )
@@ -249,13 +235,11 @@ class IdentifyService {
249
235
handleMessage ( { connection, stream, protocol } ) {
250
236
switch ( protocol ) {
251
237
case MULTICODEC_IDENTIFY :
238
+ case MULTICODEC_IDENTIFY_1_0_0 :
252
239
return this . _handleIdentify ( { connection, stream } )
253
- case MULTICODEC_IDENTIFY_LEGACY :
254
- return this . _handleIdentifyLegacy ( { connection, stream } )
255
240
case MULTICODEC_IDENTIFY_PUSH :
241
+ case MULTICODEC_IDENTIFY_PUSH_1_0_0 :
256
242
return this . _handlePush ( { connection, stream } )
257
- case MULTICODEC_IDENTIFY_PUSH_LEGACY :
258
- return this . _handlePushLegacy ( { connection, stream } )
259
243
default :
260
244
log . error ( 'cannot handle unknown protocol %s' , protocol )
261
245
}
@@ -275,45 +259,14 @@ class IdentifyService {
275
259
publicKey = this . peerId . pubKey . bytes
276
260
}
277
261
278
- const envelope = await this . _getSelfPeerRecord ( )
279
- const signedPeerRecord = envelope . marshal ( )
280
-
281
- const message = Message . encode ( {
282
- protocolVersion : PROTOCOL_VERSION ,
283
- agentVersion : AGENT_VERSION ,
284
- publicKey,
285
- signedPeerRecord,
286
- observedAddr : connection . remoteAddr . buffer ,
287
- protocols : Array . from ( this . _protocols . keys ( ) )
288
- } )
289
-
290
- pipe (
291
- [ message ] ,
292
- lp . encode ( ) ,
293
- stream ,
294
- consume
295
- )
296
- }
297
-
298
- /**
299
- * Sends the `Identify` response with listen addresses (LEGACY)
300
- * to the requesting peer over the given `connection`
301
- * @private
302
- * @param {object } options
303
- * @param {* } options.stream
304
- * @param {Connection } options.connection
305
- */
306
- _handleIdentifyLegacy ( { connection, stream } ) {
307
- let publicKey = Buffer . alloc ( 0 )
308
- if ( this . peerId . pubKey ) {
309
- publicKey = this . peerId . pubKey . bytes
310
- }
262
+ const signedPeerRecord = await this . _getSelfPeerRecord ( )
311
263
312
264
const message = Message . encode ( {
313
265
protocolVersion : PROTOCOL_VERSION ,
314
266
agentVersion : AGENT_VERSION ,
315
267
publicKey,
316
268
listenAddrs : this . _libp2p . multiaddrs . map ( ( ma ) => ma . buffer ) ,
269
+ signedPeerRecord,
317
270
observedAddr : connection . remoteAddr . buffer ,
318
271
protocols : Array . from ( this . _protocols . keys ( ) )
319
272
} )
@@ -353,6 +306,22 @@ class IdentifyService {
353
306
return log . error ( 'received invalid message' , err )
354
307
}
355
308
309
+ const id = connection . remotePeer
310
+
311
+ // Legacy
312
+ if ( ! message . signedPeerRecord ) {
313
+ try {
314
+ this . peerStore . addressBook . set ( id , message . listenAddrs . map ( ( addr ) => multiaddr ( addr ) ) )
315
+ } catch ( err ) {
316
+ return log . error ( 'received invalid listen addrs' , err )
317
+ }
318
+
319
+ // Update the protocols
320
+ this . peerStore . protoBook . set ( id , message . protocols )
321
+
322
+ return
323
+ }
324
+
356
325
// Open envelope and verify if is authenticated
357
326
let envelope
358
327
try {
@@ -372,7 +341,6 @@ class IdentifyService {
372
341
}
373
342
374
343
// Update peers data in PeerStore
375
- const id = connection . remotePeer
376
344
try {
377
345
// TODO: Store as certified record
378
346
@@ -386,45 +354,8 @@ class IdentifyService {
386
354
}
387
355
388
356
/**
389
- * Reads the Identify Push message from the given `connection`
390
- * with listen addresses (LEGACY)
391
- * @private
392
- * @param {object } options
393
- * @param {* } options.stream
394
- * @param {Connection } options.connection
395
- */
396
- async _handlePushLegacy ( { connection, stream } ) {
397
- const [ data ] = await pipe (
398
- [ ] ,
399
- stream ,
400
- lp . decode ( ) ,
401
- take ( 1 ) ,
402
- toBuffer ,
403
- collect
404
- )
405
-
406
- let message
407
- try {
408
- message = Message . decode ( data )
409
- } catch ( err ) {
410
- return log . error ( 'received invalid message' , err )
411
- }
412
-
413
- // Update peers data in PeerStore
414
- const id = connection . remotePeer
415
- try {
416
- this . peerStore . addressBook . set ( id , message . listenAddrs . map ( ( addr ) => multiaddr ( addr ) ) )
417
- } catch ( err ) {
418
- return log . error ( 'received invalid listen addrs' , err )
419
- }
420
-
421
- // Update the protocols
422
- this . peerStore . protoBook . set ( id , message . protocols )
423
- }
424
-
425
- /**
426
- * Get self signed peer record envelope.
427
- * @return {Envelope }
357
+ * Get self signed peer record raw envelope.
358
+ * @return {Buffer }
428
359
*/
429
360
async _getSelfPeerRecord ( ) {
430
361
// TODO: Verify if updated
@@ -436,7 +367,9 @@ class IdentifyService {
436
367
peerId : this . peerId ,
437
368
multiaddrs : this . _libp2p . multiaddrs
438
369
} )
439
- this . _selfRecord = await Envelope . seal ( peerRecord , this . peerId )
370
+ const envelope = await Envelope . seal ( peerRecord , this . peerId )
371
+
372
+ this . _selfRecord = envelope . marshal ( )
440
373
441
374
return this . _selfRecord
442
375
}
@@ -449,8 +382,8 @@ module.exports.IdentifyService = IdentifyService
449
382
*/
450
383
module . exports . multicodecs = {
451
384
IDENTIFY : MULTICODEC_IDENTIFY ,
452
- IDENTIFY_LEGACY : MULTICODEC_IDENTIFY_LEGACY ,
385
+ IDENTIFY_1_0_0 : MULTICODEC_IDENTIFY_1_0_0 ,
453
386
IDENTIFY_PUSH : MULTICODEC_IDENTIFY_PUSH ,
454
- IDENTIFY_PUSH_LEGACY : MULTICODEC_IDENTIFY_PUSH_LEGACY
387
+ IDENTIFY_PUSH_1_0_0 : MULTICODEC_IDENTIFY_PUSH_1_0_0
455
388
}
456
389
module . exports . Message = Message
0 commit comments