-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add multiaddr checks #27
Conversation
This adds `multiaddr` check if input is a valid multiaddr. A separate check for IPFS peer multiaddr will be added in next commit. License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
This adds `peerMultiaddr` check for easy validation of IPFS peer addresses. It is a think wrapper on top of generic `multiaddr` check coupled with `mafmt.IPFS.matches(multiaddr)` Details behind `mafmt.IPFS` can be found at https://github.com/multiformats/js-mafmt#api License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
@lidel This looks great. I do have one additional thought though. For the peer multi address check, I wonder if it would be beneficial to have an "fullPeerAddr" flag to add to the check. While both of these are valid: In our use case, I know I would require the full version of the peer address if I had the option to. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I agree that we should only add these two verifications since is-ipfs
is directed to IPFS addresses.
I also think that @obo20's idea is great: although I'm not sure if easily feasible with the current code.
License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
@obo20 "full peer addr" is a bit vague term, some long multiaddrs still require additional resolving:
Is my understanding correct that what you really want to find "direct" multiaddrs resolved to IP & PORT, to guarantee no additional resolve step is necessary? A crude but "good enough" approach for finding such multiaddrs would be to check if multiaddr starts with // this is something you could do in your app
const isIPFS = require('is-ipfs')
function isRawIpPeerMultiaddr(addr) {
if (!isIPFS.peerMultiaddr(addr)) return false
if ((addr.startsWith('/ip4/') || addr.startsWith('/ip6/')) && addr.split('/ipfs/').length === 2) return true
return false
} I'd prefer to keep |
https://github.com/ipfs/aegir/tree/v18.2.0#activate-travis License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
@@ -116,6 +141,8 @@ const ipnsSubdomain = (url) => isIpns(url, fqdnPattern, fqdnProtocolMatch, fqdnH | |||
|
|||
module.exports = { | |||
multihash: isMultihash, | |||
multiaddr: isMultiaddr, | |||
peerMultiaddr: isPeerMultiaddr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR but I'm musing on naming, I would ideally like to do something like const { isMultiaddr } = require('is-ipfs')
or import { isMultiaddr } from 'is-ipfs'
rather than have to import the whole library.
If we eventually start using es-modules we can tree shake the stuff we're not using but as it is now we're encouraging importing the whole library since (for example) const { cid } = require('is-ipfs')
would likely conflict with cid instance vars we have in code and it's not descriptive to validate a cid like if (cid(myCid))
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good point, let's track this in #28
Yes this what I was getting at. The goal of providing a multiaddr was to speed up content discovery. I'd imagine the speed benefits of these two series of actions would be about the same:
However
I'm perfectly happy with this @lidel. The additional functionality I suggested is merely a nice to have. |
Applied some suggestions from Alan. Parking this PR until multiformats/js-mafmt#39 is released and ipfs/aegir#336 is closed. |
This simplifies code by moving some types of validation into upstream libraries We also switch to latest mafmt which accepts Buffer input License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
d3cc5d8
to
ea3b8f0
Compare
We moved to Travis License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
This PR introduces utility methods for multiaddr detection:
isIPFS.multiaddr(input)
Generic
multiaddr
checks if input is a valid multiaddr.(It does not care about its type, just validity)
isIPFS.peerMultiaddr(input)
peerMultiaddr
provides easy validation of IPFS Peer addresses (direct or encapsulated).It is a thin wrapper on top of generic
isIPFS.multiaddr(input)
check coupled withmafmt.IPFS.matches(input)
from mafmt (low-level multiaddr validation library)Details behind
mafmt.IPFS
can be found at https://github.com/multiformats/js-mafmt#apiNote: I've added only this check because
is-ipfs
is IPFS-centric utility.If one needs more nuanced multiaddr validation, then mafmt should be used directly.
Closes #26
@hacdias, @obo20 – thoughts?