diff --git a/pkg/client/didexchange/client.go b/pkg/client/didexchange/client.go index f1f870c4f..2e9fb3fd0 100644 --- a/pkg/client/didexchange/client.go +++ b/pkg/client/didexchange/client.go @@ -157,7 +157,7 @@ func (c *Client) HandleInvitation(invitation *Invitation) (string, error) { return "", fmt.Errorf("failed to create DIDCommMsg: %w", err) } - connectionID, err := c.didexchangeSvc.HandleInbound(msg) + connectionID, err := c.didexchangeSvc.HandleInbound(msg, "", "") if err != nil { return "", fmt.Errorf("failed from didexchange service handle: %w", err) } diff --git a/pkg/client/didexchange/client_test.go b/pkg/client/didexchange/client_test.go index febc07f44..13029842c 100644 --- a/pkg/client/didexchange/client_test.go +++ b/pkg/client/didexchange/client_test.go @@ -589,7 +589,7 @@ func TestServiceEvents(t *testing.T) { msg, err := service.NewDIDCommMsg(request) require.NoError(t, err) - _, err = didExSvc.HandleInbound(msg) + _, err = didExSvc.HandleInbound(msg, "", "") require.NoError(t, err) select { @@ -679,7 +679,7 @@ func TestAcceptExchangeRequest(t *testing.T) { msg, err := service.NewDIDCommMsg(request) require.NoError(t, err) - _, err = didExSvc.HandleInbound(msg) + _, err = didExSvc.HandleInbound(msg, "", "") require.NoError(t, err) select { @@ -760,7 +760,7 @@ func TestAcceptInvitation(t *testing.T) { msg, svcErr := service.NewDIDCommMsg(invitation) require.NoError(t, svcErr) - _, err = didExSvc.HandleInbound(msg) + _, err = didExSvc.HandleInbound(msg, "", "") require.NoError(t, err) select { diff --git a/pkg/client/introduce/client.go b/pkg/client/introduce/client.go index 272663523..f6dfd095a 100644 --- a/pkg/client/introduce/client.go +++ b/pkg/client/introduce/client.go @@ -107,12 +107,12 @@ func (c *Client) SendProposalWithInvitation(inv *didexchange.Invitation, recipie // SendRequest sends a request // sending a request means that introducee is willing to share its invitation -func (c *Client) SendRequest(dest *service.Destination) error { +func (c *Client) SendRequest(myDID, theirDID string) error { return c.handleOutbound(&introduce.Request{ Type: introduce.RequestMsgType, ID: c.newUUID(), }, InvitationEnvelope{ - Recps: []*introduce.Recipient{{Destination: dest}}, + Recps: []*introduce.Recipient{{MyDID: myDID, TheirDID: theirDID}}, }) } @@ -164,7 +164,7 @@ func (c *Client) handleOutbound(msg interface{}, o InvitationEnvelope) error { return err } - return c.service.HandleOutbound(didMsg, o.Recps[0].Destination) + return c.service.HandleOutbound(didMsg, o.Recps[0].MyDID, o.Recps[0].MyDID) } // InvitationEnvelope keeps the information needed for sending a proposal diff --git a/pkg/client/introduce/client_test.go b/pkg/client/introduce/client_test.go index f7038a54d..718a3f73d 100644 --- a/pkg/client/introduce/client_test.go +++ b/pkg/client/introduce/client_test.go @@ -103,12 +103,12 @@ func TestClient_SendProposal(t *testing.T) { require.NotNil(t, svc) DIDComm := serviceMocks.NewMockDIDComm(ctrl) - DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any()).Return(nil) + DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) opts := InvitationEnvelope{ Recps: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: "service/endpoint1"}}, - {Destination: &service.Destination{ServiceEndpoint: "service/endpoint2"}}, + {MyDID: "My_DID1", TheirDID: "THEIR_DID1"}, + {MyDID: "My_DID2", TheirDID: "THEIR_DID2"}, }, } store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil) @@ -148,14 +148,14 @@ func TestClient_SendProposalWithInvitation(t *testing.T) { require.NotNil(t, svc) DIDComm := serviceMocks.NewMockDIDComm(ctrl) - DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any()).Return(nil) + DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) opts := InvitationEnvelope{ Inv: &didexchange.Invitation{ ID: UUID, }, Recps: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: "service/endpoint"}}, + {MyDID: "My_DID", TheirDID: "THEIR_DID"}, }, } store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil) @@ -197,7 +197,7 @@ func TestClient_HandleRequest(t *testing.T) { opts := InvitationEnvelope{ Recps: []*introduce.Recipient{ {To: &introduce.To{Name: "Carol"}}, - {Destination: &service.Destination{ServiceEndpoint: "service/endpoint"}}, + {MyDID: "My_DID2", TheirDID: "THEIR_DID2"}, }, } store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil) @@ -294,8 +294,8 @@ func TestClient_InvitationEnvelope(t *testing.T) { ID: UUID, }, Recps: []*introduce.Recipient{ - {To: &introduce.To{Name: "Carol"}, Destination: &service.Destination{ServiceEndpoint: "service/endpoint1"}}, - {Destination: &service.Destination{ServiceEndpoint: "service/endpoint1"}}, + {To: &introduce.To{Name: "Carol"}, MyDID: "My_DID1", TheirDID: "THEIR_DID1"}, + {MyDID: "My_DID2", TheirDID: "THEIR_DID2"}, }, } store.EXPECT().Get(invitationEnvelopePrefix+UUID).Return(toBytes(t, opts), nil) @@ -355,11 +355,11 @@ func TestClient_SendRequest(t *testing.T) { require.NotNil(t, svc) DIDComm := serviceMocks.NewMockDIDComm(ctrl) - DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any()).Return(nil) + DIDComm.EXPECT().HandleOutbound(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) opts := InvitationEnvelope{ Recps: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: "service/endpoint"}}, + {MyDID: "My_DID", TheirDID: "THEIR_DID"}, }, } store.EXPECT().Put(invitationEnvelopePrefix+UUID, toBytes(t, opts)).Return(nil) @@ -373,10 +373,10 @@ func TestClient_SendRequest(t *testing.T) { require.NoError(t, err) client.newUUID = func() string { return UUID } - require.NoError(t, client.SendRequest(opts.Recps[0].Destination)) + require.NoError(t, client.SendRequest(opts.Recps[0].MyDID, opts.Recps[0].TheirDID)) // with error - require.EqualError(t, client.SendRequest(opts.Recps[0].Destination), "test error") + require.EqualError(t, client.SendRequest(opts.Recps[0].MyDID, opts.Recps[0].TheirDID), "test error") } func toBytes(t *testing.T, v interface{}) []byte { diff --git a/pkg/didcomm/common/service/service.go b/pkg/didcomm/common/service/service.go index f729400e1..b23d0a4be 100644 --- a/pkg/didcomm/common/service/service.go +++ b/pkg/didcomm/common/service/service.go @@ -16,9 +16,9 @@ import ( // Handler provides protocol service handle api. type Handler interface { // HandleInbound handles inbound messages. - HandleInbound(msg *DIDCommMsg) (string, error) + HandleInbound(msg *DIDCommMsg, myDID string, theirDID string) (string, error) // HandleOutbound handles outbound messages. - HandleOutbound(msg *DIDCommMsg, dest *Destination) error + HandleOutbound(msg *DIDCommMsg, myDID, theirDID string) error } // DIDComm defines service APIs. diff --git a/pkg/didcomm/dispatcher/api.go b/pkg/didcomm/dispatcher/api.go index 99ca95b14..004b5f8df 100644 --- a/pkg/didcomm/dispatcher/api.go +++ b/pkg/didcomm/dispatcher/api.go @@ -20,4 +20,5 @@ type Service interface { // Outbound interface type Outbound interface { Send(interface{}, string, *service.Destination) error + SendToDID(msg interface{}, myDID, theirDID string) error } diff --git a/pkg/didcomm/dispatcher/outbound.go b/pkg/didcomm/dispatcher/outbound.go index 8906e015b..e18e64cbf 100644 --- a/pkg/didcomm/dispatcher/outbound.go +++ b/pkg/didcomm/dispatcher/outbound.go @@ -40,6 +40,11 @@ func NewOutbound(prov provider) *OutboundDispatcher { } } +// SendToDID msg +func (o *OutboundDispatcher) SendToDID(msg interface{}, myDID, theirDID string) error { + return nil +} + // Send msg func (o *OutboundDispatcher) Send(msg interface{}, senderVerKey string, des *service.Destination) error { for _, v := range o.outboundTransports { diff --git a/pkg/didcomm/protocol/didexchange/service.go b/pkg/didcomm/protocol/didexchange/service.go index 78420cffc..05cd0d418 100644 --- a/pkg/didcomm/protocol/didexchange/service.go +++ b/pkg/didcomm/protocol/didexchange/service.go @@ -126,7 +126,7 @@ func New(prov provider) (*Service, error) { } // HandleInbound handles inbound didexchange messages. -func (s *Service) HandleInbound(msg *service.DIDCommMsg) (string, error) { +func (s *Service) HandleInbound(msg *service.DIDCommMsg, myDID, theirDID string) (string, error) { logger.Debugf("receive inbound message : %s", msg.Payload) // fetch the thread id @@ -181,7 +181,7 @@ func (s *Service) Accept(msgType string) bool { } // HandleOutbound handles outbound didexchange messages. -func (s *Service) HandleOutbound(msg *service.DIDCommMsg, destination *service.Destination) error { +func (s *Service) HandleOutbound(msg *service.DIDCommMsg, myDID, theirDID string) error { return errors.New("not implemented") } diff --git a/pkg/didcomm/protocol/didexchange/service_test.go b/pkg/didcomm/protocol/didexchange/service_test.go index ea62e3b90..0d2f4f0f7 100644 --- a/pkg/didcomm/protocol/didexchange/service_test.go +++ b/pkg/didcomm/protocol/didexchange/service_test.go @@ -128,7 +128,7 @@ func TestService_Handle_Inviter(t *testing.T) { require.NoError(t, err) msg, err := service.NewDIDCommMsg(payloadBytes) require.NoError(t, err) - _, err = s.HandleInbound(msg) + _, err = s.HandleInbound(msg, "", "") require.NoError(t, err) select { @@ -150,7 +150,7 @@ func TestService_Handle_Inviter(t *testing.T) { didMsg, err := service.NewDIDCommMsg(payloadBytes) require.NoError(t, err) - _, err = s.HandleInbound(didMsg) + _, err = s.HandleInbound(didMsg, "", "") require.NoError(t, err) select { @@ -251,7 +251,7 @@ func TestService_Handle_Invitee(t *testing.T) { didMsg, err := service.NewDIDCommMsg(payloadBytes) require.NoError(t, err) - _, err = s.HandleInbound(didMsg) + _, err = s.HandleInbound(didMsg, "", "") require.NoError(t, err) var connID string @@ -293,7 +293,7 @@ func TestService_Handle_Invitee(t *testing.T) { didMsg, err = service.NewDIDCommMsg(payloadBytes) require.NoError(t, err) - _, err = s.HandleInbound(didMsg) + _, err = s.HandleInbound(didMsg, "", "") require.NoError(t, err) // Alice automatically sends an ACK to Bob @@ -345,7 +345,7 @@ func TestService_Handle_EdgeCases(t *testing.T) { didMsg, err := service.NewDIDCommMsg(response) require.NoError(t, err) - _, err = s.HandleInbound(didMsg) + _, err = s.HandleInbound(didMsg, "", "") require.Error(t, err) require.Contains(t, err.Error(), "handle inbound - next state : invalid state transition: "+ "null -> responded") @@ -366,7 +366,7 @@ func TestService_Handle_EdgeCases(t *testing.T) { didMsg, err := service.NewDIDCommMsg(requestBytes) require.NoError(t, err) - _, err = svc.HandleInbound(didMsg) + _, err = svc.HandleInbound(didMsg, "", "") require.Error(t, err) require.Equal(t, err.Error(), "threadID not found") }) @@ -381,7 +381,7 @@ func TestService_Handle_EdgeCases(t *testing.T) { transientStore := &mockstorage.MockStore{Store: make(map[string][]byte), ErrPut: errors.New("db error")} svc.connectionStore = NewConnectionRecorder(transientStore, nil) - _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), "")) + _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""), "", "") require.Error(t, err) require.Contains(t, err.Error(), "save connection record") }) @@ -419,7 +419,7 @@ func TestService_Handle_EdgeCases(t *testing.T) { didMsg, err := service.NewDIDCommMsg(requestBytes) require.NoError(t, err) - _, err = svc.HandleInbound(didMsg) + _, err = svc.HandleInbound(didMsg, "", "") require.NoError(t, err) }) } @@ -628,7 +628,7 @@ func TestEventsSuccess(t *testing.T) { didMsg, err := service.NewDIDCommMsg(invite) require.NoError(t, err) - _, err = svc.HandleInbound(didMsg) + _, err = svc.HandleInbound(didMsg, "", "") require.NoError(t, err) select { @@ -665,7 +665,7 @@ func TestContinueWithPublicDID(t *testing.T) { didMsg, err := service.NewDIDCommMsg(invite) require.NoError(t, err) - _, err = svc.HandleInbound(didMsg) + _, err = svc.HandleInbound(didMsg, "", "") require.NoError(t, err) } @@ -722,7 +722,7 @@ func TestEventsUserError(t *testing.T) { err = svc.connectionStore.saveNewConnectionRecord(connRec) require.NoError(t, err) - _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, id, "")) + _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, id, ""), "", "") require.NoError(t, err) select { @@ -749,7 +749,8 @@ func TestEventStoreError(t *testing.T) { } }() - _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), "")) + _, err = svc.HandleInbound( + generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), ""), "", "") require.NoError(t, err) } @@ -804,7 +805,8 @@ func TestServiceErrors(t *testing.T) { return nil, errors.New("error") }} svc.connectionStore = NewConnectionRecorder(mockStore, nil) - _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), "")) + payload := generateRequestMsgPayload(t, &protocol.MockProvider{}, randomString(), "") + _, err = svc.HandleInbound(payload, "", "") require.Error(t, err) require.Contains(t, err.Error(), "cannot fetch state from store") @@ -815,7 +817,7 @@ func TestServiceErrors(t *testing.T) { svc.connectionStore = NewConnectionRecorder(transientStore, nil) - _, err = svc.HandleInbound(msg) + _, err = svc.HandleInbound(msg, "", "") require.Error(t, err) require.Contains(t, err.Error(), "unrecognized msgType: invalid") @@ -838,7 +840,7 @@ func TestHandleOutbound(t *testing.T) { svc, err := New(&protocol.MockProvider{}) require.NoError(t, err) - err = svc.HandleOutbound(&service.DIDCommMsg{}, &service.Destination{}) + err = svc.HandleOutbound(&service.DIDCommMsg{}, "", "") require.Error(t, err) require.Contains(t, err.Error(), "not implemented") } @@ -976,8 +978,9 @@ func TestAcceptExchangeRequest(t *testing.T) { } }() - _, err = svc.HandleInbound(generateRequestMsgPayload(t, - &protocol.MockProvider{StoreProvider: mockstorage.NewMockStoreProvider()}, randomString(), invitation.ID)) + _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{ + StoreProvider: mockstorage.NewMockStoreProvider(), + }, randomString(), invitation.ID), "", "") require.NoError(t, err) select { @@ -1036,8 +1039,9 @@ func TestAcceptExchangeRequestWithPublicDID(t *testing.T) { } }() - _, err = svc.HandleInbound(generateRequestMsgPayload(t, - &protocol.MockProvider{StoreProvider: mockstorage.NewMockStoreProvider()}, randomString(), invitation.ID)) + _, err = svc.HandleInbound(generateRequestMsgPayload(t, &protocol.MockProvider{ + StoreProvider: mockstorage.NewMockStoreProvider(), + }, randomString(), invitation.ID), "", "") require.NoError(t, err) select { @@ -1098,7 +1102,7 @@ func TestAcceptInvitation(t *testing.T) { didMsg, err := service.NewDIDCommMsg(invitationBytes) require.NoError(t, err) - _, err = svc.HandleInbound(didMsg) + _, err = svc.HandleInbound(didMsg, "", "") require.NoError(t, err) select { @@ -1213,7 +1217,7 @@ func TestAcceptInvitationWithPublicDID(t *testing.T) { didMsg, err := service.NewDIDCommMsg(invitationBytes) require.NoError(t, err) - _, err = svc.HandleInbound(didMsg) + _, err = svc.HandleInbound(didMsg, "", "") require.NoError(t, err) select { diff --git a/pkg/didcomm/protocol/introduce/service.go b/pkg/didcomm/protocol/introduce/service.go index 8a4dfe090..80ed3fde1 100644 --- a/pkg/didcomm/protocol/introduce/service.go +++ b/pkg/didcomm/protocol/introduce/service.go @@ -71,10 +71,11 @@ type InvitationEnvelope interface { // Recipient keeps information needed for the service // 'To' field is needed for the proposal message -// 'Destination' field is needed for the entire protocol (e.g report-problem, proposal, ack) +// 'MyDID' and 'TheirDID' fields are needed for sending messages e.g report-problem, proposal, ack etc. type Recipient struct { - To *To - Destination *service.Destination + To *To + MyDID string `json:"my_did,omitempty"` + TheirDID string `json:"their_did,omitempty"` } // metaData type to store data for internal usage @@ -85,6 +86,9 @@ type metaData struct { // keeps a dependency for the protocol injected by Continue() function dependency InvitationEnvelope disapprove bool + inbound bool + myDID string + theirDID string // err is used to determine whether callback was stopped // e.g the user received an action event and executes Stop(err) function // in that case `err` is equal to `err` which was passing to Stop function @@ -191,7 +195,7 @@ func (s *Service) startInternalListener() { case msg := <-s.callbacks: // if no error - do handle or it was disapproved if msg.err == nil || msg.disapprove { - msg.err = s.handle(msg, nil) + msg.err = s.handle(msg) } // no error - continue @@ -201,7 +205,7 @@ func (s *Service) startInternalListener() { msg.StateName = stateNameAbandoning - if err := s.handle(msg, nil); err != nil { + if err := s.handle(msg); err != nil { logger.Errorf("listener handle: %s", err) } case event := <-s.didEvent: @@ -282,13 +286,13 @@ func (s *Service) InvitationReceived(msg service.StateMsg) error { return fmt.Errorf("invitation received new DIDComm msg: %w", err) } - _, err = s.HandleInbound(didMsg) + _, err = s.HandleInbound(didMsg, "", "") return err } // HandleInbound handles inbound message (introduce protocol) -func (s *Service) HandleInbound(msg *service.DIDCommMsg) (string, error) { +func (s *Service) HandleInbound(msg *service.DIDCommMsg, myDID, theirDID string) (string, error) { aEvent := s.ActionEvent() logger.Infof("entered into HandleInbound: %v", msg.Header) @@ -302,6 +306,11 @@ func (s *Service) HandleInbound(msg *service.DIDCommMsg) (string, error) { return "", err } + // sets inbound payload + mData.inbound = true + mData.myDID = myDID + mData.theirDID = theirDID + // trigger action event based on message type for inbound messages if canTriggerActionEvents(msg) { aEvent <- s.newDIDCommActionMsg(mData) @@ -309,11 +318,11 @@ func (s *Service) HandleInbound(msg *service.DIDCommMsg) (string, error) { } // if no action event is triggered, continue the execution - return "", s.handle(mData, nil) + return "", s.handle(mData) } // HandleOutbound handles outbound message (introduce protocol) -func (s *Service) HandleOutbound(msg *service.DIDCommMsg, dest *service.Destination) error { +func (s *Service) HandleOutbound(msg *service.DIDCommMsg, myDID, theirDID string) error { logger.Infof("entered into HandleOutbound: %v", msg.Header) mData, err := s.doHandle(msg, true) @@ -321,7 +330,11 @@ func (s *Service) HandleOutbound(msg *service.DIDCommMsg, dest *service.Destinat return err } - return s.handle(mData, dest) + // sets outbound payload + mData.myDID = myDID + mData.theirDID = theirDID + + return s.handle(mData) } // sendMsgEvents triggers the message events. @@ -516,7 +529,7 @@ func injectInvitation(msg *metaData) error { return nil } -func (s *Service) handle(msg *metaData, dest *service.Destination) error { +func (s *Service) handle(msg *metaData) error { logger.Infof("entered into private handle message: %v ", msg.Msg.Header) current := stateFromName(msg.StateName) @@ -524,7 +537,7 @@ func (s *Service) handle(msg *metaData, dest *service.Destination) error { logger.Infof("next valid state to transition -> %s ", current.Name()) for !isNoOp(current) { - next, err := s.execute(current, msg, dest) + next, err := s.execute(current, msg) if err != nil { return fmt.Errorf("execute: %w", err) } @@ -539,7 +552,7 @@ func (s *Service) handle(msg *metaData, dest *service.Destination) error { return nil } -func (s *Service) execute(next state, msg *metaData, dest *service.Destination) (state, error) { +func (s *Service) execute(next state, msg *metaData) (state, error) { s.sendMsgEvents(&service.StateMsg{ ProtocolName: Introduce, Type: service.PreState, @@ -561,10 +574,10 @@ func (s *Service) execute(next state, msg *metaData, dest *service.Destination) msg.Recipients = msg.dependency.Recipients() } - if dest != nil { - followup, err = next.ExecuteOutbound(s.ctx, msg, dest) - } else { + if msg.inbound { followup, err = next.ExecuteInbound(s.ctx, msg) + } else { + followup, err = next.ExecuteOutbound(s.ctx, msg) } if err != nil { diff --git a/pkg/didcomm/protocol/introduce/service_test.go b/pkg/didcomm/protocol/introduce/service_test.go index bb595e9dc..1f1f93d19 100644 --- a/pkg/didcomm/protocol/introduce/service_test.go +++ b/pkg/didcomm/protocol/introduce/service_test.go @@ -180,7 +180,7 @@ func TestService_HandleOutbound(t *testing.T) { msg, err := service.NewDIDCommMsg([]byte(fmt.Sprintf(`{"@id":"ID","@type":%q}`, introduce.ResponseMsgType))) require.NoError(t, err) const errMsg = "json: cannot unmarshal array into Go value of type introduce.record" - require.EqualError(t, svc.HandleOutbound(msg, nil), errMsg) + require.EqualError(t, svc.HandleOutbound(msg, "", ""), errMsg) }) t.Run("Invalid state", func(t *testing.T) { @@ -211,7 +211,7 @@ func TestService_HandleOutbound(t *testing.T) { require.NoError(t, err) ch := make(chan service.DIDCommAction) require.NoError(t, svc.RegisterActionEvent(ch)) - require.EqualError(t, svc.HandleOutbound(msg, &service.Destination{}), "invalid state transition: noop -> arranging") + require.EqualError(t, svc.HandleOutbound(msg, "", ""), "invalid state transition: noop -> arranging") }) t.Run("Inject invitation error", func(t *testing.T) { @@ -242,7 +242,7 @@ func TestService_HandleOutbound(t *testing.T) { err = svc.HandleOutbound(&service.DIDCommMsg{ Header: &service.Header{ID: "ID", Thread: decorator.Thread{ID: "thID"}, Type: introduce.ResponseMsgType}, Payload: []byte(`[]`), - }, nil) + }, "", "") const errMsg = "inject invitation: json: cannot unmarshal array into Go value of type introduce.Response" @@ -254,7 +254,7 @@ func TestService_HandleOutbound(t *testing.T) { defer ctrl.Finish() store := storageMocks.NewMockStore(ctrl) - store.EXPECT().Get("thID").Return([]byte(`{"state_name":"start","wait_count":2}`), nil) + store.EXPECT().Get("thID").Return([]byte(`{"state_name":"arranging","wait_count":2}`), nil) store.EXPECT().Put("thID", gomock.Any()).Return(errors.New("DB error")) storageProvider := storageMocks.NewMockProvider(ctrl) @@ -274,13 +274,15 @@ func TestService_HandleOutbound(t *testing.T) { defer stop(t, svc) + require.NoError(t, svc.RegisterActionEvent(make(chan service.DIDCommAction, 1))) + // inject invitation error - err = svc.HandleOutbound(&service.DIDCommMsg{ - Header: &service.Header{ID: "ID", Thread: decorator.Thread{ID: "thID"}, Type: introduce.ResponseMsgType}, + _, err = svc.HandleInbound(&service.DIDCommMsg{ + Header: &service.Header{ID: "ID", Thread: decorator.Thread{ID: "thID"}, Type: introduce.AckMsgType}, Payload: []byte(`{}`), - }, nil) + }, "", "") - const errMsg = "failed to persist state arranging: DB error" + const errMsg = "failed to persist state done: DB error" require.EqualError(t, errors.Unwrap(err), errMsg) }) @@ -312,7 +314,7 @@ func TestService_HandleOutbound(t *testing.T) { err = svc.HandleOutbound(&service.DIDCommMsg{ Header: &service.Header{ID: "ID", Thread: decorator.Thread{ID: "thID"}, Type: introduce.ResponseMsgType}, Payload: []byte(`{}`), - }, nil) + }, "", "") const errMsg = "invalid state transition: confirming -> arranging" @@ -342,7 +344,7 @@ func TestService_HandleInbound(t *testing.T) { svc, err := introduce.New(provider) require.NoError(t, err) defer stop(t, svc) - _, err = svc.HandleInbound(&service.DIDCommMsg{}) + _, err = svc.HandleInbound(&service.DIDCommMsg{}, "", "") require.EqualError(t, err, "no clients are registered to handle the message") }) @@ -370,7 +372,7 @@ func TestService_HandleInbound(t *testing.T) { ch := make(chan service.DIDCommAction) require.NoError(t, svc.RegisterActionEvent(ch)) - _, err = svc.HandleInbound(msg) + _, err = svc.HandleInbound(msg, "", "") require.EqualError(t, err, service.ErrThreadIDNotFound.Error()) }) @@ -403,7 +405,7 @@ func TestService_HandleInbound(t *testing.T) { ch := make(chan service.DIDCommAction) require.NoError(t, svc.RegisterActionEvent(ch)) - _, err = svc.HandleInbound(msg) + _, err = svc.HandleInbound(msg, "", "") require.EqualError(t, err, "cannot fetch state from store: thid=ID : test err") }) @@ -435,7 +437,7 @@ func TestService_HandleInbound(t *testing.T) { ch := make(chan service.DIDCommAction) require.NoError(t, svc.RegisterActionEvent(ch)) - _, err = svc.HandleInbound(msg) + _, err = svc.HandleInbound(msg, "", "") require.EqualError(t, err, "invalid state transition: noop -> deciding") }) @@ -466,7 +468,7 @@ func TestService_HandleInbound(t *testing.T) { ch := make(chan service.DIDCommAction) require.NoError(t, svc.RegisterActionEvent(ch)) - _, err = svc.HandleInbound(msg) + _, err = svc.HandleInbound(msg, "", "") require.EqualError(t, err, "unrecognized msgType: unknown") }) } @@ -512,15 +514,6 @@ func TestService_Proposal(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Bob} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -531,8 +524,8 @@ func TestService_Proposal(t *testing.T) { transportKey: Alice, transport: transport, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Bob}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {MyDID: Alice, TheirDID: Bob}, + {MyDID: Alice, TheirDID: Carol, To: &introduce.To{Name: Bob}}, }, }) @@ -569,7 +562,7 @@ func TestService_Proposal(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {MyDID: Alice, TheirDID: Bob, To: &introduce.To{Name: Carol}}, }, startWithProposal: true, }) @@ -584,6 +577,9 @@ func TestService_Proposal(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) // introducee side Carol @@ -596,6 +592,9 @@ func TestService_Proposal(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -610,15 +609,6 @@ func TestService_SkipProposal(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: "Public Invitation"} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -630,7 +620,7 @@ func TestService_SkipProposal(t *testing.T) { transportKey: Alice, transport: transport, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Bob}}, + {MyDID: Alice, TheirDID: Bob}, }, }) @@ -659,7 +649,7 @@ func TestService_SkipProposal(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {MyDID: Alice, TheirDID: Bob, To: &introduce.To{Name: Carol}}, }, skipProposal: true, startWithProposal: true, @@ -675,6 +665,9 @@ func TestService_SkipProposal(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) wg.Wait() @@ -692,15 +685,6 @@ func TestService_ProposalUnusual(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Carol} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -711,8 +695,8 @@ func TestService_ProposalUnusual(t *testing.T) { transportKey: Alice, transport: transport, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Bob}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {MyDID: Alice, TheirDID: Bob}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -749,7 +733,7 @@ func TestService_ProposalUnusual(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, startWithProposal: true, }) @@ -764,6 +748,9 @@ func TestService_ProposalUnusual(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) // introducee side Carol @@ -776,6 +763,9 @@ func TestService_ProposalUnusual(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -791,22 +781,6 @@ func TestService_SkipProposalWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: "Public Invitation"} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -846,6 +820,9 @@ func TestService_SkipProposalWithRequest(t *testing.T) { transport: transport, dependency: aliceDep, transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, skipProposal: true, }) @@ -858,7 +835,7 @@ func TestService_SkipProposalWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -880,23 +857,6 @@ func TestService_ProposalWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Bob} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -908,7 +868,7 @@ func TestService_ProposalWithRequest(t *testing.T) { transport: transport, recipients: []*introduce.Recipient{ {To: &introduce.To{Name: Carol}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -944,6 +904,9 @@ func TestService_ProposalWithRequest(t *testing.T) { transport: transport, dependency: aliceDep, transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, }) // introducee side Bob @@ -955,7 +918,7 @@ func TestService_ProposalWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -971,6 +934,9 @@ func TestService_ProposalWithRequest(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -989,23 +955,6 @@ func TestService_ProposalUnusualWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Carol} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1017,7 +966,7 @@ func TestService_ProposalUnusualWithRequest(t *testing.T) { transport: transport, recipients: []*introduce.Recipient{ {To: &introduce.To{Name: Carol}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -1053,6 +1002,9 @@ func TestService_ProposalUnusualWithRequest(t *testing.T) { transport: transport, dependency: aliceDep, transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, }) // introducee side Bob @@ -1064,7 +1016,7 @@ func TestService_ProposalUnusualWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -1080,6 +1032,9 @@ func TestService_ProposalUnusualWithRequest(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -1095,15 +1050,6 @@ func TestService_ProposalUnusualWithRequest(t *testing.T) { func TestService_ProposalNoInvitation(t *testing.T) { var transport = transport() - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1113,8 +1059,8 @@ func TestService_ProposalNoInvitation(t *testing.T) { transportKey: Alice, transport: transport, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Bob}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {MyDID: Alice, TheirDID: Bob}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -1150,7 +1096,7 @@ func TestService_ProposalNoInvitation(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, withSecondResponseError: true, startWithProposal: true, @@ -1166,6 +1112,9 @@ func TestService_ProposalNoInvitation(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) // introducee side Carol @@ -1178,6 +1127,9 @@ func TestService_ProposalNoInvitation(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -1195,23 +1147,6 @@ func TestService_ProposalNoInvitation(t *testing.T) { func TestService_ProposalNoInvitationWithRequest(t *testing.T) { var transport = transport() - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1222,7 +1157,7 @@ func TestService_ProposalNoInvitationWithRequest(t *testing.T) { transport: transport, recipients: []*introduce.Recipient{ {To: &introduce.To{Name: Carol}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -1250,12 +1185,15 @@ func TestService_ProposalNoInvitationWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, withSecondResponseError: true, }) @@ -1268,7 +1206,7 @@ func TestService_ProposalNoInvitationWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -1284,6 +1222,9 @@ func TestService_ProposalNoInvitationWithRequest(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -1297,15 +1238,6 @@ func TestService_ProposalStop(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Bob} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1342,7 +1274,7 @@ func TestService_ProposalStop(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, startWithProposal: true, withResponseError: true, @@ -1350,13 +1282,16 @@ func TestService_ProposalStop(t *testing.T) { // introducee side Bob go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: bob, - transport: transport, - dependency: bobDep, - didEvent: didEventBob, - transportKey: Bob, + t: t, + wg: &wg, + svc: bob, + transport: transport, + dependency: bobDep, + didEvent: didEventBob, + transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, withProposalStop: true, }) @@ -1372,23 +1307,6 @@ func TestService_ProposalStopWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Bob} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1420,12 +1338,15 @@ func TestService_ProposalStopWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, withResponseError: true, }) @@ -1438,7 +1359,7 @@ func TestService_ProposalStopWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -1456,15 +1377,6 @@ func TestService_SkipProposalStop(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: "Public Invitation"} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1502,7 +1414,7 @@ func TestService_SkipProposalStop(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, skipProposal: true, startWithProposal: true, @@ -1511,13 +1423,16 @@ func TestService_SkipProposalStop(t *testing.T) { // introducee side Bob go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: bob, - transport: transport, - dependency: bobDep, - didEvent: didEventBob, - transportKey: Bob, + t: t, + wg: &wg, + svc: bob, + transport: transport, + dependency: bobDep, + didEvent: didEventBob, + transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, withProposalStop: true, }) @@ -1533,22 +1448,6 @@ func TestService_SkipProposalStopWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: "Public Invitation"} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1581,12 +1480,15 @@ func TestService_SkipProposalStopWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, skipProposal: true, withResponseError: true, }) @@ -1600,7 +1502,7 @@ func TestService_SkipProposalStopWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -1621,15 +1523,6 @@ func TestService_ProposalStopUnusual(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Carol} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1640,8 +1533,8 @@ func TestService_ProposalStopUnusual(t *testing.T) { transportKey: Alice, transport: transport, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Bob}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {MyDID: Alice, TheirDID: Bob}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -1678,7 +1571,7 @@ func TestService_ProposalStopUnusual(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, startWithProposal: true, withSecondResponseError: true, @@ -1694,17 +1587,23 @@ func TestService_ProposalStopUnusual(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) // introducee side Carol go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: carol, - transport: transport, - dependency: carolDep, - didEvent: didEventCarol, - transportKey: Carol, + t: t, + wg: &wg, + svc: carol, + transport: transport, + dependency: carolDep, + didEvent: didEventCarol, + transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, withProposalStop: true, }) @@ -1723,23 +1622,6 @@ func TestService_ProposalStopUnusualWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Carol} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1751,7 +1633,7 @@ func TestService_ProposalStopUnusualWithRequest(t *testing.T) { transportKey: Alice, recipients: []*introduce.Recipient{ {To: &introduce.To{Name: Carol}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -1780,12 +1662,15 @@ func TestService_ProposalStopUnusualWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, withSecondResponseError: true, }) @@ -1798,7 +1683,7 @@ func TestService_ProposalStopUnusualWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -1806,13 +1691,16 @@ func TestService_ProposalStopUnusualWithRequest(t *testing.T) { // introducee side Carol go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: carol, - transport: transport, - dependency: carolDep, - didEvent: didEventCarol, - transportKey: Carol, + t: t, + wg: &wg, + svc: carol, + transport: transport, + dependency: carolDep, + didEvent: didEventCarol, + transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, withProposalStop: true, }) @@ -1827,20 +1715,6 @@ func TestService_ProposalIntroducerStopWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Bob} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1869,12 +1743,15 @@ func TestService_ProposalIntroducerStopWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, withRequestStop: true, }) @@ -1887,7 +1764,7 @@ func TestService_ProposalIntroducerStopWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -1905,21 +1782,6 @@ func TestService_ProposalIntroducerStop(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: Bob} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -1956,7 +1818,7 @@ func TestService_ProposalIntroducerStop(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, startWithProposal: true, withResponseStop: true, @@ -1972,6 +1834,9 @@ func TestService_ProposalIntroducerStop(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) wg.Wait() @@ -1987,22 +1852,6 @@ func TestService_SkipProposalIntroducerStopWithRequest(t *testing.T) { inv := &didexchange.Invitation{ID: uuid.New().String(), Label: "Public Invitation"} - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -2035,12 +1884,15 @@ func TestService_SkipProposalIntroducerStopWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, skipProposal: true, withResponseStop: true, }) @@ -2054,7 +1906,7 @@ func TestService_SkipProposalIntroducerStopWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -2073,15 +1925,6 @@ func TestService_SkipProposalIntroducerStopWithRequest(t *testing.T) { func TestService_ProposalIntroducerSecondStop(t *testing.T) { var transport = transport() - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - introduce.GetInboundDestination = func() *service.Destination { - return &service.Destination{ServiceEndpoint: Alice} - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -2091,8 +1934,8 @@ func TestService_ProposalIntroducerSecondStop(t *testing.T) { transportKey: Alice, transport: transport, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Bob}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {MyDID: Alice, TheirDID: Bob}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -2128,7 +1971,7 @@ func TestService_ProposalIntroducerSecondStop(t *testing.T) { dependency: aliceDep, transportKey: Alice, recipients: []*introduce.Recipient{ - {To: &introduce.To{Name: Carol}, Destination: &service.Destination{ServiceEndpoint: Bob}}, + {To: &introduce.To{Name: Carol}, MyDID: Alice, TheirDID: Bob}, }, startWithProposal: true, withSecondResponseStop: true, @@ -2144,6 +1987,9 @@ func TestService_ProposalIntroducerSecondStop(t *testing.T) { dependency: bobDep, didEvent: didEventBob, transportKey: Bob, + recipients: []*introduce.Recipient{ + {MyDID: Bob, TheirDID: Alice}, + }, }) // introducee side Carol @@ -2156,6 +2002,9 @@ func TestService_ProposalIntroducerSecondStop(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -2173,23 +2022,6 @@ func TestService_ProposalIntroducerSecondStop(t *testing.T) { func TestService_ProposalIntroducerSecondStopWithRequest(t *testing.T) { var transport = transport() - getInboundDestinationOriginal := introduce.GetInboundDestination - - defer func() { introduce.GetInboundDestination = getInboundDestinationOriginal }() - - // injection - idx := -1 - destinations := []*service.Destination{ - {ServiceEndpoint: Bob}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Alice}, - {ServiceEndpoint: Bob}, - } - introduce.GetInboundDestination = func() *service.Destination { - idx++ - return destinations[idx] - } - ctrl := gomock.NewController(t) defer ctrl.Finish() @@ -2200,7 +2032,7 @@ func TestService_ProposalIntroducerSecondStopWithRequest(t *testing.T) { transport: transport, recipients: []*introduce.Recipient{ {To: &introduce.To{Name: Carol}}, - {To: &introduce.To{Name: Bob}, Destination: &service.Destination{ServiceEndpoint: Carol}}, + {To: &introduce.To{Name: Bob}, MyDID: Alice, TheirDID: Carol}, }, }) @@ -2228,12 +2060,15 @@ func TestService_ProposalIntroducerSecondStopWithRequest(t *testing.T) { // introducer side Alice go checkAndHandle(&flow{ - t: t, - wg: &wg, - svc: alice, - transport: transport, - dependency: aliceDep, - transportKey: Alice, + t: t, + wg: &wg, + svc: alice, + transport: transport, + dependency: aliceDep, + transportKey: Alice, + recipients: []*introduce.Recipient{ + {MyDID: Alice, TheirDID: Bob}, + }, withSecondResponseStop: true, }) @@ -2246,7 +2081,7 @@ func TestService_ProposalIntroducerSecondStopWithRequest(t *testing.T) { dependency: bobDep, didEvent: didEventBob, recipients: []*introduce.Recipient{ - {Destination: &service.Destination{ServiceEndpoint: Alice}}, + {MyDID: Bob, TheirDID: Alice}, }, transportKey: Bob, startWithRequest: true, @@ -2262,6 +2097,9 @@ func TestService_ProposalIntroducerSecondStopWithRequest(t *testing.T) { dependency: carolDep, didEvent: didEventCarol, transportKey: Carol, + recipients: []*introduce.Recipient{ + {MyDID: Carol, TheirDID: Alice}, + }, }) wg.Wait() @@ -2271,9 +2109,9 @@ func setupIntroducer(f *flow) (*introduce.Service, *introduceMocks.MockInvitatio ctrl := f.controller disp := dispatcherMocks.NewMockOutbound(ctrl) - disp.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()). - Do(func(msg interface{}, _ string, dest *service.Destination) error { - f.transport[dest.ServiceEndpoint] <- msg + disp.EXPECT().SendToDID(gomock.Any(), f.transportKey, gomock.Any()). + Do(func(msg interface{}, _ string, dest string) error { + f.transport[dest] <- msg return nil }).AnyTimes() @@ -2304,9 +2142,9 @@ func setupIntroducee(f *flow) (*introduce.Service, *introduceMocks.MockInvitatio ctrl := f.controller disp := dispatcherMocks.NewMockOutbound(ctrl) - disp.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()). - Do(func(msg interface{}, _ string, dest *service.Destination) error { - f.transport[dest.ServiceEndpoint] <- msg + disp.EXPECT().SendToDID(gomock.Any(), f.transportKey, Alice). + Do(func(msg interface{}, _ string, dest string) error { + f.transport[dest] <- msg return nil }).AnyTimes() @@ -2336,12 +2174,12 @@ func setupIntroducee(f *flow) (*introduce.Service, *introduceMocks.MockInvitatio return svc, dep, didChan } -func handleInbound(t *testing.T, svc *introduce.Service, msg interface{}) { +func handleInbound(t *testing.T, svc *introduce.Service, msg interface{}, myDID, theirDID string) { t.Helper() resp, err := service.NewDIDCommMsg(toBytes(t, msg)) require.NoError(t, err) - _, err = svc.HandleInbound(resp) + _, err = svc.HandleInbound(resp, myDID, theirDID) require.NoError(t, err) } @@ -2368,9 +2206,7 @@ func checkAndHandle(f *flow) { go func() { // handle outbound Request msg - require.NoError(f.t, f.svc.HandleOutbound(request, &service.Destination{ - ServiceEndpoint: f.recipients[0].Destination.ServiceEndpoint, - })) + require.NoError(f.t, f.svc.HandleOutbound(request, f.recipients[0].MyDID, f.recipients[0].TheirDID)) }() checkStateMsg(f.t, sCh, service.PreState, introduce.RequestMsgType, "requesting") checkStateMsg(f.t, sCh, service.PostState, introduce.RequestMsgType, "requesting") @@ -2388,9 +2224,7 @@ func checkAndHandle(f *flow) { go func() { // handle outbound Proposal msg - require.NoError(f.t, f.svc.HandleOutbound(proposal, &service.Destination{ - ServiceEndpoint: f.recipients[0].Destination.ServiceEndpoint, - })) + require.NoError(f.t, f.svc.HandleOutbound(proposal, f.recipients[0].MyDID, f.recipients[0].TheirDID)) }() checkStateMsg(f.t, sCh, service.PreState, introduce.ProposalMsgType, "arranging") checkStateMsg(f.t, sCh, service.PostState, introduce.ProposalMsgType, "arranging") @@ -2409,7 +2243,7 @@ func checkAndHandle(f *flow) { switch iMsg := incomingMsg.(type) { case *introduce.Request: - go handleInbound(f.t, f.svc, incomingMsg) + go handleInbound(f.t, f.svc, incomingMsg, f.recipients[0].MyDID, f.recipients[0].TheirDID) if f.withRequestStop { continueActionStop(f.t, aCh, introduce.RequestMsgType) @@ -2429,7 +2263,7 @@ func checkAndHandle(f *flow) { case *introduce.Response: responseCounter++ - go handleInbound(f.t, f.svc, incomingMsg) + go handleInbound(f.t, f.svc, incomingMsg, f.recipients[0].MyDID, f.recipients[0].TheirDID) if f.withResponseStop { continueActionStop(f.t, aCh, introduce.ResponseMsgType) @@ -2507,7 +2341,7 @@ func checkAndHandle(f *flow) { return case *introduce.Proposal: - go handleInbound(f.t, f.svc, incomingMsg) + go handleInbound(f.t, f.svc, incomingMsg, f.recipients[0].MyDID, f.recipients[0].TheirDID) require.NotEmpty(f.t, iMsg.To.Name) @@ -2531,7 +2365,7 @@ func checkAndHandle(f *flow) { checkStateMsg(f.t, sCh, service.PreState, introduce.ProposalMsgType, "waiting") checkStateMsg(f.t, sCh, service.PostState, introduce.ProposalMsgType, "waiting") case *model.ProblemReport: - go handleInbound(f.t, f.svc, incomingMsg) + go handleInbound(f.t, f.svc, incomingMsg, f.recipients[0].MyDID, f.recipients[0].TheirDID) checkStateMsg(f.t, sCh, service.PreState, introduce.ProblemReportMsgType, "abandoning") checkStateMsg(f.t, sCh, service.PostState, introduce.ProblemReportMsgType, "abandoning") checkStateMsg(f.t, sCh, service.PreState, introduce.ProblemReportMsgType, "done") @@ -2539,7 +2373,7 @@ func checkAndHandle(f *flow) { return case *model.Ack: - go handleInbound(f.t, f.svc, incomingMsg) + go handleInbound(f.t, f.svc, incomingMsg, f.recipients[0].MyDID, f.recipients[0].TheirDID) checkStateMsg(f.t, sCh, service.PreState, introduce.AckMsgType, "done") checkStateMsg(f.t, sCh, service.PostState, introduce.AckMsgType, "done") diff --git a/pkg/didcomm/protocol/introduce/states.go b/pkg/didcomm/protocol/introduce/states.go index 0b3c82a19..7ce3b8c13 100644 --- a/pkg/didcomm/protocol/introduce/states.go +++ b/pkg/didcomm/protocol/introduce/states.go @@ -38,13 +38,6 @@ const ( stateNameWaiting = "waiting" ) -// GetInboundDestination gets inbound destination -// nolint: gochecknoglobals -var GetInboundDestination = func() *service.Destination { - // TODO: need to get real destination and key - return &service.Destination{} -} - type internalContext struct { dispatcher.Outbound } @@ -58,7 +51,7 @@ type state interface { // Executes this state, returning a followup state to be immediately executed as well. // The 'noOp' state should be returned if the state has no followup. ExecuteInbound(ctx internalContext, msg *metaData) (followup state, err error) - ExecuteOutbound(ctx internalContext, msg *metaData, dest *service.Destination) (followup state, err error) + ExecuteOutbound(ctx internalContext, msg *metaData) (followup state, err error) } // noOp state @@ -77,7 +70,7 @@ func (s *noOp) ExecuteInbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("cannot execute no-op") } -func (s *noOp) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *noOp) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("cannot execute no-op") } @@ -99,7 +92,7 @@ func (s *start) ExecuteInbound(ctx internalContext, _ *metaData) (state, error) return nil, errors.New("start ExecuteInbound: not implemented yet") } -func (s *start) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *start) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("start ExecuteOutbound: not implemented yet") } @@ -120,7 +113,7 @@ func (s *done) ExecuteInbound(ctx internalContext, _ *metaData) (state, error) { return &noOp{}, nil } -func (s *done) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *done) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("done ExecuteOutbound: not implemented yet") } @@ -149,7 +142,7 @@ func (s *arranging) ExecuteInbound(ctx internalContext, m *metaData) (state, err return &abandoning{}, nil } - recipients := fillMissingDestination(m.dependency.Recipients()) + recipients := fillRecipient(m.dependency.Recipients(), m) var recipient *Recipient @@ -160,23 +153,25 @@ func (s *arranging) ExecuteInbound(ctx internalContext, m *metaData) (state, err recipient = recipients[1] } - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - return &noOp{}, ctx.Send(&Proposal{ + return &noOp{}, ctx.SendToDID(&Proposal{ Type: ProposalMsgType, ID: uuid.New().String(), To: recipient.To, Thread: &decorator.Thread{ID: m.ThreadID}, - }, "", recipient.Destination) + }, recipient.MyDID, recipient.TheirDID) } -func (s *arranging) ExecuteOutbound(ctx internalContext, m *metaData, dest *service.Destination) (state, error) { +func (s *arranging) ExecuteOutbound(ctx internalContext, m *metaData) (state, error) { var proposal *Proposal if err := json.Unmarshal(m.Msg.Payload, &proposal); err != nil { return nil, fmt.Errorf("outbound unmarshal: %w", err) } - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - return &noOp{}, ctx.Send(proposal, "", dest) + if err := ctx.SendToDID(proposal, m.myDID, m.theirDID); err != nil { + return nil, fmt.Errorf("arranging: SendToDID: %w", err) + } + + return &noOp{}, nil } // delivering state @@ -220,8 +215,7 @@ func sendProblemReport(ctx internalContext, m *metaData, recipients []*Recipient } for _, recipient := range recipients { - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - if err := ctx.Send(problem, "", recipient.Destination); err != nil { + if err := ctx.SendToDID(problem, recipient.MyDID, recipient.TheirDID); err != nil { return nil, fmt.Errorf("send problem-report: %w", err) } } @@ -234,8 +228,7 @@ func deliveringSkipInvitation(ctx internalContext, m *metaData, recipients []*Re inv := m.dependency.Invitation() inv.Thread = &decorator.Thread{PID: m.ThreadID} - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - err := ctx.Send(inv, "", recipients[0].Destination) + err := ctx.SendToDID(inv, recipients[0].MyDID, recipients[0].TheirDID) if err != nil { return nil, fmt.Errorf("send inbound invitation (skip): %w", err) } @@ -244,7 +237,7 @@ func deliveringSkipInvitation(ctx internalContext, m *metaData, recipients []*Re } func (s *delivering) ExecuteInbound(ctx internalContext, m *metaData) (state, error) { - recipients := fillMissingDestination(m.dependency.Recipients()) + recipients := fillRecipient(m.dependency.Recipients(), m) if approve, ok := getApproveFromMsg(m.Msg); ok && !approve { return &abandoning{}, nil @@ -261,16 +254,16 @@ func (s *delivering) ExecuteInbound(ctx internalContext, m *metaData) (state, er m.Invitation.Thread = &decorator.Thread{PID: m.ThreadID} - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - err := ctx.Send(m.Invitation, "", recipients[toDestIDx(m.IntroduceeIndex)].Destination) - if err != nil { + recipient := recipients[toDestIDx(m.IntroduceeIndex)] + + if err := ctx.SendToDID(m.Invitation, recipient.MyDID, recipient.TheirDID); err != nil { return nil, fmt.Errorf("send inbound invitation: %w", err) } return &confirming{}, nil } -func (s *delivering) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *delivering) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("delivering ExecuteOutbound: not implemented yet") } @@ -287,14 +280,14 @@ func (s *confirming) CanTransitionTo(next state) bool { } func (s *confirming) ExecuteInbound(ctx internalContext, m *metaData) (state, error) { - recipients := fillMissingDestination(m.dependency.Recipients()) + recipients := fillRecipient(m.dependency.Recipients(), m) + recipient := recipients[m.IntroduceeIndex] - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - err := ctx.Send(&model.Ack{ + err := ctx.SendToDID(&model.Ack{ Type: AckMsgType, ID: uuid.New().String(), Thread: &decorator.Thread{ID: m.ThreadID}, - }, "", recipients[m.IntroduceeIndex].Destination) + }, recipient.MyDID, recipient.TheirDID) if err != nil { return nil, fmt.Errorf("send ack: %w", err) @@ -303,7 +296,7 @@ func (s *confirming) ExecuteInbound(ctx internalContext, m *metaData) (state, er return &done{}, nil } -func (s *confirming) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *confirming) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("confirming ExecuteOutbound: not implemented yet") } @@ -319,16 +312,23 @@ func (s *abandoning) CanTransitionTo(next state) bool { return next.Name() == stateNameDone } -func fillMissingDestination(recipients []*Recipient) []*Recipient { +func fillRecipient(recipients []*Recipient, m *metaData) []*Recipient { // for the first recipient, we may do not have a destination // in that case, we need to get destination from the inbound message // NOTE: it happens after receiving the Request message. if len(recipients) == 0 { - return append(recipients, &Recipient{Destination: GetInboundDestination()}) + return append(recipients, &Recipient{ + MyDID: m.myDID, + TheirDID: m.theirDID, + }) } - if recipients[0].Destination == nil { - recipients[0].Destination = GetInboundDestination() + if recipients[0].MyDID == "" { + recipients[0].MyDID = m.myDID + } + + if recipients[0].TheirDID == "" { + recipients[0].TheirDID = m.theirDID } return recipients @@ -338,11 +338,11 @@ func (s *abandoning) ExecuteInbound(ctx internalContext, m *metaData) (state, er var recipients []*Recipient if m.Msg.Header.Type == RequestMsgType { - recipients = fillMissingDestination(nil) + recipients = fillRecipient(nil, m) } if m.Msg.Header.Type == ResponseMsgType { - recipients = fillMissingDestination(m.Recipients) + recipients = fillRecipient(m.Recipients, m) } if approve, ok := getApproveFromMsg(m.Msg); ok && !approve { @@ -357,7 +357,7 @@ func (s *abandoning) ExecuteInbound(ctx internalContext, m *metaData) (state, er return sendProblemReport(ctx, m, recipients) } -func (s *abandoning) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *abandoning) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("abandoning ExecuteOutbound: not implemented yet") } @@ -384,17 +384,17 @@ func (s *deciding) ExecuteInbound(ctx internalContext, m *metaData) (state, erro if m.disapprove { st = &abandoning{} } - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - return st, ctx.Send(&Response{ + + return st, ctx.SendToDID(&Response{ Type: ResponseMsgType, ID: uuid.New().String(), Thread: &decorator.Thread{ID: m.ThreadID}, Invitation: inv, Approve: !m.disapprove, - }, "", GetInboundDestination()) + }, m.myDID, m.theirDID) } -func (s *deciding) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *deciding) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("deciding ExecuteOutbound: not implemented yet") } @@ -414,7 +414,7 @@ func (s *waiting) ExecuteInbound(ctx internalContext, _ *metaData) (state, error return &noOp{}, nil } -func (s *waiting) ExecuteOutbound(ctx internalContext, _ *metaData, _ *service.Destination) (state, error) { +func (s *waiting) ExecuteOutbound(ctx internalContext, _ *metaData) (state, error) { return nil, errors.New("waiting ExecuteOutbound: not implemented yet") } @@ -434,12 +434,11 @@ func (s *requesting) ExecuteInbound(ctx internalContext, _ *metaData) (state, er return nil, errors.New("requesting ExecuteInbound: not implemented yet") } -func (s *requesting) ExecuteOutbound(ctx internalContext, m *metaData, dest *service.Destination) (state, error) { +func (s *requesting) ExecuteOutbound(ctx internalContext, m *metaData) (state, error) { var req *Request if err := json.Unmarshal(m.Msg.Payload, &req); err != nil { return nil, fmt.Errorf("requesting outbound unmarshal: %w", err) } - // TODO: Add senderVerKey https://github.com/hyperledger/aries-framework-go/issues/903 - return &noOp{}, ctx.Send(req, "", dest) + return &noOp{}, ctx.SendToDID(req, m.myDID, m.theirDID) } diff --git a/pkg/didcomm/protocol/introduce/states_test.go b/pkg/didcomm/protocol/introduce/states_test.go index 4bb579381..623938ee5 100644 --- a/pkg/didcomm/protocol/introduce/states_test.go +++ b/pkg/didcomm/protocol/introduce/states_test.go @@ -45,7 +45,7 @@ func TestNoOp_ExecuteInbound(t *testing.T) { } func TestNoOp_ExecuteOutbound(t *testing.T) { - followup, err := (&noOp{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&noOp{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -74,7 +74,7 @@ func TestStart_ExecuteInbound(t *testing.T) { } func TestStart_ExecuteOutbound(t *testing.T) { - followup, err := (&start{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&start{}).ExecuteOutbound(internalContext{}, &metaData{}) require.EqualError(t, err, "start ExecuteOutbound: not implemented yet") require.Nil(t, followup) } @@ -92,7 +92,7 @@ func TestDone_ExecuteInbound(t *testing.T) { } func TestDone_ExecuteOutbound(t *testing.T) { - followup, err := (&done{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&done{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -119,12 +119,12 @@ func TestArranging_ExecuteOutbound(t *testing.T) { defer ctrl.Finish() dispatcher := dispatcherMocks.NewMockOutbound(ctrl) - dispatcher.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + dispatcher.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) ctx := internalContext{Outbound: dispatcher} followup, err := (&arranging{}).ExecuteOutbound(ctx, &metaData{ Msg: &service.DIDCommMsg{Payload: []byte(`{}`)}, - }, &service.Destination{}) + }) require.NoError(t, err) require.Equal(t, &noOp{}, followup) @@ -132,7 +132,7 @@ func TestArranging_ExecuteOutbound(t *testing.T) { errMsg := "json: cannot unmarshal array into Go value of type introduce.Proposal" followup, err = (&arranging{}).ExecuteOutbound(ctx, &metaData{ Msg: &service.DIDCommMsg{Payload: []byte(`[]`)}, - }, &service.Destination{}) + }) require.EqualError(t, errors.Unwrap(err), errMsg) require.Nil(t, followup) } @@ -155,7 +155,7 @@ func TestDelivering_CanTransitionTo(t *testing.T) { } func TestDelivering_ExecuteOutbound(t *testing.T) { - followup, err := (&delivering{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&delivering{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -178,7 +178,7 @@ func TestConfirming_CanTransitionTo(t *testing.T) { } func TestConfirming_ExecuteOutbound(t *testing.T) { - followup, err := (&confirming{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&confirming{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -206,7 +206,7 @@ func TestAbandoning_ExecuteInbound(t *testing.T) { defer ctrl.Finish() dispatcher := dispatcherMocks.NewMockOutbound(ctrl) - dispatcher.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("test error")) + dispatcher.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("test error")) followup, err := (&abandoning{}).ExecuteInbound(internalContext{Outbound: dispatcher}, &metaData{ Msg: &service.DIDCommMsg{ @@ -220,7 +220,7 @@ func TestAbandoning_ExecuteInbound(t *testing.T) { } func TestAbandoning_ExecuteOutbound(t *testing.T) { - followup, err := (&abandoning{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&abandoning{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -247,7 +247,7 @@ func TestDeciding_ExecuteInbound(t *testing.T) { defer ctrl.Finish() dispatcher := dispatcherMocks.NewMockOutbound(ctrl) - dispatcher.EXPECT().Send(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + dispatcher.EXPECT().SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) ctx := internalContext{Outbound: dispatcher} @@ -257,7 +257,7 @@ func TestDeciding_ExecuteInbound(t *testing.T) { } func TestDeciding_ExecuteOutbound(t *testing.T) { - followup, err := (&deciding{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&deciding{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -286,7 +286,7 @@ func TestWaiting_ExecuteInbound(t *testing.T) { } func TestWaiting_ExecuteOutbound(t *testing.T) { - followup, err := (&waiting{}).ExecuteOutbound(internalContext{}, &metaData{}, &service.Destination{}) + followup, err := (&waiting{}).ExecuteOutbound(internalContext{}, &metaData{}) require.Error(t, err) require.Nil(t, followup) } @@ -320,7 +320,7 @@ func TestRequesting_ExecuteOutbound(t *testing.T) { followup, err := (&requesting{}).ExecuteOutbound(internalContext{}, &metaData{ Msg: &service.DIDCommMsg{Payload: []byte(`[]`)}, - }, nil) + }) const errMsg = "requesting outbound unmarshal: json: cannot unmarshal array into Go value of type introduce.Request" diff --git a/pkg/didcomm/protocol/route/support_test.go b/pkg/didcomm/protocol/route/support_test.go index 450320668..1bf0d27cf 100644 --- a/pkg/didcomm/protocol/route/support_test.go +++ b/pkg/didcomm/protocol/route/support_test.go @@ -66,6 +66,10 @@ func (m *mockOutbound) Send(msg interface{}, senderVerKey string, des *service.D return m.validateSend(msg) } +func (m *mockOutbound) SendToDID(msg interface{}, myDID, theirDID string) error { + return nil +} + func generateRequestMsgPayload(t *testing.T, id string) *service.DIDCommMsg { requestBytes, err := json.Marshal(&Request{ Type: RequestMsgType, diff --git a/pkg/framework/context/context.go b/pkg/framework/context/context.go index d748a71ec..80ab24c5b 100644 --- a/pkg/framework/context/context.go +++ b/pkg/framework/context/context.go @@ -113,7 +113,7 @@ func (p *Provider) InboundMessageHandler() transport.InboundMessageHandler { // find the service which accepts the message type for _, svc := range p.services { if svc.Accept(msg.Header.Type) { - _, err = svc.HandleInbound(msg) + _, err = svc.HandleInbound(msg, "", "") return err } } diff --git a/pkg/internal/mock/didcomm/dispatcher/mock_outbound.go b/pkg/internal/mock/didcomm/dispatcher/mock_outbound.go index 7d0986301..ad9c9c0e4 100644 --- a/pkg/internal/mock/didcomm/dispatcher/mock_outbound.go +++ b/pkg/internal/mock/didcomm/dispatcher/mock_outbound.go @@ -18,3 +18,8 @@ type MockOutbound struct { func (m *MockOutbound) Send(msg interface{}, senderVerKey string, des *service.Destination) error { return m.SendErr } + +// SendToDID msg +func (m *MockOutbound) SendToDID(msg interface{}, myDID, theirDID string) error { + return nil +} diff --git a/pkg/internal/mock/didcomm/protocol/mock_didexchange.go b/pkg/internal/mock/didcomm/protocol/mock_didexchange.go index 7641649fd..3bcd1a75b 100644 --- a/pkg/internal/mock/didcomm/protocol/mock_didexchange.go +++ b/pkg/internal/mock/didcomm/protocol/mock_didexchange.go @@ -24,7 +24,7 @@ import ( type MockDIDExchangeSvc struct { ProtocolName string HandleFunc func(*service.DIDCommMsg) (string, error) - HandleOutboundFunc func(msg *service.DIDCommMsg, dest *service.Destination) error + HandleOutboundFunc func(msg *service.DIDCommMsg, myDID, theirDID string) error AcceptFunc func(string) bool RegisterActionEventErr error UnregisterActionEventErr error @@ -35,7 +35,7 @@ type MockDIDExchangeSvc struct { } // HandleInbound msg -func (m *MockDIDExchangeSvc) HandleInbound(msg *service.DIDCommMsg) (string, error) { +func (m *MockDIDExchangeSvc) HandleInbound(msg *service.DIDCommMsg, myDID, theirDID string) (string, error) { if m.HandleFunc != nil { return m.HandleFunc(msg) } @@ -44,9 +44,9 @@ func (m *MockDIDExchangeSvc) HandleInbound(msg *service.DIDCommMsg) (string, err } // HandleOutbound msg -func (m *MockDIDExchangeSvc) HandleOutbound(msg *service.DIDCommMsg, dest *service.Destination) error { +func (m *MockDIDExchangeSvc) HandleOutbound(msg *service.DIDCommMsg, myDID, theirDID string) error { if m.HandleOutboundFunc != nil { - return m.HandleOutboundFunc(msg, dest) + return m.HandleOutboundFunc(msg, myDID, theirDID) } return nil diff --git a/pkg/restapi/operation/didexchange/didexchange_test.go b/pkg/restapi/operation/didexchange/didexchange_test.go index 428e33dd4..85b5dfbe6 100644 --- a/pkg/restapi/operation/didexchange/didexchange_test.go +++ b/pkg/restapi/operation/didexchange/didexchange_test.go @@ -514,7 +514,7 @@ func TestAcceptExchangeRequest(t *testing.T) { msg, err := service.NewDIDCommMsg(request) require.NoError(t, err) - _, err = didExSvc.HandleInbound(msg) + _, err = didExSvc.HandleInbound(msg, "", "") require.NoError(t, err) cid := <-connID @@ -586,7 +586,7 @@ func TestAcceptInvitation(t *testing.T) { msg, err := service.NewDIDCommMsg(invitation) require.NoError(t, err) - _, err = didExSvc.HandleInbound(msg) + _, err = didExSvc.HandleInbound(msg, "", "") require.NoError(t, err) var cid string