Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-1074-did-resolver' into DEV-…
Browse files Browse the repository at this point in the history
…1074-dokerfile

Signed-off-by: Andrew Nikitin <andrew.nikitin@cheqd.io>
  • Loading branch information
Andrew Nikitin committed Apr 14, 2022
2 parents 9290a99 + daed391 commit 57eea48
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 40 deletions.
25 changes: 14 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
"flag"
"net/http"
"os"

"github.com/cheqd/cheqd-did-resolver/services"
"github.com/cheqd/cheqd-did-resolver/types"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"gopkg.in/yaml.v2"

//"net/url"
"strings"
)

Expand All @@ -18,6 +19,10 @@ type Config struct {
}

func main() {
didResolutionPath := flag.String("path", "/1.0/identifier/:did", "URL path with DID resolution endpoint")
didResolutionPort := flag.String("port", ":1313", "The endpoint port with DID resolution")
flag.Parse()

// Echo instance
e := echo.New()

Expand All @@ -40,14 +45,19 @@ func main() {
requestService := services.NewRequestService(ledgerService)

// Routes
e.GET("/identifier/:did", func(c echo.Context) error {
e.GET(*didResolutionPath, func(c echo.Context) error {
did := c.Param("did")
// decode the paramater
// did, err := url.QueryUnescape(did1)
//if err != nil {
// return c.JSON(http.StatusBadRequest, map[string]string{})
//}
accept := strings.Split(c.Request().Header.Get("accept"), ";")[0]
if strings.Contains(accept, types.ResolutionJSONLDType) {
accept = types.ResolutionDIDJSONLDType
} else {
accept = types.ResolutionDIDJSONType
}
resolutionOption := map[string]string{"Accept": accept}
e.StdLogger.Println("get did")
responseBody, err := requestService.ProcessDIDRequest(did, resolutionOption)
Expand All @@ -57,14 +67,7 @@ func main() {
status = http.StatusBadRequest
}
return c.JSONBlob(status, []byte(responseBody))
//opt := resolver.ResolutionOption{Accept: accept}
//rr := resolver.ResolveRepresentation(conn, did, opt)
//
//// add universal resolver specific data:
//rr.ResolutionMetadata.DidProperties = map[string]string{
// "method": "cosmos",
// "methodSpecificId": strings.TrimPrefix(rr.Document.Id, DidPrefix),
//}

//
//// track the resolution
//atomic.AddUint64(&rt.resolves, 1)
Expand All @@ -74,7 +77,7 @@ func main() {
})

// Start server
e.Logger.Fatal(e.Start(":1313"))
e.Logger.Fatal(e.Start(*didResolutionPort))
}

func getConfig(configFileName string) (Config, error) {
Expand Down
23 changes: 9 additions & 14 deletions services/diddoc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
// marshal in combination with our proto generator version
"encoding/json"

"github.com/cheqd/cheqd-did-resolver/types"
cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
"github.com/golang/protobuf/jsonpb" //nolint
"github.com/golang/protobuf/proto"
Expand All @@ -18,6 +17,7 @@ type DIDDocService struct {
const (
verificationMethod = "verificationMethod"
publicKeyJwk = "publicKeyJwk"
didContext = "context"
)

func (DIDDocService) MarshallProto(protoObject proto.Message) (string, error) {
Expand All @@ -37,33 +37,28 @@ func (ds DIDDocService) MarshallDID(didDoc cheqd.Did) (string, error) {
var mapDID map[string]interface{}
json.Unmarshal([]byte(jsonDID), &mapDID)

// VerKey changes
formatedVerificationMethod, err := ds.prepareJWKPubkey(didDoc)
if err != nil {
return "", err
}

mapDID[verificationMethod] = formatedVerificationMethod

// Context changes
if val, ok := mapDID[didContext]; ok {
mapDID["@"+didContext] = val
delete(mapDID, didContext)
}

result, err := json.Marshal(mapDID)
if err != nil {
return "", err
}
return string(result), nil
}

func (DIDDocService) GetResolutionDIDMetadata(contentType string, errorType string) types.ResolutionMetadata {
return types.ResolutionMetadata{}
}

func (DIDDocService) GetDID(contentType string, errorType string) cheqd.Did {
return cheqd.Did{}
}

func (DIDDocService) GetDIDMetadata(contentType string, errorType string) cheqd.Metadata {
return cheqd.Metadata{}
}

func (DIDDocService) GetDIDFragment(DIDDoc cheqd.Did) string {
//TODO: implement for dereferencing
return ""
}

Expand Down
3 changes: 0 additions & 3 deletions services/diddoc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (

cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
"github.com/stretchr/testify/require"
// jsonpb Marshaller is deprecated, but is needed because there's only one way to proto
// marshal in combination with our proto generator version
//nolint
)

func TestMarshallDID(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func openGRPCConnection(addr string) (conn *grpc.ClientConn, err error) {
}
// TODO: move to application setup
// TODO: move timeouts to a config
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn, err = grpc.DialContext(ctx, addr, opts...)
Expand Down
7 changes: 5 additions & 2 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (rs RequestService) ProcessDIDRequest(did string, params map[string]string)
metadata, err3 := rs.didDocService.MarshallProto(&didResolution.Metadata)

if err1 != nil || err2 != nil || err3 != nil {
resolutionMetadataProto := types.NewResolutionMetadata(params["Accept"],
resolutionMetadataProto := types.NewResolutionMetadata(did, params["Accept"],
types.ResolutionRepresentationNotSupported)
resolutionMetadataJson, _ := json.Marshal(resolutionMetadataProto)
return createJsonResolution("null", "null", string(resolutionMetadataJson)),
Expand All @@ -47,11 +47,14 @@ func (rs RequestService) ProcessDIDRequest(did string, params map[string]string)
// https://w3c-ccg.github.io/did-resolution/#resolving
func (rs RequestService) resolve(did string, resolutionOptions types.ResolutionOption) types.DidResolution {
didDoc, metadata, err := rs.ledgerService.QueryDIDDoc(did)
didResolutionMetadata := types.NewResolutionMetadata(resolutionOptions.Accept, "")
didResolutionMetadata := types.NewResolutionMetadata(did, resolutionOptions.Accept, "")
if err != nil {
didResolutionMetadata.ResolutionError = types.ResolutionNotFound
return types.DidResolution{ResolutionMetadata: didResolutionMetadata}
}
if didResolutionMetadata.ContentType == types.ResolutionDIDJSONLDType {
didDoc.Context = append(didDoc.Context, types.DIDSchemaJSONLD)
}
return types.DidResolution{didDoc, metadata, didResolutionMetadata}
}

Expand Down
36 changes: 27 additions & 9 deletions types/resolution_metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"strings"
"time"

cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
Expand All @@ -13,18 +14,30 @@ const (
)

const (
ResolutionJSONType = "application/json"
ResolutionJSONLDType = "application/ld+json"
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"`
}

type ResolutionMetadata struct {
ContentType string `json:"contentType,omitempty"`
ResolutionError string `json:"error,omitempty"`
Retrieved string `json:"retrieved,omitempty"`
ContentType string `json:"contentType,omitempty"`
ResolutionError string `json:"error,omitempty"`
Retrieved string `json:"retrieved,omitempty"`
DidProperties DidProperties `json:"did,omitempty"`
}

type DidProperties struct {
DidString string `json:"didString,omitempty"`
MethodSpecificId string `json:"methodSpecificId,omitempty"`
Method string `json:"method,omitempty"`
}

type DidResolution struct {
Expand All @@ -33,10 +46,15 @@ type DidResolution struct {
ResolutionMetadata ResolutionMetadata `json:"didResolutionMetadata,omitempty"`
}

func NewResolutionMetadata(contentType string, resolutionError string) ResolutionMetadata {
func NewResolutionMetadata(did string, contentType string, resolutionError string) ResolutionMetadata {
return ResolutionMetadata{
contentType,
resolutionError,
time.Now().UTC().Format(time.RFC3339),
ContentType: contentType,
ResolutionError: resolutionError,
Retrieved: time.Now().UTC().Format(time.RFC3339),
DidProperties: DidProperties{
DidString: did,
MethodSpecificId: strings.SplitN(did, ":", 4)[3],
Method: "cheqd",
},
}
}

0 comments on commit 57eea48

Please sign in to comment.