This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
/
aries.go
446 lines (359 loc) · 14.1 KB
/
aries.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package command
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/google/uuid"
jsonld "github.com/piprate/json-gold/ld"
"github.com/hyperledger/aries-framework-go/cmd/aries-agent-mobile/pkg/api"
"github.com/hyperledger/aries-framework-go/cmd/aries-agent-mobile/pkg/wrappers/config"
"github.com/hyperledger/aries-framework-go/cmd/aries-agent-mobile/pkg/wrappers/notifier"
storageWrapper "github.com/hyperledger/aries-framework-go/cmd/aries-agent-mobile/pkg/wrappers/storage"
"github.com/hyperledger/aries-framework-go/component/storageutil/cachedstore"
"github.com/hyperledger/aries-framework-go/component/storageutil/mem"
"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/controller"
"github.com/hyperledger/aries-framework-go/pkg/controller/command"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/didexchange"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/introduce"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/issuecredential"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/kms"
ldcommand "github.com/hyperledger/aries-framework-go/pkg/controller/command/ld"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/mediator"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/messaging"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/outofband"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/outofbandv2"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/presentproof"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/vcwallet"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/vdr"
"github.com/hyperledger/aries-framework-go/pkg/controller/command/verifiable"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/messaging/msghandler"
arieshttp "github.com/hyperledger/aries-framework-go/pkg/didcomm/transport/http"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport/ws"
"github.com/hyperledger/aries-framework-go/pkg/doc/ld"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries"
"github.com/hyperledger/aries-framework-go/pkg/framework/context"
ldstore "github.com/hyperledger/aries-framework-go/pkg/store/ld"
"github.com/hyperledger/aries-framework-go/pkg/vdr/httpbinding"
"github.com/hyperledger/aries-framework-go/spi/storage"
)
var logger = log.New("aries-agent-mobile/wrappers/command")
// Aries is an implementation of AriesController which handles requests locally.
type Aries struct {
framework *aries.Aries
handlers map[string]map[string]command.Exec
notifications <-chan notifier.NotificationPayload
mutex sync.RWMutex
subscribers map[string]map[string][]api.Handler
}
// NewAries returns a new Aries instance that contains handlers and an Aries framework instance.
func NewAries(opts *config.Options) (*Aries, error) {
opts.MsgHandler = msghandler.NewRegistrar()
options, err := prepareFrameworkOptions(opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare framework options: %w", err)
}
framework, err := aries.New(options...)
if err != nil {
return nil, fmt.Errorf("failed to initialize Aries framework: %w", err)
}
ctx, err := framework.Context()
if err != nil {
return nil, fmt.Errorf("failed to get Framework context: %w", err)
}
notifications := make(chan notifier.NotificationPayload)
commandHandlers, err := controller.GetCommandHandlers(ctx,
controller.WithNotifier(notifier.NewNotifier(notifications)),
controller.WithAutoAccept(opts.AutoAccept),
controller.WithMessageHandler(opts.MsgHandler),
)
if err != nil {
return nil, fmt.Errorf("failed to get command handlers: %w", err)
}
handlers := make(map[string]map[string]command.Exec)
populateHandlers(commandHandlers, handlers)
a := &Aries{
framework: framework,
handlers: handlers,
notifications: notifications,
subscribers: make(map[string]map[string][]api.Handler),
}
go a.startNotificationListener()
return a, nil
}
func prepareFrameworkOptions(opts *config.Options) ([]aries.Option, error) {
var options []aries.Option
options = append(options, aries.WithMessageServiceProvider(opts.MsgHandler))
if opts.TransportReturnRoute != "" {
options = append(options, aries.WithTransportReturnRoute(opts.TransportReturnRoute))
}
var storageProvider storage.Provider
if opts.Storage != nil {
storageProvider = storageWrapper.New(opts.Storage)
} else {
storageProvider = mem.NewProvider()
}
options = append(options, aries.WithStoreProvider(storageProvider))
for _, transport := range opts.OutboundTransport {
otOpts, err := getOutBoundTransportOpts(transport, opts.WebsocketReadLimit)
if err != nil {
return nil, fmt.Errorf("failed to prepare outbound transport opts : %w", err)
}
options = append(options, otOpts...)
}
if len(opts.HTTPResolvers) > 0 {
rsOpts, err := getResolverOpts(opts.HTTPResolvers)
if err != nil {
return nil, fmt.Errorf("failed to prepare http resolver opts : %w", err)
}
options = append(options, rsOpts...)
}
if opts.DocumentLoader != nil {
options = append(options, aries.WithJSONLDDocumentLoader(opts.DocumentLoader))
} else {
dlOpts, err := getDocumentLoaderOpts(storageProvider, opts)
if err != nil {
return nil, fmt.Errorf("failed to prepare document loader opts : %w", err)
}
options = append(options, dlOpts...)
}
return options, nil
}
func getOutBoundTransportOpts(transport string, websocketReadLimit int64) ([]aries.Option, error) {
var opts []aries.Option
switch transport {
case "http":
outbound, err := arieshttp.NewOutbound(arieshttp.WithOutboundHTTPClient(&http.Client{}))
if err != nil {
return nil, err
}
opts = append(opts, aries.WithOutboundTransports(outbound))
case "ws":
var outboundOpts []ws.OutboundClientOpt
if websocketReadLimit > 0 {
outboundOpts = append(outboundOpts, ws.WithOutboundReadLimit(websocketReadLimit))
}
opts = append(opts, aries.WithOutboundTransports(ws.NewOutbound(outboundOpts...)))
default:
return nil, fmt.Errorf("unsupported transport : %s", transport)
}
return opts, nil
}
func getResolverOpts(httpResolvers []string) ([]aries.Option, error) {
var opts []aries.Option
const numPartsResolverOption = 2
if len(httpResolvers) > 0 {
for _, httpResolver := range httpResolvers {
r := strings.Split(httpResolver, "@")
if len(r) != numPartsResolverOption {
return nil, fmt.Errorf("invalid http resolver options found")
}
httpVDR, err := httpbinding.New(r[1],
httpbinding.WithAccept(func(method string) bool { return method == r[0] }))
if err != nil {
return nil, fmt.Errorf("failed to setup http resolver : %w", err)
}
opts = append(opts, aries.WithVDR(httpVDR))
}
}
return opts, nil
}
func createJSONLdContext(storageProvider storage.Provider) (*context.Provider, error) {
contextStore, err := ldstore.NewContextStore(cachedstore.NewProvider(storageProvider, mem.NewProvider()))
if err != nil {
return nil, fmt.Errorf("create JSON-LD context store: %w", err)
}
remoteProviderStore, err := ldstore.NewRemoteProviderStore(storageProvider)
if err != nil {
return nil, fmt.Errorf("create remote provider store: %w", err)
}
ctx, err := context.New(
context.WithStorageProvider(storageProvider),
context.WithJSONLDContextStore(contextStore),
context.WithJSONLDRemoteProviderStore(remoteProviderStore),
)
if err != nil {
return nil, fmt.Errorf("context creation failed: %w", err)
}
return ctx, nil
}
func getDocumentLoaderOpts(storageProvider storage.Provider, options *config.Options) ([]aries.Option, error) {
var opts []aries.Option
ctx, err := createJSONLdContext(storageProvider)
if err != nil {
return nil, fmt.Errorf("failed to prepare json ld context : %w", err)
}
var documentLoader *ld.DocumentLoader
if options.LoadRemoteDocuments {
remoteDocumentLoader := jsonld.NewDefaultDocumentLoader(http.DefaultClient)
documentLoader, err = ld.NewDocumentLoader(ctx, ld.WithRemoteDocumentLoader(remoteDocumentLoader))
} else {
documentLoader, err = ld.NewDocumentLoader(ctx)
}
if err != nil {
return nil, fmt.Errorf("failed to prepare document loader opts : %w", err)
}
opts = append(opts, aries.WithJSONLDDocumentLoader(documentLoader))
return opts, nil
}
func populateHandlers(commands []command.Handler, pkgMap map[string]map[string]command.Exec) {
for _, cmd := range commands {
fnMap, ok := pkgMap[cmd.Name()]
if !ok {
fnMap = make(map[string]command.Exec)
}
fnMap[cmd.Method()] = cmd.Handle()
pkgMap[cmd.Name()] = fnMap
}
}
func (a *Aries) startNotificationListener() {
// listens for notifications
for notification := range a.notifications {
a.mutex.RLock()
// gets all handlers that were subscribed for the topic
for _, handlers := range a.subscribers[notification.Topic] {
// send the payload to the subscribers
for _, handler := range handlers {
if err := handler.Handle(notification.Topic, notification.Raw); err != nil {
logger.Errorf("notification listener: %v", err)
}
}
}
a.mutex.RUnlock()
}
}
// RegisterHandler registers a handler to process incoming notifications from the framework.
// Handler is implemented by mobile apps.
func (a *Aries) RegisterHandler(h api.Handler, topics string) string {
a.mutex.Lock()
defer a.mutex.Unlock()
id := uuid.New().String()
for _, topic := range strings.Split(topics, ",") {
if a.subscribers[topic] == nil {
a.subscribers[topic] = map[string][]api.Handler{}
}
a.subscribers[topic][id] = append(a.subscribers[topic][id], h)
}
return id
}
// UnregisterHandler unregisters a handler by given id.
func (a *Aries) UnregisterHandler(id string) {
a.mutex.Lock()
defer a.mutex.Unlock()
for topic := range a.subscribers {
for key := range a.subscribers[topic] {
if key == id {
delete(a.subscribers[topic], id)
}
}
}
}
// GetIntroduceController returns an Introduce instance.
func (a *Aries) GetIntroduceController() (api.IntroduceController, error) {
handlers, ok := a.handlers[introduce.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", introduce.CommandName)
}
return &Introduce{handlers: handlers}, nil
}
// GetVerifiableController returns a Verifiable instance.
func (a *Aries) GetVerifiableController() (api.VerifiableController, error) {
handlers, ok := a.handlers[verifiable.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", verifiable.CommandName)
}
return &Verifiable{handlers: handlers}, nil
}
// GetDIDExchangeController returns a DIDExchange instance.
func (a *Aries) GetDIDExchangeController() (api.DIDExchangeController, error) {
handlers, ok := a.handlers[didexchange.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", didexchange.CommandName)
}
return &DIDExchange{handlers: handlers}, nil
}
// GetIssueCredentialController returns an IssueCredential instance.
func (a *Aries) GetIssueCredentialController() (api.IssueCredentialController, error) {
handlers, ok := a.handlers[issuecredential.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", issuecredential.CommandName)
}
return &IssueCredential{handlers: handlers}, nil
}
// GetPresentProofController returns an PresentProof instance.
func (a *Aries) GetPresentProofController() (api.PresentProofController, error) {
handlers, ok := a.handlers[presentproof.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", presentproof.CommandName)
}
return &PresentProof{handlers: handlers}, nil
}
// GetVDRController returns a VDR instance.
func (a *Aries) GetVDRController() (api.VDRController, error) {
handlers, ok := a.handlers[vdr.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", vdr.CommandName)
}
return &VDR{handlers: handlers}, nil
}
// GetMediatorController returns a Mediator instance.
func (a *Aries) GetMediatorController() (api.MediatorController, error) {
handlers, ok := a.handlers[mediator.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", mediator.CommandName)
}
return &Mediator{handlers: handlers}, nil
}
// GetMessagingController returns a Messaging instance.
func (a *Aries) GetMessagingController() (api.MessagingController, error) {
handlers, ok := a.handlers[messaging.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", messaging.CommandName)
}
return &Messaging{handlers: handlers}, nil
}
// GetOutOfBandController returns a OutOfBand instance.
func (a *Aries) GetOutOfBandController() (api.OutOfBandController, error) {
handlers, ok := a.handlers[outofband.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", outofband.CommandName)
}
return &OutOfBand{handlers: handlers}, nil
}
// GetOutOfBandV2Controller returns a OutOfBandV2 instance.
func (a *Aries) GetOutOfBandV2Controller() (api.OutOfBandV2Controller, error) {
handlers, ok := a.handlers[outofbandv2.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", outofbandv2.CommandName)
}
return &OutOfBandV2{handlers: handlers}, nil
}
// GetKMSController returns a KMS instance.
func (a *Aries) GetKMSController() (api.KMSController, error) {
handlers, ok := a.handlers[kms.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", kms.CommandName)
}
return &KMS{handlers: handlers}, nil
}
// GetLDController returns an LD instance.
func (a *Aries) GetLDController() (api.LDController, error) {
handlers, ok := a.handlers[ldcommand.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", ldcommand.CommandName)
}
return &LD{handlers: handlers}, nil
}
// GetVCWalletController returns a VCWalletController instance.
func (a *Aries) GetVCWalletController() (api.VCWalletController, error) {
handlers, ok := a.handlers[vcwallet.CommandName]
if !ok {
return nil, fmt.Errorf("no handlers found for controller [%s]", vcwallet.CommandName)
}
return &VCWallet{handlers: handlers}, nil
}