mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-21 04:51:33 +00:00
chore: create signed peer record on new listen addresses in transport manager
This commit is contained in:
committed by
Vasco Santos
parent
7b93ece7f2
commit
43eda43f06
@ -64,11 +64,6 @@ class IdentifyService {
|
||||
*/
|
||||
this.connectionManager = libp2p.connectionManager
|
||||
|
||||
/**
|
||||
* @property {TransportManager}
|
||||
*/
|
||||
this.transportManager = libp2p.transportManager
|
||||
|
||||
/**
|
||||
* @property {PeerId}
|
||||
*/
|
||||
@ -96,10 +91,11 @@ class IdentifyService {
|
||||
this.identify(connection).catch(log.error)
|
||||
})
|
||||
|
||||
// When new addresses are used for listening, update self peer record
|
||||
this.transportManager.on('listening', async () => {
|
||||
await this._createSelfPeerRecord()
|
||||
this.pushToPeerStore()
|
||||
// When self multiaddrs change, trigger identify-push
|
||||
this.peerStore.on('change:multiaddrs', ({ peerId }) => {
|
||||
if (peerId.toString() === this.peerId.toString()) {
|
||||
this.pushToPeerStore()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -110,7 +106,7 @@ class IdentifyService {
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async push (connections) {
|
||||
const signedPeerRecord = await this._getSelfPeerRecord()
|
||||
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
|
||||
const protocols = Array.from(this._protocols.keys())
|
||||
|
||||
@ -260,7 +256,7 @@ class IdentifyService {
|
||||
publicKey = this.peerId.pubKey.bytes
|
||||
}
|
||||
|
||||
const signedPeerRecord = await this._getSelfPeerRecord()
|
||||
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
|
||||
const message = Message.encode({
|
||||
protocolVersion: this._host.protocolVersion,
|
||||
@ -330,40 +326,6 @@ class IdentifyService {
|
||||
// Update the protocols
|
||||
this.peerStore.protoBook.set(id, message.protocols)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get self signed peer record raw envelope.
|
||||
* @return {Promise<Uint8Array>}
|
||||
*/
|
||||
_getSelfPeerRecord () {
|
||||
const selfSignedPeerRecord = this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
|
||||
if (selfSignedPeerRecord) {
|
||||
return selfSignedPeerRecord
|
||||
}
|
||||
|
||||
return this._createSelfPeerRecord()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create self signed peer record raw envelope.
|
||||
* @return {Uint8Array}
|
||||
*/
|
||||
async _createSelfPeerRecord () {
|
||||
try {
|
||||
const peerRecord = new PeerRecord({
|
||||
peerId: this.peerId,
|
||||
multiaddrs: this._libp2p.multiaddrs
|
||||
})
|
||||
const envelope = await Envelope.seal(peerRecord, this.peerId)
|
||||
this.peerStore.addressBook.consumePeerRecord(envelope)
|
||||
|
||||
return this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
} catch (err) {
|
||||
log.error('failed to get self peer record')
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.IdentifyService = IdentifyService
|
||||
|
Reference in New Issue
Block a user