It's known that IPFS can use 2 types of CID, v0 and v1. In my contract, I let users store bytes data parameter. They can store some simple text on it or even pass the ipfs hash. This question is about passing ipfs hash.

As known, they can't pass ipfs hash, because it starts with Qm(v0) or baxy(v1) or whatever. So what they can do is whatever encoding IPFS use, they can first decode it in the client in order to get hex representation. for the version v0 CiD, it will start like this 0x1220..... . for v1, it's gonna be different for sure.

Now, contract emits this event and I catch this bytes value in my other client.

How do I figure out what the CiD is from the hex ? doing this in the client seems to be very wrong in my opinion.

if(value.startsWith(`0x1220`)) {
   let cid = bs58.decode(value);
else if(value.startsWith(`xxx`){


The problem is in the second else if. it could be anything, because who knows what the base is.. as known, for CiD v1, IPFS appends base which means which base encoding it uses so that we can decode it.

Any good ideas ?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.