mirror of
https://github.com/status-im/status-keycard-go.git
synced 2025-01-19 02:42:50 +00:00
add load keys support
This commit is contained in:
parent
ec962ae1e7
commit
3727d89ae7
@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
|
||||||
|
|
||||||
skg "github.com/status-im/status-keycard-go"
|
skg "github.com/status-im/status-keycard-go"
|
||||||
"github.com/status-im/status-keycard-go/signal"
|
"github.com/status-im/status-keycard-go/signal"
|
||||||
@ -15,9 +14,10 @@ import (
|
|||||||
|
|
||||||
var flow *skg.KeycardFlow
|
var flow *skg.KeycardFlow
|
||||||
var finished chan (struct{})
|
var finished chan (struct{})
|
||||||
var ppIdx = 0
|
var correctPairing = "KeycardTest"
|
||||||
var pairingPasses = [2]string{"WrongOne", "KeycardTest"}
|
|
||||||
var correctPIN = "123456"
|
var correctPIN = "123456"
|
||||||
|
var correctPUK = "123456123456"
|
||||||
|
var keyUID = "136cbfc087cf7df6cf3248bce7563d4253b302b2f9e2b5eef8713fa5091409bc"
|
||||||
|
|
||||||
func signalHandler(j []byte) {
|
func signalHandler(j []byte) {
|
||||||
var sig signal.Envelope
|
var sig signal.Envelope
|
||||||
@ -31,16 +31,23 @@ func signalHandler(j []byte) {
|
|||||||
case skg.CardInserted:
|
case skg.CardInserted:
|
||||||
fmt.Printf("Card inserted\n")
|
fmt.Printf("Card inserted\n")
|
||||||
case skg.SwapCard:
|
case skg.SwapCard:
|
||||||
fmt.Printf("Swap card. You have 5 seconds\n")
|
fmt.Printf("Swap card. Changing constraint\n")
|
||||||
time.Sleep(5 * time.Second)
|
flow.Resume(skg.FlowParams{skg.KeyUID: keyUID})
|
||||||
flow.Resume(skg.FlowParams{})
|
|
||||||
case skg.EnterPairing:
|
case skg.EnterPairing:
|
||||||
fmt.Printf("Entering pass: %+v\n", pairingPasses[ppIdx])
|
fmt.Printf("Entering pass: %+v\n", correctPairing)
|
||||||
flow.Resume(skg.FlowParams{skg.PairingPass: pairingPasses[ppIdx]})
|
flow.Resume(skg.FlowParams{skg.PairingPass: correctPairing})
|
||||||
ppIdx = (ppIdx + 1) % 2
|
|
||||||
case skg.EnterPIN:
|
case skg.EnterPIN:
|
||||||
fmt.Printf("Entering PIN: %+v\n", correctPIN)
|
fmt.Printf("Entering PIN: %+v\n", correctPIN)
|
||||||
flow.Resume(skg.FlowParams{skg.PIN: correctPIN})
|
flow.Resume(skg.FlowParams{skg.PIN: correctPIN})
|
||||||
|
case skg.EnterNewPIN:
|
||||||
|
fmt.Printf("Creating PIN: %+v\n", correctPIN)
|
||||||
|
flow.Resume(skg.FlowParams{skg.NewPIN: correctPIN})
|
||||||
|
case skg.EnterNewPUK:
|
||||||
|
fmt.Printf("Creating PUK: %+v\n", correctPUK)
|
||||||
|
flow.Resume(skg.FlowParams{skg.NewPUK: correctPUK})
|
||||||
|
case skg.EnterNewPair:
|
||||||
|
fmt.Printf("Creating pairing: %+v\n", correctPairing)
|
||||||
|
flow.Resume(skg.FlowParams{skg.NewPairing: correctPairing})
|
||||||
case skg.FlowResult:
|
case skg.FlowResult:
|
||||||
fmt.Printf("Flow result: %+v\n", sig.Event)
|
fmt.Printf("Flow result: %+v\n", sig.Event)
|
||||||
close(finished)
|
close(finished)
|
||||||
@ -90,10 +97,13 @@ func main() {
|
|||||||
|
|
||||||
signal.SetKeycardSignalHandler(signalHandler)
|
signal.SetKeycardSignalHandler(signalHandler)
|
||||||
|
|
||||||
|
testFlow(skg.GetAppInfo, skg.FlowParams{skg.FactoryReset: true})
|
||||||
|
testFlow(skg.LoadAccount, skg.FlowParams{skg.Mnemonic: "receive fan copper bracket end train again sustain wet siren throw cigar"})
|
||||||
|
testFlow(skg.UnpairThis, skg.FlowParams{skg.PIN: correctPIN})
|
||||||
|
testFlow(skg.RecoverAccount, skg.FlowParams{skg.PairingPass: "WrongPass", skg.PIN: "234567"})
|
||||||
|
testFlow(skg.Login, skg.FlowParams{skg.KeyUID: "60a78c98d5dd659f714eb7072bfb2c0d8a65f74a8f6aff7bb27cf56ae1feec17"})
|
||||||
testFlow(skg.GetAppInfo, skg.FlowParams{})
|
testFlow(skg.GetAppInfo, skg.FlowParams{})
|
||||||
testFlow(skg.RecoverAccount, skg.FlowParams{skg.PIN: "234567"})
|
testFlow(skg.ExportPublic, skg.FlowParams{skg.BIP44Path: "m/44'/60'/0'/0/1"})
|
||||||
testFlow(skg.Login, skg.FlowParams{})
|
|
||||||
testFlow(skg.GetAppInfo, skg.FlowParams{})
|
|
||||||
testFlow(skg.Sign, skg.FlowParams{skg.TXHash: "60a78c98d5dd659f714eb7072bfb2c0d8a65f74a8f6aff7bb27cf56ae1feec17", skg.BIP44Path: "m/44'/60'/0'/0/0"})
|
testFlow(skg.Sign, skg.FlowParams{skg.TXHash: "60a78c98d5dd659f714eb7072bfb2c0d8a65f74a8f6aff7bb27cf56ae1feec17", skg.BIP44Path: "m/44'/60'/0'/0/0"})
|
||||||
testFlow(skg.UnpairThis, skg.FlowParams{skg.PIN: correctPIN})
|
testFlow(skg.UnpairThis, skg.FlowParams{skg.PIN: correctPIN})
|
||||||
}
|
}
|
||||||
|
33
flow.go
33
flow.go
@ -101,6 +101,7 @@ func (f *KeycardFlow) runFlow() {
|
|||||||
signal.Send(FlowResult, result)
|
signal.Send(FlowResult, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
f.params = nil
|
||||||
f.state = Idle
|
f.state = Idle
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +159,18 @@ func (f *KeycardFlow) requireKeys() error {
|
|||||||
return f.pauseAndRestart(SwapCard, ErrorNoKeys)
|
return f.pauseAndRestart(SwapCard, ErrorNoKeys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *KeycardFlow) requireNoKeys() error {
|
||||||
|
if f.cardInfo.keyUID == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if overwrite, ok := f.params[Overwrite]; ok && overwrite.(bool) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.pauseAndRestart(SwapCard, ErrorHasKeys)
|
||||||
|
}
|
||||||
|
|
||||||
func (f *KeycardFlow) closeKeycard(kc *keycardContext) {
|
func (f *KeycardFlow) closeKeycard(kc *keycardContext) {
|
||||||
if kc != nil {
|
if kc != nil {
|
||||||
kc.stop()
|
kc.stop()
|
||||||
@ -336,7 +349,25 @@ func (f *KeycardFlow) exportPublicFlow(kc *keycardContext) (FlowStatus, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *KeycardFlow) loadKeysFlow(kc *keycardContext) (FlowStatus, error) {
|
func (f *KeycardFlow) loadKeysFlow(kc *keycardContext) (FlowStatus, error) {
|
||||||
return nil, errors.New("not implemented yet")
|
err := f.requireNoKeys()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = f.openSCAndAuthenticate(kc, false)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = f.loadKeys(kc)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return FlowStatus{KeyUID: f.cardInfo.keyUID}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *KeycardFlow) signFlow(kc *keycardContext) (FlowStatus, error) {
|
func (f *KeycardFlow) signFlow(kc *keycardContext) (FlowStatus, error) {
|
||||||
|
@ -101,15 +101,17 @@ func (f *KeycardFlow) initCard(kc *keycardContext) error {
|
|||||||
|
|
||||||
err := kc.init(newPIN.(string), newPUK.(string), newPairing.(string))
|
err := kc.init(newPIN.(string), newPUK.(string), newPairing.(string))
|
||||||
|
|
||||||
if err == nil {
|
if err != nil {
|
||||||
f.params[PIN] = newPIN
|
return err
|
||||||
f.params[PairingPass] = newPairing
|
|
||||||
delete(f.params, NewPIN)
|
|
||||||
delete(f.params, NewPUK)
|
|
||||||
delete(f.params, NewPairing)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
f.params[PIN] = newPIN
|
||||||
|
f.params[PairingPass] = newPairing
|
||||||
|
delete(f.params, NewPIN)
|
||||||
|
delete(f.params, NewPUK)
|
||||||
|
delete(f.params, NewPairing)
|
||||||
|
|
||||||
|
return restartErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *KeycardFlow) openSC(kc *keycardContext, giveup bool) error {
|
func (f *KeycardFlow) openSC(kc *keycardContext, giveup bool) error {
|
||||||
@ -283,9 +285,7 @@ func (f *KeycardFlow) exportKey(kc *keycardContext, path string, onlyPublic bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *KeycardFlow) exportBIP44Key(kc *keycardContext) (*KeyPair, error) {
|
func (f *KeycardFlow) exportBIP44Key(kc *keycardContext) (*KeyPair, error) {
|
||||||
path, ok := f.params[BIP44Path]
|
if path, ok := f.params[BIP44Path]; ok {
|
||||||
|
|
||||||
if ok {
|
|
||||||
return f.exportKey(kc, path.(string), true)
|
return f.exportKey(kc, path.(string), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,6 +298,29 @@ func (f *KeycardFlow) exportBIP44Key(kc *keycardContext) (*KeyPair, error) {
|
|||||||
return f.exportBIP44Key(kc)
|
return f.exportBIP44Key(kc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *KeycardFlow) loadKeys(kc *keycardContext) error {
|
||||||
|
if mnemonic, ok := f.params[Mnemonic]; ok {
|
||||||
|
keyUID, err := kc.loadMnemonic(mnemonic.(string), "")
|
||||||
|
|
||||||
|
if isSCardError(err) {
|
||||||
|
return restartErr()
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f.cardInfo.keyUID = btox(keyUID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := f.pauseAndWait(EnterMnemonic, ErrorLoading)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.loadKeys(kc)
|
||||||
|
}
|
||||||
|
|
||||||
func (f *KeycardFlow) changePIN(kc *keycardContext) error {
|
func (f *KeycardFlow) changePIN(kc *keycardContext) error {
|
||||||
if newPIN, ok := f.params[NewPIN]; ok {
|
if newPIN, ok := f.params[NewPIN]; ok {
|
||||||
err := kc.changePin(newPIN.(string))
|
err := kc.changePin(newPIN.(string))
|
||||||
|
@ -48,18 +48,19 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FlowResult = "keycard.flow-result"
|
FlowResult = "keycard.flow-result"
|
||||||
InsertCard = "keycard.action.insert-card"
|
InsertCard = "keycard.action.insert-card"
|
||||||
CardInserted = "keycard.action.card-inserted"
|
CardInserted = "keycard.action.card-inserted"
|
||||||
SwapCard = "keycard.action.swap-card"
|
SwapCard = "keycard.action.swap-card"
|
||||||
EnterPairing = "keycard.action.enter-pairing"
|
EnterPairing = "keycard.action.enter-pairing"
|
||||||
EnterPIN = "keycard.action.enter-pin"
|
EnterPIN = "keycard.action.enter-pin"
|
||||||
EnterPUK = "keycard.action.enter-puk"
|
EnterPUK = "keycard.action.enter-puk"
|
||||||
EnterNewPair = "keycard.action.enter-new-pairing"
|
EnterNewPair = "keycard.action.enter-new-pairing"
|
||||||
EnterNewPIN = "keycard.action.enter-new-pin"
|
EnterNewPIN = "keycard.action.enter-new-pin"
|
||||||
EnterNewPUK = "keycard.action.enter-new-puk"
|
EnterNewPUK = "keycard.action.enter-new-puk"
|
||||||
EnterTXHash = "keycard.action.enter-tx-hash"
|
EnterTXHash = "keycard.action.enter-tx-hash"
|
||||||
EnterPath = "keycard.action.enter-bip44-path"
|
EnterPath = "keycard.action.enter-bip44-path"
|
||||||
|
EnterMnemonic = "keycard.action.enter-mnemonic"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -70,12 +71,14 @@ const (
|
|||||||
ErrorUnknownFlow = "unknown-flow"
|
ErrorUnknownFlow = "unknown-flow"
|
||||||
ErrorNotAKeycard = "not-a-keycard"
|
ErrorNotAKeycard = "not-a-keycard"
|
||||||
ErrorNoKeys = "no-keys"
|
ErrorNoKeys = "no-keys"
|
||||||
|
ErrorHasKeys = "has-keys"
|
||||||
ErrorRequireInit = "require-init"
|
ErrorRequireInit = "require-init"
|
||||||
ErrorPairing = "pairing"
|
ErrorPairing = "pairing"
|
||||||
ErrorUnblocking = "unblocking"
|
ErrorUnblocking = "unblocking"
|
||||||
ErrorSigning = "signing"
|
ErrorSigning = "signing"
|
||||||
ErrorExporting = "exporting"
|
ErrorExporting = "exporting"
|
||||||
ErrorChanging = "changing-credentials"
|
ErrorChanging = "changing-credentials"
|
||||||
|
ErrorLoading = "loading-keys"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -104,6 +107,7 @@ const (
|
|||||||
TXHash = "tx-hash"
|
TXHash = "tx-hash"
|
||||||
BIP44Path = "bip44-path"
|
BIP44Path = "bip44-path"
|
||||||
TXSignature = "tx-signature"
|
TXSignature = "tx-signature"
|
||||||
|
Overwrite = "overwrite"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user