Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't throw on invalid b58 string in getPeerId #43

Merged
merged 6 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>"
]
}
}
18 changes: 11 additions & 7 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 @@ -249,13 +249,17 @@ Multiaddr.prototype.decapsulate = function decapsulate (addr) {
* }
*/
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) {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I think you might be asking about this part here?

I should be clearing b58str in the catch, I'll fix that. Otherwise, I think returning null on invalid/empty ids is an acceptable solutions, as long as no malformed ids are returned?


bs58.decode(b58str)
return b58str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dryajov this way you are silencing the error and returning null. Why do you think this is a better approach?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want the utility function to be more ergonomic, in the majority of cases you want to just ma.getPeerId() from a multiaddr and not worry about wrapping it in a try/catch, if the id was missing or invalid, you'll get a null. The intent here is not to return an invalid b58 string and I think this accomplishes it?

}

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