add export key

This commit is contained in:
Michele Balistreri 2021-10-21 08:48:51 +03:00
parent 27bf041c5e
commit e7dd1cc9a3
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
5 changed files with 42 additions and 9 deletions

View File

@ -237,11 +237,9 @@ func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) {
return nil, err
}
err = kc.unpairCurrent()
err = f.unpairCurrent(kc)
if isSCardError(err) {
return nil, restartErr()
} else if err != nil {
if err != nil {
return nil, err
}

View File

@ -155,3 +155,23 @@ func (f *KeycardFlow) openSCAndAuthenticate(kc *keycardContext) error {
return f.authenticate(kc)
}
func (f *KeycardFlow) unpairCurrent(kc *keycardContext) error {
err := kc.unpairCurrent()
if isSCardError(err) {
return restartErr()
}
return err
}
func (f *KeycardFlow) exportKey(kc *keycardContext, path string, onlyPublic bool) (*KeyPair, error) {
keyPair, err := kc.exportKey(true, false, onlyPublic, path)
if isSCardError(err) {
return nil, restartErr()
}
return keyPair, err
}

View File

@ -70,3 +70,12 @@ const (
maxPUKRetries = 5
maxFreeSlots = 5
)
const (
masterPath = "m"
walletRoothPath = "m/44'/60'/0'/0"
walletPath = walletRoothPath + "/0"
eip1581Path = "m/43'/60'/1581'"
whisperPath = eip1581Path + "/0'/0"
encryptionPath = eip1581Path + "/1'/0"
)

View File

@ -289,29 +289,29 @@ func (kc *keycardContext) signWithPath(data []byte, path string) (*types.Signatu
return sig, nil
}
func (kc *keycardContext) exportKey(derive bool, makeCurrent bool, onlyPublic bool, path string) ([]byte, []byte, string, error) {
func (kc *keycardContext) exportKey(derive bool, makeCurrent bool, onlyPublic bool, path string) (*KeyPair, error) {
<-kc.connected
if kc.runErr != nil {
return nil, nil, "", kc.runErr
return nil, kc.runErr
}
address := ""
privKey, pubKey, err := kc.cmdSet.ExportKey(derive, makeCurrent, onlyPublic, path)
if err != nil {
l("exportKey failed %+v", err)
return nil, nil, "", err
return nil, err
}
if pubKey != nil {
ecdsaPubKey, err := crypto.UnmarshalPubkey(pubKey)
if err != nil {
return nil, nil, "", err
return nil, err
}
address = crypto.PubkeyToAddress(*ecdsaPubKey).Hex()
}
return privKey, pubKey, address, nil
return &KeyPair{Address: address, PublicKey: pubKey, PrivateKey: privKey}, nil
}
func (kc *keycardContext) loadSeed(seed []byte) ([]byte, error) {

View File

@ -108,3 +108,9 @@ type ApplicationStatus struct {
KeyInitialized bool `json:"keyInitialized"`
Path string `json:"path"`
}
type KeyPair struct {
Address string `json:"address"`
PublicKey hexString `json:"publicKey"`
PrivateKey hexString `json:"privateKey"`
}