route

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package route 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 recivies 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/route"
	mockprotocol "github.com/hyperledger/aries-framework-go/pkg/internal/mock/didcomm/protocol"
	mockroute "github.com/hyperledger/aries-framework-go/pkg/internal/mock/didcomm/protocol/route"
	mockprovider "github.com/hyperledger/aries-framework-go/pkg/internal/mock/provider"
	mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms/legacykms"
	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.Println("failed to register the agent with router")
	}

	fmt.Println("successfully registered with router")

	// generate invitation after route has been registered
	invitation, err := didExClient.CreateInvitation("alice-agent")
	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 {
	transientStoreProvider := mockstore.NewMockStoreProvider()
	storeProvider := mockstore.NewMockStoreProvider()
	mockProvider := &mockprotocol.MockProvider{
		TransientStoreProvider: transientStoreProvider,
		StoreProvider:          storeProvider,
		ServiceMap: map[string]interface{}{
			route.Coordination: &mockroute.MockRouteSvc{},
		},
	}

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

	context := &mockprovider.Provider{
		KMSValue:                      &mockkms.CloseableKMS{CreateEncryptionKeyValue: "sample-key"},
		TransientStorageProviderValue: transientStoreProvider,
		StorageProviderValue:          storeProvider,
		ServiceMap: map[string]interface{}{
			didexchange.DIDExchange: svc,
			route.Coordination:      routeService(),
		},
	}

	return context
}

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

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

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

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client enable access to route api.

func New

func New(ctx provider) (*Client, error)

New return new instance of route client.

func (*Client) GetConnection added in v0.1.2

func (c *Client) GetConnection() (string, error)

GetConnection returns the connectionID of the router.

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 added in v0.1.2

func (c *Client) Unregister() error

Unregister unregisters the agent with the router.

Jump to

Keyboard shortcuts

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