From f284636027d7e2a5ed6bba7a8cb49e3e104abe73 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Thu, 21 Oct 2021 09:09:20 +0300 Subject: [PATCH] implement recover account --- flow.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- flow_types.go | 7 +++++++ types.go | 2 +- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/flow.go b/flow.go index ee64543..cfc017a 100644 --- a/flow.go +++ b/flow.go @@ -152,6 +152,14 @@ func (f *KeycardFlow) pauseAndRestart(action string, errMsg string) error { return restartErr() } +func (f *KeycardFlow) requireKeys() error { + if f.cardInfo.keyUID != "" { + return nil + } + + return f.pauseAndRestart(SwapCard, ErrorNoKeys) +} + func (f *KeycardFlow) closeKeycard(kc *keycardContext) { if kc != nil { kc.stop() @@ -221,13 +229,51 @@ func (f *KeycardFlow) getAppInfoFlow(kc *keycardContext) (FlowStatus, error) { } func (f *KeycardFlow) recoverAccountFlow(kc *keycardContext) (FlowStatus, error) { - err := f.openSCAndAuthenticate(kc) + err := f.requireKeys() if err != nil { return nil, err } - return nil, errors.New("not yet implemented") + err = f.openSCAndAuthenticate(kc) + + if err != nil { + return nil, err + } + + result := FlowStatus{} + + key, err := f.exportKey(kc, encryptionPath, false) + if err != nil { + return nil, err + } + result[EncKey] = key + + key, err = f.exportKey(kc, whisperPath, false) + if err != nil { + return nil, err + } + result[WhisperKey] = key + + key, err = f.exportKey(kc, walletRoothPath, true) + if err != nil { + return nil, err + } + result[WalleRootKey] = key + + key, err = f.exportKey(kc, walletPath, true) + if err != nil { + return nil, err + } + result[WalletKey] = key + + key, err = f.exportKey(kc, masterPath, true) + if err != nil { + return nil, err + } + result[MasterKey] = key + + return result, nil } func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) { diff --git a/flow_types.go b/flow_types.go index 5d2d8ca..c24a52e 100644 --- a/flow_types.go +++ b/flow_types.go @@ -51,6 +51,7 @@ const ( ErrorConnection = "connection-error" ErrorUnknownFlow = "unknown-flow" ErrorNotAKeycard = "not-a-keycard" + ErrorNoKeys = "no-keys" ) const ( @@ -63,6 +64,12 @@ const ( PUKRetries = "puk-retries" PairingPass = "pairing-pass" PIN = "pin" + MasterKey = "master-key" + WalleRootKey = "wallet-root-key" + WalletKey = "wallet-key" + EIP1581Key = "eip1581-key" + WhisperKey = "whisper-key" + EncKey = "encryption-key" ) const ( diff --git a/types.go b/types.go index 0ad73ae..8a7dbdf 100644 --- a/types.go +++ b/types.go @@ -112,5 +112,5 @@ type ApplicationStatus struct { type KeyPair struct { Address string `json:"address"` PublicKey hexString `json:"publicKey"` - PrivateKey hexString `json:"privateKey"` + PrivateKey hexString `json:"privateKey,omitempty"` }