This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
/
legacyconnection_controller_steps.go
726 lines (571 loc) · 22.4 KB
/
legacyconnection_controller_steps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
Copyright Avast Software. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package legacyconnection
import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/cucumber/godog"
"github.com/hyperledger/aries-framework-go/pkg/client/legacyconnection"
"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/common/model"
cmdkms "github.com/hyperledger/aries-framework-go/pkg/controller/command/kms"
legconncmd "github.com/hyperledger/aries-framework-go/pkg/controller/command/legacyconnection"
"github.com/hyperledger/aries-framework-go/pkg/doc/jose/jwk/jwksupport"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/test/bdd/pkg/context"
"github.com/hyperledger/aries-framework-go/test/bdd/pkg/sidetree"
"github.com/hyperledger/aries-framework-go/test/bdd/pkg/util"
)
const (
connOperationID = "/legacy/connections"
kmsOperationID = "/kms"
createInvitationPath = connOperationID + "/create-invitation"
createImplicitInvitationPath = connOperationID + "/create-implicit-invitation"
receiveInvtiationPath = connOperationID + "/receive-invitation"
acceptInvitationPath = connOperationID + "/%s/accept-invitation?public=%s" // router_connections
acceptRequestPath = connOperationID + "/%s/accept-request?public=%s"
connectionsByID = connOperationID + "/{id}"
createKeySetPath = kmsOperationID + "/keyset"
timeoutWaitForDID = 10 * time.Second
sideTreeURL = "${SIDETREE_URL}"
)
var logger = log.New("aries-framework/legacy-connection-tests")
// ControllerSteps is steps for legacy-connection with controller.
type ControllerSteps struct {
bddContext *context.BDDContext
invitations map[string]*legacyconnection.Invitation
connectionIDs map[string]string
agentServiceEndpoints map[string]string
}
// NewLegacyConnectionControllerSteps creates steps for legacy-connection with controller.
func NewLegacyConnectionControllerSteps() *ControllerSteps {
return &ControllerSteps{
invitations: make(map[string]*legacyconnection.Invitation),
connectionIDs: make(map[string]string),
agentServiceEndpoints: map[string]string{
"https://localhost:8082": "https://alice.aries.example.com:8081",
"https://localhost:9082": "https://bob.agent.example.com:9081",
},
}
}
// SetContext is called before every scenario is run with a fresh new context.
func (a *ControllerSteps) SetContext(ctx *context.BDDContext) {
a.bddContext = ctx
}
// RegisterSteps registers agent steps.
func (a *ControllerSteps) RegisterSteps(s *godog.Suite) {
s.Step(`^"([^"]*)" creates legacy invitation through controller with label "([^"]*)"$`, a.createInvitation)
s.Step(`^"([^"]*)" creates legacy invitation through controller with label "([^"]*)" and router "([^"]*)"$`,
a.createInvitationWithRouter)
s.Step(`^"([^"]*)" receives legacy invitation from "([^"]*)" through controller$`, a.receiveInvitation)
s.Step(`^"([^"]*)" approves connection invitation through controller$`, a.approveInvitation)
s.Step(`^"([^"]*)" approves connection invitation with router "([^"]*)" through controller$`,
a.approveInvitationWithRouter)
s.Step(`^"([^"]*)" approves connection request through controller`, a.ApproveRequest)
s.Step(`^"([^"]*)" approves connection request with router "([^"]*)" through controller$`,
a.ApproveRequestWithRouter)
s.Step(`^"([^"]*)" waits for legacy post state event "([^"]*)" to web notifier`, a.WaitForPostEvent)
s.Step(`^"([^"]*)" retrieves connection record through controller and validates that legacy connection state is "([^"]*)"$`, a.validateConnections) //nolint: lll
// public DID steps
s.Step(`^"([^"]*)" creates "([^"]*)" public DID through legacy controller`, a.CreatePublicDID)
s.Step(`^"([^"]*)" creates legacy invitation through controller using public DID and label "([^"]*)"$`,
a.createInvitationWithDID)
s.Step(`^"([^"]*)" approves legacy invitation with public DID through controller`,
a.approveInvitationWithPublicDID)
s.Step(`^"([^"]*)" approves connection request with public DID through controller`,
a.approveRequestWithPublicDID)
s.Step(`^"([^"]*)" initiates legacy connection through controller with "([^"]*)" using peer DID$`,
a.createImplicitInvitation)
s.Step(`^"([^"]*)" initiates legacy connection through controller with "([^"]*)" using public DID$`,
a.createImplicitInvitationWithDID)
s.Step(`^"([^"]*)" has established connection with "([^"]*)" through legacy connection using controller$`,
a.performLegacyConnection)
s.Step(`^"([^"]*)" saves the connectionID to "([^"]*)" variable$`, a.saveConnectionID)
}
// EstablishConnection establishes connection between two agents through legacy-connection protocol.
func (a *ControllerSteps) EstablishConnection(inviter, invitee string) error {
return a.performLegacyConnection(inviter, invitee)
}
// ConnectionIDs gets connection IDs.
func (a *ControllerSteps) ConnectionIDs() map[string]string {
return a.connectionIDs
}
func (a *ControllerSteps) pullEventsFromWebSocket(agentID, state string) (string, error) {
msg, err := util.PullEventsFromWebSocket(a.bddContext, agentID,
util.FilterTopic("legacyconnection_states"),
util.FilterStateID(state),
util.FilterType("post_state"),
)
if err != nil {
return "", fmt.Errorf("pull events from WebSocket: %w", err)
}
return msg.Message.Properties["connectionID"].(string), nil
}
func (a *ControllerSteps) createInvitation(inviterAgentID, label string) error {
return a.performCreateInvitation(inviterAgentID, label, "", false)
}
func (a *ControllerSteps) createInvitationWithRouter(inviterAgentID, label, router string) error {
routerID, ok := a.bddContext.Args[router]
if !ok {
return fmt.Errorf("create invitation with router: connection %q was not found", router)
}
return a.performCreateInvitation(inviterAgentID, label, routerID, false)
}
func (a *ControllerSteps) createInvitationWithDID(inviterAgentID, label string) error {
return a.performCreateInvitation(inviterAgentID, label, "", true)
}
func (a *ControllerSteps) createImplicitInvitation(inviteeAgentID, inviterAgentID string) error {
return a.performCreateImplicitInvitation(inviteeAgentID, inviterAgentID, false)
}
func (a *ControllerSteps) createImplicitInvitationWithDID(inviteeAgentID, inviterAgentID string) error {
return a.performCreateImplicitInvitation(inviteeAgentID, inviterAgentID, true)
}
func chooseConnection(url string) (string, error) {
connections := struct{ Connections []string }{}
err := util.SendHTTP(http.MethodGet, url+"/mediator/connections", nil, &connections)
if err != nil {
return "", fmt.Errorf("mediator connections: %w", err)
}
if len(connections.Connections) == 0 {
return "", nil
}
return connections.Connections[0], nil
}
func (a *ControllerSteps) performCreateInvitation(inviterAgentID, label, routerID string, useDID bool) error {
destination, ok := a.bddContext.GetControllerURL(inviterAgentID)
if !ok {
return fmt.Errorf("unable to find controller URL registered for agent [%s]",
inviterAgentID)
}
var publicDID string
if useDID {
publicDID, ok = a.bddContext.PublicDIDs[inviterAgentID]
if !ok {
return fmt.Errorf("unable to find public DID for agent [%s]", inviterAgentID)
}
}
logger.Debugf("Creating invitation from controller for agent[%s], label[%s], did[%s]",
inviterAgentID, publicDID, label)
if routerID == "" {
var err error
routerID, err = chooseConnection(destination)
if err != nil {
return err
}
}
// call controller
path := fmt.Sprintf("%s%s?alias=%s&public=%s&router_connection_id=%s",
destination, createInvitationPath, label, publicDID, routerID)
var result legconncmd.CreateInvitationResponse
err := util.SendHTTP(http.MethodPost, path, nil, &result)
if err != nil {
logger.Errorf("Failed to create invitation, cause : %s", err)
return err
}
err = a.verifyCreateInvitationResult(&result, label, useDID)
if err != nil {
return fmt.Errorf("failed to get valid payload from create invitation for agent [%s], reason: %w",
inviterAgentID, err)
}
// save invitation for later use
a.invitations[inviterAgentID] = result.Invitation
return nil
}
func (a *ControllerSteps) performCreateImplicitInvitation(inviteeAgentID, inviterAgentID string, usePublic bool) error {
destination, ok := a.bddContext.GetControllerURL(inviteeAgentID)
if !ok {
return fmt.Errorf("unable to find controller URL registered for agent [%s]",
inviterAgentID)
}
inviterDID, ok := a.bddContext.PublicDIDs[inviterAgentID]
if !ok {
return fmt.Errorf("unable to find public DID for agent [%s]", inviterAgentID)
}
var inviteeDID string
if usePublic {
inviteeDID, ok = a.bddContext.PublicDIDs[inviteeAgentID]
if !ok {
return fmt.Errorf("unable to find public DID for agent [%s]", inviteeAgentID)
}
}
logger.Debugf("Creating implicit invitation from controller for agent[%s]: inviterDID[%s], inviteeDID[%s]",
inviteeAgentID, inviterDID, inviteeDID)
// call controller
path := fmt.Sprintf("%s%s?their_did=%s&their_label=%s&my_did=%s&my_label=%s",
destination, createImplicitInvitationPath, inviterDID, inviterAgentID, inviteeDID, inviteeAgentID)
var result legconncmd.ImplicitInvitationResponse
err := util.SendHTTP(http.MethodPost, path, nil, &result)
if err != nil {
logger.Errorf("Failed to create implicit invitation, cause : %s", err)
return err
}
// validate payload
if result.ConnectionID == "" {
return fmt.Errorf("connection id is empty for create implicit invitation for agent [%s]", inviteeAgentID)
}
// invitee connectionID
a.connectionIDs[inviteeAgentID] = result.ConnectionID
a.bddContext.SaveConnectionID(inviteeAgentID, inviterAgentID, result.ConnectionID)
return nil
}
func (a *ControllerSteps) verifyCreateInvitationResult(result *legconncmd.CreateInvitationResponse,
label string, useDID bool) error {
// validate payload
if result.Invitation == nil {
return fmt.Errorf("empty invitation")
}
// verify result
if result.Alias != label {
return fmt.Errorf("invitation label mismatch, expected[%s] but got [%s]", label, result.Alias)
}
if useDID && result.Invitation.DID == "" {
return fmt.Errorf("did ID not found in created invitation")
}
if !useDID && len(result.Invitation.RecipientKeys) == 0 {
return fmt.Errorf("recipient keys not found in invitation")
}
return nil
}
func (a *ControllerSteps) receiveInvitation(inviteeAgentID, inviterAgentID string) error {
destination, ok := a.bddContext.GetControllerURL(inviteeAgentID)
if !ok {
return fmt.Errorf(" unable to find controller URL registered for agent [%s]", inviterAgentID)
}
invitation, ok := a.invitations[inviterAgentID]
if !ok {
return fmt.Errorf(" unable to find invitation for inviter [%s]", inviterAgentID)
}
message, err := json.Marshal(invitation)
if err != nil {
logger.Errorf("Failed to create receiver invitation message, cause : %s", err)
return err
}
// call controller
var result legconncmd.ReceiveInvitationResponse
err = util.SendHTTP(http.MethodPost, destination+receiveInvtiationPath, message, &result)
if err != nil {
logger.Errorf("Failed to perform receive invitation, cause : %s", err)
return err
}
// validate payload
if result.ConnectionID == "" {
return fmt.Errorf("failed to get valid payload from receive invitation call for agent [%s]", inviteeAgentID)
}
// invitee connectionID
a.connectionIDs[inviteeAgentID] = result.ConnectionID
a.bddContext.SaveConnectionID(inviteeAgentID, inviterAgentID, result.ConnectionID)
return nil
}
func (a *ControllerSteps) saveConnectionID(agentID, varName string) error {
a.bddContext.Args[varName] = a.connectionIDs[agentID]
return nil
}
func (a *ControllerSteps) approveInvitation(agentID string) error {
_, err := a.performApproveInvitation(agentID, "", false)
return err
}
func (a *ControllerSteps) approveInvitationWithRouter(agentID, router string) error {
routerID, ok := a.bddContext.Args[router]
if !ok {
return fmt.Errorf("approve invitation with router: connection %q was not found", router)
}
_, err := a.performApproveInvitation(agentID, routerID, false)
return err
}
func (a *ControllerSteps) approveInvitationWithPublicDID(agentID string) error {
_, err := a.performApproveInvitation(agentID, "", true)
return err
}
func (a *ControllerSteps) performApproveInvitation(agentID, routerID string, useDID bool) (string, error) {
connectionID, err := a.pullEventsFromWebSocket(agentID, "invited")
if err != nil {
return "", fmt.Errorf("approve connection invitation : %w", err)
}
// invitee connectionID
a.connectionIDs[agentID] = connectionID
var response legconncmd.AcceptInvitationResponse
err = a.performApprove(agentID, useDID, connectionID, routerID, acceptInvitationPath, &response)
if err != nil {
return "", err
}
if response.ConnectionID == "" {
logger.Errorf("Failed to perform approve invitation, cause : %s", err)
return "", fmt.Errorf("failed to perform approve invitation, invalid response")
}
return response.ConnectionID, nil
}
// ApproveRequest approves a request.
func (a *ControllerSteps) ApproveRequest(agentID string) error {
_, err := a.performApproveRequest(agentID, "", false)
return err
}
// ApproveRequestWithRouter approves a request.
func (a *ControllerSteps) ApproveRequestWithRouter(agentID, router string) error {
routerID, ok := a.bddContext.Args[router]
if !ok {
return fmt.Errorf("create invitation with router: connection %q was not found", router)
}
_, err := a.performApproveRequest(agentID, routerID, false)
return err
}
func (a *ControllerSteps) approveRequestWithPublicDID(agentID string) error {
_, err := a.performApproveRequest(agentID, "", true)
return err
}
func (a *ControllerSteps) performApprove(agentID string, useDID bool, connectionID, routerID, operationPath string,
response interface{}) error {
controllerURL, ok := a.bddContext.GetControllerURL(agentID)
if !ok {
return fmt.Errorf("unable to find contoller URL for agent [%s]", controllerURL)
}
var publicDID string
if useDID {
publicDID, ok = a.bddContext.PublicDIDs[agentID]
if !ok {
return fmt.Errorf("unable to find public DID for agent [%s]", agentID)
}
}
logger.Debugf("Accepting invitation from controller for agent[%s], did[%s]",
agentID, publicDID)
if routerID == "" {
var err error
routerID, err = chooseConnection(controllerURL)
if err != nil {
return err
}
}
path := controllerURL + fmt.Sprintf(operationPath, connectionID, publicDID)
path += "&router_connections=" + routerID
err := util.SendHTTP(http.MethodPost, path, nil, &response)
if err != nil {
logger.Errorf("Failed to perform approve request, cause : %s", err)
return fmt.Errorf("failed to perform approve request : %w", err)
}
return nil
}
func (a *ControllerSteps) performApproveRequest(agentID, routerID string, useDID bool) (string, error) {
connectionID, err := a.pullEventsFromWebSocket(agentID, "requested")
if err != nil {
return "", fmt.Errorf("failed to get connection ID for agent: '%v', router: '%v' from webhook, %w",
agentID, routerID, err)
}
// inviter connectionID
a.connectionIDs[agentID] = connectionID
var response legconncmd.ConnectionResponse
err = a.performApprove(agentID, useDID, connectionID, routerID, acceptRequestPath, &response)
if err != nil {
return "", err
}
if response.ConnectionID == "" {
logger.Errorf("Failed to perform approve request, cause : %s", err)
return "", fmt.Errorf("failed to perform approve request, invalid response")
}
return response.ConnectionID, nil
}
// WaitForPostEvent waits for the specific post event state.
func (a *ControllerSteps) WaitForPostEvent(agents, statesValue string) error {
for _, agent := range strings.Split(agents, ",") {
_, err := a.pullEventsFromWebSocket(agent, statesValue)
if err != nil {
return fmt.Errorf("failed to get notification from webhook, %w", err)
}
}
return nil
}
func (a *ControllerSteps) validateConnections(agents, stateValue string) error {
for _, agent := range strings.Split(agents, ",") {
if err := a.validateConnection(agent, stateValue); err != nil {
return fmt.Errorf("validate connections: %w", err)
}
}
return nil
}
func (a *ControllerSteps) validateConnection(agentID, stateValue string) error {
connectionID, ok := a.connectionIDs[agentID]
if !ok {
return fmt.Errorf(" unable to find valid connection ID for agent [%s]", connectionID)
}
destination, ok := a.bddContext.GetControllerURL(agentID)
if !ok {
return fmt.Errorf(" unable to find controller URL registered for agent [%s]", agentID)
}
logger.Debugf(" Getting connection by ID %s from %s", connectionID, destination)
// call controller
var response legconncmd.QueryConnectionResponse
err := util.SendHTTP(http.MethodGet,
destination+strings.Replace(connectionsByID, "{id}", connectionID, 1), nil, &response)
if err != nil {
logger.Errorf("Failed to perform receive invitation, cause : %s", err)
return err
}
logger.Debugf("Got connection by ID, result %s", response)
// Verify state
if response.Result.State != stateValue {
return fmt.Errorf("expected state[%s] for agent[%s], but got[%s]", stateValue, agentID, response.Result.State)
}
// Also make sure new connection is available in list of connections for given agent
return a.verifyConnectionList(agentID, stateValue, connectionID)
}
func (a *ControllerSteps) verifyConnectionList(agentID, queryState, verifyID string) error {
destination, ok := a.bddContext.GetControllerURL(agentID)
if !ok {
return fmt.Errorf(" unable to find controller URL registered for agent [%s]", agentID)
}
logger.Debugf("Getting connections by state %s from %s", queryState, destination)
// call controller
var response legconncmd.QueryConnectionsResponse
err := util.SendHTTP(http.MethodGet, destination+connOperationID+"?state="+queryState, nil, &response)
if err != nil {
logger.Errorf("Failed to perform receive invitation, cause : %s", err)
return err
}
logger.Debugf("Got %d connections for state `%s`", len(response.Results), queryState)
if len(response.Results) == 0 {
return fmt.Errorf("no connections found with state '%s' in connections list", queryState)
}
var found bool
for _, connection := range response.Results {
logger.Debugf("Connection[%s] found for agent[%s] with state[%s]", connection.ConnectionID, agentID, connection.State)
if connection.State == queryState && connection.ConnectionID == verifyID {
found = true
break
}
}
if !found {
return fmt.Errorf("no connections found with state '%s' and connection ID '%s' in connections list",
queryState, verifyID)
}
return nil
}
// CreatePublicDID step creates a public sidetree DID for the given agent. Second parameter is ignored.
func (a *ControllerSteps) CreatePublicDID(agentID, _ string) error {
_, err := a.CreatePublicDIDWithKeyType(agentID, "ED25519")
return err
}
// CreatePublicDIDWithKeyType creates a public sidetree DID with the given key type, returning the DID.
func (a *ControllerSteps) CreatePublicDIDWithKeyType(agentID, keyType string) (string, error) { //nolint:funlen,gocyclo
destination, ok := a.bddContext.GetControllerURL(agentID)
if !ok {
return "", fmt.Errorf(" unable to find controller URL registered for agent [%s]", agentID)
}
path := fmt.Sprintf("%s%s", destination, createKeySetPath)
reqBytes, err := json.Marshal(cmdkms.CreateKeySetRequest{KeyType: keyType})
if err != nil {
return "", err
}
var result cmdkms.CreateKeySetResponse
err = util.SendHTTP(http.MethodPost, path, reqBytes, &result)
if err != nil {
return "", err
}
// keys received from controller kms command are base64 RawURL encoded
verKey, err := base64.RawURLEncoding.DecodeString(result.PublicKey)
if err != nil {
return "", err
}
j, err := jwksupport.PubKeyBytesToJWK(verKey, kms.KeyType(keyType))
if err != nil {
return "", err
}
j.KeyID = result.KeyID
publicKeyRecovery, _, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return "", err
}
recoveryJWK, err := jwksupport.JWKFromKey(publicKeyRecovery)
if err != nil {
return "", err
}
publicKeyUpdate, _, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return "", err
}
updateJWK, err := jwksupport.JWKFromKey(publicKeyUpdate)
if err != nil {
return "", err
}
params := sidetree.CreateDIDParams{
URL: a.bddContext.Args[sideTreeURL] + "operations",
KeyID: result.KeyID,
JWK: j,
RecoveryJWK: recoveryJWK,
UpdateJWK: updateJWK,
ServiceEndpoint: model.NewDIDCommV1Endpoint(a.agentServiceEndpoints[destination]),
}
doc, err := sidetree.CreateDID(¶ms)
if err != nil {
return "", err
}
err = a.waitForPublicDID(doc.ID)
if err != nil {
logger.Errorf("Failed to resolve public DID created, cause : %s", err)
return "", fmt.Errorf("failed to resolve public DID created, %w", err)
}
// save public DID for later use
a.bddContext.PublicDIDs[agentID] = doc.ID
a.bddContext.PublicDIDDocs[agentID] = doc
return doc.ID, nil
}
// waitForPublicDID wait for public DID to be available before throw error after timeout.
func (a *ControllerSteps) waitForPublicDID(id string) error {
const retryDelay = 500 * time.Millisecond
endpointURL, ok := a.bddContext.Args[sideTreeURL]
if !ok {
return fmt.Errorf("failed to find sidetree URL to resolve sidetree public DID")
}
start := time.Now()
for {
if time.Since(start) > timeoutWaitForDID {
break
}
err := util.SendHTTP(http.MethodGet, endpointURL+"/identifiers/"+id, nil, nil)
if err != nil {
logger.Warnf("Failed to resolve public DID, due to error [%s] will retry", err)
time.Sleep(retryDelay)
continue
}
return nil
}
return fmt.Errorf("unable to resolve public DID [%s]", id)
}
func (a *ControllerSteps) performLegacyConnection(inviter, invitee string) error {
err := a.createInvitation(inviter, inviter)
if err != nil {
return err
}
err = a.receiveInvitation(invitee, inviter)
if err != nil {
return err
}
inviteeConnID, err := a.performApproveInvitation(invitee, "", false)
if err != nil {
return err
}
inviterConnID, err := a.performApproveRequest(inviter, "", false)
if err != nil {
return err
}
const expectedState = "completed"
agentIDs := []string{inviter, invitee}
for _, agentID := range agentIDs {
err = a.WaitForPostEvent(agentID, expectedState)
if err != nil {
return err
}
err = a.validateConnection(agentID, expectedState)
if err != nil {
return err
}
}
a.bddContext.SaveConnectionID(invitee, inviter, inviteeConnID)
a.bddContext.SaveConnectionID(inviter, invitee, inviterConnID)
return nil
}