parse export key response

This commit is contained in:
Andrea Franz 2021-10-04 15:26:08 +02:00
parent c8058144ce
commit c32310e39b
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
2 changed files with 37 additions and 4 deletions

View File

@ -229,7 +229,7 @@ func (cs *CommandSet) DeriveKey(path string) error {
return cs.checkOK(resp, err)
}
func (cs *CommandSet) ExportKey(derive bool, makeCurrent bool, onlyPublic bool, path string) ([]byte, error) {
func (cs *CommandSet) ExportKey(derive bool, makeCurrent bool, onlyPublic bool, path string) ([]byte, []byte, error) {
var p1 uint8
if derive == false {
p1 = P1ExportKeyCurrent
@ -244,18 +244,19 @@ func (cs *CommandSet) ExportKey(derive bool, makeCurrent bool, onlyPublic bool,
} else {
p2 = P2ExportKeyPrivateAndPublic
}
cmd, err := NewCommandExportKey(p1, p2, path)
if err != nil {
return nil, err
return nil, nil, err
}
resp, err := cs.sc.Send(cmd)
err = cs.checkOK(resp, err)
if err != nil {
return nil, err
return nil, nil, err
}
return resp.Data, nil
return types.ParseExportKeyResponse(resp.Data)
}
func (cs *CommandSet) SetPinlessPath(path string) error {

32
types/exported_key.go Normal file
View File

@ -0,0 +1,32 @@
package types
import (
"fmt"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/keycard-go/apdu"
)
var (
TagExportKeyTemplate = uint8(0xA1)
TagExportKeyPublic = uint8(0x81)
)
func ParseExportKeyResponse(data []byte) ([]byte, []byte, error) {
tpl, err := apdu.FindTag(data, apdu.Tag{0xA1})
if err != nil {
return nil, nil, err
}
privKey, err := apdu.FindTag(tpl, apdu.Tag{0x81})
if err != nil {
return nil, nil, err
}
ecdsaKey, err := ethcrypto.HexToECDSA(fmt.Sprintf("%x", privKey))
if err != nil {
return nil, nil, err
}
return privKey, ethcrypto.FromECDSAPub(&ecdsaKey.PublicKey), nil
}