diff --git a/services/ledger_service.go b/services/ledger_service.go index 6e89ead5..09bce950 100644 --- a/services/ledger_service.go +++ b/services/ledger_service.go @@ -20,7 +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) + QueryCollectionResources(did string) ([]*resource.ResourceHeader, error) GetNamespaces() []string } diff --git a/services/request_service.go b/services/request_service.go index 04fbc213..b0410579 100644 --- a/services/request_service.go +++ b/services/request_service.go @@ -9,6 +9,7 @@ import ( "github.com/rs/zerolog/log" cheqdTypes "github.com/cheqd/cheqd-node/x/cheqd/types" + resourceTypes "github.com/cheqd/cheqd-node/x/resource/types" cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils" "github.com/cheqd/did-resolver/types" "github.com/cheqd/did-resolver/utils" @@ -89,12 +90,12 @@ func (rs RequestService) prepareDereferencingResult(did string, dereferencingOpt return createJsonDereferencing(nil, "", string(dereferencingMetadata)) } - metadata, err := rs.didDocService.MarshallProto(&didDereferencing.Metadata) + metadata, err := json.Marshal(didDereferencing.Metadata) if err != nil { return "", err } - return createJsonDereferencing(didDereferencing.ContentStream, metadata, string(dereferencingMetadata)) + return createJsonDereferencing(didDereferencing.ContentStream, string(metadata), string(dereferencingMetadata)) } // https://w3c-ccg.github.io/did-resolution/#resolving @@ -196,6 +197,7 @@ func (rs RequestService) dereferenceSecondary(did string, fragmentId string, did if err != nil { return types.DidDereferencing{}, err } + metadata := didResolution.Metadata dereferencingMetadata := types.DereferencingMetadata(didResolution.ResolutionMetadata) if dereferencingMetadata.ResolutionError != "" { return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil @@ -204,6 +206,7 @@ func (rs RequestService) dereferenceSecondary(did string, fragmentId string, did var protoContent protoiface.MessageV1 if fragmentId != "" { protoContent = rs.didDocService.GetDIDFragment(fragmentId, didResolution.Did) + metadata = types.TransformToFragmentMetadata(metadata) } else { protoContent = &didResolution.Did } @@ -219,35 +222,18 @@ func (rs RequestService) dereferenceSecondary(did string, fragmentId string, did } contentStream := json.RawMessage(jsonFragment) - return types.DidDereferencing{ContentStream: contentStream, Metadata: didResolution.Metadata, DereferencingMetadata: dereferencingMetadata}, nil + return types.DidDereferencing{ContentStream: contentStream, Metadata: 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 + if metadata.Resources == nil { + return types.NewResolutionDidDocMetadata(metadata, []*resourceTypes.ResourceHeader{}), 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 + return types.ResolutionDidDocMetadata{}, err + } + return types.NewResolutionDidDocMetadata(metadata, resources), nil } diff --git a/services/request_service_test.go b/services/request_service_test.go index 30ff2902..33a9dd97 100644 --- a/services/request_service_test.go +++ b/services/request_service_test.go @@ -103,12 +103,22 @@ func (ls MockLedgerService) QueryResource(collectionDid string, resourceId strin return ls.Resource, isFound, nil } + +func (ls MockLedgerService) QueryCollectionResources(did string) ([]*resource.ResourceHeader, error) { + if ls.Metadata.Resources == nil { + return []*resource.ResourceHeader{}, nil + } + return []*resource.ResourceHeader{ls.Resource.Header}, nil +} + func (ls MockLedgerService) GetNamespaces() []string { return []string{"testnet", "mainnet"} } func TestResolve(t *testing.T) { validDIDDoc := validDIDDoc() + validMetadata := validMetadata() + validResource := validResource() subtests := []struct { name string ledgerService MockLedgerService @@ -117,18 +127,18 @@ func TestResolve(t *testing.T) { method string namespace string expectedDID cheqd.Did - expectedMetadata cheqd.Metadata + expectedMetadata types.ResolutionDidDocMetadata expectedError types.ErrorType }{ { name: "successful resolution", - ledgerService: NewMockLedgerService(validDIDDoc, validMetadata(), resource.Resource{}), + ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource), resolutionType: types.DIDJSONLD, identifier: validIdentifier, method: validMethod, namespace: validNamespace, expectedDID: validDIDDoc, - expectedMetadata: validMetadata(), + expectedMetadata: types.NewResolutionDidDocMetadata(validMetadata, []*resource.ResourceHeader{validResource.Header}), expectedError: "", }, { @@ -139,7 +149,7 @@ func TestResolve(t *testing.T) { method: validMethod, namespace: validNamespace, expectedDID: cheqd.Did{}, - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.ResolutionNotFound, }, { @@ -150,7 +160,7 @@ func TestResolve(t *testing.T) { method: validMethod, namespace: validNamespace, expectedDID: cheqd.Did{}, - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.ResolutionInvalidDID, }, { @@ -161,7 +171,7 @@ func TestResolve(t *testing.T) { method: "not_supported_method", namespace: validNamespace, expectedDID: cheqd.Did{}, - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.ResolutionMethodNotSupported, }, { @@ -172,7 +182,7 @@ func TestResolve(t *testing.T) { method: validMethod, namespace: "invalid_namespace", expectedDID: cheqd.Did{}, - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.ResolutionInvalidDID, }, } @@ -212,53 +222,55 @@ func TestDereferencing(t *testing.T) { validResource := validResource() validChecksum, _ := json.Marshal(validResource.Header.Checksum) validData, _ := json.Marshal(validResource.Data) + validMetadata := validMetadata() + validFragmentMetadata := types.NewResolutionDidDocMetadata(validMetadata, []*resource.ResourceHeader{}) subtests := []struct { name string ledgerService MockLedgerService dereferencingType types.ContentType didUrl string expectedContentStream string - expectedMetadata cheqd.Metadata + expectedMetadata types.ResolutionDidDocMetadata expectedError types.ErrorType }{ { name: "successful resolution", - ledgerService: NewMockLedgerService(validDIDDoc, validMetadata(), validResource), + ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource), dereferencingType: types.DIDJSONLD, didUrl: validDid, expectedContentStream: fmt.Sprintf("{\"@context\":[\"%s\"],\"id\":\"%s\",\"verificationMethod\":[{\"id\":\"%s\",\"type\":\"%s\",\"controller\":\"%s\",\"publicKeyJwk\":%s}],\"service\":[{\"id\":\"%s\",\"type\":\"%s\",\"serviceEndpoint\":\"%s\"}]}", types.DIDSchemaJSONLD, validDid, validVerificationMethod.Id, validVerificationMethod.Type, validVerificationMethod.Controller, validPubKeyJWK, validService.Id, validService.Type, validService.ServiceEndpoint), - expectedMetadata: validMetadata(), + expectedMetadata: types.NewResolutionDidDocMetadata(validMetadata, []*resource.ResourceHeader{validResource.Header}), expectedError: "", }, { name: "successful Secondary dereferencing (key)", - ledgerService: NewMockLedgerService(validDIDDoc, validMetadata(), validResource), + ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource), dereferencingType: types.DIDJSONLD, didUrl: validVerificationMethod.Id, expectedContentStream: fmt.Sprintf("{\"@context\":\"%s\",\"id\":\"%s\",\"type\":\"%s\",\"controller\":\"%s\",\"publicKeyJwk\":%s}", types.DIDSchemaJSONLD, validVerificationMethod.Id, validVerificationMethod.Type, validVerificationMethod.Controller, validPubKeyJWK), - expectedMetadata: validMetadata(), + expectedMetadata: validFragmentMetadata, expectedError: "", }, { name: "successful Secondary dereferencing (service)", - ledgerService: NewMockLedgerService(validDIDDoc, validMetadata(), validResource), + ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource), dereferencingType: types.DIDJSONLD, didUrl: validService.Id, expectedContentStream: fmt.Sprintf("{\"@context\":\"%s\",\"id\":\"%s\",\"type\":\"%s\",\"serviceEndpoint\":\"%s\"}", types.DIDSchemaJSONLD, validService.Id, validService.Type, validService.ServiceEndpoint), - expectedMetadata: validMetadata(), + expectedMetadata: validFragmentMetadata, expectedError: "", }, { name: "successful Primary dereferencing (resource)", - ledgerService: NewMockLedgerService(validDIDDoc, validMetadata(), validResource), + ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource), dereferencingType: types.DIDJSONLD, didUrl: validDid + "/resource/" + validResourceId, expectedContentStream: fmt.Sprintf("{\"@context\":[\"%s\"],\"collectionId\":\"%s\",\"id\":\"%s\",\"name\":\"%s\",\"resourceType\":\"%s\",\"mediaType\":\"%s\",\"checksum\":%s,\"data\":%s}", types.DIDSchemaJSONLD, validResource.Header.CollectionId, validResource.Header.Id, validResource.Header.Name, validResource.Header.ResourceType, validResource.Header.MediaType, validChecksum, validData), - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: "", }, { @@ -266,7 +278,7 @@ func TestDereferencing(t *testing.T) { ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}), didUrl: "unvalid_url", dereferencingType: types.DIDJSONLD, - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.DereferencingInvalidDIDUrl, }, { @@ -274,7 +286,7 @@ func TestDereferencing(t *testing.T) { ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}), dereferencingType: types.DIDJSONLD, didUrl: validDid + "/unknown_path", - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.DereferencingNotSupported, }, { @@ -282,7 +294,7 @@ func TestDereferencing(t *testing.T) { ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}), dereferencingType: types.DIDJSONLD, didUrl: validDid + "?unknown_query", - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.DereferencingNotSupported, }, { @@ -290,7 +302,7 @@ func TestDereferencing(t *testing.T) { ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}), dereferencingType: types.DIDJSONLD, didUrl: validDid + "#notFoundKey", - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.DereferencingNotFound, }, { @@ -298,7 +310,7 @@ func TestDereferencing(t *testing.T) { ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}, resource.Resource{}), dereferencingType: types.DIDJSONLD, didUrl: validDid + "/resource/00000000-0000-0000-0000-000000000000", - expectedMetadata: cheqd.Metadata{}, + expectedMetadata: types.ResolutionDidDocMetadata{}, expectedError: types.DereferencingNotFound, }, } diff --git a/types/dereferecing_metadata.go b/types/dereferecing_metadata.go index d6a624fa..c0f9fe99 100644 --- a/types/dereferecing_metadata.go +++ b/types/dereferecing_metadata.go @@ -2,8 +2,6 @@ package types import ( "encoding/json" - - cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" ) type DereferencingOption ResolutionOption @@ -11,9 +9,9 @@ type DereferencingOption ResolutionOption type DereferencingMetadata ResolutionMetadata type DidDereferencing struct { - ContentStream json.RawMessage `json:"contentStream,omitempty"` - Metadata cheqd.Metadata `json:"contentMetadata,omitempty"` - DereferencingMetadata DereferencingMetadata `json:"dereferencingMetadata,omitempty"` + ContentStream json.RawMessage `json:"contentStream,omitempty"` + Metadata ResolutionDidDocMetadata `json:"contentMetadata,omitempty"` + DereferencingMetadata DereferencingMetadata `json:"dereferencingMetadata,omitempty"` } func NewDereferencingMetadata(did string, contentType ContentType, resolutionError ErrorType) DereferencingMetadata {