From 7db19ee56d0fa46db62eeffdfcf28c39dc89c659 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 19 Jan 2023 16:23:10 -0500 Subject: [PATCH 1/4] missing unmarshalling of content bytes before encoding and generation of CID --- utils/json.go | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/utils/json.go b/utils/json.go index 8adc6a9f..2c47181f 100644 --- a/utils/json.go +++ b/utils/json.go @@ -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 } -- 2.45.2 From 44ab3f6fc04288bb3147db190d828e5e36a2fd2b Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 19 Jan 2023 17:30:47 -0500 Subject: [PATCH 2/4] unchecked error complaint from linter --- utils/json.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/utils/json.go b/utils/json.go index 2c47181f..59746c29 100644 --- a/utils/json.go +++ b/utils/json.go @@ -70,10 +70,13 @@ 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!) + np := basicnode.Prototype.Any // Pick a stle for the in-memory data. + nb := np.NewBuilder() // Create a builder. + err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in! + if err != nil { + return "", err + } + n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!) lsys := cidlink.DefaultLinkSystem() -- 2.45.2 From 7de916117dcc805e067a14036f6db0f4b2b8592a Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 19 Jan 2023 17:36:16 -0500 Subject: [PATCH 3/4] golang linting is really picky --- utils/json.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/json.go b/utils/json.go index 59746c29..31a4d67c 100644 --- a/utils/json.go +++ b/utils/json.go @@ -7,6 +7,7 @@ package utils import ( "bytes" "errors" + canonicalJson "github.com/gibson042/canonicaljson-go" "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" @@ -66,10 +67,9 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro } // CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes). +// This is combination of samples for unmarshalling and linking +// see: https://pkg.go.dev/github.com/ipld/go-ipld-prime 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. err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in! @@ -105,6 +105,6 @@ func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) { if err != nil { return "", err } - //return cborcid.String(), nil + // return cborcid.String(), nil return lnk.String(), nil } -- 2.45.2 From ac6a5602ad6c79fdf16e7b50f623568f85341ea2 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 25 Jan 2023 15:05:48 -0500 Subject: [PATCH 4/4] utils/json test for comparing known, but deprecated method to new implementation --- utils/json.go | 1 - utils/json_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 utils/json_test.go diff --git a/utils/json.go b/utils/json.go index 31a4d67c..5bb934ba 100644 --- a/utils/json.go +++ b/utils/json.go @@ -105,6 +105,5 @@ func CIDFromJSONBytesUsingIpldPrime(content []byte) (string, error) { if err != nil { return "", err } - // return cborcid.String(), nil return lnk.String(), nil } diff --git a/utils/json_test.go b/utils/json_test.go new file mode 100644 index 00000000..f44ed47d --- /dev/null +++ b/utils/json_test.go @@ -0,0 +1,30 @@ +package utils + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestAndValidateCIDGeneration(t *testing.T) { + testCases := []struct { + name string + content string + }{ + { + "empty string", "", + }, + { + "empty json", "{}", + }, + + { + "test record", "\\xa6curlohttps://cerc.iodtypex\\x19WebsiteRegistrationRecordgversione0.0.1ltls_cert_cidx.QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnRrbuild_artifact_cidx.QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9x\\x1crepo_registration_record_cidx.QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D", + }, + } + + for _, tc := range testCases { + deprecatedAndCorrect, _ := CIDFromJSONBytes([]byte(tc.content)) + newImpl, _ := CIDFromJSONBytesUsingIpldPrime([]byte(tc.content)) + require.Equal(t, deprecatedAndCorrect, newImpl, tc.name) + } +} -- 2.45.2