Apply suggestions from code review

This commit is contained in:
mmsqe 2026-01-04 22:17:04 +08:00
parent a52bb5aa00
commit c139e337e3
No known key found for this signature in database
GPG key ID: 1D6409A9D4025709
2 changed files with 23 additions and 56 deletions

View file

@ -51,28 +51,6 @@ const (
deviceInterface = 0
)
// ledgerProductIDs contains all supported Ledger USB product IDs (legacy and WebUSB).
// Device definitions taken from
// https://github.com/LedgerHQ/ledger-live/blob/05a2980e838955a11a1418da638ef8ac3df4fb74/libs/ledgerjs/packages/devices/src/index.ts
var ledgerProductIDs = []uint16{
// Original product IDs
0x0000, /* Ledger Blue */
0x0001, /* Ledger Nano S */
0x0004, /* Ledger Nano X */
0x0005, /* Ledger Nano S Plus */
0x0006, /* Ledger Nano FTS */
0x0007, /* Ledger Flex */
0x0008, /* Ledger Nano Gen5 */
0x0000, /* WebUSB Ledger Blue */
0x1000, /* WebUSB Ledger Nano S */
0x4000, /* WebUSB Ledger Nano X */
0x5000, /* WebUSB Ledger Nano S Plus */
0x6000, /* WebUSB Ledger Nano FTS */
0x7000, /* WebUSB Ledger Flex */
0x8000, /* WebUSB Ledger Nano Gen5 */
}
// Hub is a accounts.Backend that can find and handle generic USB hardware wallets.
type Hub struct {
scheme string // Protocol scheme prefixing account and wallet URLs.
@ -100,7 +78,28 @@ type Hub struct {
// NewLedgerHub creates a new hardware wallet manager for Ledger devices.
func NewLedgerHub() (*Hub, error) {
return newHub(LedgerScheme, 0x2c97, ledgerProductIDs, deviceUsagePage, deviceInterface, newLedgerDriver)
return newHub(LedgerScheme, 0x2c97, []uint16{
// Device definitions taken from
// https://github.com/LedgerHQ/ledger-live/blob/595cb73b7e6622dbbcfc11867082ddc886f1bf01/libs/ledgerjs/packages/devices/src/index.ts
// Original product IDs
0x0000, /* Ledger Blue */
0x0001, /* Ledger Nano S */
0x0004, /* Ledger Nano X */
0x0005, /* Ledger Nano S Plus */
0x0006, /* Ledger Nano FTS */
0x0007, /* Ledger Flex */
0x0008, /* Ledger Nano Gen5 */
0x0000, /* WebUSB Ledger Blue */
0x1000, /* WebUSB Ledger Nano S */
0x4000, /* WebUSB Ledger Nano X */
0x5000, /* WebUSB Ledger Nano S Plus */
0x6000, /* WebUSB Ledger Nano FTS */
0x7000, /* WebUSB Ledger Flex */
0x8000, /* WebUSB Ledger Nano Gen5 */
}, deviceUsagePage, deviceInterface, newLedgerDriver)
}
// NewTrezorHubWithHID creates a new hardware wallet manager for Trezor devices.
@ -177,7 +176,7 @@ func (hub *Hub) refreshWallets() {
return
}
}
infos, err := usbEnumerate(hub.vendorID, 0)
infos, err := hid.Enumerate(hub.vendorID, 0)
if err != nil {
failcount := hub.enumFails.Add(1)
if runtime.GOOS == "linux" {
@ -297,6 +296,3 @@ func (hub *Hub) updater() {
hub.stateLock.Unlock()
}
}
// usbEnumerate is an alias for hid.Enumerate, allowing for easier mocking/testing.
var usbEnumerate = hid.Enumerate

View file

@ -1,29 +0,0 @@
package usbwallet
import (
"fmt"
"testing"
"github.com/karalabe/hid"
)
func TestWallets(t *testing.T) {
devices := make([]hid.DeviceInfo, 0)
for i, productID := range ledgerProductIDs {
devices = append(devices, hid.DeviceInfo{
ProductID: productID, UsagePage: deviceUsagePage, Interface: deviceInterface,
Path: fmt.Sprintf("/dev/hidraw%d", i),
})
}
usbEnumerate = func(vendorID, productID uint16) ([]hid.DeviceInfo, error) { return devices, nil }
defer func() { usbEnumerate = hid.Enumerate }()
hub, err := NewLedgerHub()
if err != nil {
t.Fatalf("Failed to create hub: %v", err)
}
wallets := hub.Wallets()
if len(wallets) != len(devices) {
t.Errorf("Expected %d wallets, got %d", len(devices), len(wallets))
}
}