Skip to content

Commit 4799b5a

Browse files
gballetkaralabe
authored andcommitted
accounts/usbwallet: support webusb for Trezor wallets
1 parent 7a22da9 commit 4799b5a

12 files changed

Lines changed: 443 additions & 34 deletions

File tree

‎accounts/usbwallet/hub.go‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ func NewTrezorHub() (*Hub, error) {
8989
return newHub(TrezorScheme, 0x534c, []uint16{0x0001 /* Trezor 1 */}, 0xff00, 0, newTrezorDriver)
9090
}
9191

92+
// NewWebUSBTrezorHub creates a new hardware wallet manager for Trezor devices with
93+
// firmware version > 1.8.0
94+
func NewWebUSBTrezorHub() (*Hub, error) {
95+
return newHub(TrezorScheme, 0x1209, []uint16{0x53c1 /* Trezor 1 WebUSB */}, 0, 0, newTrezorDriver)
96+
}
97+
9298
// newHub creates a new hardware wallet manager for generic USB devices.
9399
func newHub(scheme string, vendorID uint16, productIDs []uint16, usageID uint16, endpointID int, makeDriver func(log.Logger) driver) (*Hub, error) {
94100
if !hid.Supported() {
@@ -148,9 +154,19 @@ func (hub *Hub) refreshWallets() {
148154
return
149155
}
150156
}
151-
for _, info := range hid.Enumerate(hub.vendorID, 0) {
157+
infos, err := hid.Enumerate(hub.vendorID, 0)
158+
if err != nil {
159+
if runtime.GOOS == "linux" {
160+
// See rationale before the enumeration why this is needed and only on Linux.
161+
hub.commsLock.Unlock()
162+
}
163+
log.Error("error enumerating USB enumeration: ", "code", err)
164+
return
165+
}
166+
for _, info := range infos {
152167
for _, id := range hub.productIDs {
153-
if info.ProductID == id && (info.UsagePage == hub.usageID || info.Interface == hub.endpointID) {
168+
_, pid, endpoint, _ /* FIXME usageID */ := info.IDs()
169+
if pid == id && ( /* FIXME usageID == hub.usageID || */ endpoint == hub.endpointID) {
154170
devices = append(devices, info)
155171
break
156172
}
@@ -169,7 +185,7 @@ func (hub *Hub) refreshWallets() {
169185
)
170186

171187
for _, device := range devices {
172-
url := accounts.URL{Scheme: hub.scheme, Path: device.Path}
188+
url := accounts.URL{Scheme: hub.scheme, Path: device.GetPath()}
173189

174190
// Drop wallets in front of the next device or those that failed for some reason
175191
for len(hub.wallets) > 0 {

‎accounts/usbwallet/trezor.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
"github.com/golang/protobuf/proto"
3737
)
3838

39+
var ErrInvalidDeviceType = errors.New("trezor: invalid device type")
40+
3941
// ErrTrezorPINNeeded is returned if opening the trezor requires a PIN code. In
4042
// this case, the calling application should display a pinpad and send back the
4143
// encoded passphrase.

‎accounts/usbwallet/wallet.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type wallet struct {
7878
url *accounts.URL // Textual URL uniquely identifying this wallet
7979

8080
info hid.DeviceInfo // Known USB device infos about the wallet
81-
device *hid.Device // USB device advertising itself as a hardware wallet
81+
device hid.Device // USB device advertising itself as a hardware wallet
8282

8383
accounts []accounts.Account // List of derive accounts pinned on the hardware wallet
8484
paths map[common.Address]accounts.DerivationPath // Known derivation paths for signing operations

‎node/config.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
507507
} else {
508508
backends = append(backends, trezorhub)
509509
}
510+
// Start a USB hub for Trezor hardware wallets (WebUSB version)
511+
if trezorhub, err := usbwallet.NewWebUSBTrezorHub(); err != nil {
512+
log.Warn(fmt.Sprintf("Failed to start Trezor hub, disabling: %v", err))
513+
} else {
514+
backends = append(backends, trezorhub)
515+
}
510516
}
511517
if len(conf.SmartCardDaemonPath) > 0 {
512518
// Start a smart card hub

‎signer/core/api.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ func StartClefAccountManager(ksLocation string, nousb, lightKDF bool) *accounts.
151151
backends = append(backends, trezorhub)
152152
log.Debug("Trezor support enabled")
153153
}
154+
// Start a USB hub for Trezor hardware wallets (WebUSB version)
155+
if trezorhub, err := usbwallet.NewWebUSBTrezorHub(); err != nil {
156+
log.Warn(fmt.Sprintf("Failed to start Trezor hub, disabling: %v", err))
157+
} else {
158+
backends = append(backends, trezorhub)
159+
log.Debug("Trezor support enabled")
160+
}
154161
}
155162
// Clef doesn't allow insecure http account unlock.
156163
return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, backends...)

‎vendor/github.com/karalabe/hid/generic.go‎

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/karalabe/hid/hid.go‎

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vendor/github.com/karalabe/hid/hid_disabled.go‎

Lines changed: 33 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)