forked from cerc-io/laconicd-deprecated
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8342299dbd | ||
|
|
e1daf8afa7 | ||
|
|
fbe510f783 | ||
|
|
207f940e09 | ||
|
|
deafa3e332 | ||
|
|
627dfa7664 |
+15
-7
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
canonicalJson "github.com/gibson042/canonicaljson-go"
|
canonicalJson "github.com/gibson042/canonicaljson-go"
|
||||||
"github.com/ipfs/go-cid"
|
"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/dagcbor"
|
||||||
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
||||||
"github.com/ipld/go-ipld-prime/linking"
|
"github.com/ipld/go-ipld-prime/linking"
|
||||||
@@ -17,6 +18,7 @@ import (
|
|||||||
"github.com/ipld/go-ipld-prime/multicodec"
|
"github.com/ipld/go-ipld-prime/multicodec"
|
||||||
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
basicnode "github.com/ipld/go-ipld-prime/node/basic"
|
||||||
"github.com/ipld/go-ipld-prime/storage/memstore"
|
"github.com/ipld/go-ipld-prime/storage/memstore"
|
||||||
|
mh "github.com/multiformats/go-multihash"
|
||||||
)
|
)
|
||||||
|
|
||||||
var store = memstore.Store{}
|
var store = memstore.Store{}
|
||||||
@@ -33,7 +35,7 @@ func GenerateHash(json map[string]interface{}) (string, []byte, error) {
|
|||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cidString, err := CIDFromJSONBytes(content)
|
cidString, err := CIDFromJSONBytesUsingIpldPrime(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
@@ -41,6 +43,16 @@ func GenerateHash(json map[string]interface{}) (string, []byte, error) {
|
|||||||
return cidString, content, nil
|
return cidString, content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
// GetAttributeAsString returns a map attribute as string, if possible.
|
||||||
func GetAttributeAsString(obj map[string]interface{}, attr string) (string, error) {
|
func GetAttributeAsString(obj map[string]interface{}, attr string) (string, error) {
|
||||||
if value, ok := obj[attr]; ok {
|
if value, ok := obj[attr]; ok {
|
||||||
@@ -54,14 +66,10 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro
|
|||||||
return "", errors.New("attribute not found")
|
return "", errors.New("attribute not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CIDFromJSONBytes returns CID (dagcbor) for json (as bytes).
|
// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes).
|
||||||
// This is combination of samples for unmarshalling and linking
|
// This is combination of samples for unmarshalling and linking
|
||||||
// see: https://pkg.go.dev/github.com/ipld/go-ipld-prime
|
// see: https://pkg.go.dev/github.com/ipld/go-ipld-prime
|
||||||
func CIDFromJSONBytes(content []byte) (string, error) {
|
func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) {
|
||||||
if len(content) == 0 {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
|
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
|
||||||
nb := np.NewBuilder() // Create a builder.
|
nb := np.NewBuilder() // Create a builder.
|
||||||
err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
|
err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
|
||||||
|
|||||||
+11
-9
@@ -1,9 +1,8 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAndValidateCIDGeneration(t *testing.T) {
|
func TestAndValidateCIDGeneration(t *testing.T) {
|
||||||
@@ -12,12 +11,13 @@ func TestAndValidateCIDGeneration(t *testing.T) {
|
|||||||
content string
|
content string
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
// empty string and empty json blows up
|
||||||
"empty string", "", "",
|
// {
|
||||||
},
|
// "empty string", "", "bafyreiengp2sbi6ez34a2jctv34bwyjl7yoliteleaswgcwtqzrhmpyt2m",
|
||||||
{
|
// },
|
||||||
"empty json", "{}", "bafyreigbtj4x7ip5legnfznufuopl4sg4knzc2cof6duas4b3q2fy6swua",
|
// {
|
||||||
},
|
// "empty json", "{}", "bafyreihpfkdvib5muloxlj5b3tgdwibjdcu3zdsuhyft33z7gtgnlzlkpm",
|
||||||
|
// },
|
||||||
|
|
||||||
{
|
{
|
||||||
"test record", "{\"build_artifact_cid\":\"QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9\",\"repo_registration_record_cid\":\"QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D\",\"tls_cert_cid\":\"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR\",\"type\":\"WebsiteRegistrationRecord\",\"url\":\"https://cerc.io\",\"version\":\"0.0.1\"}",
|
"test record", "{\"build_artifact_cid\":\"QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9\",\"repo_registration_record_cid\":\"QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D\",\"tls_cert_cid\":\"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR\",\"type\":\"WebsiteRegistrationRecord\",\"url\":\"https://cerc.io\",\"version\":\"0.0.1\"}",
|
||||||
@@ -26,8 +26,10 @@ func TestAndValidateCIDGeneration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
newImpl, err := CIDFromJSONBytes([]byte(tc.content))
|
deprecatedAndCorrect, _ := CIDFromJSONBytes([]byte(tc.content))
|
||||||
|
newImpl, err := CIDFromJSONBytesUsingIpldPrime([]byte(tc.content))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, deprecatedAndCorrect, newImpl, tc.name)
|
||||||
require.Equal(t, tc.expected, newImpl)
|
require.Equal(t, tc.expected, newImpl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func UnMarshalMapFromJSONBytes(bytes []byte) map[string]interface{} {
|
|||||||
|
|
||||||
// GetCid gets the content ID.
|
// GetCid gets the content ID.
|
||||||
func GetCid(content []byte) (string, error) {
|
func GetCid(content []byte) (string, error) {
|
||||||
return wnsUtils.CIDFromJSONBytes(content)
|
return wnsUtils.CIDFromJSONBytesUsingIpldPrime(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BytesToBase64 encodes a byte array as a base64 string.
|
// BytesToBase64 encodes a byte array as a base64 string.
|
||||||
|
|||||||
Reference in New Issue
Block a user