5

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

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 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.

[~]$ ipfs cat <hash-id that exists in the ipfs domain>
//returns a results maybe after 10 minutes if the hash-id string is valid.

[~]$ ipfs cat <hash-id that does not exists in the ipfs domain>
//HALTS. and does not return me that hash-id is invalid.

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?

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 that does not exist in the ipfs domain and should have returned false.

Thank you for your valuable time and help.

  • I am not sure exactly what you are asking for here. How would you know that your hash string is invalid? Seems like you are looking for a way to reverse a hashing function – MrChico Nov 30 '16 at 10:35
  • Hello Sir, thank you for your response, when I run ipfs add -r somefile.txt this would return added <hash_id> somefile.txt. And later I can do this: ipfs cat <hash_id>. But if I give some random hash_id that is not exist in the ipfs domain: ipfs cat <some_hash_id> will enter into while() and halts. This will not return me that that ipfs hash_id exists in the ipfs domain. So there is no way I can know that is the entered hash_id points to a allocated memory. – alper Nov 30 '16 at 10:53
  • Before running ipfs cat <hash_id> or ipfs ls <hash_id>; I want to know that; does <hash_id> string is valid and exist in the ipfs domain so it will be okay for me to wait ipfs {cat | ls} ’s response even it takes a long time. – alper Nov 30 '16 at 10:54
  • 2
    IPFS is a decentralized system, there is no central domain where you can do a lookup to find your hash. When looking up files, you're asking the network to find nodes storing the content behind a unique hash. You cannot know whether it exists beforehand – MrChico Nov 30 '16 at 13:22
  • 1
    Because hash functions are one way functions that output a random-looking string of a set length. With a good hash function, the likelihood of two different inputs generating the same output is extremely low. Check out collision resistance here: en.wikipedia.org/wiki/Cryptographic_hash_function – MrChico Dec 4 '16 at 12:48
3

There is really no way you can do what you want. If you think about it some more and take into account the architecture of a distributed p2p system such as IPFS - the definition of being "valid" or "existent" is not really that obvious.

If you ask IPFS for a file matching a given hash - it walks through a set of available nodes in the network trying to find the one that has the file available. But if it can not find none - it can be for various reasons. For unpopular content that is not cached by many nodes and only few nodes pin it, there is a chance that the node publishing this file is simply down at this particular moment.

As you described - you can try timeouts - but they can give you false negatives.

What you can do though is you can use IPNS to name a file (to be more precise - the hash of a file) and use the name to navigate to it. Of course you can break it if you publish the wrong hash but it is easy to avoid by automating adding a file to IPFS and naming it in IPNS.

0

You may look the implementation of Martin "Mr Chico" Lundfall

https://github.com/MrChico/verifyIPFS

I'm copying an extract of the README

# verifyIPFS
Smart contract library functions for recreating the hash of an ipfs object given its content.

## Usage
Add the data file to IPFS:

ipfs add testfile -q

Will return:

QmRsjnNkEpnDdmYB7wMR7FSy1eGZ12pDuhST3iNLJTzAXF

Now try the same thing with the `generateHash`-function:

(truffle console)

truffle(default)> fs = require('fs')
truffle(default)> var verify; verifyIPFS.new().then(a => {verify = a})
truffle(default)> var testfile = fs.readFileSync('./testfile').toString()
truffle(default)> verify.generateHash(testfile).then(a => {console.log(web3.toAscii(a))})

returns

QmRsjnNkEpnDdmYB7wMR7FSy1eGZ12pDuhST3iNLJTzAXF
  • 1
    This is not the correct approach. It does not verify that given ipfs hash-id exist in the ipfs domain or not. On read me it talks about that: "verifyHash takes a string and an IPFS-hash, and returns true if they match". – alper Nov 30 '16 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.