github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/mediator/example_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package mediator
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  
    13  	didexClient "github.com/hyperledger/aries-framework-go/pkg/client/didexchange"
    14  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
    15  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/didexchange"
    16  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
    17  	mockprotocol "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol"
    18  	mockroute "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol/mediator"
    19  	mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms"
    20  	mockprovider "github.com/hyperledger/aries-framework-go/pkg/mock/provider"
    21  	mockstore "github.com/hyperledger/aries-framework-go/pkg/mock/storage"
    22  )
    23  
    24  func Example() {
    25  	// Create DID Exchange Client and perform DID Exchange with the Router
    26  	didExClient, err := didexClient.New(didClientMockContext())
    27  	if err != nil {
    28  		fmt.Println("failed to create client for Alice")
    29  	}
    30  
    31  	// Get the connection ID
    32  	routerConnID := performDIDExchangeWithRouter(didExClient)
    33  
    34  	// Create Route Client
    35  	client, err := New(mockContext())
    36  	if err != nil {
    37  		fmt.Println("failed to create route client")
    38  	}
    39  
    40  	// Register agent with the router
    41  	err = client.Register(routerConnID)
    42  	if err != nil {
    43  		fmt.Printf("failed to register the agent with router : %s\n", err.Error())
    44  	}
    45  
    46  	fmt.Println("successfully registered with router")
    47  
    48  	// generate invitation after route has been registered
    49  	invitation, err := didExClient.CreateInvitation("alice-agent", didexClient.WithRouterConnectionID("xyz"))
    50  	if err != nil {
    51  		fmt.Println("failed to create invitation after route registration")
    52  	}
    53  
    54  	fmt.Println(invitation.ServiceEndpoint)
    55  	fmt.Println(invitation.RoutingKeys)
    56  
    57  	// Output: successfully registered with router
    58  	// http://router.example.com
    59  	// [abc xyz]
    60  }
    61  
    62  func performDIDExchangeWithRouter(client *didexClient.Client) string {
    63  	router, err := didexClient.New(didClientMockContext())
    64  	if err != nil {
    65  		fmt.Println("failed to create client for Bob")
    66  	}
    67  
    68  	routerActions := make(chan service.DIDCommAction)
    69  
    70  	err = router.RegisterActionEvent(routerActions)
    71  	if err != nil {
    72  		fmt.Println("failed to create Bob's action channel")
    73  	}
    74  
    75  	go func() {
    76  		service.AutoExecuteActionEvent(routerActions)
    77  	}()
    78  
    79  	aliceActions := make(chan service.DIDCommAction)
    80  
    81  	err = client.RegisterActionEvent(aliceActions)
    82  	if err != nil {
    83  		fmt.Println("failed to create Alice's action channel")
    84  	}
    85  
    86  	go func() {
    87  		service.AutoExecuteActionEvent(aliceActions)
    88  	}()
    89  
    90  	invitation, err := router.CreateInvitation("router invites alice")
    91  	if err != nil {
    92  		fmt.Printf("failed to create invitation: %s\n", err)
    93  	}
    94  
    95  	connectionID, err := client.HandleInvitation(invitation)
    96  	if err != nil {
    97  		fmt.Printf("failed to handle invitation: %s\n", err)
    98  	}
    99  
   100  	return connectionID
   101  }
   102  
   103  func didClientMockContext() *mockprovider.Provider {
   104  	protocolStateStoreProvider := mockstore.NewMockStoreProvider()
   105  	storeProvider := mockstore.NewMockStoreProvider()
   106  	mockProvider := &mockprotocol.MockProvider{
   107  		ProtocolStateStoreProvider: protocolStateStoreProvider,
   108  		StoreProvider:              storeProvider,
   109  		ServiceMap: map[string]interface{}{
   110  			mediator.Coordination: &mockroute.MockMediatorSvc{},
   111  		},
   112  	}
   113  
   114  	svc, err := didexchange.New(mockProvider)
   115  	if err != nil {
   116  		panic(err)
   117  	}
   118  
   119  	ed25519KH, err := mockkms.CreateMockED25519KeyHandle()
   120  	if err != nil {
   121  		panic(err)
   122  	}
   123  
   124  	context := &mockprovider.Provider{
   125  		KMSValue:                          &mockkms.KeyManager{CreateKeyValue: ed25519KH},
   126  		ProtocolStateStorageProviderValue: protocolStateStoreProvider,
   127  		StorageProviderValue:              storeProvider,
   128  		ServiceMap: map[string]interface{}{
   129  			didexchange.DIDExchange: svc,
   130  			mediator.Coordination:   routeService(),
   131  		},
   132  	}
   133  
   134  	return context
   135  }
   136  
   137  func mockContext() provider {
   138  	return &mockprovider.Provider{
   139  		ServiceValue: routeService(),
   140  	}
   141  }
   142  
   143  func routeService() *mockroute.MockMediatorSvc {
   144  	return &mockroute.MockMediatorSvc{
   145  		RegisterFunc: func(connectionID string, options ...mediator.ClientOption) error {
   146  			if connectionID == "" {
   147  				return errors.New("connection ID is mandatory")
   148  			}
   149  
   150  			return nil
   151  		},
   152  		Connections:    []string{"xyz"},
   153  		RouterEndpoint: "http://router.example.com",
   154  		RoutingKeys:    []string{"abc", "xyz"},
   155  	}
   156  }