Skip to content

Commit

Permalink
give more details in the input reader to inprove user interaction ref f…
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvaro Denis committed Jan 15, 2020
1 parent 8a27de7 commit 0c8ef9b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
13 changes: 9 additions & 4 deletions src/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
60 changes: 41 additions & 19 deletions src/integration/proxy/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proxy //nolint goimports

import (
"errors"
"fmt"
"github.com/gogo/protobuf/proto"
"io/ioutil"
"sync"
Expand All @@ -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(),
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/skywallet/skywallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c8ef9b

Please sign in to comment.