This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
controller.go
178 lines (148 loc) · 5.36 KB
/
controller.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// Package controller provides command handlers.
package controller
import (
"fmt"
ariescmd "github.com/hyperledger/aries-framework-go/pkg/controller/command"
"github.com/hyperledger/aries-framework-go/pkg/controller/webnotifier"
"github.com/hyperledger/aries-framework-go/pkg/framework/context"
"github.com/trustbloc/agent-sdk/pkg/controller/command"
"github.com/trustbloc/agent-sdk/pkg/controller/command/blindedrouting"
didclientcmd "github.com/trustbloc/agent-sdk/pkg/controller/command/didclient"
mediatorclientcmd "github.com/trustbloc/agent-sdk/pkg/controller/command/mediatorclient"
"github.com/trustbloc/agent-sdk/pkg/controller/command/store"
"github.com/trustbloc/agent-sdk/pkg/controller/rest"
blindedroutingrest "github.com/trustbloc/agent-sdk/pkg/controller/rest/blindedrouting"
"github.com/trustbloc/agent-sdk/pkg/controller/rest/didclient"
"github.com/trustbloc/agent-sdk/pkg/controller/rest/mediatorclient"
)
const wsPath = "/ws"
type allOpts struct {
blocDomain string
unanchoredDIDMaxLifeTime int
didAnchorOrigin string
sidetreeToken string
msgHandler ariescmd.MessageHandler
notifier ariescmd.Notifier
webhookURLs []string
}
// Opt represents a controller option.
type Opt func(opts *allOpts)
// WithBlocDomain is an option allowing for the trustbloc domain to be set.
func WithBlocDomain(blocDomain string) Opt {
return func(opts *allOpts) {
opts.blocDomain = blocDomain
}
}
// WithDidAnchorOrigin is an option allowing for the did anchor origin.
func WithDidAnchorOrigin(origin string) Opt {
return func(opts *allOpts) {
opts.didAnchorOrigin = origin
}
}
// WithSidetreeToken is an option allowing for the sidetree token.
func WithSidetreeToken(token string) Opt {
return func(opts *allOpts) {
opts.sidetreeToken = token
}
}
// WithUnanchoredDIDMaxLifeTime option is max time for unanchored to be trusted .
func WithUnanchoredDIDMaxLifeTime(seconds int) Opt {
return func(opts *allOpts) {
opts.unanchoredDIDMaxLifeTime = seconds
}
}
// WithMessageHandler is an option allowing for the message handler to be set.
func WithMessageHandler(handler ariescmd.MessageHandler) Opt {
return func(opts *allOpts) {
opts.msgHandler = handler
}
}
// WithWebhookURLs is an option for setting up a webhook dispatcher which will notify clients of events.
func WithWebhookURLs(webhookURLs ...string) Opt {
return func(opts *allOpts) {
opts.webhookURLs = webhookURLs
}
}
// WithNotifier is an option for setting up a notifier which will notify clients of events.
func WithNotifier(notifier ariescmd.Notifier) Opt {
return func(opts *allOpts) {
opts.notifier = notifier
}
}
// GetCommandHandlers returns all command handlers provided by controller.
func GetCommandHandlers(ctx *context.Provider, opts ...Opt) ([]command.Handler, error) { //nolint:interfacer
cmdOpts := &allOpts{}
// Apply options
for _, opt := range opts {
opt(cmdOpts)
}
notifier := cmdOpts.notifier
if notifier == nil {
notifier = webnotifier.New(wsPath, cmdOpts.webhookURLs)
}
// did client command operation.
didClientCmd, err := didclientcmd.New(cmdOpts.blocDomain, cmdOpts.didAnchorOrigin, cmdOpts.sidetreeToken,
cmdOpts.unanchoredDIDMaxLifeTime, ctx)
if err != nil {
return nil, fmt.Errorf("failed to initialize DID client: %w", err)
}
// mediator client command operation,
mediatorClientCmd, err := mediatorclientcmd.New(ctx, cmdOpts.msgHandler, notifier)
if err != nil {
return nil, err
}
// blindedRoutingCmd command operation.
blindedRoutingCmd, err := blindedrouting.New(ctx, cmdOpts.msgHandler, notifier)
if err != nil {
return nil, err
}
storeCmd, err := store.New(ctx)
if err != nil {
return nil, err
}
// creat handlers for all command operations.
var allHandlers []command.Handler
allHandlers = append(allHandlers, didClientCmd.GetHandlers()...)
allHandlers = append(allHandlers, mediatorClientCmd.GetHandlers()...)
allHandlers = append(allHandlers, blindedRoutingCmd.GetHandlers()...)
allHandlers = append(allHandlers, storeCmd.GetHandlers()...)
return allHandlers, nil
}
// GetRESTHandlers returns all REST handlers provided by controller.
func GetRESTHandlers(ctx *context.Provider, opts ...Opt) ([]rest.Handler, error) { //nolint:interfacer
restOpts := &allOpts{}
// Apply options
for _, opt := range opts {
opt(restOpts)
}
notifier := restOpts.notifier
if notifier == nil {
notifier = webnotifier.New(wsPath, restOpts.webhookURLs)
}
// DID Client REST operation.
didClientOp, err := didclient.New(ctx, restOpts.blocDomain, restOpts.didAnchorOrigin, restOpts.sidetreeToken,
restOpts.unanchoredDIDMaxLifeTime)
if err != nil {
return nil, err
}
// mediator client REST operation.
mediatorClientOp, err := mediatorclient.New(ctx, restOpts.msgHandler, notifier)
if err != nil {
return nil, err
}
// blinded routing REST operation.
blindedRoutingOp, err := blindedroutingrest.New(ctx, restOpts.msgHandler, notifier)
if err != nil {
return nil, err
}
// creat handlers from all REST operations.
var allHandlers []rest.Handler
allHandlers = append(allHandlers, didClientOp.GetRESTHandlers()...)
allHandlers = append(allHandlers, mediatorClientOp.GetRESTHandlers()...)
allHandlers = append(allHandlers, blindedRoutingOp.GetRESTHandlers()...)
return allHandlers, nil
}