Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check provided string is a valid IPFS or IPNS path?

I want to check provided string is a valid IPFS or IPNS path or not.

For example when I do:

ipfs ls <invalid-hash>
//waits keep searching ... 

This will not return anything but halts within the program, so I would need to wait not knowing given hash is valid or not.

I have used: https://github.com/xicombd/is-ipfs but an invalid-hash string that I am giving into functions returns true, so it does not work properly on my side, any advice?

I could wait N-seconds as a threshold for ipfs to return results for ipfs cat valid-hash, ipfs ls valid-hash but results of ipfs cat valid-hash or ipfs ls valid-hash may take longer than N-seconds, which is not trustable to rely on.

For example: (I am running commands inside node app.)

[$] node 
const isIPFS = require('is-ipfs')
> isIPFS.multihash('QmYooooooooooooooooooooooooooooooooaoooooooooo')
true //returns true but it is an invalid ipfs hash, should have returned false.

Thank you for your valuable time and help.

like image 541
alper Avatar asked Sep 17 '25 19:09

alper


1 Answers

It seems like the isIPFS library only verifies the validity of a hash on a syntaxical level (Does it start with Qm ? Is it the expected length ?), but does not check wether or not a file can be adressed using this hash.

In fact, I don't think it's technically feasible to assert that no file can be retrieved using a certain hash, apart from waiting N seconds for an answer.

Think about IPFS the same way you'd think about HTTP. Sometimes an HTTP request takes minutes to be completed for some reasons, but it doesn't mean the IP+port you're trying to connect leads nowhere. Your browser or app will decide to timeout after an arbitrary amount of time so as not to wait forever, but you can't have a scientific proof that nothing can be reached using a certain HTTP address/IPFS hash.

like image 89
bcolin Avatar answered Sep 19 '25 18:09

bcolin