Skip to content

Commit

Permalink
DEV-1172: Update dta models
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Apr 20, 2022
1 parent ce94577 commit b73fa75
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ func main() {
e.GET(didResolutionPath, func(c echo.Context) error {
did := c.Param("did")
accept := strings.Split(c.Request().Header.Get("accept"), ";")[0]
if strings.Contains(accept, types.ResolutionJSONLDType) {
accept = types.ResolutionDIDJSONLDType
var acceptOption types.ContentType
if strings.Contains(accept, string(types.JSONLD)) {
acceptOption = types.DIDJSONLD
} else {
accept = types.ResolutionDIDJSONType
acceptOption = types.DIDJSON
}
e.StdLogger.Println("get did")
responseBody, err := requestService.ProcessDIDRequest(did, types.ResolutionOption{Accept: accept})
responseBody, err := requestService.ProcessDIDRequest(did, types.ResolutionOption{Accept: acceptOption})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
Expand Down
18 changes: 10 additions & 8 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,22 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO
didResolutionMetadata.ResolutionError = types.ResolutionNotFound
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}, nil
}
if didResolutionMetadata.ContentType == types.ResolutionDIDJSONLDType {
if didResolutionMetadata.ContentType == types.DIDJSONLD {
didDoc.Context = append(didDoc.Context, types.DIDSchemaJSONLD)
}
return types.DidResolution{didDoc, metadata, didResolutionMetadata}, nil
}

// https://w3c-ccg.github.io/did-resolution/#dereferencing
// func (RequestService) dereference(didUrl string, dereferenceOptions map[string]string) (string, string, string) {
// did, metadata, err := LedgerService.QueryDIDDoc()
// if err != nil {
// didResolutionMetadata = ResolutionErr(ResolutionNotFound)
// }
// return dereferencingMetadata, contentStream, contentMetadata
// }
func (rs RequestService) dereference(didUrl string, dereferenceOptions types.DereferencingOption) (types.DidDereferencing, error) {
didResolution, err := rs.Resolve(did, types.ResolutionOption(dereferenceOptions))
if err != nil {
didResolutionMetadata = ResolutionErr(ResolutionNotFound)
}
dereferencingMetadata := types.DereferencingMetadata(didResolution.ResolutionMetadata)
contentMetadata := didResolution.Metadata
return types.DidDereferencing{contentStream, dereferencingMetadata, contentMetadata}, nil
}

func createJsonResolution(didDoc string, metadata string, resolutionMetadata string) string {
if didDoc == "" {
Expand Down
10 changes: 5 additions & 5 deletions services/request_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestResolve(t *testing.T) {
{
name: "successful resolution",
ledgerService: NewMockLedgerService(validDIDDoc, validMetadata),
resolutionType: types.ResolutionDIDJSONLDType,
resolutionType: types.DIDJSONLD,
identifier: validIdentifier,
method: validMethod,
expectedDID: validDIDDoc,
Expand All @@ -66,7 +66,7 @@ func TestResolve(t *testing.T) {
{
name: "DID not found",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}),
resolutionType: types.ResolutionDIDJSONLDType,
resolutionType: types.DIDJSONLD,
identifier: validIdentifier,
method: validMethod,
expectedDID: cheqd.Did{},
Expand All @@ -76,7 +76,7 @@ func TestResolve(t *testing.T) {
{
name: "invalid DID",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}),
resolutionType: types.ResolutionDIDJSONLDType,
resolutionType: types.DIDJSONLD,
identifier: "oooooo0000OOOO_invalid_did",
method: validMethod,
expectedDID: cheqd.Did{},
Expand All @@ -86,7 +86,7 @@ func TestResolve(t *testing.T) {
{
name: "invalid method",
ledgerService: NewMockLedgerService(cheqd.Did{}, cheqd.Metadata{}),
resolutionType: types.ResolutionDIDJSONLDType,
resolutionType: types.DIDJSONLD,
identifier: validIdentifier,
method: "not_supported_method",
expectedDID: cheqd.Did{},
Expand All @@ -104,7 +104,7 @@ func TestResolve(t *testing.T) {
MethodSpecificId: subtest.identifier,
Method: subtest.method,
}
if (subtest.resolutionType == types.ResolutionDIDJSONLDType || subtest.resolutionType == types.ResolutionJSONLDType) && subtest.expectedError == "" {
if (subtest.resolutionType == types.DIDJSONLD || subtest.resolutionType == types.JSONLD) && subtest.expectedError == "" {
subtest.expectedDID.Context = []string{types.DIDSchemaJSONLD}
}

Expand Down
24 changes: 3 additions & 21 deletions types/resolution_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,12 @@ import (
cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
)

type ResolutionError string

const (
ResolutionInvalidDID ResolutionError = "invalidDid"
ResolutionNotFound ResolutionError = "notFound"
ResolutionMethodNotSupported ResolutionError = "methodNotSupported"
)

const (
ResolutionDIDJSONType = "application/did+json"
ResolutionDIDJSONLDType = "application/did+ld+json"
ResolutionJSONLDType = "application/ld+json"
)

const (
DIDSchemaJSONLD = "https://ww.w3.org/ns/did/v1"
)

type ResolutionOption struct {
Accept string `json:"accept,omitempty"`
Accept ContentType `json:"accept,omitempty"`
}

type ResolutionMetadata struct {
ContentType string `json:"contentType,omitempty"`
ContentType ContentType `json:"contentType,omitempty"`
ResolutionError ResolutionError `json:"error,omitempty"`
Retrieved string `json:"retrieved,omitempty"`
DidProperties DidProperties `json:"did,omitempty"`
Expand All @@ -48,7 +30,7 @@ type DidResolution struct {
ResolutionMetadata ResolutionMetadata `json:"didResolutionMetadata,omitempty"`
}

func NewResolutionMetadata(did string, contentType string, resolutionError ResolutionError) ResolutionMetadata {
func NewResolutionMetadata(did string, contentType ContentType, resolutionError ResolutionError) ResolutionMetadata {
method, _, id, _ := cheqdUtils.TrySplitDID(did)
return ResolutionMetadata{
ContentType: contentType,
Expand Down

0 comments on commit b73fa75

Please sign in to comment.