@@ -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
} )
@@ -350,6 +303,22 @@ class IdentifyService {
350
303
return log . error ( 'received invalid message' , err )
351
304
}
352
305
306
+ const id = connection . remotePeer
307
+
308
+ // Legacy
309
+ if ( ! message . signedPeerRecord ) {
310
+ try {
311
+ this . peerStore . addressBook . set ( id , message . listenAddrs . map ( ( addr ) => multiaddr ( addr ) ) )
312
+ } catch ( err ) {
313
+ return log . error ( 'received invalid listen addrs' , err )
314
+ }
315
+
316
+ // Update the protocols
317
+ this . peerStore . protoBook . set ( id , message . protocols )
318
+
319
+ return
320
+ }
321
+
353
322
// Open envelope and verify if is authenticated
354
323
let envelope
355
324
try {
@@ -369,7 +338,6 @@ class IdentifyService {
369
338
}
370
339
371
340
// Update peers data in PeerStore
372
- const id = connection . remotePeer
373
341
try {
374
342
// TODO: Store as certified record
375
343
@@ -383,45 +351,8 @@ class IdentifyService {
383
351
}
384
352
385
353
/**
386
- * Reads the Identify Push message from the given `connection`
387
- * with listen addresses (LEGACY)
388
- * @private
389
- * @param {object } options
390
- * @param {* } options.stream
391
- * @param {Connection } options.connection
392
- */
393
- async _handlePushLegacy ( { connection, stream } ) {
394
- const [ data ] = await pipe (
395
- [ ] ,
396
- stream ,
397
- lp . decode ( ) ,
398
- take ( 1 ) ,
399
- toBuffer ,
400
- collect
401
- )
402
-
403
- let message
404
- try {
405
- message = Message . decode ( data )
406
- } catch ( err ) {
407
- return log . error ( 'received invalid message' , err )
408
- }
409
-
410
- // Update peers data in PeerStore
411
- const id = connection . remotePeer
412
- try {
413
- this . peerStore . addressBook . set ( id , message . listenAddrs . map ( ( addr ) => multiaddr ( addr ) ) )
414
- } catch ( err ) {
415
- return log . error ( 'received invalid listen addrs' , err )
416
- }
417
-
418
- // Update the protocols
419
- this . peerStore . protoBook . set ( id , message . protocols )
420
- }
421
-
422
- /**
423
- * Get self signed peer record envelope.
424
- * @return {Envelope }
354
+ * Get self signed peer record raw envelope.
355
+ * @return {Buffer }
425
356
*/
426
357
async _getSelfPeerRecord ( ) {
427
358
// TODO: Verify if updated
@@ -433,7 +364,9 @@ class IdentifyService {
433
364
peerId : this . peerId ,
434
365
multiaddrs : this . _libp2p . multiaddrs
435
366
} )
436
- this . _selfRecord = await Envelope . seal ( peerRecord , this . peerId )
367
+ const envelope = await Envelope . seal ( peerRecord , this . peerId )
368
+
369
+ this . _selfRecord = envelope . marshal ( )
437
370
438
371
return this . _selfRecord
439
372
}
@@ -446,8 +379,8 @@ module.exports.IdentifyService = IdentifyService
446
379
*/
447
380
module . exports . multicodecs = {
448
381
IDENTIFY : MULTICODEC_IDENTIFY ,
449
- IDENTIFY_LEGACY : MULTICODEC_IDENTIFY_LEGACY ,
382
+ IDENTIFY_1_0_0 : MULTICODEC_IDENTIFY_1_0_0 ,
450
383
IDENTIFY_PUSH : MULTICODEC_IDENTIFY_PUSH ,
451
- IDENTIFY_PUSH_LEGACY : MULTICODEC_IDENTIFY_PUSH_LEGACY
384
+ IDENTIFY_PUSH_1_0_0 : MULTICODEC_IDENTIFY_PUSH_1_0_0
452
385
}
453
386
module . exports . Message = Message
0 commit comments