forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.go
657 lines (557 loc) · 20.3 KB
/
framework.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package aries
import (
"fmt"
"strings"
"github.com/google/uuid"
"github.com/piprate/json-gold/ld"
"github.com/hyperledger/aries-framework-go/pkg/crypto"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/dispatcher"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/messenger"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/packager"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/packer"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
"github.com/hyperledger/aries-framework-go/pkg/doc/jsonld"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api"
vdrapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
"github.com/hyperledger/aries-framework-go/pkg/framework/context"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/pkg/secretlock"
"github.com/hyperledger/aries-framework-go/pkg/store/did"
"github.com/hyperledger/aries-framework-go/pkg/store/verifiable"
"github.com/hyperledger/aries-framework-go/pkg/vdr"
"github.com/hyperledger/aries-framework-go/pkg/vdr/key"
"github.com/hyperledger/aries-framework-go/pkg/vdr/peer"
"github.com/hyperledger/aries-framework-go/spi/storage"
)
const (
defaultEndpoint = "didcomm:transport/queue"
defaultMasterKeyURI = "local-lock://default/master/key/"
)
// Aries provides access to the context being managed by the framework. The context can be used to create aries clients.
type Aries struct {
storeProvider storage.Provider
protocolStateStoreProvider storage.Provider
protocolSvcCreators []api.ProtocolSvcCreator
services []dispatcher.ProtocolService
msgSvcProvider api.MessageServiceProvider
outboundDispatcher dispatcher.Outbound
messenger service.MessengerHandler
outboundTransports []transport.OutboundTransport
inboundTransports []transport.InboundTransport
kms kms.KeyManager
kmsCreator kms.Creator
secretLock secretlock.Service
crypto crypto.Crypto
packagerCreator packager.Creator
packager transport.Packager
packerCreator packer.Creator
packerCreators []packer.Creator
primaryPacker packer.Packer
packers []packer.Packer
vdrRegistry vdrapi.Registry
vdr []vdrapi.VDR
verifiableStore verifiable.Store
didConnectionStore did.ConnectionStore
jsonldDocumentLoader ld.DocumentLoader
transportReturnRoute string
id string
keyType kms.KeyType
keyAgreementType kms.KeyType
mediaTypeProfiles []string
}
// Option configures the framework.
type Option func(opts *Aries) error
// New initializes the Aries framework based on the set of options provided. This function returns a framework
// which can be used to manage Aries clients by getting the framework context.
func New(opts ...Option) (*Aries, error) {
frameworkOpts := &Aries{}
// generate framework configs from options
for _, option := range opts {
err := option(frameworkOpts)
if err != nil {
closeErr := frameworkOpts.Close()
return nil, fmt.Errorf("close err: %v Error in option passed to New: %w", closeErr, err)
}
}
// generate a random framework ID
frameworkOpts.id = uuid.New().String()
// get the default framework options
err := defFrameworkOpts(frameworkOpts)
if err != nil {
return nil, fmt.Errorf("default option initialization failed: %w", err)
}
// TODO: https://github.com/hyperledger/aries-framework-go/issues/212
// Define clear relationship between framework and context.
// Details - The code creates context without protocolServices. The protocolServicesCreators are dependent
// on the context. The inbound transports require ctx.InboundMessageHandler(), which in-turn depends on
// protocolServices. At the moment, there is a looping issue among these.
return initializeServices(frameworkOpts)
}
func initializeServices(frameworkOpts *Aries) (*Aries, error) {
// Order of initializing service is important
// Create kms
if e := createKMS(frameworkOpts); e != nil {
return nil, e
}
// Create vdr
if e := createVDR(frameworkOpts); e != nil {
return nil, e
}
// create packers and packager (must be done after KMS and connection store)
if err := createPackersAndPackager(frameworkOpts); err != nil {
return nil, err
}
// Create outbound dispatcher
if err := createOutboundDispatcher(frameworkOpts); err != nil {
return nil, err
}
// Create messenger handler
if err := createMessengerHandler(frameworkOpts); err != nil {
return nil, err
}
// Create DID connection store
if err := createDIDConnectionStore(frameworkOpts); err != nil {
return nil, err
}
// Load services
if err := loadServices(frameworkOpts); err != nil {
return nil, err
}
// Start inbound/outbound transports
if err := startTransports(frameworkOpts); err != nil {
return nil, err
}
return frameworkOpts, nil
}
// WithMessengerHandler injects a messenger handler to the Aries framework.
func WithMessengerHandler(mh service.MessengerHandler) Option {
return func(opts *Aries) error {
opts.messenger = mh
return nil
}
}
// WithOutboundTransports injects an outbound transports to the Aries framework.
func WithOutboundTransports(outboundTransports ...transport.OutboundTransport) Option {
return func(opts *Aries) error {
opts.outboundTransports = append(opts.outboundTransports, outboundTransports...)
return nil
}
}
// WithInboundTransport injects an inbound transport to the Aries framework.
func WithInboundTransport(inboundTransport ...transport.InboundTransport) Option {
return func(opts *Aries) error {
opts.inboundTransports = append(opts.inboundTransports, inboundTransport...)
return nil
}
}
// WithTransportReturnRoute injects transport return route option to the Aries framework. Acceptable values - "none",
// "all" or "thread". RFC - https://github.com/hyperledger/aries-rfcs/tree/master/features/0092-transport-return-route.
// Currently, framework supports "all" and "none" option with WebSocket transport ("thread" is not supported).
func WithTransportReturnRoute(transportReturnRoute string) Option {
return func(opts *Aries) error {
// "thread" option is not supported at the moment.
if transportReturnRoute != decorator.TransportReturnRouteNone &&
transportReturnRoute != decorator.TransportReturnRouteAll {
return fmt.Errorf("invalid transport return route option : %s", transportReturnRoute)
}
opts.transportReturnRoute = transportReturnRoute
return nil
}
}
// WithStoreProvider injects a storage provider to the Aries framework.
func WithStoreProvider(prov storage.Provider) Option {
return func(opts *Aries) error {
opts.storeProvider = prov
return nil
}
}
// WithProtocolStateStoreProvider injects a protocol state storage provider to the Aries framework.
func WithProtocolStateStoreProvider(prov storage.Provider) Option {
return func(opts *Aries) error {
opts.protocolStateStoreProvider = prov
return nil
}
}
// WithProtocols injects a protocol service to the Aries framework.
func WithProtocols(protocolSvcCreator ...api.ProtocolSvcCreator) Option {
return func(opts *Aries) error {
opts.protocolSvcCreators = append(opts.protocolSvcCreators, protocolSvcCreator...)
return nil
}
}
// WithSecretLock injects a SecretLock service to the Aries framework.
func WithSecretLock(s secretlock.Service) Option {
return func(opts *Aries) error {
opts.secretLock = s
return nil
}
}
// WithKMS injects a KMS service to the Aries framework.
func WithKMS(k kms.Creator) Option {
return func(opts *Aries) error {
opts.kmsCreator = k
return nil
}
}
// WithCrypto injects a crypto service to the Aries framework.
func WithCrypto(c crypto.Crypto) Option {
return func(opts *Aries) error {
opts.crypto = c
return nil
}
}
// WithVDR injects a VDR service to the Aries framework.
func WithVDR(v vdrapi.VDR) Option {
return func(opts *Aries) error {
opts.vdr = append(opts.vdr, v)
return nil
}
}
// WithMessageServiceProvider injects a message service provider to the Aries framework.
// Message service provider returns list of message services which can be used to provide custom handle
// functionality based on incoming messages type and purpose.
func WithMessageServiceProvider(msv api.MessageServiceProvider) Option {
return func(opts *Aries) error {
opts.msgSvcProvider = msv
return nil
}
}
// WithPacker injects at least one Packer service into the Aries framework,
// with the primary Packer being used for inbound/outbound communication
// and the additional packers being available for unpacking inbound messages.
func WithPacker(primary packer.Creator, additionalPackers ...packer.Creator) Option {
return func(opts *Aries) error {
opts.packerCreator = primary
opts.packerCreators = append(opts.packerCreators, additionalPackers...)
return nil
}
}
// WithVerifiableStore injects a verifiable credential store.
func WithVerifiableStore(store verifiable.Store) Option {
return func(opts *Aries) error {
opts.verifiableStore = store
return nil
}
}
// WithDIDConnectionStore injects a DID connection store.
func WithDIDConnectionStore(store did.ConnectionStore) Option {
return func(opts *Aries) error {
opts.didConnectionStore = store
return nil
}
}
// WithJSONLDDocumentLoader injects a JSON-LD document loader.
func WithJSONLDDocumentLoader(loader ld.DocumentLoader) Option {
return func(opts *Aries) error {
opts.jsonldDocumentLoader = loader
return nil
}
}
// WithKeyType injects a default signing key type.
func WithKeyType(keyType kms.KeyType) Option {
return func(opts *Aries) error {
opts.keyType = keyType
return nil
}
}
// WithKeyAgreementType injects a default encryption key type.
func WithKeyAgreementType(keyAgreementType kms.KeyType) Option {
return func(opts *Aries) error {
opts.keyAgreementType = keyAgreementType
return nil
}
}
// WithMediaTypeProfiles injects a default media types profile.
func WithMediaTypeProfiles(mediaTypeProfiles []string) Option {
return func(opts *Aries) error {
opts.mediaTypeProfiles = make([]string, len(mediaTypeProfiles))
copy(opts.mediaTypeProfiles, mediaTypeProfiles)
return nil
}
}
// Context provides a handle to the framework context.
func (a *Aries) Context() (*context.Provider, error) {
return context.New(
context.WithOutboundDispatcher(a.outboundDispatcher),
context.WithMessengerHandler(a.messenger),
context.WithOutboundTransports(a.outboundTransports...),
context.WithProtocolServices(a.services...),
context.WithKMS(a.kms),
context.WithSecretLock(a.secretLock),
context.WithCrypto(a.crypto),
context.WithServiceEndpoint(serviceEndpoint(a)),
context.WithRouterEndpoint(routingEndpoint(a)),
context.WithStorageProvider(a.storeProvider),
context.WithProtocolStateStorageProvider(a.protocolStateStoreProvider),
context.WithPacker(a.primaryPacker, a.packers...),
context.WithPackager(a.packager),
context.WithVDRegistry(a.vdrRegistry),
context.WithTransportReturnRoute(a.transportReturnRoute),
context.WithAriesFrameworkID(a.id),
context.WithMessageServiceProvider(a.msgSvcProvider),
context.WithVerifiableStore(a.verifiableStore),
context.WithDIDConnectionStore(a.didConnectionStore),
context.WithJSONLDDocumentLoader(a.jsonldDocumentLoader),
context.WithKeyType(a.keyType),
context.WithKeyAgreementType(a.keyAgreementType),
context.WithMediaTypeProfiles(a.mediaTypeProfiles),
)
}
// Messenger returns messenger for sending messages through this agent framework
// TODO should use dedicated messenger interface instead of Outbound dispatcher [Issue #1058].
func (a *Aries) Messenger() service.Messenger {
return a.messenger
}
// Close frees resources being maintained by the framework.
func (a *Aries) Close() error {
if a.storeProvider != nil {
err := a.storeProvider.Close()
if err != nil {
return fmt.Errorf("failed to close the store: %w", err)
}
}
if a.protocolStateStoreProvider != nil {
err := a.protocolStateStoreProvider.Close()
if err != nil {
return fmt.Errorf("failed to close the store: %w", err)
}
}
for _, inbound := range a.inboundTransports {
if err := inbound.Stop(); err != nil {
return fmt.Errorf("inbound transport close failed: %w", err)
}
}
return a.closeVDR()
}
func (a *Aries) closeVDR() error {
if a.vdrRegistry != nil {
if err := a.vdrRegistry.Close(); err != nil {
return fmt.Errorf("vdr registry close failed: %w", err)
}
}
return nil
}
func createKMS(frameworkOpts *Aries) error {
ctx, err := context.New(
context.WithStorageProvider(frameworkOpts.storeProvider),
context.WithSecretLock(frameworkOpts.secretLock),
)
if err != nil {
return fmt.Errorf("create context failed: %w", err)
}
frameworkOpts.kms, err = frameworkOpts.kmsCreator(ctx)
if err != nil {
return fmt.Errorf("create KMS failed: %w", err)
}
return nil
}
func createVDR(frameworkOpts *Aries) error {
ctx, err := context.New(
context.WithKMS(frameworkOpts.kms),
context.WithCrypto(frameworkOpts.crypto),
context.WithStorageProvider(frameworkOpts.storeProvider),
context.WithServiceEndpoint(serviceEndpoint(frameworkOpts)),
)
if err != nil {
return fmt.Errorf("create context failed: %w", err)
}
var opts []vdr.Option
for _, v := range frameworkOpts.vdr {
opts = append(opts, vdr.WithVDR(v))
}
p, err := peer.New(ctx.StorageProvider())
if err != nil {
return fmt.Errorf("create new vdr peer failed: %w", err)
}
opts = append(opts,
vdr.WithVDR(p),
vdr.WithDefaultServiceType(vdrapi.DIDCommServiceType),
vdr.WithDefaultServiceEndpoint(ctx.ServiceEndpoint()),
)
k := key.New()
opts = append(opts, vdr.WithVDR(k))
frameworkOpts.vdrRegistry = vdr.New(opts...)
return nil
}
func createMessengerHandler(frameworkOpts *Aries) error {
if frameworkOpts.messenger != nil {
return nil
}
ctx, err := context.New(
context.WithOutboundDispatcher(frameworkOpts.outboundDispatcher),
context.WithStorageProvider(frameworkOpts.storeProvider),
)
if err != nil {
return fmt.Errorf("context creation failed: %w", err)
}
frameworkOpts.messenger, err = messenger.NewMessenger(ctx)
return err
}
func createOutboundDispatcher(frameworkOpts *Aries) error {
ctx, err := context.New(
context.WithKMS(frameworkOpts.kms),
context.WithCrypto(frameworkOpts.crypto),
context.WithOutboundTransports(frameworkOpts.outboundTransports...),
context.WithPackager(frameworkOpts.packager),
context.WithTransportReturnRoute(frameworkOpts.transportReturnRoute),
context.WithVDRegistry(frameworkOpts.vdrRegistry),
context.WithStorageProvider(frameworkOpts.storeProvider),
context.WithProtocolStateStorageProvider(frameworkOpts.protocolStateStoreProvider),
)
if err != nil {
return fmt.Errorf("context creation failed: %w", err)
}
frameworkOpts.outboundDispatcher, err = dispatcher.NewOutbound(ctx)
if err != nil {
return fmt.Errorf("failed to init outbound dispatcher: %w", err)
}
return nil
}
func createDIDConnectionStore(frameworkOpts *Aries) error {
if frameworkOpts.didConnectionStore != nil {
return nil
}
ctx, err := context.New(
context.WithStorageProvider(frameworkOpts.storeProvider),
context.WithVDRegistry(frameworkOpts.vdrRegistry),
)
if err != nil {
return fmt.Errorf("context creation failed: %w", err)
}
frameworkOpts.didConnectionStore, err = did.NewConnectionStore(ctx)
return err
}
func createJSONLDDocumentLoader(frameworkOpts *Aries) error {
if frameworkOpts.jsonldDocumentLoader != nil {
return nil
}
l, err := jsonld.NewDocumentLoader(frameworkOpts.storeProvider)
if err != nil {
return fmt.Errorf("document loader creation failed: %w", err)
}
frameworkOpts.jsonldDocumentLoader = l
return nil
}
func startTransports(frameworkOpts *Aries) error {
ctx, err := context.New(
context.WithCrypto(frameworkOpts.crypto),
context.WithPackager(frameworkOpts.packager),
context.WithProtocolServices(frameworkOpts.services...),
context.WithAriesFrameworkID(frameworkOpts.id),
context.WithMessageServiceProvider(frameworkOpts.msgSvcProvider),
context.WithMessengerHandler(frameworkOpts.messenger),
context.WithDIDConnectionStore(frameworkOpts.didConnectionStore),
context.WithKeyType(frameworkOpts.keyType),
context.WithKeyAgreementType(frameworkOpts.keyAgreementType),
context.WithMediaTypeProfiles(frameworkOpts.mediaTypeProfiles),
)
if err != nil {
return fmt.Errorf("context creation failed: %w", err)
}
for _, inbound := range frameworkOpts.inboundTransports {
// Start the inbound transport
if err = inbound.Start(ctx); err != nil {
return fmt.Errorf("inbound transport start failed: %w", err)
}
}
// Start the outbound transport
for _, outbound := range frameworkOpts.outboundTransports {
if err = outbound.Start(ctx); err != nil {
return fmt.Errorf("outbound transport start failed: %w", err)
}
}
return nil
}
func loadServices(frameworkOpts *Aries) error {
ctx, err := context.New(
context.WithOutboundDispatcher(frameworkOpts.outboundDispatcher),
context.WithMessengerHandler(frameworkOpts.messenger),
context.WithStorageProvider(frameworkOpts.storeProvider),
context.WithProtocolStateStorageProvider(frameworkOpts.protocolStateStoreProvider),
context.WithKMS(frameworkOpts.kms),
context.WithCrypto(frameworkOpts.crypto),
context.WithPackager(frameworkOpts.packager),
context.WithServiceEndpoint(serviceEndpoint(frameworkOpts)),
context.WithRouterEndpoint(routingEndpoint(frameworkOpts)),
context.WithVDRegistry(frameworkOpts.vdrRegistry),
context.WithVerifiableStore(frameworkOpts.verifiableStore),
context.WithDIDConnectionStore(frameworkOpts.didConnectionStore),
context.WithMessageServiceProvider(frameworkOpts.msgSvcProvider),
context.WithJSONLDDocumentLoader(frameworkOpts.jsonldDocumentLoader),
)
if err != nil {
return fmt.Errorf("create context failed: %w", err)
}
for _, v := range frameworkOpts.protocolSvcCreators {
svc, svcErr := v(ctx)
if svcErr != nil {
return fmt.Errorf("new protocol service failed: %w", svcErr)
}
frameworkOpts.services = append(frameworkOpts.services, svc)
// after service was successfully created we need to add it to the context
// since the introduce protocol depends on did-exchange
if err := context.WithProtocolServices(frameworkOpts.services...)(ctx); err != nil {
return err
}
}
return nil
}
func createPackersAndPackager(frameworkOpts *Aries) error {
ctx, err := context.New(
context.WithCrypto(frameworkOpts.crypto),
context.WithStorageProvider(frameworkOpts.storeProvider),
context.WithKMS(frameworkOpts.kms),
)
if err != nil {
return fmt.Errorf("create packer context failed: %w", err)
}
frameworkOpts.primaryPacker, err = frameworkOpts.packerCreator(ctx)
if err != nil {
return fmt.Errorf("create packer failed: %w", err)
}
for _, pC := range frameworkOpts.packerCreators {
if pC == nil {
continue
}
p, e := pC(ctx)
if e != nil {
return fmt.Errorf("create packer failed: %w", e)
}
frameworkOpts.packers = append(frameworkOpts.packers, p)
}
ctx, err = context.New(context.WithPacker(frameworkOpts.primaryPacker, frameworkOpts.packers...),
context.WithStorageProvider(frameworkOpts.storeProvider), context.WithVDRegistry(frameworkOpts.vdrRegistry))
if err != nil {
return fmt.Errorf("create packager context failed: %w", err)
}
frameworkOpts.packager, err = frameworkOpts.packagerCreator(ctx)
if err != nil {
return fmt.Errorf("create packager failed: %w", err)
}
return nil
}
func serviceEndpoint(frameworkOpts *Aries) string {
return fetchEndpoint(frameworkOpts, "ws")
}
func routingEndpoint(frameworkOpts *Aries) string {
return fetchEndpoint(frameworkOpts, "http")
}
func fetchEndpoint(frameworkOpts *Aries, defaultScheme string) string {
// TODO https://github.com/hyperledger/aries-framework-go/issues/1161 Select Service and Router
// endpoint from Multiple Inbound Transports
for _, inbound := range frameworkOpts.inboundTransports {
if strings.HasPrefix(inbound.Endpoint(), defaultScheme) {
return inbound.Endpoint()
}
}
if len(frameworkOpts.inboundTransports) > 0 {
return frameworkOpts.inboundTransports[0].Endpoint()
}
return defaultEndpoint
}