88 lines
1.7 KiB
Go
Raw Normal View History

package statuskeycardgo
2021-09-13 13:09:23 +02:00
2021-10-15 12:38:06 +03:00
import (
2021-10-18 14:50:56 +03:00
"encoding/binary"
2021-10-15 12:38:06 +03:00
"encoding/hex"
2021-10-18 14:50:56 +03:00
2021-10-19 12:19:02 +03:00
"github.com/ebfe/scard"
2021-10-20 11:44:57 +03:00
keycard "github.com/status-im/keycard-go"
2022-08-04 11:21:23 +02:00
"github.com/status-im/keycard-go/derivationpath"
2021-10-18 14:50:56 +03:00
ktypes "github.com/status-im/keycard-go/types"
2021-10-15 12:38:06 +03:00
)
2021-09-13 13:09:23 +02:00
2021-10-19 12:19:02 +03:00
func isSCardError(err error) bool {
2021-10-27 08:46:00 +03:00
_, ok := err.(scard.Error)
2021-10-19 12:19:02 +03:00
return ok
}
2021-10-22 12:57:53 +03:00
func getRetries(err error) (int, bool) {
2021-10-20 11:44:57 +03:00
if wrongPIN, ok := err.(*keycard.WrongPINError); ok {
return wrongPIN.RemainingAttempts, ok
2021-10-22 12:57:53 +03:00
} else if wrongPUK, ok := err.(*keycard.WrongPUKError); ok {
return wrongPUK.RemainingAttempts, ok
2021-10-20 11:44:57 +03:00
} else {
return 0, false
}
}
2021-10-27 08:46:00 +03:00
func btox(bytes []byte) string {
2021-10-15 12:38:06 +03:00
return hex.EncodeToString(bytes)
}
2021-10-18 14:50:56 +03:00
2021-10-27 08:46:00 +03:00
func xtob(str string) ([]byte, error) {
return hex.DecodeString(str)
}
2021-10-18 14:50:56 +03:00
func bytesToInt(s []byte) int {
if len(s) > 4 {
return 0
}
var b [4]byte
copy(b[4-len(s):], s)
return int(binary.BigEndian.Uint32(b[:]))
}
func toAppInfo(r *ktypes.ApplicationInfo) ApplicationInfo {
return ApplicationInfo{
Initialized: r.Initialized,
InstanceUID: r.InstanceUID,
Version: bytesToInt(r.Version),
AvailableSlots: bytesToInt(r.AvailableSlots),
KeyUID: r.KeyUID,
}
}
2021-10-19 12:19:02 +03:00
func toPairInfo(r *ktypes.PairingInfo) *PairingInfo {
return &PairingInfo{
Key: r.Key,
Index: r.Index,
}
}
2021-10-27 08:46:00 +03:00
func toSignature(r *ktypes.Signature) *Signature {
return &Signature{
R: r.R(),
S: r.S(),
V: r.V(),
}
}
2022-08-04 11:21:23 +02:00
func toMetadata(r *ktypes.Metadata) *Metadata {
paths := r.Paths()
wallets := make([]Wallet, len(paths))
tmp := []uint32{0x8000002c, 0x8000003c, 0x80000000, 0x00000000, 0x00000000}
for i, p := range paths {
tmp[4] = p
path := derivationpath.Encode(tmp)
wallets[i].Path = path
}
return &Metadata{
Name: r.Name(),
Wallets: wallets,
}
}