github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/presentproof/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 presentproof
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  	"time"
    13  
    14  	"github.com/golang/mock/gomock"
    15  	"github.com/google/uuid"
    16  
    17  	"github.com/hyperledger/aries-framework-go/component/storageutil/mem"
    18  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
    19  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/messenger"
    20  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/presentproof"
    21  	mocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/client/presentproof"
    22  	dispatchermocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/didcomm/dispatcher"
    23  	messengermocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/didcomm/messenger"
    24  	protocolmocks "github.com/hyperledger/aries-framework-go/pkg/internal/gomocks/didcomm/protocol/presentproof"
    25  	"github.com/hyperledger/aries-framework-go/pkg/store/connection"
    26  )
    27  
    28  // payload represents a transport message structure.
    29  type payload struct {
    30  	msg      []byte
    31  	myDID    string
    32  	theirDID string
    33  }
    34  
    35  //nolint:forbidigo
    36  func mockContext(agent string, tr map[string]chan payload) Provider {
    37  	ctrl := gomock.NewController(nil)
    38  
    39  	outbound := dispatchermocks.NewMockOutbound(ctrl)
    40  	outbound.EXPECT().
    41  		SendToDID(gomock.Any(), gomock.Any(), gomock.Any()).
    42  		DoAndReturn(func(msg interface{}, myDID, theirDID string) error {
    43  			src, err := json.Marshal(msg)
    44  			if err != nil {
    45  				fmt.Println(err)
    46  			}
    47  
    48  			tr[theirDID] <- payload{
    49  				msg:      src,
    50  				myDID:    theirDID,
    51  				theirDID: myDID,
    52  			}
    53  
    54  			return nil
    55  		}).AnyTimes()
    56  
    57  	store := mem.NewProvider()
    58  
    59  	mProvider := messengermocks.NewMockProvider(ctrl)
    60  	mProvider.EXPECT().StorageProvider().Return(store)
    61  	mProvider.EXPECT().OutboundDispatcher().Return(outbound)
    62  
    63  	provider := protocolmocks.NewMockProvider(ctrl)
    64  	provider.EXPECT().StorageProvider().Return(store).MaxTimes(2)
    65  
    66  	msgSvc, err := messenger.NewMessenger(mProvider)
    67  	if err != nil {
    68  		fmt.Println(err)
    69  	}
    70  
    71  	provider.EXPECT().Messenger().Return(msgSvc)
    72  
    73  	svc, err := presentproof.New(provider)
    74  	if err != nil {
    75  		fmt.Println(err)
    76  	}
    77  
    78  	go func() {
    79  		for {
    80  			select {
    81  			case msg := <-tr[agent]:
    82  				didMap, err := service.ParseDIDCommMsgMap(msg.msg)
    83  				if err != nil {
    84  					fmt.Println(err)
    85  				}
    86  
    87  				fmt.Println(agent, "received", didMap.Type(), "from", msg.theirDID)
    88  
    89  				if err = msgSvc.HandleInbound(didMap, service.NewDIDCommContext(msg.myDID, msg.theirDID, nil)); err != nil {
    90  					fmt.Println(err)
    91  				}
    92  
    93  				_, err = svc.HandleInbound(didMap, service.NewDIDCommContext(msg.myDID, msg.theirDID, nil))
    94  				if err != nil {
    95  					fmt.Println(err)
    96  				}
    97  			case <-time.After(time.Second):
    98  				return
    99  			}
   100  		}
   101  	}()
   102  
   103  	provider1 := mocks.NewMockProvider(ctrl)
   104  	provider1.EXPECT().Service(gomock.Any()).Return(svc, nil)
   105  
   106  	return provider1
   107  }
   108  
   109  func ExampleClient_SendRequestPresentation() {
   110  	transport := map[string]chan payload{
   111  		Alice: make(chan payload),
   112  		Bob:   make(chan payload),
   113  	}
   114  
   115  	// Alice creates client
   116  	clientAlice, err := New(mockContext(Alice, transport))
   117  	if err != nil {
   118  		panic(err)
   119  	}
   120  
   121  	// Alice registers channel for actions
   122  	actionsAlice := make(chan service.DIDCommAction)
   123  
   124  	err = clientAlice.RegisterActionEvent(actionsAlice)
   125  	if err != nil {
   126  		panic(err)
   127  	}
   128  
   129  	// Bob creates client
   130  	clientBob, err := New(mockContext(Bob, transport))
   131  	if err != nil {
   132  		panic(err)
   133  	}
   134  
   135  	// Bob registers channel for actions
   136  	actionsBob := make(chan service.DIDCommAction)
   137  
   138  	err = clientBob.RegisterActionEvent(actionsBob)
   139  	if err != nil {
   140  		panic(err)
   141  	}
   142  
   143  	go func() {
   144  		for {
   145  			var acceptErr error
   146  
   147  			select {
   148  			case e := <-actionsAlice:
   149  				acceptErr = clientAlice.AcceptPresentation(e.Properties.All()["piid"].(string))
   150  			case e := <-actionsBob:
   151  				acceptErr = clientBob.AcceptRequestPresentation(e.Properties.All()["piid"].(string), &Presentation{}, nil)
   152  			}
   153  
   154  			if acceptErr != nil {
   155  				fmt.Println(acceptErr)
   156  			}
   157  		}
   158  	}()
   159  
   160  	conn := connection.Record{
   161  		ConnectionID: uuid.New().String(),
   162  		MyDID:        Alice,
   163  		TheirDID:     Bob,
   164  	}
   165  
   166  	// Alice
   167  	waitForAlice := waitForFn(clientAlice)
   168  	// Bob
   169  	waitForBob := waitForFn(clientBob)
   170  
   171  	_, err = clientAlice.SendRequestPresentation(&RequestPresentation{}, &conn)
   172  	if err != nil {
   173  		fmt.Println(err)
   174  	}
   175  
   176  	waitForAlice()
   177  	waitForBob()
   178  
   179  	// Output:
   180  	// Bob received https://didcomm.org/present-proof/2.0/request-presentation from Alice
   181  	// Alice received https://didcomm.org/present-proof/2.0/presentation from Bob
   182  }
   183  
   184  func ExampleClient_SendRequestPresentation_using_v3() {
   185  	transport := map[string]chan payload{
   186  		Alice: make(chan payload),
   187  		Bob:   make(chan payload),
   188  	}
   189  
   190  	// Alice creates client
   191  	clientAlice, err := New(mockContext(Alice, transport))
   192  	if err != nil {
   193  		panic(err)
   194  	}
   195  
   196  	// Alice registers channel for actions
   197  	actionsAlice := make(chan service.DIDCommAction)
   198  
   199  	err = clientAlice.RegisterActionEvent(actionsAlice)
   200  	if err != nil {
   201  		panic(err)
   202  	}
   203  
   204  	// Bob creates client
   205  	clientBob, err := New(mockContext(Bob, transport))
   206  	if err != nil {
   207  		panic(err)
   208  	}
   209  
   210  	// Bob registers channel for actions
   211  	actionsBob := make(chan service.DIDCommAction)
   212  
   213  	err = clientBob.RegisterActionEvent(actionsBob)
   214  	if err != nil {
   215  		panic(err)
   216  	}
   217  
   218  	go func() {
   219  		for {
   220  			var acceptErr error
   221  
   222  			select {
   223  			case e := <-actionsAlice:
   224  				acceptErr = clientAlice.AcceptPresentation(e.Properties.All()["piid"].(string))
   225  			case e := <-actionsBob:
   226  				acceptErr = clientBob.AcceptRequestPresentation(e.Properties.All()["piid"].(string), &Presentation{}, nil)
   227  			}
   228  
   229  			if acceptErr != nil {
   230  				fmt.Println(acceptErr)
   231  			}
   232  		}
   233  	}()
   234  
   235  	conn := connection.Record{
   236  		ConnectionID:   uuid.New().String(),
   237  		MyDID:          Alice,
   238  		TheirDID:       Bob,
   239  		DIDCommVersion: service.V2,
   240  	}
   241  
   242  	// Alice
   243  	waitForAlice := waitForFn(clientAlice)
   244  	// Bob
   245  	waitForBob := waitForFn(clientBob)
   246  
   247  	_, err = clientAlice.SendRequestPresentation(&RequestPresentation{}, &conn)
   248  	if err != nil {
   249  		fmt.Println(err)
   250  	}
   251  
   252  	waitForAlice()
   253  	waitForBob()
   254  
   255  	// Output:
   256  	// Bob received https://didcomm.org/present-proof/3.0/request-presentation from Alice
   257  	// Alice received https://didcomm.org/present-proof/3.0/presentation from Bob
   258  }
   259  
   260  func ExampleClient_SendRequestPresentation_third() {
   261  	transport := map[string]chan payload{
   262  		Alice: make(chan payload),
   263  		Bob:   make(chan payload),
   264  	}
   265  
   266  	// Alice creates client
   267  	clientAlice, err := New(mockContext(Alice, transport))
   268  	if err != nil {
   269  		panic(err)
   270  	}
   271  
   272  	// Alice registers channel for actions
   273  	actionsAlice := make(chan service.DIDCommAction)
   274  
   275  	err = clientAlice.RegisterActionEvent(actionsAlice)
   276  	if err != nil {
   277  		panic(err)
   278  	}
   279  
   280  	// Bob creates client
   281  	clientBob, err := New(mockContext(Bob, transport))
   282  	if err != nil {
   283  		panic(err)
   284  	}
   285  
   286  	// Bob registers channel for actions
   287  	actionsBob := make(chan service.DIDCommAction)
   288  
   289  	err = clientBob.RegisterActionEvent(actionsBob)
   290  	if err != nil {
   291  		panic(err)
   292  	}
   293  
   294  	go func() {
   295  		for {
   296  			var acceptErr error
   297  
   298  			select {
   299  			case e := <-actionsAlice:
   300  				acceptErr = clientAlice.AcceptPresentation(e.Properties.All()["piid"].(string))
   301  			case e := <-actionsBob:
   302  				acceptErr = clientBob.AcceptRequestPresentation(e.Properties.All()["piid"].(string), &Presentation{}, nil)
   303  			}
   304  
   305  			if acceptErr != nil {
   306  				fmt.Println(acceptErr)
   307  			}
   308  		}
   309  	}()
   310  
   311  	conn := connection.Record{
   312  		ConnectionID: uuid.New().String(),
   313  		MyDID:        Alice,
   314  		TheirDID:     Bob,
   315  	}
   316  
   317  	// Alice
   318  	waitForAlice := waitForFn(clientAlice)
   319  	// Bob
   320  	waitForBob := waitForFn(clientBob)
   321  
   322  	_, err = clientAlice.SendRequestPresentation(&RequestPresentation{WillConfirm: true}, &conn)
   323  	if err != nil {
   324  		fmt.Println(err)
   325  	}
   326  
   327  	waitForAlice()
   328  	waitForBob()
   329  
   330  	// Output:
   331  	// Bob received https://didcomm.org/present-proof/2.0/request-presentation from Alice
   332  	// Alice received https://didcomm.org/present-proof/2.0/presentation from Bob
   333  	// Bob received https://didcomm.org/present-proof/2.0/ack from Alice
   334  }
   335  
   336  func ExampleClient_SendRequestPresentation_using_v3_second() {
   337  	transport := map[string]chan payload{
   338  		Alice: make(chan payload),
   339  		Bob:   make(chan payload),
   340  	}
   341  
   342  	// Alice creates client
   343  	clientAlice, err := New(mockContext(Alice, transport))
   344  	if err != nil {
   345  		panic(err)
   346  	}
   347  
   348  	// Alice registers channel for actions
   349  	actionsAlice := make(chan service.DIDCommAction)
   350  
   351  	err = clientAlice.RegisterActionEvent(actionsAlice)
   352  	if err != nil {
   353  		panic(err)
   354  	}
   355  
   356  	// Bob creates client
   357  	clientBob, err := New(mockContext(Bob, transport))
   358  	if err != nil {
   359  		panic(err)
   360  	}
   361  
   362  	// Bob registers channel for actions
   363  	actionsBob := make(chan service.DIDCommAction)
   364  
   365  	err = clientBob.RegisterActionEvent(actionsBob)
   366  	if err != nil {
   367  		panic(err)
   368  	}
   369  
   370  	go func() {
   371  		for {
   372  			var acceptErr error
   373  
   374  			select {
   375  			case e := <-actionsAlice:
   376  				acceptErr = clientAlice.AcceptPresentation(e.Properties.All()["piid"].(string))
   377  			case e := <-actionsBob:
   378  				acceptErr = clientBob.AcceptRequestPresentation(e.Properties.All()["piid"].(string), &Presentation{}, nil)
   379  			}
   380  
   381  			if acceptErr != nil {
   382  				fmt.Println(acceptErr)
   383  			}
   384  		}
   385  	}()
   386  
   387  	conn := connection.Record{
   388  		ConnectionID:   uuid.New().String(),
   389  		MyDID:          Alice,
   390  		TheirDID:       Bob,
   391  		DIDCommVersion: service.V2,
   392  	}
   393  
   394  	// Alice
   395  	waitForAlice := waitForFn(clientAlice)
   396  	// Bob
   397  	waitForBob := waitForFn(clientBob)
   398  
   399  	_, err = clientAlice.SendRequestPresentation(&RequestPresentation{WillConfirm: true}, &conn)
   400  	if err != nil {
   401  		fmt.Println(err)
   402  	}
   403  
   404  	waitForAlice()
   405  	waitForBob()
   406  
   407  	// Output:
   408  	// Bob received https://didcomm.org/present-proof/3.0/request-presentation from Alice
   409  	// Alice received https://didcomm.org/present-proof/3.0/presentation from Bob
   410  	// Bob received https://didcomm.org/present-proof/3.0/ack from Alice
   411  }
   412  
   413  // nolint: gocyclo
   414  func ExampleClient_SendProposePresentation() {
   415  	transport := map[string]chan payload{
   416  		Alice: make(chan payload),
   417  		Bob:   make(chan payload),
   418  	}
   419  
   420  	// Alice creates client
   421  	clientAlice, err := New(mockContext(Alice, transport))
   422  	if err != nil {
   423  		panic(err)
   424  	}
   425  
   426  	// Alice registers channel for actions
   427  	actionsAlice := make(chan service.DIDCommAction)
   428  
   429  	err = clientAlice.RegisterActionEvent(actionsAlice)
   430  	if err != nil {
   431  		panic(err)
   432  	}
   433  
   434  	// Bob creates client
   435  	clientBob, err := New(mockContext(Bob, transport))
   436  	if err != nil {
   437  		panic(err)
   438  	}
   439  
   440  	// Bob registers channel for actions
   441  	actionsBob := make(chan service.DIDCommAction)
   442  
   443  	err = clientBob.RegisterActionEvent(actionsBob)
   444  	if err != nil {
   445  		panic(err)
   446  	}
   447  
   448  	go func() {
   449  		for {
   450  			var acceptErr error
   451  
   452  			var e service.DIDCommAction
   453  
   454  			select {
   455  			case e = <-actionsAlice:
   456  			case e = <-actionsBob:
   457  			}
   458  
   459  			piid, ok := e.Properties.All()["piid"].(string)
   460  			if !ok {
   461  				fmt.Println("empty piid")
   462  			}
   463  
   464  			if e.Message.Type() == presentproof.PresentationMsgTypeV2 {
   465  				acceptErr = clientBob.AcceptPresentation(piid)
   466  			}
   467  
   468  			if e.Message.Type() == presentproof.ProposePresentationMsgTypeV2 {
   469  				acceptErr = clientBob.AcceptProposePresentation(piid, &RequestPresentation{WillConfirm: true})
   470  			}
   471  
   472  			if e.Message.Type() == presentproof.RequestPresentationMsgTypeV2 {
   473  				acceptErr = clientAlice.AcceptRequestPresentation(piid, &Presentation{}, nil)
   474  			}
   475  
   476  			if acceptErr != nil {
   477  				fmt.Println(acceptErr)
   478  			}
   479  		}
   480  	}()
   481  
   482  	conn := connection.Record{
   483  		ConnectionID: uuid.New().String(),
   484  		MyDID:        Alice,
   485  		TheirDID:     Bob,
   486  	}
   487  
   488  	// Alice
   489  	waitForAlice := waitForFn(clientAlice)
   490  	// Bob
   491  	waitForBob := waitForFn(clientBob)
   492  
   493  	_, err = clientAlice.SendProposePresentation(&ProposePresentation{}, &conn)
   494  	if err != nil {
   495  		fmt.Println(err)
   496  	}
   497  
   498  	waitForAlice()
   499  	waitForBob()
   500  
   501  	// Output:
   502  	// Bob received https://didcomm.org/present-proof/2.0/propose-presentation from Alice
   503  	// Alice received https://didcomm.org/present-proof/2.0/request-presentation from Bob
   504  	// Bob received https://didcomm.org/present-proof/2.0/presentation from Alice
   505  	// Alice received https://didcomm.org/present-proof/2.0/ack from Bob
   506  }
   507  
   508  // nolint: gocyclo
   509  func ExampleClient_SendProposePresentation_using_v3() {
   510  	transport := map[string]chan payload{
   511  		Alice: make(chan payload),
   512  		Bob:   make(chan payload),
   513  	}
   514  
   515  	// Alice creates client
   516  	clientAlice, err := New(mockContext(Alice, transport))
   517  	if err != nil {
   518  		panic(err)
   519  	}
   520  
   521  	// Alice registers channel for actions
   522  	actionsAlice := make(chan service.DIDCommAction)
   523  
   524  	err = clientAlice.RegisterActionEvent(actionsAlice)
   525  	if err != nil {
   526  		panic(err)
   527  	}
   528  
   529  	// Bob creates client
   530  	clientBob, err := New(mockContext(Bob, transport))
   531  	if err != nil {
   532  		panic(err)
   533  	}
   534  
   535  	// Bob registers channel for actions
   536  	actionsBob := make(chan service.DIDCommAction)
   537  
   538  	err = clientBob.RegisterActionEvent(actionsBob)
   539  	if err != nil {
   540  		panic(err)
   541  	}
   542  
   543  	go func() {
   544  		for {
   545  			var acceptErr error
   546  
   547  			var e service.DIDCommAction
   548  
   549  			select {
   550  			case e = <-actionsAlice:
   551  			case e = <-actionsBob:
   552  			}
   553  
   554  			piid, ok := e.Properties.All()["piid"].(string)
   555  			if !ok {
   556  				fmt.Println("empty piid")
   557  			}
   558  
   559  			if e.Message.Type() == presentproof.PresentationMsgTypeV3 {
   560  				acceptErr = clientBob.AcceptPresentation(piid)
   561  			}
   562  
   563  			if e.Message.Type() == presentproof.ProposePresentationMsgTypeV3 {
   564  				rp3 := &RequestPresentation{}
   565  				rp3.WillConfirm = true
   566  
   567  				acceptErr = clientBob.AcceptProposePresentation(piid, rp3)
   568  			}
   569  
   570  			if e.Message.Type() == presentproof.RequestPresentationMsgTypeV3 {
   571  				acceptErr = clientAlice.AcceptRequestPresentation(piid, &Presentation{}, nil)
   572  			}
   573  
   574  			if acceptErr != nil {
   575  				fmt.Println(acceptErr)
   576  			}
   577  		}
   578  	}()
   579  
   580  	conn := connection.Record{
   581  		ConnectionID:   uuid.New().String(),
   582  		MyDID:          Alice,
   583  		TheirDID:       Bob,
   584  		DIDCommVersion: service.V2,
   585  	}
   586  
   587  	// Alice
   588  	waitForAlice := waitForFn(clientAlice)
   589  	// Bob
   590  	waitForBob := waitForFn(clientBob)
   591  
   592  	_, err = clientAlice.SendProposePresentation(&ProposePresentation{}, &conn)
   593  	if err != nil {
   594  		fmt.Println(err)
   595  	}
   596  
   597  	waitForAlice()
   598  	waitForBob()
   599  
   600  	// Output:
   601  	// Bob received https://didcomm.org/present-proof/3.0/propose-presentation from Alice
   602  	// Alice received https://didcomm.org/present-proof/3.0/request-presentation from Bob
   603  	// Bob received https://didcomm.org/present-proof/3.0/presentation from Alice
   604  	// Alice received https://didcomm.org/present-proof/3.0/ack from Bob
   605  }
   606  
   607  func waitForFn(c *Client) func() {
   608  	const stateDone = "done"
   609  
   610  	agent := make(chan service.StateMsg, 10)
   611  
   612  	if err := c.RegisterMsgEvent(agent); err != nil {
   613  		panic(err)
   614  	}
   615  
   616  	done := make(chan struct{})
   617  
   618  	return func() {
   619  		go func() {
   620  			for st := range agent {
   621  				if st.StateID == stateDone && st.Type == service.PostState {
   622  					done <- struct{}{}
   623  				}
   624  			}
   625  		}()
   626  
   627  		select {
   628  		case <-done:
   629  		case <-time.After(time.Second):
   630  			fmt.Println("timeout") //nolint:forbidigo
   631  		}
   632  	}
   633  }