NewCidV0 function question

Hi,

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

import meaning

u "github.com/ipfs/go-ipfs-util"
mc "github.com/multiformats/go-multicodec"
cid "github.com/ipfs/go-cid"
echo "Hello Univers" | ipfs add 

Running the previous command produce this CID

QmdeNaZ3WAMjWCmjj6A9JxHPCYJ2gLP1o9zfoYkzpx1Kfq

However when I run this line

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

I get this CID

QmWLrKj9DgWWDvcGytgnQtrvYdXwaR6XfvSCeffmZCcGnU

Tried adding a \n but still get the wrong CID

QmcNnD12npk64WECdkxTXv9a5UvbFhNRNgLzK8T2BSToRC

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 :

bafkreigqr5a34zlu7bowdn4ksh3nn2oqpb7vi5zao6j43jv74ehbgvwhcm

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 ?

Thanks

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