1
1
'use strict'
2
2
3
- const { Buffer } = require ( 'buffer' )
4
3
const debug = require ( 'debug' )
4
+ const log = debug ( 'libp2p:identify' )
5
+ log . error = debug ( 'libp2p:identify:error' )
6
+
7
+ const errCode = require ( 'err-code' )
8
+ const { Buffer } = require ( 'buffer' )
5
9
const pb = require ( 'it-protocol-buffers' )
6
10
const lp = require ( 'it-length-prefixed' )
7
11
const pipe = require ( 'it-pipe' )
@@ -13,8 +17,8 @@ const { toBuffer } = require('it-buffer')
13
17
14
18
const Message = require ( './message' )
15
19
16
- const log = debug ( 'libp2p:identify ')
17
- log . error = debug ( 'libp2p:identify:error ')
20
+ const Envelope = require ( '../record/envelope ')
21
+ const PeerRecord = require ( '../record/peer-record ')
18
22
19
23
const {
20
24
MULTICODEC_IDENTIFY ,
@@ -25,10 +29,7 @@ const {
25
29
PROTOCOL_VERSION
26
30
} = require ( './consts' )
27
31
28
- const errCode = require ( 'err-code' )
29
32
const { messages, codes } = require ( '../errors' )
30
- const Envelope = require ( '../record-manager/envelope' )
31
- const PeerRecord = require ( '../record-manager/peer-record' )
32
33
33
34
class IdentifyService {
34
35
/**
@@ -83,6 +84,9 @@ class IdentifyService {
83
84
this . _protocols = protocols
84
85
85
86
this . handleMessage = this . handleMessage . bind ( this )
87
+
88
+ // TODO: this should be stored in the certified AddressBook in follow up PR
89
+ this . _selfRecord = undefined
86
90
}
87
91
88
92
/**
@@ -108,7 +112,7 @@ class IdentifyService {
108
112
)
109
113
}
110
114
111
- const envelope = this . _libp2p . recordManager . getPeerRecord ( )
115
+ const envelope = await this . _getSelfPeerRecord ( )
112
116
const signedPeerRecord = envelope . marshal ( )
113
117
114
118
await pipe (
@@ -272,7 +276,7 @@ class IdentifyService {
272
276
publicKey = this . peerId . pubKey . bytes
273
277
}
274
278
275
- const envelope = this . _libp2p . recordManager . getPeerRecord ( )
279
+ const envelope = await this . _getSelfPeerRecord ( )
276
280
const signedPeerRecord = envelope . marshal ( )
277
281
278
282
const message = Message . encode ( {
@@ -418,6 +422,25 @@ class IdentifyService {
418
422
// Update the protocols
419
423
this . peerStore . protoBook . set ( id , message . protocols )
420
424
}
425
+
426
+ /**
427
+ * Get self signed peer record envelope.
428
+ * @return {Envelope }
429
+ */
430
+ async _getSelfPeerRecord ( ) {
431
+ // TODO: Verify if updated
432
+ if ( this . _selfRecord ) {
433
+ return this . _selfRecord
434
+ }
435
+
436
+ const peerRecord = new PeerRecord ( {
437
+ peerId : this . peerId ,
438
+ multiaddrs : this . _libp2p . multiaddrs
439
+ } )
440
+ this . _selfRecord = await Envelope . seal ( peerRecord , this . peerId )
441
+
442
+ return this . _selfRecord
443
+ }
421
444
}
422
445
423
446
module . exports . IdentifyService = IdentifyService
0 commit comments