Skip to content

Commit

Permalink
Merge pull request #18 from troian/fix-ledger-pid
Browse files Browse the repository at this point in the history
fix: use productID to detect ledger type
  • Loading branch information
jleni authored Apr 7, 2021
2 parents 73b5de1 + a706abf commit 704180b
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions ledger_hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import (
const (
VendorLedger = 0x2c97
UsagePageLedgerNanoS = 0xffa0
//ProductNano = 1
Channel = 0x0101
PacketSize = 64
Channel = 0x0101
PacketSize = 64
)

type LedgerAdminHID struct{}
Expand All @@ -42,6 +41,12 @@ type LedgerDeviceHID struct {
readChannel chan []byte
}

// list of supported product ids as well as their corresponding interfaces
var supportedLedgerProductID = map[uint16]int{
0x4011: 0, // Ledger Nano X
0x1011: 0, // Ledger Nano S
}

func NewLedgerAdmin() *LedgerAdminHID {
return &LedgerAdminHID{}
}
Expand Down Expand Up @@ -72,9 +77,11 @@ func (admin *LedgerAdminHID) ListDevices() ([]string, error) {
func isLedgerDevice(d hid.DeviceInfo) bool {
deviceFound := d.UsagePage == UsagePageLedgerNanoS
// Workarounds for possible empty usage pages
return deviceFound ||
(d.Product == "Nano S" && d.Interface == 0) ||
(d.Product == "Nano X" && d.Interface == 0)
if interfaceID, supported := supportedLedgerProductID[d.ProductID]; deviceFound || (supported && (interfaceID == d.Interface)) {
return true
}

return false
}

func (admin *LedgerAdminHID) CountDevices() int {
Expand All @@ -92,8 +99,8 @@ func (admin *LedgerAdminHID) CountDevices() int {

func newDevice(dev *hid.Device) *LedgerDeviceHID {
return &LedgerDeviceHID{
device: dev,
readCo: new(sync.Once),
device: dev,
readCo: new(sync.Once),
readChannel: make(chan []byte),
}
}
Expand Down

0 comments on commit 704180b

Please sign in to comment.