2022-04-05 07:09:27 +00:00
|
|
|
//
|
|
|
|
// Copyright 2020 Wireline, Inc.
|
|
|
|
//
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-06-03 14:06:06 +00:00
|
|
|
|
2022-05-23 07:08:46 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"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"
|
2022-04-05 07:09:27 +00:00
|
|
|
|
|
|
|
canonicalJson "github.com/gibson042/canonicaljson-go"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2022-05-23 07:08:46 +00:00
|
|
|
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
2022-04-05 07:09:27 +00:00
|
|
|
mh "github.com/multiformats/go-multihash"
|
|
|
|
)
|
|
|
|
|
2022-05-23 07:10:27 +00:00
|
|
|
var store = memstore.Store{}
|
|
|
|
|
2022-05-23 07:08:46 +00:00
|
|
|
func init() {
|
|
|
|
multicodec.RegisterEncoder(0x71, dagcbor.Encode)
|
|
|
|
multicodec.RegisterDecoder(0x71, dagcbor.Decode)
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:09:27 +00:00
|
|
|
// GenerateHash returns the hash of the canonicalized JSON input.
|
|
|
|
func GenerateHash(json map[string]interface{}) (string, []byte, error) {
|
|
|
|
content, err := canonicalJson.Marshal(json)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-23 07:08:46 +00:00
|
|
|
//cid, err := CIDFromJSONBytes(content)
|
|
|
|
cidString, err := CIDFromJSONBytesUsingIpldPrime(content)
|
2022-04-05 07:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-23 07:08:46 +00:00
|
|
|
return cidString, content, nil
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CIDFromJSONBytes returns CID (cbor) for json (as bytes).
|
|
|
|
func CIDFromJSONBytes(content []byte) (string, error) {
|
|
|
|
cid, err := cbor.FromJSON(bytes.NewReader(content), mh.SHA2_256, -1)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttributeAsString returns a map attribute as string, if possible.
|
|
|
|
func GetAttributeAsString(obj map[string]interface{}, attr string) (string, error) {
|
|
|
|
if value, ok := obj[attr]; ok {
|
|
|
|
if valueStr, ok := value.(string); ok {
|
|
|
|
return valueStr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("attribute not of string type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("attribute not found")
|
|
|
|
}
|
2022-05-23 07:08:46 +00:00
|
|
|
|
|
|
|
// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes).
|
|
|
|
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
|
|
|
lsys := cidlink.DefaultLinkSystem()
|
|
|
|
|
|
|
|
// We want to store the serialized data somewhere.
|
|
|
|
// We'll use an in-memory store for this. (It's a package scoped variable.)
|
|
|
|
// You can use any kind of storage system here;
|
|
|
|
// or if you need even more control, you could also write a function that conforms to the linking.BlockWriteOpener interface.
|
|
|
|
lsys.SetWriteStorage(&store)
|
|
|
|
// To create any links, first we need a LinkPrototype.
|
|
|
|
// This gathers together any parameters that might be needed when making a link.
|
|
|
|
// (For CIDs, the version, the codec, and the multihash type are all parameters we'll need.)
|
|
|
|
// Often, you can probably make this a constant for your whole application.
|
|
|
|
lp := cidlink.LinkPrototype{Prefix: cid.Prefix{
|
|
|
|
Version: 1, // Usually '1'.
|
|
|
|
Codec: 0x71, // 0x71 means "dag-cbor" -- See the multicodecs table: https://github.com/multiformats/multicodec/
|
2022-06-03 14:06:06 +00:00
|
|
|
MhType: 0x12, // 0x12 means "sha2-256" -- See the multicodecs table: https://github.com/multiformats/multicodec/
|
|
|
|
MhLength: 32, // sha2-256 hash has a 32-byte sum.
|
2022-05-23 07:08:46 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
lp, // The LinkPrototype says what codec and hashing to use.
|
|
|
|
n, // And here's our data.
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return lnk.String(), nil
|
|
|
|
}
|