From f0d4995b0979010635c6becc7a6d4d52712cb6ff Mon Sep 17 00:00:00 2001 From: Arnab Ghose Date: Fri, 22 Sep 2023 15:22:21 +0530 Subject: [PATCH] fix: normalized rdf string came empty even after passing correct JsonLD struct. This issue was solved by converting the jsonLd struct to an interface{} object and fed to the normalize function --- x/ssi/ld-context/types.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/x/ssi/ld-context/types.go b/x/ssi/ld-context/types.go index db5c922..2985532 100644 --- a/x/ssi/ld-context/types.go +++ b/x/ssi/ld-context/types.go @@ -1,6 +1,7 @@ package ldcontext import ( + "encoding/json" "fmt" "github.com/hypersign-protocol/hid-node/x/ssi/types" @@ -56,19 +57,39 @@ func NewJsonLdDid(didDoc *types.Did) *JsonLdDid { return jsonLdDoc } +// Convert JsonLdDid to interface +func jsonLdDidToInterface(jsonLd *JsonLdDid) interface{} { + var intf interface{} + + jsonLdBytes, err := json.Marshal(jsonLd) + if err != nil { + panic(err) + } + + err = json.Unmarshal(jsonLdBytes, &intf) + if err != nil { + panic(err) + } + + return intf +} + // NormalizeWithURDNA2015 performs RDF Canonization upon JsonLdDid using URDNA2015 // algorithm and returns the canonized document in string -func (doc *JsonLdDid) NormalizeWithURDNA2015() (string, error) { +func (jsonLd *JsonLdDid) NormalizeWithURDNA2015() (string, error) { proc := ld.NewJsonLdProcessor() options := ld.NewJsonLdOptions("") options.Algorithm = ld.AlgorithmURDNA2015 options.Format = "application/n-quads" - normalisedJsonLdDid, err := proc.Normalize(doc, options) + normalisedJsonLdDid, err := proc.Normalize(jsonLdDidToInterface(jsonLd), options) if err != nil { return "", fmt.Errorf("unable to Normalize DID Document: %v", err.Error()) } canonizedDocString := normalisedJsonLdDid.(string) + if canonizedDocString == "" { + return "", fmt.Errorf("normalization yield empty RDF string for did document: %v", jsonLd.Id) + } return canonizedDocString, nil }