Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added libusb as transport (it should be compatible with osx & win now) #2

Closed
wants to merge 1 commit into from

Conversation

conejoninja
Copy link
Owner

need testing on those platforms

@krzysztofantczak
Copy link

krzysztofantczak commented Dec 22, 2016

Hey,

While cross compiling under ubuntu 16 (linux subsystem on windows) for windows, so linux -> windows, against libusb-1.0.9 using mingw32 64bit:

 CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -x -o tests/trezor.exe  tests/trezor.go
...cut...

x86_64-w64-mingw32-gcc -I . -m64 -mthreads -fmessage-length=0 -I/usr/include/libusb-1.0 -I $WORK/github.com/gotmc/libusb/_obj/ -g -O2 -o $WORK/github.com/gotmc/libusb/_obj/speeds.cgo2.o -c $WORK/github.com/gotmc/libusb/_obj/speeds.cgo2.c
x86_64-w64-mingw32-gcc -I . -m64 -mthreads -fmessage-length=0 -I/usr/include/libusb-1.0 -I $WORK/github.com/gotmc/libusb/_obj/ -g -O2 -o $WORK/github.com/gotmc/libusb/_obj/syncio.cgo2.o -c $WORK/github.com/gotmc/libusb/_obj/syncio.cgo2.c
x86_64-w64-mingw32-gcc -I . -m64 -mthreads -fmessage-length=0 -I/usr/include/libusb-1.0 -I $WORK/github.com/gotmc/libusb/_obj/ -g -O2 -o $WORK/github.com/gotmc/libusb/_obj/version.cgo2.o -c $WORK/github.com/gotmc/libusb/_obj/version.cgo2.c
x86_64-w64-mingw32-gcc -I . -m64 -mthreads -fmessage-length=0 -o $WORK/github.com/gotmc/libusb/_obj/_cgo_.o $WORK/github.com/gotmc/libusb/_obj/_cgo_main.o $WORK/github.com/gotmc/libusb/_obj/_cgo_export.o $WORK/github.com/gotmc/libusb/_obj/asyncio.cgo2.o $WORK/github.com/gotmc/libusb/_obj/context.cgo2.o $WORK/github.com/gotmc/libusb/_obj/descriptors.cgo2.o $WORK/github.com/gotmc/libusb/_obj/device.cgo2.o $WORK/github.com/gotmc/libusb/_obj/endpoint.cgo2.o $WORK/github.com/gotmc/libusb/_obj/handle.cgo2.o $WORK/github.com/gotmc/libusb/_obj/interfaces.cgo2.o $WORK/github.com/gotmc/libusb/_obj/miscellaneous.cgo2.o $WORK/github.com/gotmc/libusb/_obj/setupdata.cgo2.o $WORK/github.com/gotmc/libusb/_obj/speeds.cgo2.o $WORK/github.com/gotmc/libusb/_obj/syncio.cgo2.o $WORK/github.com/gotmc/libusb/_obj/version.cgo2.o -g -O2 -lusb-1.0
# github.com/gotmc/libusb
/tmp/go-build759437143/github.com/gotmc/libusb/_obj/device.cgo2.o: In function `_cgo_24bac8a7b786_C2func_libusb_get_port_number':
/mnt/d/homedir/.gvm/pkgsets/go1.6.3/sh.salva.spm/src/github.com/gotmc/libusb/device.go:112: undefined reference to `libusb_get_port_number'
/tmp/go-build759437143/github.com/gotmc/libusb/_obj/device.cgo2.o: In function `_cgo_24bac8a7b786_Cfunc_libusb_get_port_number':
/mnt/d/homedir/.gvm/pkgsets/go1.6.3/sh.salva.spm/src/github.com/gotmc/libusb/device.go:258: undefined reference to `libusb_get_port_number'
/tmp/go-build759437143/github.com/gotmc/libusb/_obj/handle.cgo2.o: In function `_cgo_24bac8a7b786_Cfunc_libusb_set_auto_detach_kernel_driver':
/mnt/d/homedir/.gvm/pkgsets/go1.6.3/sh.salva.spm/src/github.com/gotmc/libusb/handle.go:226: undefined reference to `libusb_set_auto_detach_kernel_driver'
/tmp/go-build759437143/github.com/gotmc/libusb/_obj/miscellaneous.cgo2.o: In function `_cgo_24bac8a7b786_Cfunc_libusb_setlocale':
/mnt/d/homedir/.gvm/pkgsets/go1.6.3/sh.salva.spm/src/github.com/gotmc/libusb/miscellaneous.go:89: undefined reference to `libusb_setlocale'
/tmp/go-build759437143/github.com/gotmc/libusb/_obj/miscellaneous.cgo2.o: In function `_cgo_24bac8a7b786_Cfunc_libusb_strerror':
/mnt/d/homedir/.gvm/pkgsets/go1.6.3/sh.salva.spm/src/github.com/gotmc/libusb/miscellaneous.go:103: undefined reference to `libusb_strerror'
collect2: error: ld returned 1 exit status

libusb was configured and installed as follows:

./configure --host=x86_64-w64-mingw32 --prefix=/usr/x86_64-w64-mingw32
make
sudo make install

Any ideas how can i deal with that? I was trying to google around, but most people had issues with libusb which was not found by compiler, so... not my case ;/

@conejoninja
Copy link
Owner Author

Did you try to compile it directly under windows? I don't really have experience with cross-compiling nor windows, but we'll try to get access to one and test it (main reason this wasn't merged before)

@krzysztofantczak
Copy link

krzysztofantczak commented Dec 22, 2016

Ok. I updated libusb to version 1.0.20 and this problem doesn't exists anymore, but...

tests/trezor.go:30: undefined: "github.com/zserge/hid".UsbWalk

trezor.go is exactly this file: https://github.com/conejoninja/tesoro/blob/d66480c89d8ce9813462f37a7dff835b17083619/example/main.go

As far as i can see, "github.com/zserge/hid" has no support for windows. I recently have been playing around with few HID libs for GO, and the only one that i found working on 2 tested platforms (linux and windows, but they are claiming OSX also works) was https://github.com/flynn/hid. Would it be hard to switch? I didn't digged much into "tesoro" source code yet.

@conejoninja
Copy link
Owner Author

zserge/hid is a go-only usb library, that works on linux only, I added libusb to support win/mac but never fully tested it.
As I develop on linux, I use zerge/hid, but you should fine if you use the osx/libusb example : https://github.com/conejoninja/tesoro/blob/d66480c89d8ce9813462f37a7dff835b17083619/example/osx/main.go

Not really sure what happens with those files if cross-compiling/testing/... on other platform that are not linux (sorry)

@krzysztofantczak
Copy link

krzysztofantczak commented Dec 22, 2016

Ok, there is some progress. App compiles correctly, no errors, no warnings. But when running it under windows i'm getting:

=> Failed opening the USB:  LIBUSB_ERROR_NO_DEVICE: No such device (it may have been disconnected)

So, it fails on line 56, which is device, err := usbDevice.Open(). Odd...

UPDATE:
And it seems, its libusb fault :( (or its GO wrapper). I'm able to list all USB devices (including TREZOR), but there is no way to actually open it - as for now.

If You like to test it (ie. thru wine on linux - if there is actual access to USB ports thru it, no idea):

# DEPS
apt-get install gcc-mingw-w64

# LIBUSB
# Download: http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.tar.bz2
tar jxvf libusb-1.0.20.tar.bz2
cd libusb-1.0.20

# 32 bit
./configure --host=i686-w64-mingw32 --prefix=/usr/i686-w64-mingw32
make
sudo make install

# 64 bit
./configure --host=x86_64-w64-mingw32 --prefix=/usr/x86_64-w64-mingw32
make
sudo make install

and then:

CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -x -o tests/trezor.exe  tests/trezor.go

Just remember to apply additional steps on your GO setup regarding cross compilation (easy few steps described in golang DOCS). Unless You are using something like GVM, it has cross compilation support included and applies those GO steps automatically.

UPDATE2:
Btw. it would be awesome if You could hide implementation responsible for finding TREZOR inside of actual transport "driver". This way You could switch between them easly without touching find loop. Or just leave this idea open, perhaps i'll try to mess around when i get some time to work on it (if You are fine with it of course).

UPDATE3:
Just to be safe in my "investigation", i tested produced binaries (for libusb and usb HID - the one i mentioned before) on different windows computer with clean system. I'm getting exactly the same results. Well, error for HID lib is a bit different:

Overlapped I/O operation is in progress.

Reall, really odd. No idea what to think about it. Btw. i'm using trezor via chrome (also password manager), all works good.

@conejoninja conejoninja closed this Jul 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants