From 589c5507163becda4aaf9e7460eeaca202b65687 Mon Sep 17 00:00:00 2001 From: toktar Date: Tue, 2 Aug 2022 18:28:59 +0300 Subject: [PATCH 1/3] feat: Add Resource fields to DID Doc Metadata + tests --- types/did_doc_metadata.go | 16 ++++-- types/did_doc_metadata_test.go | 90 ++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 types/did_doc_metadata_test.go diff --git a/types/did_doc_metadata.go b/types/did_doc_metadata.go index 48e891d3..618ac70f 100644 --- a/types/did_doc_metadata.go +++ b/types/did_doc_metadata.go @@ -14,11 +14,14 @@ type ResolutionDidDocMetadata struct { } type ResourcePreview struct { - ResourceURI string `json:"resourceURI,omitempty"` - Name string `json:"name,omitempty"` - ResourceType string `json:"resourceType,omitempty"` - MediaType string `json:"mediaType,omitempty"` - Created string `json:"created,omitempty"` + ResourceURI string `json:"resourceURI,omitempty"` + Name string `json:"name,omitempty"` + ResourceType string `json:"resourceType,omitempty"` + MediaType string `json:"mediaType,omitempty"` + Created string `json:"created,omitempty"` + Checksum []byte `json:"checksum,omitempty"` + PreviousVersionId string `json:"previousVersionId,omitempty"` + NextVersionId string `json:"nextVersionId,omitempty"` } func NewResolutionDidDocMetadata(did string, metadata cheqd.Metadata, resources []*resource.ResourceHeader) ResolutionDidDocMetadata { @@ -39,6 +42,9 @@ func NewResolutionDidDocMetadata(did string, metadata cheqd.Metadata, resources r.ResourceType, r.MediaType, r.Created, + r.Checksum, + r.PreviousVersionId, + r.NextVersionId, } newMetadata.Resources = append(newMetadata.Resources, resourcePreview) } diff --git a/types/did_doc_metadata_test.go b/types/did_doc_metadata_test.go new file mode 100644 index 00000000..1980e8fa --- /dev/null +++ b/types/did_doc_metadata_test.go @@ -0,0 +1,90 @@ +package types + +import ( + "testing" + + cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" + resource "github.com/cheqd/cheqd-node/x/resource/types" + "github.com/stretchr/testify/require" +) + +func TestNewResolutionDidDocMetadata(t *testing.T) { + validIdentifier := "N22KY2Dyvmuu2Pyy" + validDid := "did:cheqd:mainnet:" + validIdentifier + validResourceId := "18e9d838-0bea-435b-964b-c6529ede6d2b" + resourceHeader := resource.ResourceHeader{ + CollectionId: validIdentifier, + Id: validResourceId, + Name: "Existing Resource Name", + ResourceType: "CL-Schema", + MediaType: "application/json", + Checksum: []byte("test_checksum")} + + validMetadataResource := ResourcePreview{ + ResourceURI: validDid + RESOURCE_PATH + resourceHeader.Id, + Name: resourceHeader.Name, + ResourceType: resourceHeader.ResourceType, + MediaType: resourceHeader.MediaType, + Created: resourceHeader.Created, + Checksum: resourceHeader.Checksum, + PreviousVersionId: resourceHeader.PreviousVersionId, + NextVersionId: resourceHeader.NextVersionId, + } + + subtests := []struct { + name string + metadata cheqd.Metadata + resources []*resource.ResourceHeader + expectedResult ResolutionDidDocMetadata + }{ + { + name: "matadata with resource", + metadata: cheqd.Metadata{ + VersionId: "test_version_id", + Deactivated: false, + Resources: []string{validResourceId}, + }, + resources: []*resource.ResourceHeader{&resourceHeader}, + expectedResult: ResolutionDidDocMetadata{ + VersionId: "test_version_id", + Deactivated: false, + Resources: []ResourcePreview{validMetadataResource}, + }, + }, + { + name: "matadata without resource in metadata", + metadata: cheqd.Metadata{ + VersionId: "test_version_id", + Deactivated: false, + }, + resources: []*resource.ResourceHeader{&resourceHeader}, + expectedResult: ResolutionDidDocMetadata{ + VersionId: "test_version_id", + Deactivated: false, + }, + }, + { + name: "matadata with resources", + metadata: cheqd.Metadata{ + VersionId: "test_version_id", + Deactivated: false, + Resources: []string{validResourceId}, + }, + resources: []*resource.ResourceHeader{}, + expectedResult: ResolutionDidDocMetadata{ + VersionId: "test_version_id", + Deactivated: false, + }, + }, + } + + for _, subtest := range subtests { + t.Run(subtest.name, func(t *testing.T) { + + result := NewResolutionDidDocMetadata(validDid, subtest.metadata, subtest.resources) + + require.EqualValues(t, subtest.expectedResult, result) + // require.EqualValues(t, subtest.expectedError, err) + }) + } +} From a5b81e9f0b5b89f988f5c230e3e2f01d292a1475 Mon Sep 17 00:00:00 2001 From: toktar Date: Tue, 2 Aug 2022 18:35:33 +0300 Subject: [PATCH 2/3] fix linter issues --- README.md | 2 +- types/did_doc_metadata_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cfc4aceb..9803fb48 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ cheqd DID resovler offers multiple implementations for resolving cheqd DIDs, according to the [cheqd DID method](https://docs.cheqd.io/node/architecture/adr-list/adr-002-cheqd-did-method#:~:text=Summary,on%20the%20Cosmos%20blockchain%20framework.) -This resolver aims to make it easy for third parties to resolve cheqd DIDs, using either a Full DID resolver, a Light DID resolver or through the [Universal Resolver](https://dev.uniresolver.io/). +This resolver aims to make it easy for third parties to resolve cheqd DIDs, using either a Full DID resolver, a Light DID resolver or through the [Universal Resolver](https://github.com/decentralized-identity/universal-resolver). ## Example DIDs diff --git a/types/did_doc_metadata_test.go b/types/did_doc_metadata_test.go index 1980e8fa..5b79bbf2 100644 --- a/types/did_doc_metadata_test.go +++ b/types/did_doc_metadata_test.go @@ -18,7 +18,8 @@ func TestNewResolutionDidDocMetadata(t *testing.T) { Name: "Existing Resource Name", ResourceType: "CL-Schema", MediaType: "application/json", - Checksum: []byte("test_checksum")} + Checksum: []byte("test_checksum"), + } validMetadataResource := ResourcePreview{ ResourceURI: validDid + RESOURCE_PATH + resourceHeader.Id, @@ -34,7 +35,7 @@ func TestNewResolutionDidDocMetadata(t *testing.T) { subtests := []struct { name string metadata cheqd.Metadata - resources []*resource.ResourceHeader + resources []*resource.ResourceHeader expectedResult ResolutionDidDocMetadata }{ { @@ -80,7 +81,6 @@ func TestNewResolutionDidDocMetadata(t *testing.T) { for _, subtest := range subtests { t.Run(subtest.name, func(t *testing.T) { - result := NewResolutionDidDocMetadata(validDid, subtest.metadata, subtest.resources) require.EqualValues(t, subtest.expectedResult, result) From 8e1e130913535ee26942f77a769a2533753f2c56 Mon Sep 17 00:00:00 2001 From: toktar Date: Wed, 3 Aug 2022 18:52:43 +0300 Subject: [PATCH 3/3] Change Checksum to hex for resource --- services/diddoc_service.go | 3 ++- services/request_service_test.go | 4 ++-- types/dereferencing_content_stream.go | 2 +- types/did_doc_metadata.go | 6 ++++-- types/did_doc_metadata_test.go | 3 ++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/services/diddoc_service.go b/services/diddoc_service.go index e349f622..15b4619c 100644 --- a/services/diddoc_service.go +++ b/services/diddoc_service.go @@ -5,6 +5,7 @@ import ( // jsonpb Marshaller is deprecated, but is needed because there's only one way to proto // marshal in combination with our proto generator version "encoding/json" + "fmt" "strings" cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" @@ -88,7 +89,7 @@ func (ds DIDDocService) MarshallContentStream(contentStream protoiface.MessageV1 ResourceType: contentStream.Header.ResourceType, MediaType: contentStream.Header.MediaType, Created: contentStream.Header.Created, - Checksum: contentStream.Header.Checksum, + Checksum: fmt.Sprintf("%x", contentStream.Header.Checksum), PreviousVersionId: contentStream.Header.PreviousVersionId, NextVersionId: contentStream.Header.NextVersionId, Data: contentStream.Data, diff --git a/services/request_service_test.go b/services/request_service_test.go index 1fad69a1..c1dead4b 100644 --- a/services/request_service_test.go +++ b/services/request_service_test.go @@ -221,7 +221,7 @@ func TestDereferencing(t *testing.T) { validVerificationMethod := validVerificationMethod() validService := validService() validResource := validResource() - validChecksum, _ := json.Marshal(validResource.Header.Checksum) + validChecksum := fmt.Sprintf("%x", validResource.Header.Checksum) validData, _ := json.Marshal(validResource.Data) validMetadata := validMetadata() validFragmentMetadata := types.NewResolutionDidDocMetadata(validDid, validMetadata, []*resource.ResourceHeader{}) @@ -269,7 +269,7 @@ func TestDereferencing(t *testing.T) { ledgerService: NewMockLedgerService(validDIDDoc, validMetadata, validResource), dereferencingType: types.DIDJSONLD, didUrl: validDid + types.RESOURCE_PATH + validResourceId, - expectedContentStream: fmt.Sprintf("{\"@context\":[\"%s\"],\"collectionId\":\"%s\",\"id\":\"%s\",\"name\":\"%s\",\"resourceType\":\"%s\",\"mediaType\":\"%s\",\"checksum\":%s,\"data\":%s}", + 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: types.ResolutionDidDocMetadata{}, expectedError: "", diff --git a/types/dereferencing_content_stream.go b/types/dereferencing_content_stream.go index c71ba40a..70c6a55b 100644 --- a/types/dereferencing_content_stream.go +++ b/types/dereferencing_content_stream.go @@ -8,7 +8,7 @@ type DereferencedResource struct { ResourceType string `json:"resourceType,omitempty"` MediaType string `json:"mediaType,omitempty"` Created string `json:"created,omitempty"` - Checksum []byte `json:"checksum,omitempty"` + Checksum string `json:"checksum,omitempty"` PreviousVersionId string `json:"previousVersionId,omitempty"` NextVersionId string `json:"nextVersionId,omitempty"` Data []byte `json:"data,omitempty"` diff --git a/types/did_doc_metadata.go b/types/did_doc_metadata.go index 618ac70f..35ded6a0 100644 --- a/types/did_doc_metadata.go +++ b/types/did_doc_metadata.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" resource "github.com/cheqd/cheqd-node/x/resource/types" ) @@ -19,7 +21,7 @@ type ResourcePreview struct { ResourceType string `json:"resourceType,omitempty"` MediaType string `json:"mediaType,omitempty"` Created string `json:"created,omitempty"` - Checksum []byte `json:"checksum,omitempty"` + Checksum string `json:"checksum,omitempty"` PreviousVersionId string `json:"previousVersionId,omitempty"` NextVersionId string `json:"nextVersionId,omitempty"` } @@ -42,7 +44,7 @@ func NewResolutionDidDocMetadata(did string, metadata cheqd.Metadata, resources r.ResourceType, r.MediaType, r.Created, - r.Checksum, + fmt.Sprintf("%x", r.Checksum), r.PreviousVersionId, r.NextVersionId, } diff --git a/types/did_doc_metadata_test.go b/types/did_doc_metadata_test.go index 5b79bbf2..bfdb6bba 100644 --- a/types/did_doc_metadata_test.go +++ b/types/did_doc_metadata_test.go @@ -1,6 +1,7 @@ package types import ( + "fmt" "testing" cheqd "github.com/cheqd/cheqd-node/x/cheqd/types" @@ -27,7 +28,7 @@ func TestNewResolutionDidDocMetadata(t *testing.T) { ResourceType: resourceHeader.ResourceType, MediaType: resourceHeader.MediaType, Created: resourceHeader.Created, - Checksum: resourceHeader.Checksum, + Checksum: fmt.Sprintf("%x", resourceHeader.Checksum), PreviousVersionId: resourceHeader.PreviousVersionId, NextVersionId: resourceHeader.NextVersionId, }