Skip to content

Commit de38a1d

Browse files
authored
Merge pull request #19588 from gballet/trezor-fix-ownlib
accounts/usbwallet: add webusb trezor support
2 parents 42b81f9 + 5d68400 commit de38a1d

122 files changed

Lines changed: 20971 additions & 16318 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎accounts/usbwallet/hub.go‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/accounts"
2626
"github.com/ethereum/go-ethereum/event"
2727
"github.com/ethereum/go-ethereum/log"
28-
"github.com/karalabe/hid"
28+
"github.com/karalabe/usb"
2929
)
3030

3131
// LedgerScheme is the protocol scheme prefixing account and wallet URLs.
@@ -84,14 +84,20 @@ func NewLedgerHub() (*Hub, error) {
8484
}, 0xffa0, 0, newLedgerDriver)
8585
}
8686

87-
// NewTrezorHub creates a new hardware wallet manager for Trezor devices.
88-
func NewTrezorHub() (*Hub, error) {
89-
return newHub(TrezorScheme, 0x534c, []uint16{0x0001 /* Trezor 1 */}, 0xff00, 0, newTrezorDriver)
87+
// NewTrezorHubWithHID creates a new hardware wallet manager for Trezor devices.
88+
func NewTrezorHubWithHID() (*Hub, error) {
89+
return newHub(TrezorScheme, 0x534c, []uint16{0x0001 /* Trezor HID */}, 0xff00, 0, newTrezorDriver)
90+
}
91+
92+
// NewTrezorHubWithWebUSB creates a new hardware wallet manager for Trezor devices with
93+
// firmware version > 1.8.0
94+
func NewTrezorHubWithWebUSB() (*Hub, error) {
95+
return newHub(TrezorScheme, 0x1209, []uint16{0x53c1 /* Trezor WebUSB */}, 0xffff /* No usage id on webusb, don't match unset (0) */, 0, newTrezorDriver)
9096
}
9197

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) {
94-
if !hid.Supported() {
100+
if !usb.Supported() {
95101
return nil, errors.New("unsupported platform")
96102
}
97103
hub := &Hub{
@@ -133,7 +139,7 @@ func (hub *Hub) refreshWallets() {
133139
return
134140
}
135141
// Retrieve the current list of USB wallet devices
136-
var devices []hid.DeviceInfo
142+
var devices []usb.DeviceInfo
137143

138144
if runtime.GOOS == "linux" {
139145
// hidapi on Linux opens the device during enumeration to retrieve some infos,
@@ -148,8 +154,18 @@ func (hub *Hub) refreshWallets() {
148154
return
149155
}
150156
}
151-
for _, info := range hid.Enumerate(hub.vendorID, 0) {
157+
infos, err := usb.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 {
168+
// Windows and Macos use UsageID matching, Linux uses Interface matching
153169
if info.ProductID == id && (info.UsagePage == hub.usageID || info.Interface == hub.endpointID) {
154170
devices = append(devices, info)
155171
break

‎accounts/usbwallet/trezor.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,13 @@ func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, er
192192
if _, err := w.trezorExchange(&trezor.EthereumGetAddress{AddressN: derivationPath}, address); err != nil {
193193
return common.Address{}, err
194194
}
195-
return common.BytesToAddress(address.GetAddress()), nil
195+
if addr := address.GetAddressBin(); len(addr) > 0 { // Older firmwares use binary fomats
196+
return common.BytesToAddress(addr), nil
197+
}
198+
if addr := address.GetAddressHex(); len(addr) > 0 { // Newer firmwares use hexadecimal fomats
199+
return common.HexToAddress(addr), nil
200+
}
201+
return common.Address{}, errors.New("missing derived address")
196202
}
197203

198204
// trezorSign sends the transaction to the Trezor wallet, and waits for the user
@@ -211,7 +217,10 @@ func (w *trezorDriver) trezorSign(derivationPath []uint32, tx *types.Transaction
211217
DataLength: &length,
212218
}
213219
if to := tx.To(); to != nil {
214-
request.To = (*to)[:] // Non contract deploy, set recipient explicitly
220+
// Non contract deploy, set recipient explicitly
221+
hex := to.Hex()
222+
request.ToHex = &hex // Newer firmwares (old will ignore)
223+
request.ToBin = (*to)[:] // Older firmwares (new will ignore)
215224
}
216225
if length > 1024 { // Send the data chunked if that was requested
217226
request.DataInitialChunk, data = data[:1024], data[1024:]

0 commit comments

Comments
 (0)