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

Commit

Permalink
feat: Packager now holds multiple packers, indexed by Typ field.
Browse files Browse the repository at this point in the history
Packager can select Packer for decryption based on the Typ field of
the input message.

Fixes #273

Signed-off-by: Filip Burlacu <[email protected]>
  • Loading branch information
Filip Burlacu committed Nov 8, 2019
1 parent ef1983c commit a352bb4
Show file tree
Hide file tree
Showing 38 changed files with 972 additions and 592 deletions.
14 changes: 14 additions & 0 deletions pkg/didcomm/common/transport/envelope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package transport

// Envelope contain msg, FromVerKey and ToVerKeys
type Envelope struct {
Message []byte
FromVerKey string
ToVerKeys []string
}
38 changes: 38 additions & 0 deletions pkg/didcomm/common/transport/packager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package transport

// Packager manages the handling, building and parsing of DIDComm raw messages in JSON envelopes.
//
// These envelopes are used as wire-level wrappers of messages sent in Aries agent-agent communication.
type Packager interface {
// PackMessage Pack a message for one or more recipients.
//
// Args:
//
// envelope: The message to pack
//
// Returns:
//
// []byte: The packed message
//
// error: error
PackMessage(envelope *Envelope) ([]byte, error)

// UnpackMessage Unpack a message.
//
// Args:
//
// encMessage: The encrypted message
//
// Returns:
//
// envelope: unpack message
//
// error: error
UnpackMessage(encMessage []byte) (*Envelope, error)
}
4 changes: 2 additions & 2 deletions pkg/didcomm/dispatcher/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package dispatcher

import (
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/envelope"
transport2 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/transport"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
)

Expand All @@ -26,7 +26,7 @@ type Outbound interface {

// Provider interface for outbound ctx
type Provider interface {
Packager() envelope.Packager
Packager() transport2.Packager
OutboundTransports() []transport.OutboundTransport
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/didcomm/dispatcher/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"fmt"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/envelope"
transport2 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/transport"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
)

// OutboundDispatcher dispatch msgs to destination
type OutboundDispatcher struct {
outboundTransports []transport.OutboundTransport
packager envelope.Packager
packager transport2.Packager
}

// NewOutbound return new dispatcher outbound instance
Expand All @@ -39,7 +39,7 @@ func (o *OutboundDispatcher) Send(msg interface{}, senderVerKey string, des *ser
}

packedMsg, err := o.packager.PackMessage(
&envelope.Envelope{Message: bytes, FromVerKey: senderVerKey, ToVerKeys: des.RecipientKeys})
&transport2.Envelope{Message: bytes, FromVerKey: senderVerKey, ToVerKeys: des.RecipientKeys})
if err != nil {
return fmt.Errorf("failed to pack msg: %w", err)
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/didcomm/dispatcher/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ import (
"github.com/stretchr/testify/require"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/envelope"
transport2 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/transport"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
mockdidcomm "github.com/hyperledger/aries-framework-go/pkg/internal/mock/didcomm"
mockpackager "github.com/hyperledger/aries-framework-go/pkg/internal/mock/didcomm/envelope"
mockpackager "github.com/hyperledger/aries-framework-go/pkg/internal/mock/didcomm/packager"
)

func TestOutboundDispatcher_Send(t *testing.T) {
t.Run("test success", func(t *testing.T) {
o := NewOutbound(&provider{packagerValue: &mockpackager.BasePackager{},
o := NewOutbound(&provider{packagerValue: &mockpackager.Packager{},
outboundTransportsValue: []transport.OutboundTransport{&mockdidcomm.MockOutboundTransport{AcceptValue: true}}})
require.NoError(t, o.Send("data", "", &service.Destination{ServiceEndpoint: "url"}))
})

t.Run("test no outbound transport found", func(t *testing.T) {
o := NewOutbound(&provider{packagerValue: &mockpackager.BasePackager{},
o := NewOutbound(&provider{packagerValue: &mockpackager.Packager{},
outboundTransportsValue: []transport.OutboundTransport{&mockdidcomm.MockOutboundTransport{AcceptValue: false}}})
err := o.Send("data", "", &service.Destination{ServiceEndpoint: "url"})
require.Error(t, err)
require.Contains(t, err.Error(), "no outbound transport found for serviceEndpoint: url")
})

t.Run("test pack msg failure", func(t *testing.T) {
o := NewOutbound(&provider{packagerValue: &mockpackager.BasePackager{PackErr: fmt.Errorf("pack error")},
o := NewOutbound(&provider{packagerValue: &mockpackager.Packager{PackErr: fmt.Errorf("pack error")},
outboundTransportsValue: []transport.OutboundTransport{&mockdidcomm.MockOutboundTransport{AcceptValue: true}}})
err := o.Send("data", "", &service.Destination{ServiceEndpoint: "url"})
require.Error(t, err)
require.Contains(t, err.Error(), "pack error")
})

t.Run("test outbound send failure", func(t *testing.T) {
o := NewOutbound(&provider{packagerValue: &mockpackager.BasePackager{},
o := NewOutbound(&provider{packagerValue: &mockpackager.Packager{},
outboundTransportsValue: []transport.OutboundTransport{
&mockdidcomm.MockOutboundTransport{AcceptValue: true, SendErr: fmt.Errorf("send error")}}})
err := o.Send("data", "", &service.Destination{ServiceEndpoint: "url"})
Expand All @@ -53,11 +53,11 @@ func TestOutboundDispatcher_Send(t *testing.T) {
}

type provider struct {
packagerValue envelope.Packager
packagerValue transport2.Packager
outboundTransportsValue []transport.OutboundTransport
}

func (p *provider) Packager() envelope.Packager {
func (p *provider) Packager() transport2.Packager {
return p.packagerValue
}

Expand Down
50 changes: 0 additions & 50 deletions pkg/didcomm/envelope/api.go

This file was deleted.

Loading

0 comments on commit a352bb4

Please sign in to comment.