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

Commit

Permalink
feat: Framework Init - base template with Outbound transport config
Browse files Browse the repository at this point in the history
- Add New(opts) function which takes in the options to configuration
- Support outbound transport configuration

Signed-off-by: Rolson Quadras <[email protected]>
  • Loading branch information
rolsonquadras committed Aug 13, 2019
1 parent a8eff94 commit 2265127
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 10 deletions.
13 changes: 9 additions & 4 deletions pkg/didcomm/protocol/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ type Connection struct {
DIDDoc *did.Doc `json:"did_doc,omitempty"`
}

// Context context for exchange protocol
type Context interface {
OutboundTransport() transport.OutboundTransport
}

// GenerateInviteWithPublicDID generates the DID exchange invitation string with public DID
func GenerateInviteWithPublicDID(invite *Invitation) (string, error) {
if invite.ID == "" || invite.DID == "" {
Expand All @@ -87,28 +92,28 @@ func GenerateInviteWithKeyAndEndpoint(invite *Invitation) (string, error) {
}

// SendExchangeRequest sends exchange request
func SendExchangeRequest(exchangeRequest *Request, destination string, transport transport.OutboundTransport) error {
func SendExchangeRequest(ctx Context, exchangeRequest *Request, destination string) error {
if exchangeRequest == nil {
return errors.New("exchangeRequest cannot be nil")
}

exchangeRequest.Type = connectionRequest

// ignore response data as it is not used in this communication mode as defined in the spec
_, err := marshalAndSend(exchangeRequest, "Error Marshalling Exchange Request", destination, transport)
_, err := marshalAndSend(exchangeRequest, "Error Marshalling Exchange Request", destination, ctx.OutboundTransport())
return err
}

// SendExchangeResponse sends exchange response
func SendExchangeResponse(exchangeResponse *Response, destination string, transport transport.OutboundTransport) error {
func SendExchangeResponse(ctx Context, exchangeResponse *Response, destination string) error {
if exchangeResponse == nil {
return errors.New("exchangeResponse cannot be nil")
}

exchangeResponse.Type = connectionResponse

// ignore response data as it is not used in this communication mode as defined in the spec
_, err := marshalAndSend(exchangeResponse, "Error Marshalling Exchange Response", destination, transport)
_, err := marshalAndSend(exchangeResponse, "Error Marshalling Exchange Response", destination, ctx.OutboundTransport())
return err
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/didcomm/protocol/exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package exchange
import (
"testing"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
mocktransport "github.com/hyperledger/aries-framework-go/pkg/internal/didcomm/transport/mock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -83,19 +84,19 @@ func TestGenerateInviteWithKeyAndEndpoint(t *testing.T) {
}

func TestSendRequest(t *testing.T) {
oTr := mocktransport.NewOutboundTransport(successResponse)
ctx := mockContext{}

req := &Request{
ID: "5678876542345",
Label: "Bob",
}

require.NoError(t, SendExchangeRequest(req, destinationURL, oTr))
require.Error(t, SendExchangeRequest(nil, destinationURL, oTr))
require.NoError(t, SendExchangeRequest(&ctx, req, destinationURL))
require.Error(t, SendExchangeRequest(&ctx, nil, destinationURL))
}

func TestSendResponse(t *testing.T) {
oTr := mocktransport.NewOutboundTransport(successResponse)
ctx := mockContext{}

resp := &Response{
ID: "12345678900987654321",
Expand All @@ -104,6 +105,13 @@ func TestSendResponse(t *testing.T) {
},
}

require.NoError(t, SendExchangeResponse(resp, destinationURL, oTr))
require.Error(t, SendExchangeResponse(nil, destinationURL, oTr))
require.NoError(t, SendExchangeResponse(&ctx, resp, destinationURL))
require.Error(t, SendExchangeResponse(&ctx, nil, destinationURL))
}

type mockContext struct {
}

func (ctx *mockContext) OutboundTransport() transport.OutboundTransport {
return mocktransport.NewOutboundTransport(successResponse)
}
22 changes: 22 additions & 0 deletions pkg/framework/aries/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package aries

import "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"

// Context provides handles to various configs to be used by the clients
type Context struct {
outboundTransport transport.OutboundTransport
}

// ContextOption configure the context.
type ContextOption func(opts *Context) error

// OutboundTransport provides handle to the outbound transport
func (ctx *Context) OutboundTransport() transport.OutboundTransport {
return ctx.outboundTransport
}
40 changes: 40 additions & 0 deletions pkg/framework/aries/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package aries

import (
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
errors "golang.org/x/xerrors"
)

// defFramework provides default framework configs
type defFramework struct{}

// outboundTransport provides default OutboundTransport
func (d defFramework) outboundTransport() (transport.OutboundTransport, error) {
// TODO - https://github.com/hyperledger/aries-framework-go/issues/83
return nil, nil
}

// defFrameworkOpts provides default framework options
func defFrameworkOpts() ([]Option, error) {
// get the default framework configs
def := defFramework{}

// outbound transport
ot, err := def.outboundTransport()
if err != nil {
return nil, errors.Errorf("Default outbound transport init failed : %w", err)
}

var opts []Option

opt := WithOutboundTransport(ot)
opts = append(opts, opt)

return opts, nil
}
66 changes: 66 additions & 0 deletions pkg/framework/aries/framework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package aries

import (
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
errors "golang.org/x/xerrors"
)

// Aries provides access to clients being managed by the framework.
type Aries struct {
outboundTransport transport.OutboundTransport
}

// Option configures the framework.
type Option func(opts *Aries) error

// New initializes the Aries framework based on the set of options provided.
func New(opts ...Option) (*Aries, error) {
// get the default framework options
defOpts, err := defFrameworkOpts()
if err != nil {
return nil, errors.Errorf("Default option init failed : %w", err)
}

frameworkOpts := &Aries{}

// generate framework configs from options
for _, option := range append(defOpts, opts...) {
err := option(frameworkOpts)
if err != nil {
return nil, errors.Errorf("Error in option passed to New: %w", err)
}
}

return frameworkOpts, nil
}

// WithOutboundTransport injects a OutboundTransport interface to the Aries
func WithOutboundTransport(ot transport.OutboundTransport) Option {
return func(opts *Aries) error {
opts.outboundTransport = ot
return nil
}
}

// Context provides handle to framework context
func (a *Aries) Context(opts ...ContextOption) (*Context, error) {
ctx := &Context{
outboundTransport: a.outboundTransport,
}

// generate context from options
for _, option := range append(opts) {
err := option(ctx)
if err != nil {
return nil, errors.Errorf("Framework context creation failed: %w", err)
}
}

return ctx, nil
}
52 changes: 52 additions & 0 deletions pkg/framework/aries/framework_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package aries

import (
"testing"

"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/exchange"
mocktransport "github.com/hyperledger/aries-framework-go/pkg/internal/didcomm/transport/mock"
"github.com/stretchr/testify/require"
errors "golang.org/x/xerrors"
)

func TestFramework(t *testing.T) {
// framework new - error
_, err := New(func(opts *Aries) error {
return errors.New("error creating the framework option")
})
require.Error(t, err)

// framework new - success
ot := mocktransport.NewOutboundTransport("success")
aries, err := New(WithOutboundTransport(ot))
require.NoError(t, err)

req := &exchange.Request{
ID: "5678876542345",
Label: "Bob",
}

// context - success
ctx, err := aries.Context()
require.NoError(t, err)
require.NoError(t, exchange.SendExchangeRequest(ctx, req, "http://example/didexchange"))

ctx, err = aries.Context(func(opts *Context) error {
opts.outboundTransport = ot
return nil
})
require.NoError(t, err)
require.Error(t, exchange.SendExchangeRequest(ctx, req, ""))

// context - error
_, err = aries.Context(func(opts *Context) error {
return errors.New("error creating the context option")
})
require.Error(t, err)
}

0 comments on commit 2265127

Please sign in to comment.