Skip to content

Commit

Permalink
fix: normalized rdf string came empty even after passing correct Json…
Browse files Browse the repository at this point in the history
…LD struct. This issue was solved by converting the jsonLd struct to an interface{} object and fed to the normalize function
  • Loading branch information
arnabghose997 committed Sep 22, 2023
1 parent 07d115b commit f0d4995
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions x/ssi/ld-context/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ldcontext

import (
"encoding/json"
"fmt"

"github.com/hypersign-protocol/hid-node/x/ssi/types"
Expand Down Expand Up @@ -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
}

0 comments on commit f0d4995

Please sign in to comment.