github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/didexchange/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 didexchange 8 9 import ( 10 "fmt" 11 12 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 13 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/didexchange" 14 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator" 15 mockprotocol "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol" 16 mockroute "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol/mediator" 17 mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms" 18 mockprovider "github.com/hyperledger/aries-framework-go/pkg/mock/provider" 19 mockstore "github.com/hyperledger/aries-framework-go/pkg/mock/storage" 20 ) 21 22 func Example() { 23 bob, err := New(mockContext()) 24 if err != nil { 25 fmt.Println("failed to create client for Bob") 26 } 27 28 bobActions := make(chan service.DIDCommAction, 1) 29 30 err = bob.RegisterActionEvent(bobActions) 31 if err != nil { 32 fmt.Println("failed to create Bob's action channel") 33 } 34 35 go func() { 36 service.AutoExecuteActionEvent(bobActions) 37 }() 38 39 alice, err := New(mockContext()) 40 if err != nil { 41 fmt.Println("failed to create client for Alice") 42 } 43 44 aliceActions := make(chan service.DIDCommAction, 1) 45 46 err = alice.RegisterActionEvent(aliceActions) 47 if err != nil { 48 fmt.Println("failed to create Alice's action channel") 49 } 50 51 go func() { 52 service.AutoExecuteActionEvent(aliceActions) 53 }() 54 55 invitation, err := bob.CreateInvitation("bob invites alice") 56 if err != nil { 57 fmt.Printf("failed to create invitation: %s\n", err) 58 } 59 60 connectionID, err := alice.HandleInvitation(invitation) 61 if err != nil { 62 fmt.Printf("failed to handle invitation: %s\n", err) 63 } 64 65 connection, err := alice.GetConnection(connectionID) 66 if err != nil { 67 fmt.Printf("failed to get connection: %s\n", err) 68 } 69 70 fmt.Println(connection.TheirLabel) 71 72 // Output: bob invites alice 73 } 74 75 func ExampleNew() { 76 ctx := mockContext() 77 78 c, err := New(ctx) 79 if err != nil { 80 fmt.Println(err) 81 } 82 83 if c != nil { 84 fmt.Println("client created") 85 } else { 86 fmt.Println("client is nil") 87 } 88 89 // Output: client created 90 } 91 92 func ExampleClient_CreateInvitation() { 93 bob, err := New(mockContext()) 94 if err != nil { 95 fmt.Println("failed to create client for Bob") 96 } 97 98 invitation, err := bob.CreateInvitation("bob invites julia") 99 if err != nil { 100 fmt.Printf("failed to create invitation: %s\n", err) 101 } 102 103 fmt.Println(invitation.Label) 104 105 // Output: bob invites julia 106 } 107 108 func ExampleClient_CreateInvitationWithDID() { 109 bob, err := New(mockContext()) 110 if err != nil { 111 fmt.Println("failed to create client for Bob") 112 } 113 114 invitation, err := bob.CreateInvitationWithDID("bob invites maria", "did:example:abc-123") 115 if err != nil { 116 fmt.Printf("failed to create invitation with DID: %s\n", err) 117 } 118 119 fmt.Println(invitation.DID) 120 121 // Output: did:example:abc-123 122 } 123 124 func mockContext() provider { 125 protocolStateStoreProvider := mockstore.NewMockStoreProvider() 126 storeProvider := mockstore.NewMockStoreProvider() 127 mockProvider := &mockprotocol.MockProvider{ 128 ProtocolStateStoreProvider: protocolStateStoreProvider, 129 StoreProvider: storeProvider, 130 ServiceMap: map[string]interface{}{ 131 mediator.Coordination: &mockroute.MockMediatorSvc{}, 132 }, 133 } 134 135 svc, err := didexchange.New(mockProvider) 136 if err != nil { 137 panic(err) 138 } 139 140 mockSigningKey, err := mockkms.CreateMockED25519KeyHandle() 141 if err != nil { 142 panic(err) 143 } 144 145 context := &mockprovider.Provider{ 146 KMSValue: &mockkms.KeyManager{ 147 CreateKeyID: "signing-key-123", 148 CreateKeyValue: mockSigningKey, 149 }, 150 ProtocolStateStorageProviderValue: protocolStateStoreProvider, 151 StorageProviderValue: storeProvider, 152 ServiceMap: map[string]interface{}{ 153 didexchange.DIDExchange: svc, 154 mediator.Coordination: &mockroute.MockMediatorSvc{}, 155 }, 156 } 157 158 return context 159 }