feat: append peer id to multiaddr if not there

This commit is contained in:
David Dias 2017-03-29 20:02:41 +01:00
parent 300936f2f3
commit 59ea9c388f
2 changed files with 12 additions and 1 deletions

View File

@ -42,6 +42,7 @@
"dependencies": {
"libp2p-ping": "~0.3.2",
"libp2p-swarm": "~0.28.0",
"mafmt": "^2.1.8",
"multiaddr": "^2.3.0",
"peer-book": "~0.3.2",
"peer-id": "~0.8.5",
@ -54,4 +55,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>"
]
}
}

View File

@ -3,6 +3,7 @@
const Swarm = require('libp2p-swarm')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const mafmt = require('mafmt')
const PeerBook = require('peer-book')
const multiaddr = require('multiaddr')
const EventEmitter = require('events').EventEmitter
@ -47,6 +48,8 @@ class Node extends EventEmitter {
this.swarm.on('peer-mux-closed', (peerInfo) => {
this.emit('peer:disconnect', peerInfo)
// TODO remove this line
this.peerBook.removeByB58String(peerInfo.id.toB58String())
})
}
@ -104,6 +107,13 @@ class Node extends EventEmitter {
}
})
// so that we can have webrtc-star addrs without adding manually the id
this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => {
if (!mafmt.IPFS.matches(ma)) {
ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String())
}
})
this.swarm.listen((err) => {
if (err) {
return callback(err)