242 lines
4.3 KiB
Go
Raw Normal View History

package statuskeycardgo
2021-10-14 13:14:43 +03:00
2021-10-15 10:35:01 +03:00
import (
"errors"
"github.com/status-im/status-keycard-go/signal"
)
2021-10-14 13:14:43 +03:00
2021-10-20 11:44:57 +03:00
type cardStatus struct {
instanceUID string
keyUID string
freeSlots int
pinRetries int
pukRetries int
}
2021-10-18 14:50:56 +03:00
type KeycardFlow struct {
flowType FlowType
state runState
wakeUp chan (struct{})
2021-10-19 12:19:02 +03:00
pairings *pairingStore
params FlowParams
2021-10-20 11:44:57 +03:00
cardInfo cardStatus
2021-10-18 14:50:56 +03:00
}
func NewFlow(storageDir string) (*KeycardFlow, error) {
2021-10-19 12:19:02 +03:00
p, err := newPairingStore(storageDir)
if err != nil {
return nil, err
}
2021-10-18 14:50:56 +03:00
flow := &KeycardFlow{
2021-10-19 12:19:02 +03:00
wakeUp: make(chan (struct{})),
pairings: p,
2021-10-14 13:14:43 +03:00
}
return flow, nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) Start(flowType FlowType, params FlowParams) error {
2021-10-15 10:35:01 +03:00
if f.state != Idle {
2021-10-14 13:14:43 +03:00
return errors.New("already running")
}
f.flowType = flowType
f.params = params
2021-10-15 10:35:01 +03:00
f.state = Running
2021-10-14 13:14:43 +03:00
go f.runFlow()
return nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) Resume(params FlowParams) error {
2021-10-15 10:35:01 +03:00
if f.state != Paused {
2021-10-14 13:14:43 +03:00
return errors.New("only paused flows can be resumed")
}
for k, v := range params {
f.params[k] = v
}
2021-10-15 10:35:01 +03:00
f.state = Resuming
2021-10-14 13:14:43 +03:00
f.wakeUp <- struct{}{}
return nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) Cancel() error {
2021-10-14 13:14:43 +03:00
prevState := f.state
2021-10-15 10:35:01 +03:00
if prevState != Idle {
2021-10-14 13:14:43 +03:00
return errors.New("cannot cancel idle flow")
}
2021-10-15 10:35:01 +03:00
f.state = Cancelling
if prevState == Paused {
2021-10-14 13:14:43 +03:00
f.wakeUp <- struct{}{}
}
return nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) runFlow() {
2021-10-18 10:19:09 +03:00
var result FlowStatus
2021-10-18 16:25:20 +03:00
var err error
for {
2021-10-20 11:44:57 +03:00
f.cardInfo = cardStatus{freeSlots: -1, pinRetries: -1, pukRetries: -1}
2021-10-18 16:25:20 +03:00
result, err = f.connectedFlow()
2021-10-15 10:35:01 +03:00
2021-10-18 16:25:20 +03:00
if _, ok := err.(*restartError); !ok {
if result == nil {
result = FlowStatus{ErrorKey: err.Error()}
}
break
}
2021-10-15 10:35:01 +03:00
}
if f.state != Cancelling {
signal.SendEvent(FlowResult, result)
}
f.state = Idle
}
2021-10-20 11:44:57 +03:00
func (f *KeycardFlow) pause(action string, errMsg string) {
status := FlowParams{}
if errMsg != "" {
status[ErrorKey] = errMsg
}
if f.cardInfo.freeSlots != -1 {
status[InstanceUID] = f.cardInfo.instanceUID
status[KeyUID] = f.cardInfo.keyUID
status[FreeSlots] = f.cardInfo.freeSlots
}
if f.cardInfo.pinRetries != -1 {
status[PINRetries] = f.cardInfo.pinRetries
status[PUKRetries] = f.cardInfo.pukRetries
}
2021-10-15 10:35:01 +03:00
signal.SendEvent(action, status)
f.state = Paused
}
2021-10-20 11:44:57 +03:00
func (f *KeycardFlow) pauseAndWait(action string, errMsg string) error {
2021-10-20 12:01:38 +03:00
if f.state == Cancelling {
return errors.New("cancel")
}
2021-10-20 11:44:57 +03:00
f.pause(action, errMsg)
2021-10-15 10:35:01 +03:00
<-f.wakeUp
2021-10-15 12:38:06 +03:00
if f.state == Resuming {
f.state = Running
2021-10-18 16:29:55 +03:00
return nil
2021-10-15 12:38:06 +03:00
} else {
2021-10-18 16:29:55 +03:00
return errors.New("cancel")
2021-10-15 12:38:06 +03:00
}
2021-10-15 10:35:01 +03:00
}
2021-10-20 11:44:57 +03:00
func (f *KeycardFlow) pauseAndRestart(action string, errMsg string) error {
err := f.pauseAndWait(action, errMsg)
2021-10-18 16:34:15 +03:00
if err != nil {
return err
}
return restartErr()
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) closeKeycard(kc *keycardContext) {
2021-10-15 10:35:01 +03:00
if kc != nil {
kc.stop()
}
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) connect() *keycardContext {
2021-10-15 10:35:01 +03:00
kc, err := startKeycardContext()
if err != nil {
return nil
}
2021-10-20 11:44:57 +03:00
f.pause(InsertCard, "")
2021-10-15 10:35:01 +03:00
select {
case <-f.wakeUp:
if f.state != Cancelling {
panic("Resuming is not expected during connection")
}
return nil
case <-kc.connected:
if kc.runErr != nil {
return nil
}
2021-10-18 14:50:56 +03:00
signal.SendEvent(CardInserted, FlowStatus{})
2021-10-15 10:35:01 +03:00
return kc
}
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) connectedFlow() (FlowStatus, error) {
kc := f.connect()
defer f.closeKeycard(kc)
2021-10-15 12:38:06 +03:00
2021-10-20 12:01:38 +03:00
if kc == nil {
return nil, errors.New(ErrorConnection)
2021-10-18 14:50:56 +03:00
}
2021-10-20 12:01:38 +03:00
if factoryReset, ok := f.params[FactoryReset]; ok && factoryReset.(bool) {
err := f.factoryReset(kc)
2021-10-15 12:38:06 +03:00
2021-10-20 12:01:38 +03:00
if err != nil {
return nil, err
2021-10-15 12:38:06 +03:00
}
}
2021-10-14 13:14:43 +03:00
2021-10-20 12:01:38 +03:00
err := f.selectKeycard(kc)
2021-10-19 12:19:02 +03:00
if err != nil {
2021-10-20 12:01:38 +03:00
return nil, err
2021-10-19 12:19:02 +03:00
}
2021-10-20 12:01:38 +03:00
switch f.flowType {
case GetAppInfo:
return f.getAppInfoFlow(kc)
case RecoverAccount:
return f.recoverAccountFlow(kc)
case UnpairThis:
return f.unpairThisFlow(kc)
default:
return nil, errors.New(ErrorUnknownFlow)
2021-10-19 12:19:02 +03:00
}
2021-10-18 16:25:20 +03:00
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) getAppInfoFlow(kc *keycardContext) (FlowStatus, error) {
return FlowStatus{ErrorKey: ErrorOK, AppInfo: toAppInfo(kc.cmdSet.ApplicationInfo)}, nil
2021-10-20 11:44:57 +03:00
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) recoverAccountFlow(kc *keycardContext) (FlowStatus, error) {
err := f.openSCAndAuthenticate(kc)
2021-10-19 12:19:02 +03:00
if err != nil {
2021-10-20 12:01:38 +03:00
return nil, err
2021-10-19 12:19:02 +03:00
}
2021-10-20 12:01:38 +03:00
return nil, errors.New("not yet implemented")
2021-10-18 16:25:20 +03:00
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) {
2021-10-18 16:25:20 +03:00
err := f.openSCAndAuthenticate(kc)
2021-10-15 12:38:06 +03:00
if err != nil {
2021-10-18 16:25:20 +03:00
return nil, err
2021-10-15 12:38:06 +03:00
}
2021-10-18 16:25:20 +03:00
return nil, errors.New("not yet implemented")
2021-10-18 15:47:27 +03:00
}