Skip to content

Commit a52bb5a

Browse files
committed
add test
1 parent bdad11a commit a52bb5a

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

accounts/usbwallet/hub.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@ const refreshCycle = time.Second
4343
// trashing.
4444
const refreshThrottling = 500 * time.Millisecond
4545

46+
const (
47+
// deviceUsagePage identifies Ledger devices by HID usage page (0xffa0) on Windows and macOS.
48+
// See: https://github.com/LedgerHQ/ledger-live/blob/05a2980e838955a11a1418da638ef8ac3df4fb74/libs/ledgerjs/packages/hw-transport-node-hid-noevents/src/TransportNodeHid.ts
49+
deviceUsagePage = 0xffa0
50+
// deviceInterface identifies Ledger devices by USB interface number (0) on Linux.
51+
deviceInterface = 0
52+
)
53+
54+
// ledgerProductIDs contains all supported Ledger USB product IDs (legacy and WebUSB).
55+
// Device definitions taken from
56+
// https://github.com/LedgerHQ/ledger-live/blob/05a2980e838955a11a1418da638ef8ac3df4fb74/libs/ledgerjs/packages/devices/src/index.ts
57+
var ledgerProductIDs = []uint16{
58+
// Original product IDs
59+
0x0000, /* Ledger Blue */
60+
0x0001, /* Ledger Nano S */
61+
0x0004, /* Ledger Nano X */
62+
0x0005, /* Ledger Nano S Plus */
63+
0x0006, /* Ledger Nano FTS */
64+
0x0007, /* Ledger Flex */
65+
0x0008, /* Ledger Nano Gen5 */
66+
67+
0x0000, /* WebUSB Ledger Blue */
68+
0x1000, /* WebUSB Ledger Nano S */
69+
0x4000, /* WebUSB Ledger Nano X */
70+
0x5000, /* WebUSB Ledger Nano S Plus */
71+
0x6000, /* WebUSB Ledger Nano FTS */
72+
0x7000, /* WebUSB Ledger Flex */
73+
0x8000, /* WebUSB Ledger Nano Gen5 */
74+
}
75+
4676
// Hub is a accounts.Backend that can find and handle generic USB hardware wallets.
4777
type Hub struct {
4878
scheme string // Protocol scheme prefixing account and wallet URLs.
@@ -70,28 +100,7 @@ type Hub struct {
70100

71101
// NewLedgerHub creates a new hardware wallet manager for Ledger devices.
72102
func NewLedgerHub() (*Hub, error) {
73-
return newHub(LedgerScheme, 0x2c97, []uint16{
74-
75-
// Device definitions taken from
76-
// https://github.com/LedgerHQ/ledger-live/blob/595cb73b7e6622dbbcfc11867082ddc886f1bf01/libs/ledgerjs/packages/devices/src/index.ts
77-
78-
// Original product IDs
79-
0x0000, /* Ledger Blue */
80-
0x0001, /* Ledger Nano S */
81-
0x0004, /* Ledger Nano X */
82-
0x0005, /* Ledger Nano S Plus */
83-
0x0006, /* Ledger Nano FTS */
84-
0x0007, /* Ledger Flex */
85-
0x0008, /* Ledger Nano Gen5 */
86-
87-
0x0000, /* WebUSB Ledger Blue */
88-
0x1000, /* WebUSB Ledger Nano S */
89-
0x4000, /* WebUSB Ledger Nano X */
90-
0x5000, /* WebUSB Ledger Nano S Plus */
91-
0x6000, /* WebUSB Ledger Nano FTS */
92-
0x7000, /* WebUSB Ledger Flex */
93-
0x8000, /* WebUSB Ledger Nano Gen5 */
94-
}, 0xffa0, 0, newLedgerDriver)
103+
return newHub(LedgerScheme, 0x2c97, ledgerProductIDs, deviceUsagePage, deviceInterface, newLedgerDriver)
95104
}
96105

97106
// NewTrezorHubWithHID creates a new hardware wallet manager for Trezor devices.
@@ -168,7 +177,7 @@ func (hub *Hub) refreshWallets() {
168177
return
169178
}
170179
}
171-
infos, err := hid.Enumerate(hub.vendorID, 0)
180+
infos, err := usbEnumerate(hub.vendorID, 0)
172181
if err != nil {
173182
failcount := hub.enumFails.Add(1)
174183
if runtime.GOOS == "linux" {
@@ -288,3 +297,6 @@ func (hub *Hub) updater() {
288297
hub.stateLock.Unlock()
289298
}
290299
}
300+
301+
// usbEnumerate is an alias for hid.Enumerate, allowing for easier mocking/testing.
302+
var usbEnumerate = hid.Enumerate

accounts/usbwallet/hub_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package usbwallet
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/karalabe/hid"
8+
)
9+
10+
func TestWallets(t *testing.T) {
11+
devices := make([]hid.DeviceInfo, 0)
12+
for i, productID := range ledgerProductIDs {
13+
devices = append(devices, hid.DeviceInfo{
14+
ProductID: productID, UsagePage: deviceUsagePage, Interface: deviceInterface,
15+
Path: fmt.Sprintf("/dev/hidraw%d", i),
16+
})
17+
}
18+
usbEnumerate = func(vendorID, productID uint16) ([]hid.DeviceInfo, error) { return devices, nil }
19+
defer func() { usbEnumerate = hid.Enumerate }()
20+
21+
hub, err := NewLedgerHub()
22+
if err != nil {
23+
t.Fatalf("Failed to create hub: %v", err)
24+
}
25+
wallets := hub.Wallets()
26+
if len(wallets) != len(devices) {
27+
t.Errorf("Expected %d wallets, got %d", len(devices), len(wallets))
28+
}
29+
}

0 commit comments

Comments
 (0)