missing unmarshalling of content bytes before encoding and generation of CID

This commit is contained in:
Michael Shaw 2023-01-19 16:23:10 -05:00
parent 6c597101ea
commit 15db2d3849

View File

@ -7,18 +7,16 @@ package utils
import (
"bytes"
"errors"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/fluent"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/storage/memstore"
canonicalJson "github.com/gibson042/canonicaljson-go"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/multicodec"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/ipld/go-ipld-prime/storage/memstore"
mh "github.com/multiformats/go-multihash"
)
@ -69,6 +67,14 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro
// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes).
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
//This is combination of samples for unmarshalling and linking
//see: https://pkg.go.dev/github.com/ipld/go-ipld-prime
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
nb := np.NewBuilder() // Create a builder.
dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!)
lsys := cidlink.DefaultLinkSystem()
// We want to store the serialized data somewhere.
@ -87,14 +93,6 @@ func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
MhLength: 32, // sha2-256 hash has a 32-byte sum.
}}
// And we need some data to link to! Here's a quick piece of example data:
n, err := fluent.Build(basicnode.Prototype.Any, func(na fluent.NodeAssembler) {
na.AssignBytes(content)
})
if err != nil {
return "", err
}
// Now: time to apply the LinkSystem, and do the actual store operation!
lnk, err := lsys.Store(
linking.LinkContext{}, // The zero value is fine. Configure it it you want cancellability or other features.
@ -104,5 +102,6 @@ func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
if err != nil {
return "", err
}
//return cborcid.String(), nil
return lnk.String(), nil
}