Skip to content

Commit

Permalink
feat: don't throw on invalid b58 string in getPeerId (#43)
Browse files Browse the repository at this point in the history
* feat: added getPeerId method to extract the peer id from the address
  • Loading branch information
dryajov authored and daviddias committed Mar 28, 2017
1 parent d632745 commit ec23f14
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"npm-to-cdn-bot (by Forbes Lindesay) <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
31 changes: 16 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Multiaddr.prototype.inspect = function inspect () {
Multiaddr.prototype.protos = function protos () {
return map(this.protoCodes(), function (code) {
return extend(protocols(code))
// copy to prevent users from modifying the internal objs.
// copy to prevent users from modifying the internal objs.
})
}

Expand Down Expand Up @@ -235,27 +235,28 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) {
/**
* Extract the peerId if the multiaddr contains one
*
* @return {String} peerId - The id of the peer
* @return {String|null} peerId - The id of the peer or null if invalid or missing from the ma
* @example
* const mh1 = Multiaddr('/ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string')
* // <Multiaddr 0408080808060438 - /ip4/8.8.8.8/tcp/1080/ipfs/QmValidBase58string>
*
* const peerId
*
* try {
* peerId = mh1.getPeerId()
* } catch (err) {
* // not a valid base58 address
* }
* // should return QmValidBase58string or null if the id is missing or invalid
* const peerId = mh1.getPeerId()
*/
Multiaddr.prototype.getPeerId = function getPeerId () {
let b58str = this.stringTuples().filter((tuple) => {
if (tuple[0] === protocols.names['ipfs'].code) {
return true
}
})[0][1]
let b58str = null
try {
b58str = this.stringTuples().filter((tuple) => {
if (tuple[0] === protocols.names['ipfs'].code) {
return true
}
})[0][1]

bs58.decode(b58str)
} catch (e) {
b58str = null
}

bs58.decode(b58str)
return b58str
}

Expand Down
16 changes: 16 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,22 @@ describe('helpers', () => {
})
})

describe('.getPeerId should parse id from multiaddr', () => {
it('parses extracts the peer Id from a multiaddr', () => {
expect(
multiaddr('/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC').getPeerId()
).to.equal('QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC')
})
})

describe('.getPeerId should return null on missing peer id in multiaddr', () => {
it('parses extracts the peer Id from a multiaddr', () => {
expect(
multiaddr('/ip4/0.0.0.0/tcp/1234/utp').getPeerId()
).to.be.null()
})
})

describe('multiaddr.isMultiaddr', () => {
it('handles different inputs', () => {
expect(multiaddr.isMultiaddr(multiaddr('/'))).to.be.eql(true)
Expand Down

0 comments on commit ec23f14

Please sign in to comment.