-
Notifications
You must be signed in to change notification settings - Fork 19
/
service_run.go
947 lines (837 loc) · 31.3 KB
/
service_run.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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
vgclose "code.vegaprotocol.io/vega/libs/close"
vgjob "code.vegaprotocol.io/vega/libs/job"
vgterm "code.vegaprotocol.io/vega/libs/term"
vgzap "code.vegaprotocol.io/vega/libs/zap"
"code.vegaprotocol.io/vega/paths"
coreversion "code.vegaprotocol.io/vega/version"
walletapi "code.vegaprotocol.io/vega/wallet/api"
"code.vegaprotocol.io/vega/wallet/api/interactor"
netStoreV1 "code.vegaprotocol.io/vega/wallet/network/store/v1"
"code.vegaprotocol.io/vega/wallet/preferences"
"code.vegaprotocol.io/vega/wallet/service"
svcStoreV1 "code.vegaprotocol.io/vega/wallet/service/store/v1"
serviceV1 "code.vegaprotocol.io/vega/wallet/service/v1"
serviceV2 "code.vegaprotocol.io/vega/wallet/service/v2"
"code.vegaprotocol.io/vega/wallet/service/v2/connections"
tokenStoreV1 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/longliving/v1"
sessionStoreV1 "code.vegaprotocol.io/vega/wallet/service/v2/connections/store/session/v1"
"code.vegaprotocol.io/vega/wallet/version"
"code.vegaprotocol.io/vega/wallet/wallets"
"github.com/golang/protobuf/jsonpb"
"github.com/muesli/cancelreader"
"golang.org/x/term"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
const MaxConsentRequests = 100
var (
ErrEnableAutomaticConsentFlagIsRequiredWithoutTTY = errors.New("--automatic-consent flag is required without TTY")
ErrMsysUnsupported = errors.New("this command is not supported on msys, please use a standard windows terminal")
)
var (
runServiceLong = cli.LongDesc(`
Start a Vega wallet service behind an HTTP server.
By default, every incoming transactions will have to be reviewed in the
terminal.
To terminate the service, hit ctrl+c.
NOTE: The --output flag is ignored in this command.
WARNING: This command is not supported on msys, due to some system
incompatibilities with the user input management.
Non-exhaustive list of affected systems: Cygwin, minty, git-bash.
`)
runServiceExample = cli.Examples(`
# Start the service
{{.Software}} service run --network NETWORK
# Start the service with automatic consent of incoming transactions
{{.Software}} service run --network NETWORK --automatic-consent
# Start the service without verifying network version compatibility
{{.Software}} service run --network NETWORK --no-version-check
`)
)
type ServicePreCheck func(rf *RootFlags) error
type RunServiceHandler func(io.Writer, *RootFlags, *RunServiceFlags) error
func NewCmdRunService(w io.Writer, rf *RootFlags) *cobra.Command {
return BuildCmdRunService(w, RunService, rf)
}
func BuildCmdRunService(w io.Writer, handler RunServiceHandler, rf *RootFlags) *cobra.Command {
f := &RunServiceFlags{}
cmd := &cobra.Command{
Use: "run",
Short: "Start the Vega wallet service",
Long: runServiceLong,
Example: runServiceExample,
RunE: func(_ *cobra.Command, _ []string) error {
if err := f.Validate(rf); err != nil {
return err
}
if err := handler(w, rf, f); err != nil {
return err
}
return nil
},
}
cmd.Flags().StringVarP(&f.Network,
"network", "n",
"",
"Network configuration to use",
)
cmd.Flags().BoolVar(&f.EnableAutomaticConsent,
"automatic-consent",
false,
"Automatically approve incoming transaction. Only use this flag when you have absolute trust in incoming transactions!",
)
cmd.Flags().BoolVar(&f.LoadTokens,
"load-tokens",
false,
"Load the sessions with long-living tokens",
)
cmd.PersistentFlags().BoolVar(&f.NoVersionCheck,
"no-version-check",
false,
"Do not check the network version compatibility",
)
cmd.Flags().StringVar(&f.TokensPassphraseFile,
"tokens-passphrase-file",
"",
"Path to the file containing the tokens database passphrase",
)
autoCompleteNetwork(cmd, rf.Home)
return cmd
}
type RunServiceFlags struct {
Network string
EnableAutomaticConsent bool
LoadTokens bool
TokensPassphraseFile string
NoVersionCheck bool
tokensPassphrase string
}
func (f *RunServiceFlags) Validate(rf *RootFlags) error {
if len(f.Network) == 0 {
return flags.MustBeSpecifiedError("network")
}
if !f.LoadTokens && f.TokensPassphraseFile != "" {
return flags.OneOfParentsFlagMustBeSpecifiedError("tokens-passphrase-file", "load-tokens")
}
if f.LoadTokens {
if err := ensureAPITokenStoreIsInit(rf); err != nil {
return err
}
passphrase, err := flags.GetPassphraseWithOptions(flags.PassphraseOptions{Name: "tokens"}, f.TokensPassphraseFile)
if err != nil {
return err
}
f.tokensPassphrase = passphrase
}
return nil
}
func RunService(w io.Writer, rf *RootFlags, f *RunServiceFlags) error {
if err := ensureNotRunningInMsys(); err != nil {
return err
}
p := printer.NewInteractivePrinter(w)
vegaPaths := paths.New(rf.Home)
cliLog, cliLogPath, _, err := buildJSONFileLogger(vegaPaths, paths.WalletCLILogsHome, "info")
if err != nil {
return err
}
defer vgzap.Sync(cliLog)
cliLog = cliLog.Named("command")
if rf.Output == flags.InteractiveOutput && version.IsUnreleased() {
cliLog.Warn("Current software is an unreleased version", zap.String("version", coreversion.Get()))
str := p.String()
str.CrossMark().DangerText("You are running an unreleased version of the Vega wallet (").DangerText(coreversion.Get()).DangerText(").").NextLine()
str.Pad().DangerText("Use it at your own risk!").NextSection()
p.Print(str)
} else {
cliLog.Warn("Current software is a released version", zap.String("version", coreversion.Get()))
}
p.Print(p.String().CheckMark().Text("CLI logs located at: ").SuccessText(cliLogPath).NextLine())
closer := vgclose.NewCloser()
defer closer.CloseAll()
walletStore, err := wallets.InitialiseStoreFromPaths(vegaPaths, true)
if err != nil {
cliLog.Error("Could not initialise wallets store", zap.Error(err))
return fmt.Errorf("could not initialise wallets store: %w", err)
}
closer.Add(walletStore.Close)
netStore, err := netStoreV1.InitialiseStore(vegaPaths)
if err != nil {
cliLog.Error("Could not initialise network store", zap.Error(err))
return fmt.Errorf("could not initialise network store: %w", err)
}
svcStore, err := svcStoreV1.InitialiseStore(vegaPaths)
if err != nil {
cliLog.Error("Could not initialise service store", zap.Error(err))
return fmt.Errorf("could not initialise service store: %w", err)
}
sessionStore, err := sessionStoreV1.InitialiseStore(vegaPaths)
if err != nil {
cliLog.Error("Could not initialise session store", zap.Error(err))
return fmt.Errorf("could not initialise session store: %w", err)
}
var tokenStore connections.TokenStore
if f.LoadTokens {
cliLog.Warn("Long-living tokens enabled")
p.Print(p.String().WarningBangMark().WarningText("Long-living tokens enabled").NextLine())
s, err := tokenStoreV1.InitialiseStore(vegaPaths, f.tokensPassphrase)
if err != nil {
if errors.Is(err, tokenStoreV1.ErrWrongPassphrase) {
return err
}
return fmt.Errorf("couldn't load the token store: %w", err)
}
closer.Add(s.Close)
tokenStore = s
} else {
s := tokenStoreV1.NewEmptyStore()
tokenStore = s
}
loggerBuilderFunc := func(levelName string) (*zap.Logger, zap.AtomicLevel, error) {
svcLog, svcLogPath, level, err := buildJSONFileLogger(vegaPaths, paths.WalletServiceLogsHome, levelName)
if err != nil {
return nil, zap.AtomicLevel{}, err
}
p.Print(p.String().CheckMark().Text("Service logs located at: ").SuccessText(svcLogPath).NextLine())
return svcLog, level, nil
}
consentRequests := make(chan serviceV1.ConsentRequest, MaxConsentRequests)
sentTransactions := make(chan serviceV1.SentTransaction)
closer.Add(func() {
close(consentRequests)
close(sentTransactions)
})
jobRunner := vgjob.NewRunner(context.Background())
policy, err := buildPolicy(jobRunner.Ctx(), cliLog, p, f, consentRequests, sentTransactions)
if err != nil {
return err
}
receptionChanForParking := make(chan interactor.Interaction, 1000)
closer.Add(func() {
close(receptionChanForParking)
})
seqInteractor := interactor.NewParallelInteractor(jobRunner.Ctx(), receptionChanForParking)
connectionsManager, err := connections.NewManager(serviceV2.NewStdTime(), walletStore, tokenStore, sessionStore, seqInteractor)
if err != nil {
return fmt.Errorf("could not create the connection manager: %w", err)
}
closer.Add(func() {
connectionsManager.EndAllSessionConnections()
})
serviceStarter := service.NewStarter(walletStore, netStore, svcStore, connectionsManager, policy, seqInteractor, loggerBuilderFunc)
rc, err := serviceStarter.Start(jobRunner, f.Network, f.NoVersionCheck)
if err != nil {
cliLog.Error("Failed to start HTTP server", zap.Error(err))
jobRunner.StopAllJobs()
return err
}
cliLog.Info("Starting HTTP service", zap.String("url", rc.ServiceURL))
p.Print(p.String().CheckMark().Text("Starting HTTP service at: ").SuccessText(rc.ServiceURL).NextSection())
receptionChanForFrontend := make(chan interactor.Interaction, 1000)
closer.Add(func() {
close(receptionChanForFrontend)
})
jobRunner.Go(func(jobCtx context.Context) {
startInteractionParking(cliLog, jobCtx, receptionChanForParking, receptionChanForFrontend)
})
jobRunner.Go(func(jobCtx context.Context) {
for {
select {
case <-jobCtx.Done():
cliLog.Info("Stop listening to incoming interactions in front-end")
return
case interaction := <-receptionChanForFrontend:
handleAPIv2Request(jobCtx, interaction, f.EnableAutomaticConsent, p)
case consentRequest := <-consentRequests:
handleAPIv1Request(consentRequest, cliLog, p, sentTransactions)
}
}
})
waitUntilInterruption(jobRunner.Ctx(), cliLog, p, rc.ErrCh)
// Wait for all goroutine to exit.
cliLog.Info("Waiting for the service to stop")
p.Print(p.String().BlueArrow().Text("Waiting for the service to stop...").NextLine())
jobRunner.StopAllJobs()
cliLog.Info("The service stopped")
p.Print(p.String().CheckMark().Text("The service stopped.").NextLine())
return nil
}
func buildPolicy(ctx context.Context, cliLog *zap.Logger, p *printer.InteractivePrinter, f *RunServiceFlags, consentRequests chan serviceV1.ConsentRequest, sentTransactions chan serviceV1.SentTransaction) (serviceV1.Policy, error) {
if vgterm.HasTTY() {
cliLog.Info("TTY detected")
if f.EnableAutomaticConsent {
cliLog.Info("Automatic consent enabled")
p.Print(p.String().WarningBangMark().WarningText("Automatic consent enabled").NextLine())
return serviceV1.NewAutomaticConsentPolicy(), nil
}
cliLog.Info("Explicit consent enabled")
p.Print(p.String().CheckMark().Text("Explicit consent enabled").NextLine())
return serviceV1.NewExplicitConsentPolicy(ctx, consentRequests, sentTransactions), nil
}
cliLog.Info("No TTY detected")
if !f.EnableAutomaticConsent {
cliLog.Error("Explicit consent can't be used when no TTY is attached to the process")
return nil, ErrEnableAutomaticConsentFlagIsRequiredWithoutTTY
}
cliLog.Info("Automatic consent enabled.")
return serviceV1.NewAutomaticConsentPolicy(), nil
}
func buildJSONFileLogger(vegaPaths paths.Paths, logDir paths.StatePath, logLevel string) (*zap.Logger, string, zap.AtomicLevel, error) {
loggerConfig := vgzap.DefaultConfig()
loggerConfig = vgzap.WithFileOutputForDedicatedProcess(loggerConfig, vegaPaths.StatePathFor(logDir))
logFilePath := loggerConfig.OutputPaths[0]
loggerConfig = vgzap.WithJSONFormat(loggerConfig)
loggerConfig = vgzap.WithLevel(loggerConfig, logLevel)
level := loggerConfig.Level
logger, err := vgzap.Build(loggerConfig)
if err != nil {
return nil, "", zap.AtomicLevel{}, fmt.Errorf("could not setup the logger: %w", err)
}
return logger, logFilePath, level, nil
}
// waitUntilInterruption will wait for a sigterm or sigint interrupt.
func waitUntilInterruption(ctx context.Context, cliLog *zap.Logger, p *printer.InteractivePrinter, errChan <-chan error) {
gracefulStop := make(chan os.Signal, 1)
defer func() {
signal.Stop(gracefulStop)
close(gracefulStop)
}()
signal.Notify(gracefulStop, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
for {
select {
case sig := <-gracefulStop:
cliLog.Info("OS signal received", zap.String("signal", fmt.Sprintf("%+v", sig)))
str := p.String()
str.NextSection().WarningBangMark().WarningText(fmt.Sprintf("Signal \"%+v\" received.", sig)).NextLine()
str.Pad().WarningText("You can hit CTRL+C once again to forcefully exit, but some resources may not be properly cleaned up.").NextSection()
p.Print(str)
return
case err := <-errChan:
cliLog.Error("Initiating shutdown due to an internal error reported by the service", zap.Error(err))
return
case <-ctx.Done():
cliLog.Info("Stop listening to OS signals")
return
}
}
}
func handleAPIv1Request(consentRequest serviceV1.ConsentRequest, log *zap.Logger, p *printer.InteractivePrinter, sentTransactions chan serviceV1.SentTransaction) {
m := jsonpb.Marshaler{Indent: " "}
marshalledTx, err := m.MarshalToString(consentRequest.Tx)
if err != nil {
log.Error("could not marshal transaction from consent request", zap.Error(err))
panic(err)
}
str := p.String()
str.BlueArrow().Text("New transaction received: ").NextLine()
str.InfoText(marshalledTx).NextLine()
p.Print(str)
if flags.DoYouApproveTx() {
log.Info("user approved the signing of the transaction", zap.Any("transaction", marshalledTx))
consentRequest.Confirmation <- serviceV1.ConsentConfirmation{Decision: true}
p.Print(p.String().CheckMark().SuccessText("Transaction approved").NextLine())
sentTx := <-sentTransactions
log.Info("transaction sent", zap.Any("ID", sentTx.TxID), zap.Any("hash", sentTx.TxHash))
if sentTx.Error != nil {
log.Error("transaction failed", zap.Any("transaction", marshalledTx))
p.Print(p.String().DangerBangMark().DangerText("Transaction failed").NextLine())
p.Print(p.String().DangerBangMark().DangerText("Error: ").DangerText(sentTx.Error.Error()).NextSection())
} else {
log.Info("transaction sent", zap.Any("hash", sentTx.TxHash))
p.Print(p.String().CheckMark().Text("Transaction with hash ").SuccessText(sentTx.TxHash).Text(" sent!").NextSection())
}
} else {
log.Info("user rejected the signing of the transaction", zap.Any("transaction", marshalledTx))
consentRequest.Confirmation <- serviceV1.ConsentConfirmation{Decision: false}
p.Print(p.String().DangerBangMark().DangerText("Transaction rejected").NextSection())
}
}
func handleAPIv2Request(ctx context.Context, interaction interactor.Interaction, enableAutomaticConsent bool, p *printer.InteractivePrinter) {
switch data := interaction.Data.(type) {
case interactor.InteractionSessionBegan:
p.Print(p.String().NextLine())
case interactor.InteractionSessionEnded:
p.Print(p.String().NextLine())
case interactor.RequestWalletConnectionReview:
p.Print(p.String().BlueArrow().Text("The application \"").InfoText(data.Hostname).Text("\" wants to connect to your wallet.").NextLine())
var connectionApproval string
approved, err := yesOrNo(ctx, data.ControlCh, p.String().QuestionMark().Text("Do you approve connecting your wallet to this application?"), p)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
if approved {
p.Print(p.String().CheckMark().Text("Connection approved.").NextLine())
connectionApproval = string(preferences.ApprovedOnlyThisTime)
} else {
p.Print(p.String().CrossMark().Text("Connection rejected.").NextLine())
connectionApproval = string(preferences.RejectedOnlyThisTime)
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.WalletConnectionDecisionName,
Data: interactor.WalletConnectionDecision{
ConnectionApproval: connectionApproval,
},
}
case interactor.RequestWalletSelection:
str := p.String().BlueArrow().Text("Here are the available wallets:").NextLine()
for _, w := range data.AvailableWallets {
str.ListItem().Text("- ").InfoText(w).NextLine()
}
p.Print(str)
selectedWallet, err := readInput(ctx, data.ControlCh, p.String().QuestionMark().Text("Which wallet do you want to use? "), p, data.AvailableWallets)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.SelectedWalletName,
Data: interactor.SelectedWallet{
Wallet: selectedWallet,
},
}
case interactor.RequestPassphrase:
if len(data.Reason) != 0 {
str := p.String().BlueArrow().Text(data.Reason).NextLine()
p.Print(str)
}
passphrase, err := readPassphrase(ctx, data.ControlCh, p.String().BlueArrow().Text("Enter the passphrase for the wallet \"").InfoText(data.Wallet).Text("\": "), p)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.EnteredPassphraseName,
Data: interactor.EnteredPassphrase{
Passphrase: passphrase,
},
}
case interactor.ErrorOccurred:
if data.Type == string(walletapi.InternalErrorType) {
str := p.String().DangerBangMark().DangerText("An internal error occurred: ").DangerText(data.Error).NextLine()
str.DangerBangMark().DangerText("The request has been canceled.").NextLine()
p.Print(str)
} else if data.Type == string(walletapi.UserErrorType) {
p.Print(p.String().DangerBangMark().DangerText(data.Error).NextLine())
} else {
p.Print(p.String().DangerBangMark().DangerText(fmt.Sprintf("Error: %s (%s)", data.Error, data.Type)).NextLine())
}
case interactor.Log:
str := p.String()
switch data.Type {
case string(walletapi.InfoLog):
str.BlueArrow()
case string(walletapi.ErrorLog):
str.CrossMark()
case string(walletapi.WarningLog):
str.WarningBangMark()
case string(walletapi.SuccessLog):
str.CheckMark()
default:
str.Text("- ")
}
p.Print(str.Text(data.Message).NextLine())
case interactor.RequestSucceeded:
if data.Message == "" {
p.Print(p.String().CheckMark().SuccessText("Request succeeded").NextLine())
} else {
p.Print(p.String().CheckMark().SuccessText(data.Message).NextLine())
}
case interactor.RequestPermissionsReview:
str := p.String().BlueArrow().Text("The application \"").InfoText(data.Hostname).Text("\" requires the following permissions for \"").InfoText(data.Wallet).Text("\":").NextLine()
for perm, access := range data.Permissions {
str.ListItem().Text("- ").InfoText(perm).Text(": ").InfoText(access).NextLine()
}
p.Print(str)
approved, err := yesOrNo(ctx, data.ControlCh, p.String().QuestionMark().Text("Do you want to grant these permissions?"), p)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
if approved {
p.Print(p.String().CheckMark().Text("Permissions update approved.").NextLine())
} else {
p.Print(p.String().CrossMark().Text("Permissions update rejected.").NextLine())
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.DecisionName,
Data: interactor.Decision{
Approved: approved,
},
}
case interactor.RequestTransactionReviewForSending:
str := p.String().BlueArrow().Text("The application \"").InfoText(data.Hostname).Text("\" wants to send the following transaction:").NextLine()
str.Pad().Text("Using the key: ").InfoText(data.PublicKey).NextLine()
str.Pad().Text("From the wallet: ").InfoText(data.Wallet).NextLine()
fmtCmd := strings.Replace(" "+data.Transaction, "\n", "\n ", -1)
str.InfoText(fmtCmd).NextLine()
p.Print(str)
approved := true
if enableAutomaticConsent {
p.Print(p.String().CheckMark().Text("Sending automatically approved.").NextLine())
} else {
a, err := yesOrNo(ctx, data.ControlCh, p.String().QuestionMark().Text("Do you want to send this transaction?"), p)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
approved = a
if approved {
p.Print(p.String().CheckMark().Text("Sending approved.").NextLine())
} else {
p.Print(p.String().CrossMark().Text("Sending rejected.").NextLine())
}
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.DecisionName,
Data: interactor.Decision{
Approved: approved,
},
}
case interactor.RequestTransactionReviewForSigning:
str := p.String().BlueArrow().Text("The application \"").InfoText(data.Hostname).Text("\" wants to sign the following transaction:").NextLine()
str.Pad().Text("Using the key: ").InfoText(data.PublicKey).NextLine()
str.Pad().Text("From the wallet: ").InfoText(data.Wallet).NextLine()
fmtCmd := strings.Replace(" "+data.Transaction, "\n", "\n ", -1)
str.InfoText(fmtCmd).NextLine()
p.Print(str)
approved := true
if enableAutomaticConsent {
p.Print(p.String().CheckMark().Text("Signing automatically approved.").NextLine())
} else {
a, err := yesOrNo(ctx, data.ControlCh, p.String().QuestionMark().Text("Do you want to sign this transaction?"), p)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
approved = a
if approved {
p.Print(p.String().CheckMark().Text("Signing approved.").NextLine())
} else {
p.Print(p.String().CrossMark().Text("Signing rejected.").NextLine())
}
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.DecisionName,
Data: interactor.Decision{
Approved: approved,
},
}
case interactor.RequestTransactionReviewForChecking:
str := p.String().BlueArrow().Text("The application \"").InfoText(data.Hostname).Text("\" wants to check the following transaction:").NextLine()
str.Pad().Text("Using the key: ").InfoText(data.PublicKey).NextLine()
str.Pad().Text("From the wallet: ").InfoText(data.Wallet).NextLine()
fmtCmd := strings.Replace(" "+data.Transaction, "\n", "\n ", -1)
str.InfoText(fmtCmd).NextLine()
p.Print(str)
approved := true
if enableAutomaticConsent {
p.Print(p.String().CheckMark().Text("Checking automatically approved.").NextLine())
} else {
a, err := yesOrNo(ctx, data.ControlCh, p.String().QuestionMark().Text("Do you allow the network to check this transaction?"), p)
if err != nil {
p.Print(p.String().CrossMark().DangerText(err.Error()).NextLine())
return
}
approved = a
if approved {
p.Print(p.String().CheckMark().Text("Checking approved.").NextLine())
} else {
p.Print(p.String().CrossMark().Text("Checking rejected.").NextLine())
}
}
data.ResponseCh <- interactor.Interaction{
TraceID: interaction.TraceID,
Name: interactor.DecisionName,
Data: interactor.Decision{
Approved: approved,
},
}
case interactor.TransactionFailed:
str := p.String()
str.DangerBangMark().DangerText("The transaction failed.").NextLine()
str.Pad().DangerText(data.Error.Error()).NextLine()
str.Pad().Text("Sent at: ").Text(data.SentAt.Format(time.ANSIC)).NextLine()
p.Print(str)
case interactor.TransactionSucceeded:
str := p.String()
str.CheckMark().SuccessText("The transaction has been delivered.").NextLine()
str.Pad().Text("Transaction hash: ").SuccessText(data.TxHash).NextLine()
str.Pad().Text("Sent at: ").Text(data.SentAt.Format(time.ANSIC)).NextLine()
p.Print(str)
default:
panic(fmt.Sprintf("unhandled interaction: %q", interaction.Name))
}
}
func readInput(ctx context.Context, controlCh chan error, question *printer.FormattedString, p *printer.InteractivePrinter, options []string) (string, error) {
inputCh := make(chan string)
defer close(inputCh)
reader, err := cancelreader.NewReader(os.Stdin)
if err != nil {
return "", fmt.Errorf("could not initialize the input reader: %w", err)
}
defer reader.Cancel()
go func() {
for {
p.Print(question)
answer, err := readString(reader)
if err != nil {
return
}
if len(options) == 0 {
inputCh <- answer
return
}
for _, option := range options {
if answer == option {
inputCh <- answer
return
}
}
if len(answer) > 0 {
p.Print(p.String().DangerBangMark().DangerText(fmt.Sprintf("%q is not a valid option", answer)).NextLine())
}
}
}()
select {
case <-ctx.Done():
return "", ctx.Err()
case err := <-controlCh:
reader.Cancel()
return "", err
case input := <-inputCh:
return input, nil
}
}
func yesOrNo(ctx context.Context, controlCh <-chan error, question *printer.FormattedString, p *printer.InteractivePrinter) (bool, error) {
choiceCh := make(chan bool)
defer close(choiceCh)
reader, err := cancelreader.NewReader(os.Stdin)
if err != nil {
return false, fmt.Errorf("could not initialize the input reader: %w", err)
}
defer reader.Cancel()
go func() {
question.Text(" (yes/no) ")
for {
p.Print(question)
answer, err := readString(reader)
if err != nil {
return
}
answer = strings.ToLower(answer)
switch answer {
case "yes", "y":
choiceCh <- true
return
case "no", "n":
choiceCh <- false
return
default:
if len(answer) > 0 {
p.Print(p.String().DangerBangMark().DangerText(fmt.Sprintf("%q is not a valid answer, enter \"yes\" or \"no\"\n", answer)))
}
}
}
}()
select {
case <-ctx.Done():
return false, ctx.Err()
case err := <-controlCh:
reader.Cancel()
return false, err
case choice, ok := <-choiceCh:
return ok && choice, nil
}
}
func readString(reader io.Reader) (string, error) {
var line string
for {
var input [50]byte
bytesRead, err := reader.Read(input[:])
// As said in the Read documentation:
// Callers should treat a return of 0 and nil as indicating that
// nothing happened; in particular it does not indicate EOF.
if bytesRead == 0 && err == nil {
continue
}
if bytesRead == 0 && err != nil && !errors.Is(err, io.EOF) {
return "", err
}
// As said in the Read documentation:
// Callers should always process the n > 0 bytes returned before
// considering the error err. Doing so correctly handles I/O errors
// that happen after reading some bytes and also both of the
// allowed EOF behaviors.
line += string(input[:bytesRead])
// Verify if the input chunk contains the "enter" key.
if strings.ContainsAny(line, "\r\n") || err != nil {
break
}
}
return strings.Trim(line, " \r\n\t"), nil // nolint:nilerr
}
// ensureNotRunningInMsys verifies if the underlying shell is not running on
// msys.
// This command is not supported on msys, due to some system incompatibilities
// with the user input management.
// Non-exhaustive list of affected systems: Cygwin, minty, git-bash.
func ensureNotRunningInMsys() error {
ms := os.Getenv("MSYSTEM")
if ms != "" {
return ErrMsysUnsupported
}
return nil
}
func readPassphrase(ctx context.Context, controlCh chan error, question *printer.FormattedString, p *printer.InteractivePrinter) (string, error) {
inputCh := make(chan string)
defer close(inputCh)
// We cannot interrupt cleanly an on-going password read. So, at least, we
// ensure it can stop on the next password attempt.
shouldStop := atomic.Bool{}
waitForExitInput := make(chan interface{})
go func() {
for {
p.Print(question)
passphrase, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(fmt.Errorf("could not read passphrase: %w", err))
}
p.Print(p.String().NextLine())
if shouldStop.Load() {
close(waitForExitInput)
return
}
if len(passphrase) > 0 {
inputCh <- string(passphrase)
return
}
}
}()
select {
case <-ctx.Done():
return "", ctx.Err()
case err := <-controlCh:
shouldStop.Store(true)
<-waitForExitInput
return "", err
case input := <-inputCh:
return input, nil
}
}
func startInteractionParking(log *zap.Logger, ctx context.Context, inboundCh <-chan interactor.Interaction, outboundCh chan<- interactor.Interaction) {
sessionsOrder := []string{}
parkedInteractionSessions := map[string]chan interactor.Interaction{}
defer func() {
for _, iChan := range parkedInteractionSessions {
close(iChan)
}
}()
for {
select {
case <-ctx.Done():
log.Info("Stop listening to incoming interactions in parking")
return
case interaction, ok := <-inboundCh:
if !ok {
return
}
if len(sessionsOrder) == 0 {
sessionsOrder = append(sessionsOrder, interaction.TraceID)
}
// If the interaction we receive is from the session currently
// handled in the frontend, we transmit it immediately.
if sessionsOrder[0] == interaction.TraceID {
outboundCh <- interaction
// If this is the last interaction for the current session, we
// free up the resources and transmit the next session to the UI.
if _, ok := interaction.Data.(interactor.InteractionSessionEnded); ok {
sessionsOrder = switchToNextSession(sessionsOrder, parkedInteractionSessions, outboundCh)
}
} else {
// If not, then we park it until the current session end.
parkedSessionCh, ok := parkedInteractionSessions[interaction.TraceID]
if !ok {
// First time we see this session, we track it.
parkedSessionCh = make(chan interactor.Interaction, 100)
parkedInteractionSessions[interaction.TraceID] = parkedSessionCh
sessionsOrder = append(sessionsOrder, interaction.TraceID)
}
parkedSessionCh <- interaction
}
}
}
}
func switchToNextSession(sessionsOrder []string, parkedInteractionSessions map[string]chan interactor.Interaction, outboundCh chan<- interactor.Interaction) []string {
// Pop this session out the queue, and move onto the next
// session.
sessionsOrder = sessionsOrder[1:]
if len(sessionsOrder) == 0 {
return sessionsOrder
}
currentSessionCh := parkedInteractionSessions[sessionsOrder[0]]
hasInteractionsToSend := true
for hasInteractionsToSend {
select {
case currentSessionInteraction, ok := <-currentSessionCh:
if !ok {
hasInteractionsToSend = false
break
}
outboundCh <- currentSessionInteraction
if _, ok := currentSessionInteraction.Data.(interactor.InteractionSessionEnded); ok {
// We remove the session and its interactions buffer from the
// parked ones, because the next interactions we will receive
// for that session will be transmitted immediately.
close(currentSessionCh)
delete(parkedInteractionSessions, sessionsOrder[0])
// The session is already finished, move to the next until we
// transmitted all parked session or until a session is ongoing.
return switchToNextSession(sessionsOrder, parkedInteractionSessions, outboundCh)
}
default:
hasInteractionsToSend = false
}
}
// We remove the session and its interactions buffer from the
// parked ones, because the next interactions we will receive
// for that session will be transmitted immediately.
close(currentSessionCh)
delete(parkedInteractionSessions, sessionsOrder[0])
return sessionsOrder
}