Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 7d13ea6

Browse files
dryajovdaviddias
authored andcommitted
fix: multiaddr parsing (#53)
* fix: correct multiaddr parsing and decapsulation * fix: correct repo url
1 parent a9e2062 commit 7d13ea6

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"repository": {
1616
"type": "git",
17-
"url": "https://github.com/libp2p/js-ipfs-railing.git"
17+
"url": "https://github.com/libp2p/js-libp2p-railing.git"
1818
},
1919
"keywords": [
2020
"IPFS"
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"debug": "^2.6.3",
41+
"lodash": "^4.17.4",
4142
"multiaddr": "^2.2.2",
4243
"peer-id": "~0.8.4",
4344
"peer-info": "~0.8.4"
@@ -53,4 +54,4 @@
5354
"Nuno Nogueira <nunofmn@gmail.com>",
5455
"Richard Littauer <richard.littauer@gmail.com>"
5556
]
56-
}
57+
}

src/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const PeerInfo = require('peer-info')
55
const multiaddr = require('multiaddr')
66
const EventEmitter = require('events').EventEmitter
77
const debug = require('debug')
8+
const includes = require('lodash/includes')
89

910
const log = debug('libp2p:railing')
1011
log.error = debug('libp2p:railing:error')
@@ -19,15 +20,28 @@ class Railing extends EventEmitter {
1920
setImmediate(callback)
2021
setImmediate(() => {
2122
this.bootstrapers.forEach((candidate) => {
22-
// TODO: It would be awesome to get better tools at extracting things
23-
// from multiaddr
24-
const split = candidate.split('/')
23+
candidate = multiaddr(candidate)
24+
25+
let ma
26+
if (includes(candidate.protoNames(), 'ipfs')) {
27+
ma = candidate.decapsulate('ipfs')
28+
}
29+
30+
// TODO: switch for multiaddr.getPeerId once merged
31+
let peerIdB58Str
32+
try {
33+
peerIdB58Str = candidate.stringTuples().filter((tuple) => {
34+
if (tuple[0] === candidate.protos().filter((proto) => {
35+
return proto.name === 'ipfs'
36+
})[0].code) {
37+
return true
38+
}
39+
})[0][1]
40+
} catch (e) {
41+
throw new Error('Error extracting IPFS id from multiaddr', e)
42+
}
2543

26-
const ma = multiaddr(split.splice(0, 5).join('/'))
27-
28-
const peerIdB58Str = split[1]
2944
const peerId = PeerId.createFromB58String(peerIdB58Str)
30-
3145
PeerInfo.create(peerId, (err, peerInfo) => {
3246
if (err) {
3347
return log.error('Error creating PeerInfo from bootstrap peer', err)

0 commit comments

Comments
 (0)