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

Commit

Permalink
fix: multiaddr parsing (#53)
Browse files Browse the repository at this point in the history
* fix: correct multiaddr parsing and decapsulation

* fix: correct repo url
  • Loading branch information
dryajov authored and daviddias committed Mar 23, 2017
1 parent a9e2062 commit 7d13ea6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/libp2p/js-ipfs-railing.git"
"url": "https://github.com/libp2p/js-libp2p-railing.git"
},
"keywords": [
"IPFS"
Expand All @@ -38,6 +38,7 @@
},
"dependencies": {
"debug": "^2.6.3",
"lodash": "^4.17.4",
"multiaddr": "^2.2.2",
"peer-id": "~0.8.4",
"peer-info": "~0.8.4"
Expand All @@ -53,4 +54,4 @@
"Nuno Nogueira <nunofmn@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>"
]
}
}
28 changes: 21 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const EventEmitter = require('events').EventEmitter
const debug = require('debug')
const includes = require('lodash/includes')

const log = debug('libp2p:railing')
log.error = debug('libp2p:railing:error')
Expand All @@ -19,15 +20,28 @@ class Railing extends EventEmitter {
setImmediate(callback)
setImmediate(() => {
this.bootstrapers.forEach((candidate) => {
// TODO: It would be awesome to get better tools at extracting things
// from multiaddr
const split = candidate.split('/')
candidate = multiaddr(candidate)

let ma
if (includes(candidate.protoNames(), 'ipfs')) {
ma = candidate.decapsulate('ipfs')
}

// TODO: switch for multiaddr.getPeerId once merged
let peerIdB58Str
try {
peerIdB58Str = candidate.stringTuples().filter((tuple) => {
if (tuple[0] === candidate.protos().filter((proto) => {
return proto.name === 'ipfs'
})[0].code) {
return true
}
})[0][1]
} catch (e) {
throw new Error('Error extracting IPFS id from multiaddr', e)
}

const ma = multiaddr(split.splice(0, 5).join('/'))

const peerIdB58Str = split[1]
const peerId = PeerId.createFromB58String(peerIdB58Str)

PeerInfo.create(peerId, (err, peerInfo) => {
if (err) {
return log.error('Error creating PeerInfo from bootstrap peer', err)
Expand Down

0 comments on commit 7d13ea6

Please sign in to comment.