From e0f146b6eae10e5cfc344f1ee575564009d2e7a3 Mon Sep 17 00:00:00 2001 From: algoidan Date: Mon, 23 Aug 2021 15:03:41 +0300 Subject: [PATCH 1/3] fixing the usb enum process --- daemon/kmd/wallet/driver/ledger_hid.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/daemon/kmd/wallet/driver/ledger_hid.go b/daemon/kmd/wallet/driver/ledger_hid.go index 22a8e05ace..b6656259b9 100644 --- a/daemon/kmd/wallet/driver/ledger_hid.go +++ b/daemon/kmd/wallet/driver/ledger_hid.go @@ -202,7 +202,13 @@ func LedgerEnumerate() ([]hid.DeviceInfo, error) { } var infos []hid.DeviceInfo + // The enumeration process is based on: + // https://github.com/LedgerHQ/blue-loader-python/blob/master/ledgerblue/comm.py#L212 + // we search for the Ledger Vendor id and igonre devices that don't have specific usagepage or interface for _, info := range hid.Enumerate(ledgerVendorID, 0) { + if info.UsagePage != 0xffa0 && info.Interface != 0 { + continue + } infos = append(infos, info) } From 20fc5ed41bf1d79fcccdc4977ee8f5abd098c51c Mon Sep 17 00:00:00 2001 From: algoidan Date: Wed, 25 Aug 2021 23:25:27 +0300 Subject: [PATCH 2/3] fixing usb bug on windows --- daemon/kmd/wallet/driver/ledger_hid.go | 8 ++++++++ go.mod | 2 +- go.sum | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/daemon/kmd/wallet/driver/ledger_hid.go b/daemon/kmd/wallet/driver/ledger_hid.go index b6656259b9..06a0f05081 100644 --- a/daemon/kmd/wallet/driver/ledger_hid.go +++ b/daemon/kmd/wallet/driver/ledger_hid.go @@ -20,6 +20,7 @@ import ( "encoding/binary" "fmt" "os" + "runtime" "github.com/karalabe/hid" ) @@ -82,6 +83,13 @@ func (l *LedgerUSB) WritePackets(msg []byte) error { if err != nil { return err } + // on Windows: + // The usb library adds one extra byte to the input passed to the USB device + // so the written bytes are larger than what we've send + // https://github.com/karalabe/hid/blob/9c14560f9ee858c43f40b5cd01392b167aacf4e8/hid_enabled.go#L167 + if runtime.GOOS == "windows" && cc > 0 { + cc = cc - 1 + } if cc != len(packet) { return fmt.Errorf("WritePackets: short write: %d != %d", cc, len(packet)) } diff --git a/go.mod b/go.mod index f779709580..e78060cd21 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jmoiron/sqlx v1.2.0 - github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2 + github.com/karalabe/hid v1.0.0 github.com/labstack/echo/v4 v4.1.17 github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-sqlite3 v1.10.0 diff --git a/go.sum b/go.sum index 1966c8ede6..7a2a5d938d 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2 h1:BkkpZxPVs3gIf+3Tejt8lWzuo2P29N1ChGUMEpuSJ8U= github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2/go.mod h1:YvbcH+3Wo6XPs9nkgTY3u19KXLauXW+J5nB7hEHuX0A= +github.com/karalabe/hid v1.0.0 h1:+/CIMNXhSU/zIJgnIvBD2nKHxS/bnRHhhs9xBryLpPo= +github.com/karalabe/hid v1.0.0/go.mod h1:Vr51f8rUOLYrfrWDFlV12GGQgM5AT8sVh+2fY4MPeu8= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= From 356a4e2bf90af1e2d4a3efdcb1a1f2d7238337f7 Mon Sep 17 00:00:00 2001 From: algoidan Date: Wed, 1 Sep 2021 17:52:25 +0300 Subject: [PATCH 3/3] some CRs fixes --- daemon/kmd/wallet/driver/ledger_hid.go | 9 +++------ go.sum | 2 -- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/daemon/kmd/wallet/driver/ledger_hid.go b/daemon/kmd/wallet/driver/ledger_hid.go index 06a0f05081..8094df50bb 100644 --- a/daemon/kmd/wallet/driver/ledger_hid.go +++ b/daemon/kmd/wallet/driver/ledger_hid.go @@ -20,12 +20,12 @@ import ( "encoding/binary" "fmt" "os" - "runtime" "github.com/karalabe/hid" ) const ledgerVendorID = 0x2c97 +const ledgerUsagePage = 0xffa0 // LedgerUSB is a wrapper around a Ledger USB HID device, used to implement // the protocol used for sending messages to the application running on the @@ -87,10 +87,7 @@ func (l *LedgerUSB) WritePackets(msg []byte) error { // The usb library adds one extra byte to the input passed to the USB device // so the written bytes are larger than what we've send // https://github.com/karalabe/hid/blob/9c14560f9ee858c43f40b5cd01392b167aacf4e8/hid_enabled.go#L167 - if runtime.GOOS == "windows" && cc > 0 { - cc = cc - 1 - } - if cc != len(packet) { + if cc < len(packet) { return fmt.Errorf("WritePackets: short write: %d != %d", cc, len(packet)) } @@ -214,7 +211,7 @@ func LedgerEnumerate() ([]hid.DeviceInfo, error) { // https://github.com/LedgerHQ/blue-loader-python/blob/master/ledgerblue/comm.py#L212 // we search for the Ledger Vendor id and igonre devices that don't have specific usagepage or interface for _, info := range hid.Enumerate(ledgerVendorID, 0) { - if info.UsagePage != 0xffa0 && info.Interface != 0 { + if info.UsagePage != ledgerUsagePage && info.Interface != 0 { continue } infos = append(infos, info) diff --git a/go.sum b/go.sum index 7a2a5d938d..c4fe5d0397 100644 --- a/go.sum +++ b/go.sum @@ -72,8 +72,6 @@ github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2 h1:BkkpZxPVs3gIf+3Tejt8lWzuo2P29N1ChGUMEpuSJ8U= -github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2/go.mod h1:YvbcH+3Wo6XPs9nkgTY3u19KXLauXW+J5nB7hEHuX0A= github.com/karalabe/hid v1.0.0 h1:+/CIMNXhSU/zIJgnIvBD2nKHxS/bnRHhhs9xBryLpPo= github.com/karalabe/hid v1.0.0/go.mod h1:Vr51f8rUOLYrfrWDFlV12GGQgM5AT8sVh+2fY4MPeu8= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=