How to check if the file exists for a given IPFS hash

We are creating a website, whose one aspect of registration process involves providing “Terms of Service” as IPFS hash. The hash is later used to download the actual “terms of service” and present it to a new user.

Currently if an invalid hash is provided, for a file that does not exist on Infura, our call to “get” that hash hangs for 60 seconds. Is there a way to check if the hash is valid i.e. if its related file really exists. ?

1 Like

Hi @om26er did you pin ToS file using our IPFS service?

Yes and no :slight_smile: We use Infura’s IPFS gateway, and will be recommending our users to use as well. However the issue here is a bit different. I want to be able to know if the hash the user provided us points to an existing object in your IPFS service or not.

Currently the only way to check that is to wait for the http call to timeout.

Unfortunately IPFS doesn’t allow verifying that a hash maps to an existing object without trying to retrieve that object from the node and p2p network. This is something that will be improved with our IPFS offering later this year though.

Let us know if you come up with a work around.

thanks for the general infos and outlook!

we will need to have a workaround. here is a sketch of my current thinking on this.

the core issue is UX related. when a user does a call to our service to create some object (here, a data market with user defined ToS):

create_market(..., tos_hash, ...)

this call should a) return quickly, but b) also only accept a tos_hash that does actually point to a file existing on IPFS.

therefor I think we should provide an additional helper in our service:

does_exist(hash, recheck=False) -> true|false, last_checked, mime_type, size

this function should cache the IPFS stuff in our service (either only the metadata, or also contents).

a user should then call does_exist before calling create_market. the latter does immediately know if the file exists by looking into the cache in our service.

calling does_exist - when no cache info exists - the UI must be prepared for return taking a longer time (any timeouts are defined internally in our service or enforced by the IPFS gateways like in Infura).

calling does_exist - when a cache entry exists - the UI will get an immediate return (from the cache in our service).

having called does_exist before when a hash didn’t exist yet, and hence a “negative cache line” exists, a user must set recheck=True to enforce a rechecking for the now supposedly existing file.

thus, a cache line can move from “nonexistent” to “existent” or “cached”, and from “existent” to “cached”, but never from “existent|cached” to “nonexistent”.


as a addon, we can provide upload_file() in our service, which immediately marks the file as “cached” in our service, submits the file to IPFS via Infura in the background, and returns immediately.

1 Like