Skip to content

Commit 59ea9c3

Browse files
committed
feat: append peer id to multiaddr if not there
1 parent 300936f commit 59ea9c3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"dependencies": {
4343
"libp2p-ping": "~0.3.2",
4444
"libp2p-swarm": "~0.28.0",
45+
"mafmt": "^2.1.8",
4546
"multiaddr": "^2.3.0",
4647
"peer-book": "~0.3.2",
4748
"peer-id": "~0.8.5",
@@ -54,4 +55,4 @@
5455
"greenkeeperio-bot <support@greenkeeper.io>",
5556
"mayerwin <mayerwin@users.noreply.github.com>"
5657
]
57-
}
58+
}

src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Swarm = require('libp2p-swarm')
44
const PeerId = require('peer-id')
55
const PeerInfo = require('peer-info')
6+
const mafmt = require('mafmt')
67
const PeerBook = require('peer-book')
78
const multiaddr = require('multiaddr')
89
const EventEmitter = require('events').EventEmitter
@@ -47,6 +48,8 @@ class Node extends EventEmitter {
4748

4849
this.swarm.on('peer-mux-closed', (peerInfo) => {
4950
this.emit('peer:disconnect', peerInfo)
51+
// TODO remove this line
52+
this.peerBook.removeByB58String(peerInfo.id.toB58String())
5053
})
5154
}
5255

@@ -104,6 +107,13 @@ class Node extends EventEmitter {
104107
}
105108
})
106109

110+
// so that we can have webrtc-star addrs without adding manually the id
111+
this.peerInfo.multiaddrs = this.peerInfo.multiaddrs.map((ma) => {
112+
if (!mafmt.IPFS.matches(ma)) {
113+
ma = ma.encapsulate('/ipfs/' + this.peerInfo.id.toB58String())
114+
}
115+
})
116+
107117
this.swarm.listen((err) => {
108118
if (err) {
109119
return callback(err)

0 commit comments

Comments
 (0)