From 1d73452ff6173ba98b125ca73373443ae39b6000 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Tue, 7 Mar 2023 14:51:54 +0530 Subject: [PATCH 1/3] replace fn --- x/auction/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 0f375972..b1cae14f 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -417,7 +417,7 @@ func (k Keeper) RevealBid(ctx sdk.Context, msg types.MsgRevealBid) (*types.Aucti return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal string.") } - cid, err := wnsUtils.CIDFromJSONBytes(revealBytes) + cid, err := wnsUtils.CIDFromJSONBytesUsingIpldPrime(revealBytes) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal JSON.") } -- 2.45.2 From 8932d69f368daeee510dce13721d0ee491185526 Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Thu, 9 Mar 2023 00:47:04 +0530 Subject: [PATCH 2/3] fix unit test CID --- utils/json.go | 16 ++++------------ utils/json_test.go | 18 ++++++++---------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/utils/json.go b/utils/json.go index 5bb934ba..72309576 100644 --- a/utils/json.go +++ b/utils/json.go @@ -10,7 +10,6 @@ import ( 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" @@ -18,7 +17,6 @@ import ( "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" ) var store = memstore.Store{} @@ -43,16 +41,6 @@ func GenerateHash(json map[string]interface{}) (string, []byte, error) { 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. func GetAttributeAsString(obj map[string]interface{}, attr string) (string, error) { if value, ok := obj[attr]; ok { @@ -70,6 +58,10 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro // 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) { + if len(content) == 0 { + return "", nil + } + 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! diff --git a/utils/json_test.go b/utils/json_test.go index 36e94c20..45e345e2 100644 --- a/utils/json_test.go +++ b/utils/json_test.go @@ -1,8 +1,9 @@ package utils import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestAndValidateCIDGeneration(t *testing.T) { @@ -11,13 +12,12 @@ func TestAndValidateCIDGeneration(t *testing.T) { content string expected string }{ - // empty string and empty json blows up - // { - // "empty string", "", "bafyreiengp2sbi6ez34a2jctv34bwyjl7yoliteleaswgcwtqzrhmpyt2m", - // }, - // { - // "empty json", "{}", "bafyreihpfkdvib5muloxlj5b3tgdwibjdcu3zdsuhyft33z7gtgnlzlkpm", - // }, + { + "empty string", "", "", + }, + { + "empty json", "{}", "bafyreigbtj4x7ip5legnfznufuopl4sg4knzc2cof6duas4b3q2fy6swua", + }, { "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,10 +26,8 @@ func TestAndValidateCIDGeneration(t *testing.T) { } for _, tc := range testCases { - deprecatedAndCorrect, _ := CIDFromJSONBytes([]byte(tc.content)) newImpl, err := CIDFromJSONBytesUsingIpldPrime([]byte(tc.content)) require.NoError(t, err) - require.Equal(t, deprecatedAndCorrect, newImpl, tc.name) require.Equal(t, tc.expected, newImpl) } } -- 2.45.2 From db2c3afef1aac6e9a061b4b7417e2929cf96cb9a Mon Sep 17 00:00:00 2001 From: 0xmuralik Date: Fri, 10 Mar 2023 16:50:47 +0530 Subject: [PATCH 3/3] rename func --- utils/json.go | 6 +++--- utils/json_test.go | 2 +- x/auction/keeper/keeper.go | 2 +- x/registry/helpers/helpers.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/json.go b/utils/json.go index 72309576..b10c501a 100644 --- a/utils/json.go +++ b/utils/json.go @@ -33,7 +33,7 @@ func GenerateHash(json map[string]interface{}) (string, []byte, error) { return "", nil, err } - cidString, err := CIDFromJSONBytesUsingIpldPrime(content) + cidString, err := CIDFromJSONBytes(content) if err != nil { return "", nil, err } @@ -54,10 +54,10 @@ func GetAttributeAsString(obj map[string]interface{}, attr string) (string, erro return "", errors.New("attribute not found") } -// CIDFromJSONBytesUsingIpldPrime returns CID (dagcbor) for json (as bytes). +// CIDFromJSONBytes 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) { +func CIDFromJSONBytes(content []byte) (string, error) { if len(content) == 0 { return "", nil } diff --git a/utils/json_test.go b/utils/json_test.go index 45e345e2..921a7f05 100644 --- a/utils/json_test.go +++ b/utils/json_test.go @@ -26,7 +26,7 @@ func TestAndValidateCIDGeneration(t *testing.T) { } for _, tc := range testCases { - newImpl, err := CIDFromJSONBytesUsingIpldPrime([]byte(tc.content)) + newImpl, err := CIDFromJSONBytes([]byte(tc.content)) require.NoError(t, err) require.Equal(t, tc.expected, newImpl) } diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index b1cae14f..0f375972 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -417,7 +417,7 @@ func (k Keeper) RevealBid(ctx sdk.Context, msg types.MsgRevealBid) (*types.Aucti return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal string.") } - cid, err := wnsUtils.CIDFromJSONBytesUsingIpldPrime(revealBytes) + cid, err := wnsUtils.CIDFromJSONBytes(revealBytes) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal JSON.") } diff --git a/x/registry/helpers/helpers.go b/x/registry/helpers/helpers.go index 7ce4c7d3..07041252 100644 --- a/x/registry/helpers/helpers.go +++ b/x/registry/helpers/helpers.go @@ -70,7 +70,7 @@ func UnMarshalMapFromJSONBytes(bytes []byte) map[string]interface{} { // GetCid gets the content ID. func GetCid(content []byte) (string, error) { - return wnsUtils.CIDFromJSONBytesUsingIpldPrime(content) + return wnsUtils.CIDFromJSONBytes(content) } // BytesToBase64 encodes a byte array as a base64 string. -- 2.45.2