From 0c8ef9ba67817a6057580259fb130913c72db8b6 Mon Sep 17 00:00:00 2001 From: Alvaro Denis Date: Tue, 14 Jan 2020 19:02:13 -0500 Subject: [PATCH] give more details in the input reader to inprove user interaction ref #165 --- src/cli/utils.go | 13 +++++-- src/integration/proxy/sequencer.go | 60 ++++++++++++++++++++---------- src/skywallet/skywallet.go | 8 ++++ 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/cli/utils.go b/src/cli/utils.go index e89602a..9806fa5 100644 --- a/src/cli/utils.go +++ b/src/cli/utils.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/fibercrypto/fibercryptowallet/src/util/logging" "github.com/sirupsen/logrus" "github.com/fibercrypto/skywallet-go/src/integration/proxy" "github.com/fibercrypto/skywallet-go/src/skywallet" @@ -41,10 +42,14 @@ func createDevice(devType string) (skywallet.Devicer, error) { return nil, err } } - return proxy.NewSequencer(device, false, func() string{ - var line string - fmt.Scanln(&line) - return line + return proxy.NewSequencer(device, false, func(kind skywallet.InputRequestKind, title, message string) (string, error) { + if kind != skywallet.RequestJustInformingUser { + logging.NewMasterLogger().Printf(title) + var line string + fmt.Scanln(&line) + return line, nil + } + return "", nil }), nil } diff --git a/src/integration/proxy/sequencer.go b/src/integration/proxy/sequencer.go index 2f52218..f6d031b 100644 --- a/src/integration/proxy/sequencer.go +++ b/src/integration/proxy/sequencer.go @@ -2,7 +2,6 @@ package proxy //nolint goimports import ( "errors" - "fmt" "github.com/gogo/protobuf/proto" "io/ioutil" "sync" @@ -20,11 +19,11 @@ type Sequencer struct { log *logging.MasterLogger logCli *logging.MasterLogger dev skywallet.Devicer - scan func()string + scan func(requestKind skywallet.InputRequestKind, title, message string) (string, error) } // NewSequencer create a new sequencer instance -func NewSequencer(dev skywallet.Devicer, cliSpeechless bool, scanner func()string) skywallet.Devicer { +func NewSequencer(dev skywallet.Devicer, cliSpeechless bool, scanner func(requestKind skywallet.InputRequestKind, title, message string) (string, error)) skywallet.Devicer { sq := &Sequencer{ log: logging.NewMasterLogger(), logCli: logging.NewMasterLogger(), @@ -71,27 +70,38 @@ func (sq *Sequencer) handleInputInteraction(msg wire.Message) (wire.Message, err sq.log.Errorln("pmr.Type should not be null") return wire.Message{}, errors.New("unexpected null object") } - switch *pmr.Type { - case messages.PinMatrixRequestType_PinMatrixRequestType_Current: - sq.log.Infoln("enter current pin:") - case messages.PinMatrixRequestType_PinMatrixRequestType_NewFirst: - sq.log.Infoln("enter new pin:") - case messages.PinMatrixRequestType_PinMatrixRequestType_NewSecond: - sq.log.Infoln("confirm new pin:") - default: - errStr := "unexpected PinMatrixRequestType" - sq.log.WithField("type", *pmr.Type).Errorln(errStr) - return wire.Message{}, errors.New(errStr) + str4PinType, err := func(t messages.PinMatrixRequestType) (string, error) { + switch t { + case messages.PinMatrixRequestType_PinMatrixRequestType_Current: + return "enter current pin:", nil + case messages.PinMatrixRequestType_PinMatrixRequestType_NewFirst: + return "enter new pin:", nil + case messages.PinMatrixRequestType_PinMatrixRequestType_NewSecond: + return "confirm new pin:", nil + default: + return "", errors.New("unexpected PinMatrixRequestType") + } + }(*pmr.Type) + if err != nil { + sq.log.WithField("type", *pmr.Type).Errorln(err.Error()) + return wire.Message{}, err + } + pinEnc, err := sq.scan(skywallet.RequestKindPinMatrix, str4PinType, "") + if err != nil { + sq.log.WithError(err).Errorln("reading user input") + return wire.Message{}, err } - pinEnc := sq.scan() if msg, err = sq.dev.PinMatrixAck(pinEnc); err != nil { sq.log.WithError(err).Errorln("pin matrixAck ack: sending message failed") return wire.Message{}, err } return sq.handleInputInteraction(msg) } else if msg.Kind == uint16(messages.MessageType_MessageType_PassphraseRequest) { - sq.log.Println("PassphraseRequest request:") - passphrase := sq.scan() + passphrase, err := sq.scan(skywallet.RequestKindPassphrase, "PassphraseRequest request:", "passprase TODO chnageit") + if err != nil { + sq.log.WithError(err).Errorln("reading user input") + return wire.Message{}, err + } msg, err = sq.dev.PassphraseAck(passphrase) msgStr, err := handleResponse(msg, err) if err != nil { @@ -100,13 +110,25 @@ func (sq *Sequencer) handleInputInteraction(msg wire.Message) (wire.Message, err } sq.logCli.Infof("PassphraseAck response:", msgStr) } else if msg.Kind == uint16(messages.MessageType_MessageType_WordRequest) { - fmt.Printf("Word: ") - word := sq.scan() + word, err := sq.scan(skywallet.RequestKindWord, "Word:", "wordd TODO change it") + if err != nil { + sq.log.WithError(err).Errorln("reading user input") + return wire.Message{}, err + } if msg, err = sq.dev.WordAck(word); err != nil { sq.log.WithError(err).Errorln("word ack: sending message failed") return msg, err } } else if msg.Kind == uint16(messages.MessageType_MessageType_ButtonRequest) { + _, err := sq.scan( + skywallet.RequestJustInformingUser, + "Verify the information in the device", + "Be careful on checking all the details in the device screen, if all this" + + " is right, then take the required action to continue...") + if err != nil { + sq.log.WithError(err).Errorln("reading user input") + return wire.Message{}, err + } if msg, err = sq.dev.ButtonAck(); err != nil { sq.log.WithError(err).Errorln("handling message failed") return msg, err diff --git a/src/skywallet/skywallet.go b/src/skywallet/skywallet.go index 1227649..5f4dc66 100644 --- a/src/skywallet/skywallet.go +++ b/src/skywallet/skywallet.go @@ -64,6 +64,14 @@ const ( firstHardenedChild = uint32(0x80000000) ) +type InputRequestKind uint32 +const ( + RequestKindPinMatrix InputRequestKind = iota + RequestKindPassphrase + RequestKindWord + RequestJustInformingUser +) + //go:generate mockery -name Devicer -case underscore -inpkg -testonly // Devicer provides api for the hw wallet functions