Skip to content

Commit

Permalink
Add resourcePreview to Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 16, 2022
1 parent 2836ab5 commit f2dc3f3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
25 changes: 25 additions & 0 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
type LedgerServiceI interface {
QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool, error)
QueryResource(collectionDid string, resourceId string) (resource.Resource, bool, error)
QueryCollectionResources(did string) ([]resource.ResourceHeader, error)
GetNamespaces() []string
}

Expand Down Expand Up @@ -85,6 +86,30 @@ func (ls LedgerService) QueryResource(collectionDid string, resourceId string) (
return *resourceResponse.Resource, true, err
}

func (ls LedgerService) QueryCollectionResources(did string) ([]*resource.ResourceHeader, error) {
_, namespace, _, _ := cheqdUtils.TrySplitDID(did)
serverAddr, namespaceFound := ls.ledgers[namespace]
if !namespaceFound {
return []*resource.ResourceHeader{}, fmt.Errorf("namespace not supported: %s", namespace)
}

conn, err := ls.openGRPCConnection(serverAddr)
if err != nil {
log.Error().Err(err).Msg("QueryResource: failed connection")
return []*resource.ResourceHeader{}, err
}

log.Info().Msgf("Querying did resource: %s", did)

client := resource.NewQueryClient(conn)
resourceResponse, err := client.CollectionResources(context.Background(), &resource.QueryGetCollectionResourcesRequest{CollectionId: did})
if err != nil {
return []*resource.ResourceHeader{}, err
}

return resourceResponse.Resources, nil
}

func (ls *LedgerService) RegisterLedger(namespace string, url string) error {
if namespace == "" {
err := errors.New("namespace cannot be empty")
Expand Down
43 changes: 39 additions & 4 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/rs/zerolog/log"

cheqdTypes "github.com/cheqd/cheqd-node/x/cheqd/types"
cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
"github.com/cheqd/did-resolver/types"
"github.com/cheqd/did-resolver/utils"
Expand Down Expand Up @@ -59,16 +60,16 @@ func (rs RequestService) prepareResolutionResult(did string, resolutionOptions t
return "", err
}

metadata, err := rs.didDocService.MarshallProto(&didResolution.Metadata)
metadata, err := json.Marshal(&didResolution.Metadata)
if err != nil {
return "", err
}

if didResolution.ResolutionMetadata.ResolutionError != "" {
didDoc, metadata = "", ""
didDoc, metadata = "", []byte{}
}

return createJsonResolution(didDoc, metadata, string(resolutionMetadata))
return createJsonResolution(didDoc, string(metadata), string(resolutionMetadata))
}

func (rs RequestService) prepareDereferencingResult(did string, dereferencingOptions types.DereferencingOption) (string, error) {
Expand Down Expand Up @@ -117,6 +118,11 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO
return types.DidResolution{}, err
}

resolvedMetadata, err := rs.ResolveMetadata(did, metadata)
if err != nil {
return types.DidResolution{}, err
}

if !isFound {
didResolutionMetadata.ResolutionError = types.ResolutionNotFound
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
Expand All @@ -125,7 +131,7 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO
if didResolutionMetadata.ContentType == types.DIDJSONLD {
didDoc.Context = append(didDoc.Context, types.DIDSchemaJSONLD)
}
return types.DidResolution{Did: didDoc, Metadata: metadata, ResolutionMetadata: didResolutionMetadata}, nil
return types.DidResolution{Did: didDoc, Metadata: resolvedMetadata, ResolutionMetadata: didResolutionMetadata}, nil
}

// https://w3c-ccg.github.io/did-resolution/#dereferencing
Expand Down Expand Up @@ -216,6 +222,35 @@ func (rs RequestService) dereferenceSecondary(did string, fragmentId string, did
return types.DidDereferencing{ContentStream: contentStream, Metadata: didResolution.Metadata, DereferencingMetadata: dereferencingMetadata}, nil
}

func (rs RequestService) ResolveMetadata(did string, metadata cheqdTypes.Metadata) (types.ResolutionDidDocMetadata, error) {
newMetadata := types.ResolutionDidDocMetadata{
metadata.Created,
metadata.Updated,
metadata.Deactivated,
metadata.VersionId,
[]types.ResourcePreview{},
}
if metadata.Resources != nil {
return newMetadata, nil
}
resources, err := rs.ledgerService.QueryCollectionResources(did)
if err != nil {
return newMetadata, err
}
for _, r := range resources {
resourcePreview := types.ResourcePreview {
r.Id,
r.Name,
r.ResourceType,
r.MediaType,
r.Created,
}
newMetadata.Resources = append(newMetadata.Resources, resourcePreview)
}
return newMetadata, nil

}

func createJsonResolution(didDoc string, metadata string, resolutionMetadata string) (string, error) {
if didDoc == "" {
didDoc = "null"
Expand Down
6 changes: 3 additions & 3 deletions types/resolution_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type DidProperties struct {
}

type DidResolution struct {
Did cheqd.Did `json:"didDocument,omitempty"`
Metadata cheqd.Metadata `json:"didDocumentMetadata,omitempty"`
ResolutionMetadata ResolutionMetadata `json:"didResolutionMetadata,omitempty"`
Did cheqd.Did `json:"didDocument,omitempty"`
Metadata ResolutionDidDocMetadata `json:"didDocumentMetadata,omitempty"`
ResolutionMetadata ResolutionMetadata `json:"didResolutionMetadata,omitempty"`
}

func NewResolutionMetadata(didUrl string, contentType ContentType, resolutionError ErrorType) ResolutionMetadata {
Expand Down

0 comments on commit f2dc3f3

Please sign in to comment.