forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
528 lines (422 loc) · 12.7 KB
/
main.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
// +build js,wasm
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"syscall/js"
"time"
"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
"github.com/hyperledger/aries-framework-go/component/storage/indexeddb"
"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/controller"
cmdctrl "github.com/hyperledger/aries-framework-go/pkg/controller/command"
"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/framework/aries"
"github.com/hyperledger/aries-framework-go/pkg/vdr/httpbinding"
spilog "github.com/hyperledger/aries-framework-go/spi/log"
)
func init() {
log.Initialize(New())
}
const (
wasmStartupTopic = "asset-ready"
handleResultFn = "handleResult"
ariesCommandPkg = "aries"
ariesStartFn = "Start"
ariesStopFn = "Stop"
workers = 2
)
var logger = log.New("aries-js-worker")
// TODO Signal JS when WASM is loaded and ready.
// This is being used in tests for now.
var (
ready = make(chan struct{}) //nolint:gochecknoglobals
isTest = false //nolint:gochecknoglobals
)
// command is received from JS.
type command struct {
ID string `json:"id"`
Pkg string `json:"pkg"`
Fn string `json:"fn"`
Payload map[string]interface{} `json:"payload"`
}
// result is sent back to JS.
type result struct {
ID string `json:"id"`
IsErr bool `json:"isErr"`
ErrMsg string `json:"errMsg"`
Payload map[string]interface{} `json:"payload"`
Topic string `json:"topic"`
}
// ariesStartOpts contains opts for starting aries.
type ariesStartOpts struct {
Label string `json:"agent-default-label"`
HTTPResolvers []string `json:"http-resolver-url"`
AutoAccept bool `json:"auto-accept"`
OutboundTransport []string `json:"outbound-transport"`
TransportReturnRoute string `json:"transport-return-route"`
LogLevel string `json:"log-level"`
DBNamespace string `json:"db-namespace"`
}
// main registers the 'handleMsg' function in the JS context's global scope to receive commands.
// results are posted back to the 'handleResult' JS function.
func main() {
// TODO: capacity was added due to deadlock. Looks like js worker are not able to pick up 'output chan *result'.
// Another fix for that is to wrap 'in <- cmd' in a goroutine. e.g go func() { in <- cmd }()
// We need to figure out what is the root cause of deadlock and fix it properly.
input := make(chan *command, 10)
output := make(chan *result)
go pipe(input, output)
go sendTo(output)
js.Global().Set("handleMsg", js.FuncOf(takeFrom(input)))
postInitMsg()
if isTest {
ready <- struct{}{}
}
select {}
}
func takeFrom(in chan *command) func(js.Value, []js.Value) interface{} {
return func(_ js.Value, args []js.Value) interface{} {
cmd := &command{}
if err := json.Unmarshal([]byte(args[0].String()), cmd); err != nil {
logger.Errorf("aries wasm: unable to unmarshal input=%s. err=%s", args[0].String(), err)
return nil
}
in <- cmd
return nil
}
}
func pipe(input chan *command, output chan *result) {
handlers := testHandlers()
addAriesHandlers(handlers)
for w := 0; w < workers; w++ {
go worker(input, output, handlers)
}
}
func worker(input chan *command, output chan *result, handlers map[string]map[string]func(*command) *result) {
for c := range input {
if c.ID == "" {
logger.Warnf("aries wasm: missing ID for input: %v", c)
}
if pkg, found := handlers[c.Pkg]; found {
if fn, found := pkg[c.Fn]; found {
output <- fn(c)
continue
}
}
output <- handlerNotFoundErr(c)
}
}
func sendTo(out chan *result) {
for r := range out {
out, err := json.Marshal(r)
if err != nil {
logger.Errorf("aries wasm: failed to marshal response for id=%s err=%s ", r.ID, err)
}
js.Global().Call(handleResultFn, string(out))
}
}
func cmdExecToFn(exec cmdctrl.Exec) func(*command) *result {
return func(c *command) *result {
b, er := json.Marshal(c.Payload)
if er != nil {
return &result{
ID: c.ID,
IsErr: true,
ErrMsg: fmt.Sprintf("aries wasm: failed to unmarshal payload. err=%s", er),
}
}
req := bytes.NewBuffer(b)
var buf bytes.Buffer
err := exec(&buf, req)
if err != nil {
return newErrResult(c.ID, fmt.Sprintf("code: %+v, message: %s", err.Code(), err.Error()))
}
payload := make(map[string]interface{})
if len(buf.Bytes()) > 0 {
if err := json.Unmarshal(buf.Bytes(), &payload); err != nil {
return &result{
ID: c.ID,
IsErr: true,
ErrMsg: fmt.Sprintf(
"aries wasm: failed to unmarshal command result=%+v err=%s",
buf.String(), err),
}
}
}
return &result{
ID: c.ID,
Payload: payload,
}
}
}
func testHandlers() map[string]map[string]func(*command) *result {
return map[string]map[string]func(*command) *result{
"test": {
"echo": func(c *command) *result {
return &result{
ID: c.ID,
Payload: map[string]interface{}{"echo": c.Payload},
}
},
"throwError": func(c *command) *result {
return newErrResult(c.ID, "an error !!")
},
"timeout": func(c *command) *result {
const echoTimeout = 10 * time.Second
time.Sleep(echoTimeout)
return &result{
ID: c.ID,
Payload: map[string]interface{}{"echo": c.Payload},
}
},
},
}
}
func isStartCommand(c *command) bool {
return c.Pkg == ariesCommandPkg && c.Fn == ariesStartFn
}
func isStopCommand(c *command) bool {
return c.Pkg == ariesCommandPkg && c.Fn == ariesStopFn
}
func handlerNotFoundErr(c *command) *result {
if isStartCommand(c) {
return newErrResult(c.ID, "Aries agent already started")
} else if isStopCommand(c) {
return newErrResult(c.ID, "Aries agent not running")
}
return newErrResult(c.ID, fmt.Sprintf("invalid pkg/fn: %s/%s, make sure aries is started", c.Pkg, c.Fn))
}
func addAriesHandlers(pkgMap map[string]map[string]func(*command) *result) {
fnMap := make(map[string]func(*command) *result)
fnMap[ariesStartFn] = func(c *command) *result {
cOpts, err := startOpts(c.Payload)
if err != nil {
return newErrResult(c.ID, err.Error())
}
err = setLogLevel(cOpts.LogLevel)
if err != nil {
return newErrResult(c.ID, err.Error())
}
options, err := ariesOpts(cOpts)
if err != nil {
return newErrResult(c.ID, err.Error())
}
msgHandler := msghandler.NewRegistrar()
options = append(options, aries.WithMessageServiceProvider(msgHandler))
a, err := aries.New(options...)
if err != nil {
return newErrResult(c.ID, err.Error())
}
ctx, err := a.Context()
if err != nil {
return newErrResult(c.ID, err.Error())
}
commands, err := controller.GetCommandHandlers(ctx, controller.WithMessageHandler(msgHandler),
controller.WithDefaultLabel(cOpts.Label), controller.WithNotifier(&jsNotifier{}))
if err != nil {
return newErrResult(c.ID, err.Error())
}
// add command handlers
addCommandHandlers(commands, pkgMap)
// add stop aries handler
addStopAriesHandler(a, pkgMap)
return &result{
ID: c.ID,
Payload: map[string]interface{}{"message": "aries agent started successfully"},
}
}
pkgMap[ariesCommandPkg] = fnMap
}
func addCommandHandlers(commands []cmdctrl.Handler, pkgMap map[string]map[string]func(*command) *result) {
for _, cmd := range commands {
fnMap, ok := pkgMap[cmd.Name()]
if !ok {
fnMap = make(map[string]func(*command) *result)
}
fnMap[cmd.Method()] = cmdExecToFn(cmd.Handle())
pkgMap[cmd.Name()] = fnMap
}
}
func addStopAriesHandler(a *aries.Aries, pkgMap map[string]map[string]func(*command) *result) {
fnMap := make(map[string]func(*command) *result)
fnMap[ariesStopFn] = func(c *command) *result {
err := a.Close()
if err != nil {
return newErrResult(c.ID, err.Error())
}
// reset handlers when stopped
for k := range pkgMap {
delete(pkgMap, k)
}
// put back start command once stopped
addAriesHandlers(pkgMap)
return &result{
ID: c.ID,
Payload: map[string]interface{}{"message": "aries agent stopped"},
}
}
pkgMap[ariesCommandPkg] = fnMap
}
func newErrResult(id, msg string) *result {
return &result{
ID: id,
IsErr: true,
ErrMsg: "aries wasm: " + msg,
}
}
func startOpts(payload map[string]interface{}) (*ariesStartOpts, error) {
opts := &ariesStartOpts{}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: opts,
})
if err != nil {
return nil, err
}
err = decoder.Decode(payload)
if err != nil {
return nil, err
}
return opts, nil
}
func ariesOpts(opts *ariesStartOpts) ([]aries.Option, error) {
msgHandler := msghandler.NewRegistrar()
var options []aries.Option
options = append(options, aries.WithMessageServiceProvider(msgHandler))
if opts.TransportReturnRoute != "" {
options = append(options, aries.WithTransportReturnRoute(opts.TransportReturnRoute))
}
store, err := indexeddb.NewProvider(opts.DBNamespace)
if err != nil {
return nil, err
}
options = append(options, aries.WithStoreProvider(store))
for _, transport := range opts.OutboundTransport {
switch transport {
case "http":
outbound, err := arieshttp.NewOutbound(arieshttp.WithOutboundHTTPClient(&http.Client{}))
if err != nil {
return nil, err
}
options = append(options, aries.WithOutboundTransports(outbound))
case "ws":
options = append(options, aries.WithOutboundTransports(ws.NewOutbound()))
default:
return nil, fmt.Errorf("unsupported transport : %s", transport)
}
}
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...)
}
return options, 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 setLogLevel(logLevel string) error {
if logLevel != "" {
level, err := log.ParseLevel(logLevel)
if err != nil {
return err
}
log.SetLevel("", level)
logger.Infof("log level set to `%s`", logLevel)
}
return nil
}
// jsNotifier notifies about all incoming events.
type jsNotifier struct {
}
// Notify is mock implementation of webhook notifier Notify().
func (n *jsNotifier) Notify(topic string, message []byte) error {
payload := make(map[string]interface{})
if err := json.Unmarshal(message, &payload); err != nil {
return err
}
out, err := json.Marshal(&result{
ID: uuid.New().String(),
Topic: topic,
Payload: payload,
})
if err != nil {
return err
}
js.Global().Call(handleResultFn, string(out))
return nil
}
func postInitMsg() {
if isTest {
return
}
out, err := json.Marshal(&result{
ID: uuid.New().String(),
Topic: wasmStartupTopic,
})
if err != nil {
panic(err)
}
js.Global().Call(handleResultFn, string(out))
}
// New returns new Logger.
func New() *Logger {
return &Logger{}
}
// Logger describes logger structure.
type Logger struct{}
// GetLogger returns logger implementation.
func (l *Logger) GetLogger(module string) spilog.Logger {
return loggerWrapper{}
}
type loggerWrapper struct{}
func (w loggerWrapper) Fatalf(msg string, args ...interface{}) {
w.write("log_error", fmt.Sprintf(msg, args...))
}
func (w loggerWrapper) Panicf(msg string, args ...interface{}) {
w.write("log_error", fmt.Sprintf(msg, args...))
}
func (w loggerWrapper) Debugf(msg string, args ...interface{}) {
w.write("log_debug", fmt.Sprintf(msg, args...))
}
func (w loggerWrapper) Infof(msg string, args ...interface{}) {
w.write("log_info", fmt.Sprintf(msg, args...))
}
func (w loggerWrapper) Warnf(msg string, args ...interface{}) {
w.write("log_warn", fmt.Sprintf(msg, args...))
}
func (w loggerWrapper) Errorf(msg string, args ...interface{}) {
w.write("log_error", fmt.Sprintf(msg, args...))
}
func (w loggerWrapper) write(t, msg string) {
js.Global().Call("print_log", t, msg)
}