Skip to content

Commit

Permalink
Merge pull request hyperledger-archives#1005 from fqutishat/998
Browse files Browse the repository at this point in the history
feat: Add SendRequest in route service
  • Loading branch information
troyronda authored Dec 20, 2019
2 parents 9fd4dad + 97c5f01 commit 98b4760
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 201 deletions.
51 changes: 51 additions & 0 deletions pkg/didcomm/protocol/route/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,25 @@ import (
"errors"
"fmt"

"github.com/google/uuid"

"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/dispatcher"
"github.com/hyperledger/aries-framework-go/pkg/doc/did"
vdriapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdri"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/pkg/storage"
)

var logger = log.New("aries-framework/did-exchange/service")

// constants for did
const (
ed25519KeyType = "Ed25519VerificationKey2018"
didCommServiceType = "did-communication"
)

// constants for route coordination spec types
const (
// Coordination route coordination protocol
Expand Down Expand Up @@ -66,6 +76,7 @@ type provider interface {
StorageProvider() storage.Provider
InboundTransportEndpoint() string
KMS() kms.KeyManager
VDRIRegistry() vdriapi.Registry
}

// Service for Route Coordination protocol.
Expand All @@ -77,6 +88,7 @@ type Service struct {
outbound dispatcher.Outbound
endpoint string
kms kms.KeyManager
vdr vdriapi.Registry
}

// New return route coordination service.
Expand All @@ -91,6 +103,7 @@ func New(prov provider) (*Service, error) {
outbound: prov.OutboundDispatcher(),
endpoint: prov.InboundTransportEndpoint(),
kms: prov.KMS(),
vdr: prov.VDRIRegistry(),
}, nil
}

Expand Down Expand Up @@ -130,6 +143,44 @@ func (s *Service) HandleOutbound(msg *service.DIDCommMsg, destination *service.D
return errors.New("not implemented")
}

// SendRequest send route request
func (s *Service) SendRequest(myDID, theirDID string) (string, error) {
// TODO refactor after https://github.com/hyperledger/aries-framework-go/issues/725 is merged
// Get sender ver keys from sender did
myDIDDoc, err := s.vdr.Resolve(myDID)
if err != nil {
return "", fmt.Errorf("failed to resolve did: %w", err)
}

senderVerKeys, ok := did.LookupRecipientKeys(myDIDDoc, didCommServiceType, ed25519KeyType)
if !ok {
return "", fmt.Errorf("sender verification keys not found")
}

// Get destination from receiver did
theirDIDDoc, err := s.vdr.Resolve(theirDID)
if err != nil {
return "", fmt.Errorf("failed to resolve did: %w", err)
}

destination, err := service.CreateDestination(theirDIDDoc)
if err != nil {
return "", fmt.Errorf("prepare destination from their did: %w", err)
}

// send the request
req := &Request{
ID: uuid.New().String(),
Type: RequestMsgType,
}

if err := s.outbound.Send(req, senderVerKeys[0], destination); err != nil {
return "", fmt.Errorf("failed to send route request: %w", err)
}

return req.ID, nil
}

// Accept checks whether the service can handle the message type.
func (s *Service) Accept(msgType string) bool {
switch msgType {
Expand Down
Loading

0 comments on commit 98b4760

Please sign in to comment.