-
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
Multiaddress checking support #26
Comments
@obo20 care to elaborate? any thoughts on how it should work? Should it just check if it is a valid multiaddr in general, Quick notes: Basic validation could be done via: const Multiaddr = require('multiaddr')
function isMultiaddr (input) {
try {
new Multiaddr(input)
return true
} catch (e) {
return false
}
} With the above, the public check API would work like this: isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234') // true
isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234/http') // true
isIPFS.multiaddr('/ip6/::1/udp/1234') // true
isIPFS.multiaddr('/yoloinvalid/::1/udp/1234') // false For checking if multiaddr is of a specific type, the mafmt API can be used. const mafmt = require('msfmt')
function isPeerMultiaddr (input) {
return isMultiaddr (input) && mafmt.IPFS.matches(input)
} isIPFS.peerMultiaddr('/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4') // true
isIPFS.peerMultiaddr('/ip4/127.0.0.1/tcp/1234/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj') // true
isIPFS.peerMultiaddr('/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj') // true
isIPFS.peerMultiaddr('/ip4/127.0.0.1/udp/1234') // false cc @hacdias @olizilla – Thoughts? I imagine |
@lidel yes, I believe it would be useful on that case (Web UI). We could build the functions dynamically from |
@lidel I was specifically referring to the valid peer multi-address checking, although I'm sure some people might find the normal multiaddr checking useful as well. The examples you provided for each option seem perfect to me! |
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 with `mafmt.IPFS.matches(input)` from [mafmt](https://github.com/multiformats/js-mafmt) (low-level multiaddr validation library) Details behind `mafmt.IPFS` can be found at https://github.com/multiformats/js-mafmt#api **Note:** I've added only this check because `is-ipfs` is IPFS-centric utility. If one needs more nuanced multiaddr validation, then [mafmt](https://github.com/multiformats/js-mafmt) should be used directly. ---- Closes #26
Would it be possible to get support for checking a valid multiaddress? I want to validate peer multiaddresses being passed into our API before attempting to connect to them via "ipfs swarm connect"
The text was updated successfully, but these errors were encountered: