>>109080665
>convert datastore key to CID
I had the LLM change the code to be better. However, it only worked in certain cases. Some of the CIDs it came up with from said key resulted in this error:
>$ ipfs dag export $str >/dev/null
>Error: root blk load failed: protobuf: (PBNode) invalid wireType, expected 2, got 7
>$ # trying CIDv0 or non-raw when it wasn't
Long story short, here's the better code by an LLM, minor corrections by me:
package main
import (
"fmt"
"log"
"github.com/ipfs/boxo/datastore/dshelp"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
)
func main() {
dsKey := datastore.NewKey("/CIQB46MJBZUVA2EWELIXMOJ2D7L7ZQRHTWMGPB5OTA2OKASUAPP52DQ")
mhBytes, err := dshelp.DsKeyToMultihash(dsKey)
if err != nil {
log.Fatal(err)
}
// try casting to a CID (may return v0)
if c, err := cid.Cast(mhBytes); err == nil {
if c.Version() == 0 {
// build equivalent CIDv1 using same codec and multihash
c1 := cid.NewCidV1(c.Type(), c.Hash())
// also build CIDv1 with Raw codec
c1raw := cid.NewCidV1(cid.Raw, c.Hash())
fmt.Println(c.String()) // CIDv0 (Qm...)
fmt.Println(c1.String()) // CIDv1 (bafy...)
fmt.Println(c1raw.String()) // CIDv1 with Raw codec (bafk...)
return
}
// already CIDv1: print original and also Raw variant
fmt.Println(c.String())
fmt.Println(cid.NewCidV1(cid.Raw, c.Hash()).String())
return
}
// fallback: construct CIDv1 with Raw codec
c1 := cid.NewCidV1(cid.Raw, mhBytes)
fmt.Println(c1.String())
}
This image is the CID resulting from running ./dskey in the past. It's an Angry Video Game Nerd (AVGN) pic.