Skip to content

Commit

Permalink
Add Resource dereferencing
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jun 27, 2022
1 parent c744bf2 commit 9bb5beb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
54 changes: 44 additions & 10 deletions services/ledger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"google.golang.org/grpc/credentials"

cheqd "github.com/cheqd/cheqd-node/x/cheqd/types"
resource "github.com/cheqd/cheqd-node/x/resource/types"
cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
Expand Down Expand Up @@ -43,21 +44,12 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
return cheqd.Did{}, cheqd.Metadata{}, false, fmt.Errorf("namespace not supported: %s", namespace)
}

log.Info().Msgf("Connecting to the ledger: %s", serverAddr)
conn, err := ls.openGRPCConnection(serverAddr)
conn, err := ls.openConnection(serverAddr);
if err != nil {
log.Error().Err(err).Msg("QueryDIDDoc: failed connection")
return cheqd.Did{}, cheqd.Metadata{}, false, err
}

defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}(conn)

log.Info().Msgf("Querying did doc: %s", did)
client := cheqd.NewQueryClient(conn)
didDocResponse, err := client.Did(context.Background(), &cheqd.QueryGetDidRequest{Id: did})
Expand All @@ -68,6 +60,48 @@ func (ls LedgerService) QueryDIDDoc(did string) (cheqd.Did, cheqd.Metadata, bool
return *didDocResponse.Did, *didDocResponse.Metadata, true, err
}

func (ls LedgerService) QueryResource(collectionDid string, resourceId string) (resource.Resource, bool, error) {
collectionId, namespace, _, _ := cheqdUtils.TrySplitDID(collectionDid)
serverAddr, namespaceFound := ls.ledgers[namespace]
if !namespaceFound {
return cheqd.Did{}, false, fmt.Errorf("namespace not supported: %s", namespace)
}

conn, err := ls.openConnection(serverAddr);
if err != nil {
log.Error().Err(err).Msg("QueryResource: failed connection")
return resource.Resource{}, false, err
}

log.Info().Msgf("Querying did resource: %s, %s", collectionDid, resourceId)

client := resource.NewQueryClient(conn)
resourceResponse, err := client.Resource(context.Background(), &resource.QueryGetResourceRequest{CollectionId: collectionId, Id: resourceId})
if err != nil {
return resource.Resource{}, false, nil
}

return *resourceResponse.Resource, true, err
}

func (ls LedgerService) openConnection(serverAddr string) (*grpc.ClientConn, error) {
log.Info().Msgf("Connecting to the ledger: %s", serverAddr)
conn, err := ls.openGRPCConnection(serverAddr)
if err != nil {
return nil, err
}

defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Panic().Err(err).Msg("QueryDIDDoc: failed to close connection")
panic(err)
}
}(conn)

return conn, nil
}

func (ls *LedgerService) RegisterLedger(namespace string, url string) error {
if namespace == "" {
err := errors.New("namespace cannot be empty")
Expand Down
23 changes: 19 additions & 4 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

cheqdUtils "github.com/cheqd/cheqd-node/x/cheqd/utils"
"github.com/cheqd/did-resolver/types"
"github.com/cheqd/did-resolver/utils"
"google.golang.org/protobuf/runtime/protoiface"
)

type RequestService struct {
Expand Down Expand Up @@ -135,7 +137,7 @@ func (rs RequestService) Dereference(didUrl string, dereferenceOptions types.Der
log.Info().Msgf("did: %s, path: %s, query: %s, fragmentId: %s", did, path, query, fragmentId)

// TODO: implement
if path != "" || query != "" {
if query != "" {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotSupported)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}
Expand All @@ -153,14 +155,27 @@ func (rs RequestService) Dereference(didUrl string, dereferenceOptions types.Der
if dereferencingMetadata.ResolutionError != "" {
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}
var protoiface.MessageV1 contentStream
var protoiface.MessageV1 contentMetadata
if path != "" {
resourceId := utils.GetResourceId(path)
// Only `resource` path is supported
if resourceId == "" {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotSupported)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}
contentStream = rs.didDocService.GetResource(collectionId, resourceId)

} else if fragmentId != "" {
contentStream = rs.didDocService.GetDIDFragment(fragmentId, didResolution.Did)
contentMetadata = didResolution.Metadata
}

contentStream := rs.didDocService.GetDIDFragment(fragmentId, didResolution.Did)
if contentStream == nil {
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingFragmentNotFound)
dereferencingMetadata := types.NewDereferencingMetadata(didUrl, dereferenceOptions.Accept, types.DereferencingNotFound)
return types.DidDereferencing{DereferencingMetadata: dereferencingMetadata}, nil
}

contentMetadata := didResolution.Metadata
return types.DidDereferencing{ContentStream: contentStream, Metadata: contentMetadata, DereferencingMetadata: dereferencingMetadata}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const (
)

const (
DereferencingInvalidDIDUrl ErrorType = "invalidDidUrl"
DereferencingFragmentNotFound ErrorType = "FragmentNotFound"
DereferencingNotSupported ErrorType = "NotSupportedUrl"
DereferencingInvalidDIDUrl ErrorType = "invalidDidUrl"
DereferencingNotFound ErrorType = "NotFound"
DereferencingNotSupported ErrorType = "NotSupportedUrl"
)

type ContentType string
Expand Down

0 comments on commit 9bb5beb

Please sign in to comment.