I need to compute the CID of some data in Golang, right now I'm using this code, but the CID I receive is different than what I get when I upload the file to Filecoin or add it to a Lotus node.
This is my code:
"github.com/ipfs/go-cid"
ma "github.com/multiformats/go-multiaddr"
mh "github.com/multiformats/go-multihash"
func Cid(data []byte) {
hash, err := mh.Sum(data, mh.SHA2_256, -1)
if err != nil {
panic(err)
}
c, err := cid.V1Builder{Codec: cid.DagProtobuf, MhType: mh.SHA2_256, MhLength: -1}.Sum(hash)
fmt.Println("Filecoin CID:", c.String())
}
If I have a file with the content Hello
, the above function prints the following cid: bafybeievjvim3p2qexhlxek5yqftxsizm6fx2mtrgrsyfx43f52sozkwga
If I add it to Lotus, or upload to Filecoin I get: bafkreiayl6g3gitr7ys7kyng7sjywlrgimdoymco3jiyab6rozecmoazne
I would like to have a function that returns me the same CID.
I figured it out, I had to chunk the data as well. My updated code is below. If I now upload the same file to Filecoin I get the same CID.