Manually calculate the IPFS CID v2


I have been searching forever but somehow I am never really getting where I want to be.

I simply want to use the contents of a file + filename to calculate the CID but with plain code and not with any other library. None of the IPFS CLI commands seem to be able to give me sets of data that I can transform to bytes(either from hex or base58) that will match the CID(ofcourse 1220) if I sha256 it.

So I would really like some simple (javascript) example on how this can be done. Given UTF8:
999999999999999999999999999999999999999999999999999999999999999900000000000000000000000000000000 with filename inputs.bytes

What should I do eventually get to CID: QmdyL59DkUaj6EMcDD3b4rMchGZFJnasT6C6N2RvpGLYXi

The reason I want from content to CID is that I eventually want to be able to validate small files in solidity and perform file format checks. So I want to be able to calculate the CID FROM only it’s contents. Eventually I want to reconstruct a top level folder as well, but that is a problem I think I can solve if I can get to a CID from content only.

Really hope someone can help, because I am a bit lost here, the best I got so far is to manually construct some protobuff file and handle the varint etc (Note that I want to do this in solidity)

This is a common question that comes up a lot on this forum. Use the search to find the other discussions. I think someone did post a working solution perhaps, but it assumes a specific chunking algo, and other parameters. There’s no one-to-one mapping of data files to CIDs which has confused lots and lots of people. That is, unless you define ahead of time a bunch of parameters that go into the system, you can’t be sure what the output CID will be, so it’s not a simple algorithm like a SHA-256. Depending on which different parameters you use you can get DIFFERENT CIDs for the same file.

Yeah I figured it wasn’t that easy, but I am able to control the parameters of the files, so that is no problem really.

Also, I am happy to search but so far I came out empty-handed. Maybe just using the wrong keywords, most things related to multihash specifics don’t go further than the generic description and the more deepdive topics on chunks and trees all reference the API. I already got to the Protobuff2 encoding of the filesystem, so there is that.

Maybe there is some specific wording used to go to an even deeper worked out explanation? Seems it shouldn’t be too difficult to prop up an example if all params are known in advance.

Here’s some prior discussions I found…

Thnx, these were indeed what I found, but they still use the default libs to actually preform the functions.

I finally got there though, for a single chunk for now, in Solidity.



I think you meant CIDv0, not v2

Yeah i wanted to do this too. but ended up using the ipfs-http-client lib.

const { create, urlSource } = require(‘ipfs-http-client’)
const file = await ipfs.add(urlSource(http://blah/myfile.blah))
fileCID = file.cid

1 Like