diff --git a/services/ledger_service.go b/services/ledger_service.go index b7d0d60e..6e89ead5 100644 --- a/services/ledger_service.go +++ b/services/ledger_service.go @@ -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 } @@ -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") diff --git a/services/request_service.go b/services/request_service.go index 4e706720..04fbc213 100644 --- a/services/request_service.go +++ b/services/request_service.go @@ -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" @@ -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) { @@ -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 @@ -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 @@ -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" diff --git a/types/resolution_metadata.go b/types/resolution_metadata.go index 19d8a794..edbbd7db 100644 --- a/types/resolution_metadata.go +++ b/types/resolution_metadata.go @@ -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 {