NewCidV0 function question


I am trying to understand how Cid creation functions work but i face an issue.

import meaning

u ""
mc ""
cid ""
echo "Hello Univers" | ipfs add 

Running the previous command produce this CID


However when I run this line

fmt.Println(cid.NewCidV0(u.Hash([]byte("Hello Univers"))))

I get this CID


Tried adding a \n but still get the wrong CID


What I truly don’t understand is that it works for Cidv1

if i run this command :

echo "Hello Univers" | ipfs add --cid-version=1

I will get this cid :


and if I run this line

fmt.Println(cid.NewCidV1(uint64(mc.Raw),u.Hash([]byte("Hello Univers\n"))))

I will get the same CID.

Any ideas on why it works on v1 and not en v0 ?


The quick version is: ipfs add adds the data as a protobuf block, and your code adds it as a raw leaf. When using cidv1, it defaults to using raw leaves.

You can force ipfs add to use raw leaves (raw), or you can make your code generate a protobuf (dag-pb) to reverse their default behavior.

1 Like

I don’t think you can use ipfs with cidv0 and raw-leaves, but if you could the result would be what you are expecting. You can extract your cidv0 from the cidv1 and they should be the same too.

1 Like

Thanks @ylempereur @hector