Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add more Resource fields to DID Doc Metadata #23

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion services/diddoc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions services/request_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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: "",
Expand Down
2 changes: 1 addition & 1 deletion types/dereferencing_content_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
18 changes: 13 additions & 5 deletions types/did_doc_metadata.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -14,11 +16,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 string `json:"checksum,omitempty"`
PreviousVersionId string `json:"previousVersionId,omitempty"`
NextVersionId string `json:"nextVersionId,omitempty"`
}

func NewResolutionDidDocMetadata(did string, metadata cheqd.Metadata, resources []*resource.ResourceHeader) ResolutionDidDocMetadata {
Expand All @@ -39,6 +44,9 @@ func NewResolutionDidDocMetadata(did string, metadata cheqd.Metadata, resources
r.ResourceType,
r.MediaType,
r.Created,
fmt.Sprintf("%x", r.Checksum),
r.PreviousVersionId,
r.NextVersionId,
}
newMetadata.Resources = append(newMetadata.Resources, resourcePreview)
}
Expand Down
91 changes: 91 additions & 0 deletions types/did_doc_metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package types

import (
"fmt"
"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: fmt.Sprintf("%x", 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)
})
}
}