Skip to content

Commit

Permalink
feat: append peer id to multiaddr if not there
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Mar 29, 2017
1 parent 300936f commit 59ea9c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -54,4 +55,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>"
]
}
}
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 59ea9c3

Please sign in to comment.