mediator

package
v0.0.0-...-57c6170 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mediator enables the agent to register with the router. Once the agent is registered, the Router is responsible for routing/forwarding the DIDComm messages to the agent. During router registration, the agent receives routers service endpoint and routing keys. These details are used in DID Exchange Invitation or DID Document Service Descriptor.

Example
package main

import (
	"errors"
	"fmt"

	didexClient "github.com/hyperledger/aries-framework-go/pkg/client/didexchange"
	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/didexchange"
	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
	mockprotocol "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol"
	mockroute "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol/mediator"
	mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms"
	mockprovider "github.com/hyperledger/aries-framework-go/pkg/mock/provider"
	mockstore "github.com/hyperledger/aries-framework-go/pkg/mock/storage"
)

func main() {
	// Create DID Exchange Client and perform DID Exchange with the Router
	didExClient, err := didexClient.New(didClientMockContext())
	if err != nil {
		fmt.Println("failed to create client for Alice")
	}

	// Get the connection ID
	routerConnID := performDIDExchangeWithRouter(didExClient)

	// Create Route Client
	client, err := New(mockContext())
	if err != nil {
		fmt.Println("failed to create route client")
	}

	// Register agent with the router
	err = client.Register(routerConnID)
	if err != nil {
		fmt.Printf("failed to register the agent with router : %s\n", err.Error())
	}

	fmt.Println("successfully registered with router")

	// generate invitation after route has been registered
	invitation, err := didExClient.CreateInvitation("alice-agent", didexClient.WithRouterConnectionID("xyz"))
	if err != nil {
		fmt.Println("failed to create invitation after route registration")
	}

	fmt.Println(invitation.ServiceEndpoint)
	fmt.Println(invitation.RoutingKeys)

}

func performDIDExchangeWithRouter(client *didexClient.Client) string {
	router, err := didexClient.New(didClientMockContext())
	if err != nil {
		fmt.Println("failed to create client for Bob")
	}

	routerActions := make(chan service.DIDCommAction)

	err = router.RegisterActionEvent(routerActions)
	if err != nil {
		fmt.Println("failed to create Bob's action channel")
	}

	go func() {
		service.AutoExecuteActionEvent(routerActions)
	}()

	aliceActions := make(chan service.DIDCommAction)

	err = client.RegisterActionEvent(aliceActions)
	if err != nil {
		fmt.Println("failed to create Alice's action channel")
	}

	go func() {
		service.AutoExecuteActionEvent(aliceActions)
	}()

	invitation, err := router.CreateInvitation("router invites alice")
	if err != nil {
		fmt.Printf("failed to create invitation: %s\n", err)
	}

	connectionID, err := client.HandleInvitation(invitation)
	if err != nil {
		fmt.Printf("failed to handle invitation: %s\n", err)
	}

	return connectionID
}

func didClientMockContext() *mockprovider.Provider {
	protocolStateStoreProvider := mockstore.NewMockStoreProvider()
	storeProvider := mockstore.NewMockStoreProvider()
	mockProvider := &mockprotocol.MockProvider{
		ProtocolStateStoreProvider: protocolStateStoreProvider,
		StoreProvider:              storeProvider,
		ServiceMap: map[string]interface{}{
			mediator.Coordination: &mockroute.MockMediatorSvc{},
		},
	}

	svc, err := didexchange.New(mockProvider)
	if err != nil {
		panic(err)
	}

	ed25519KH, err := mockkms.CreateMockED25519KeyHandle()
	if err != nil {
		panic(err)
	}

	context := &mockprovider.Provider{
		KMSValue:                          &mockkms.KeyManager{CreateKeyValue: ed25519KH},
		ProtocolStateStorageProviderValue: protocolStateStoreProvider,
		StorageProviderValue:              storeProvider,
		ServiceMap: map[string]interface{}{
			didexchange.DIDExchange: svc,
			mediator.Coordination:   routeService(),
		},
	}

	return context
}

func mockContext() provider {
	return &mockprovider.Provider{
		ServiceValue: routeService(),
	}
}

func routeService() *mockroute.MockMediatorSvc {
	return &mockroute.MockMediatorSvc{
		RegisterFunc: func(connectionID string, options ...mediator.ClientOption) error {
			if connectionID == "" {
				return errors.New("connection ID is mandatory")
			}

			return nil
		},
		Connections:    []string{"xyz"},
		RouterEndpoint: "http://router.example.com",
		RoutingKeys:    []string{"abc", "xyz"},
	}
}
Output:

successfully registered with router
http://router.example.com
[abc xyz]

Index

Examples

Constants

View Source
const (
	// ProtocolName is the framework's friendly name for the route-coordination protocol.
	ProtocolName = mediator.Coordination
	// RequestMsgType defines the route coordination request message type.
	RequestMsgType = mediator.RequestMsgType
)

Variables

This section is empty.

Functions

func WithTimeout

func WithTimeout(t time.Duration) mediator.ClientOption

WithTimeout option is for definition timeout value waiting for responses received from the router.

Types

type Client

type Client struct {
	service.Event
	// contains filtered or unexported fields
}

Client enable access to route api.

func New

func New(ctx provider, options ...mediator.ClientOption) (*Client, error)

New return new instance of route client.

func (*Client) GetConfig

func (c *Client) GetConfig(connID string) (*mediator.Config, error)

GetConfig returns the router's configuration.

func (*Client) GetConnections

func (c *Client) GetConnections(options ...ConnectionOption) ([]string, error)

GetConnections returns router`s connections.

func (*Client) Register

func (c *Client) Register(connectionID string) error

Register the agent with the router(passed in connectionID). This function asks router's permission to publish it's endpoint and routing keys.

func (*Client) Unregister

func (c *Client) Unregister(connID string) error

Unregister unregisters the agent with the router.

type ConnectionOption

type ConnectionOption = mediator.ConnectionOption

ConnectionOption option for Client.GetConnections.

type Request

type Request = mediator.Request

Request is the route-request message of this protocol.

func NewRequest

func NewRequest() *Request

NewRequest creates a new request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL