IPFS – Validating IPFS Hash String

ipfs

From this question we know that we cannot verify ipfs hash exist beforehand.

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.

When we add a folder or file into IPFS it will generate some ipfs hash for example: QmXjkFQjnD8i8ntmwehoAHBfJEApETx8ebScyVzAHqgjpD. It seems like all hashes start with "Qm" and has 46 characters (if I didn't count it wrong). Is these cases are always true?

$ ipfs add -r folder
added QmXjkFQjnD8i8ntmwehoAHBfJEApETx8ebScyVzAHqgjpD folder/avatar/avatar.c

[Q] How could I verify a input string if the string always have some properties to be an ipfs hash? The goal of this to force user to enter at least string that matches the size of the ipfs hash and other properties if exist.

Such as:

  • String should start with "Qm"
  • String should be 46 characters.

Best Answer

IPFS specifies that the hash should be a multihash. This consists of a byte indicating the hashing algorithm, followed by another byte for length. This is then base58-encoded.

The hashing algorithm is normally sha256, identified by 0x12, with a length of 0x20. This encodes as "Qm".

In theory other algorithms are legal in IPFS, so if you've got code you won't be able to change that needs to work in future you shouldn't assume the "Qm". You could detect some illegal cases by checking the length byte and seeing if it matches the length. There are some libraries listed here that will do this for you, or you can look at them to see how they work.

However I don't think other algorithms are commonly used at the moment, so if you're more worried about input mistakes than future compatibility you could probably just check for this format and update your code if people start using something else in serious numbers.

Related Topic