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

Multiaddress checking support #26

Closed
obo20 opened this issue Feb 22, 2019 · 3 comments
Closed

Multiaddress checking support #26

obo20 opened this issue Feb 22, 2019 · 3 comments
Assignees

Comments

@obo20
Copy link

obo20 commented Feb 22, 2019

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"

@lidel
Copy link
Member

lidel commented Feb 23, 2019

@obo20 care to elaborate? any thoughts on how it should work?

Should it just check if it is a valid multiaddr in general,
or do you want to ensure it is a valid ipfs peer multiaddr ? (or both)


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.
We could add a peer multiaddr check to is-ipfs like this:

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 peerMultiaddr would be useful in the context of ipfs/ipfs-webui#969

@hacdias
Copy link
Member

hacdias commented Feb 24, 2019

@lidel yes, I believe it would be useful on that case (Web UI). We could build the functions dynamically from maft.${STH} --> isSthMultiaddr.

@obo20
Copy link
Author

obo20 commented Feb 24, 2019

@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!

@lidel lidel added the status/in-progress In progress label Feb 25, 2019
@lidel lidel self-assigned this Feb 25, 2019
@lidel lidel closed this as completed in #27 Mar 3, 2019
lidel added a commit that referenced this issue Mar 3, 2019
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
@ghost ghost removed the status/in-progress In progress label Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants