Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1962 from soluchok/issue-1945
Browse files Browse the repository at this point in the history
feat: Present Proof - added correlation id
  • Loading branch information
George Aristy authored Jun 27, 2020
2 parents 70d2484 + 8509b7f commit cbafe55
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 587 deletions.
16 changes: 10 additions & 6 deletions pkg/controller/command/presentproof/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ func (c *Command) SendRequestPresentation(rw io.Writer, req io.Reader) command.E
return command.NewValidationError(InvalidRequestErrorCode, errors.New(errEmptyRequestPresentation))
}

if _, err := c.client.SendRequestPresentation(args.RequestPresentation, args.MyDID, args.TheirDID); err != nil {
piid, err := c.client.SendRequestPresentation(args.RequestPresentation, args.MyDID, args.TheirDID)
if err != nil {
logutil.LogError(logger, commandName, sendRequestPresentation, err.Error())
return command.NewExecuteError(SendRequestPresentationErrorCode, err)
}

command.WriteNillableResponse(rw, &SendRequestPresentationResponse{}, logger)
command.WriteNillableResponse(rw, &SendRequestPresentationResponse{
PIID: piid,
}, logger)

logutil.LogDebug(logger, commandName, sendRequestPresentation, successString)

Expand Down Expand Up @@ -210,14 +213,15 @@ func (c *Command) SendProposePresentation(rw io.Writer, req io.Reader) command.E
return command.NewValidationError(InvalidRequestErrorCode, errors.New(errEmptyProposePresentation))
}

// TODO correlate outbound request-presentation & propose-presentation to their responses
// https://github.com/hyperledger/aries-framework-go/issues/1945
if _, err := c.client.SendProposePresentation(args.ProposePresentation, args.MyDID, args.TheirDID); err != nil {
piid, err := c.client.SendProposePresentation(args.ProposePresentation, args.MyDID, args.TheirDID)
if err != nil {
logutil.LogError(logger, commandName, sendProposePresentation, err.Error())
return command.NewExecuteError(SendProposePresentationErrorCode, err)
}

command.WriteNillableResponse(rw, &SendProposePresentationResponse{}, logger)
command.WriteNillableResponse(rw, &SendProposePresentationResponse{
PIID: piid,
}, logger)

logutil.LogDebug(logger, commandName, sendProposePresentation, successString)

Expand Down
10 changes: 8 additions & 2 deletions pkg/controller/command/presentproof/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ type SendProposePresentationArgs struct {
//
// Represents a SendProposePresentation response message
//
type SendProposePresentationResponse struct{}
type SendProposePresentationResponse struct {
// PIID Protocol instance ID. It can be used as a correlation ID
PIID string `json:"piid"`
}

// SendRequestPresentationArgs model
//
Expand All @@ -173,4 +176,7 @@ type SendRequestPresentationArgs struct {
//
// Represents a SendRequestPresentation response message
//
type SendRequestPresentationResponse struct{}
type SendRequestPresentationResponse struct {
// PIID Protocol instance ID. It can be used as a correlation ID
PIID string `json:"piid"`
}
10 changes: 8 additions & 2 deletions pkg/controller/rest/presentproof/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ type presentProofSendRequestPresentationRequest struct { // nolint: unused,deadc
// swagger:response presentProofSendRequestPresentationResponse
type presentProofSendRequestPresentationResponse struct { // nolint: unused,deadcode
// in: body
Body struct{}
Body struct {
// PIID Protocol instance ID. It can be used as a correlation ID
PIID string `json:"piid"`
}
}

// presentProofSendProposePresentationRequest model
Expand Down Expand Up @@ -84,7 +87,10 @@ type presentProofSendProposePresentationRequest struct { // nolint: unused,deadc
// swagger:response presentProofSendProposePresentationResponse
type presentProofSendProposePresentationResponse struct { // nolint: unused,deadcode
// in: body
Body struct{}
Body struct {
// PIID Protocol instance ID. It can be used as a correlation ID
PIID string `json:"piid"`
}
}

// presentProofAcceptRequestPresentationRequest model
Expand Down
74 changes: 74 additions & 0 deletions pkg/didcomm/protocol/presentproof/properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package presentproof

import "errors"

const (
myDIDPropKey = "myDID"
theirDIDPropKey = "theirDID"
piidPropKey = "piid"
errorPropKey = "error"
)

type eventProps struct {
myDID string
theirDID string
piid string
err error
}

func newEventProps(md *metaData) *eventProps {
return &eventProps{
myDID: md.MyDID,
theirDID: md.TheirDID,
piid: md.PIID,
err: md.err,
}
}

func (e *eventProps) MyDID() string {
return e.myDID
}

func (e *eventProps) TheirDID() string {
return e.theirDID
}

func (e *eventProps) PIID() string {
return e.piid
}

func (e eventProps) Err() error {
if errors.As(e.err, &customError{}) {
return nil
}

return e.err
}

// All implements EventProperties interface
func (e eventProps) All() map[string]interface{} {
all := map[string]interface{}{}
if e.myDID != "" {
all[myDIDPropKey] = e.myDID
}

if e.theirDID != "" {
all[theirDIDPropKey] = e.theirDID
}

if e.piid != "" {
all[piidPropKey] = e.piid
}

if e.Err() != nil {
all[errorPropKey] = e.Err()
}

return all
}
41 changes: 41 additions & 0 deletions pkg/didcomm/protocol/presentproof/properties_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package presentproof

import (
"errors"
"testing"

"github.com/stretchr/testify/require"
)

func TestEventProps_All(t *testing.T) {
md := &metaData{}
md.MyDID = "MyDID"
md.TheirDID = "TheirDID"
md.PIID = "PIID"
md.err = errors.New("error")

props := newEventProps(md)

require.Equal(t, md.MyDID, props.MyDID())
require.Equal(t, md.TheirDID, props.TheirDID())
require.Equal(t, md.PIID, props.PIID())
require.Equal(t, md.err, props.Err())
require.Equal(t, 4, len(props.All()))

md.err = customError{errors.New("error")}
md.MyDID = ""

props = newEventProps(md)

require.Equal(t, md.MyDID, props.MyDID())
require.Equal(t, md.TheirDID, props.TheirDID())
require.Equal(t, md.PIID, props.PIID())
require.Equal(t, nil, props.Err())
require.Equal(t, 2, len(props.All()))
}
50 changes: 11 additions & 39 deletions pkg/didcomm/protocol/presentproof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,27 +568,14 @@ func (s *Service) newDIDCommActionMsg(md *metaData) service.DIDCommAction {
md.err = customError{error: cErr}
s.processCallback(md)
},
Properties: &eventProps{
myDID: md.MyDID,
theirDID: md.TheirDID,
},
Properties: newEventProps(md),
}
}

func (s *Service) execute(next state, md *metaData) (state, stateAction, error) {
s.sendMsgEvents(&service.StateMsg{
ProtocolName: Name,
Type: service.PreState,
Msg: md.msgClone,
StateID: next.Name(),
})
s.sendMsgEvents(md, next.Name(), service.PreState)

defer s.sendMsgEvents(&service.StateMsg{
ProtocolName: Name,
Type: service.PostState,
Msg: md.msgClone,
StateID: next.Name(),
})
defer s.sendMsgEvents(md, next.Name(), service.PostState)

if err := s.middleware.Handle(md); err != nil {
return nil, nil, fmt.Errorf("middleware: %w", err)
Expand All @@ -598,10 +585,16 @@ func (s *Service) execute(next state, md *metaData) (state, stateAction, error)
}

// sendMsgEvents triggers the message events.
func (s *Service) sendMsgEvents(msg *service.StateMsg) {
func (s *Service) sendMsgEvents(md *metaData, stateID string, stateType service.StateMsgType) {
// trigger the message events
for _, handler := range s.MsgEvents() {
handler <- *msg
handler <- service.StateMsg{
ProtocolName: Name,
Type: stateType,
Msg: md.msgClone,
StateID: stateID,
Properties: newEventProps(md),
}
}
}

Expand All @@ -620,24 +613,3 @@ func (s *Service) Accept(msgType string) bool {

return false
}

type eventProps struct {
myDID string
theirDID string
}

func (e *eventProps) MyDID() string {
return e.myDID
}

func (e *eventProps) TheirDID() string {
return e.theirDID
}

// All implements EventProperties interface
func (e *eventProps) All() map[string]interface{} {
return map[string]interface{}{
"myDID": e.MyDID(),
"theirDID": e.TheirDID(),
}
}
1 change: 1 addition & 0 deletions test/bdd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/piprate/json-gold v0.3.0
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/trustbloc/sidetree-core-go v0.1.3
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
google.golang.org/genproto v0.0.0-20200211111953-2dc5924e3898 // indirect
google.golang.org/grpc v1.27.1 // indirect
Expand Down
Loading

0 comments on commit cbafe55

Please sign in to comment.