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

Replace USB-CCID (smartcard) by USB-HID #4203

Merged
merged 1 commit into from
Sep 24, 2018

Conversation

cslashm
Copy link
Contributor

@cslashm cslashm commented Aug 1, 2018

Remove PCSC dependencies which is a bit hard (not user friendly) to install on Linux and Mac

This code seems to work under Linux (more test incoming)
It needs to be tested under Mac and Windows

In order to compile it requires the following packages:

Linux:

  • libhidapi-dev
  • libhidapi-libusb0

Windows (msys2/64):

  • mingw-w64-x86_64-hidapi

Mac: (edited)

  • hidapi (using brew)

@selsta
Copy link
Collaborator

selsta commented Aug 1, 2018

macOS

Installed hidapi using brew. Had to link CoreFoundation and CoreIO to compile:

commit e20ea748059c4435b58876ac18d8f483eee84eba
Author: selsta <selsta@sent.at>
Date:   Wed Aug 1 11:12:04 2018 +0200

    Fix compilation macOS

diff --git a/CMakeLists.txt b/CMakeLists.txt
index be6a6a07..616f6014 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -832,6 +832,13 @@ endif()
 
 list(APPEND EXTRA_LIBRARIES ${CMAKE_DL_LIBS})
 
+if (APPLE)
+  find_library(COREFOUNDATION CoreFoundation)
+  find_library(IOKIT IOKit)
+  list(APPEND EXTRA_LIBRARIES ${IOKIT})
+  list(APPEND EXTRA_LIBRARIES ${COREFOUNDATION})
+endif()
+
 option(USE_READLINE "Build with GNU readline support." ON)
 if(USE_READLINE)
   find_package(Readline)

@@ -52,7 +52,7 @@
#define USE_DEVICE_LEDGER 1
#endif

#if !defined(HAVE_PCSC)
#if !defined(HAVE_HIDAPI)
#undef USE_DEVICE_LEDGER
#define USE_DEVICE_LEDGER 0
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to set USE_DEVICE_LEDGER to 0 or 1, but the code above uses #ifdef, not #if

@iDunk5400
Copy link
Contributor

Windows

2018-08-01 15:37:07.899	40400	INFO 	logging	contrib/epee/src/mlog.cpp:235	New log categories: *:WARNING,net:FATAL,net.p2p:FATAL,net.cn:FATAL,global:INFO,verify:FATAL,stacktrace:INFO,logging:INFO,msgwriter:INFO
2018-08-01 15:37:07.899	40400	INFO 	logging	contrib/epee/src/mlog.cpp:235	New log categories: *:DEBUG
2018-08-01 15:37:07.899	40400	INFO 	msgwriter	src/common/scoped_message_writer.h:102	This is the command line monero wallet. It needs to connect to a monero
daemon to work correctly.
WARNING: Do not reuse your Monero keys on another fork, UNLESS this fork has key reuse mitigations built in. Doing so will harm your privacy.

2018-08-01 15:37:07.900	40400	INFO 	msgwriter	src/common/scoped_message_writer.h:102	Monero 'Lithium Luna' (v0.12.3.0-master-53f5aeca)
2018-08-01 15:37:07.900	40400	INFO 	wallet.wallet2	src/wallet/wallet_args.cpp:204	Setting log level = 2
2018-08-01 15:37:07.900	40400	INFO 	wallet.wallet2	src/wallet/wallet_args.cpp:207	Logging to: C:\msys64\home\user\monero\build\release\bin\monero-wallet-cli.log
2018-08-01 15:37:07.900	40400	INFO 	msgwriter	src/common/scoped_message_writer.h:102	Logging to C:\msys64\home\user\monero\build\release\bin\monero-wallet-cli.log
2018-08-01 15:37:09.850	40400	DEBUG	device.ledger	src/device/device_ledger.cpp:195	Device 0 Created
2018-08-01 15:37:09.990	40400	INFO 	wallet.wallet2	src/wallet/wallet2.cpp:5822	ringdb path set to C:\ProgramData\.shared-ringdb
2018-08-01 15:37:10.068	40400	DEBUG	device	src/cryptonote_basic/account.cpp:61	account_keys::set_device device type: N2hw6ledger13device_ledgerE
2018-08-01 15:37:10.068	40400	DEBUG	ledger	src/cryptonote_basic/account.cpp:138	device type: N2hw6ledger13device_ledgerE
2018-08-01 15:37:10.068	40400	DEBUG	device.ledger	src/device/device_ledger.cpp:551	Device 0 HIDUSB inited
2018-08-01 15:37:10.176	40400	DEBUG	device.ledger	src/device/device_ledger.cpp:470	CMD  : 00 02 00 00 01 00
2018-08-01 15:37:10.177	40400	DEBUG	device.ledger	src/device/device_ledger.cpp:258	HID > : 01010500000006000200000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2018-08-01 15:37:10.180	40400	DEBUG	device.ledger	src/device/device_ledger.cpp:258	HID < : 01050000000000020000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2018-08-01 15:37:10.180	40400	ERROR	device.ledger	src/device/device_ledger.cpp:357	Wrong Channel
2018-08-01 15:37:10.180	40400	ERROR	msgwriter	src/common/scoped_message_writer.h:102	Error: failed to generate new wallet: Wrong Channel
2018-08-01 15:37:10.181	40400	ERROR	wallet.simplewallet	src/simplewallet/simplewallet.cpp:3120	account creation failed
2018-08-01 15:37:10.181	40400	ERROR	wallet.simplewallet	src/simplewallet/simplewallet.cpp:7730	Failed to initialize wallet
2018-08-01 15:37:10.182	40400	DEBUG	net	contrib/epee/include/net/net_helper.h:512	Problems at cancel: The file handle supplied is not valid
2018-08-01 15:37:10.182	40400	DEBUG	net	contrib/epee/include/net/net_helper.h:515	Problems at shutdown: The file handle supplied is not valid
2018-08-01 15:37:10.182	40400	DEBUG	device.ledger	src/device/device_ledger.cpp:200	Device 0 Destroyed

The log looks the same when the smartcard device is disabled in Device Manager.
BTW, I had to add libsetupapi.a to link line of the binaries (WIN32) to fix unresolved dependencies.

@selsta
Copy link
Collaborator

selsta commented Aug 2, 2018

macOS

Wallet creation, refresh works. Transfer is buggy. You have to time the accept fee press perfectly (not too fast and not too slow), or else it will display Error: unexpected error: Wrong Channel.

@iDunk5400
Copy link
Contributor

Windows

Looks the same with 80c1fe9.

2018-08-02 16:05:57.181	17116	INFO 	msgwriter	src/common/scoped_message_writer.h:102	Monero 'Lithium Luna' (v0.12.3.0-master-6a592a16)
2018-08-02 16:05:57.182	17116	INFO 	wallet.wallet2	src/wallet/wallet_args.cpp:204	Setting log level = 2
2018-08-02 16:05:57.182	17116	INFO 	wallet.wallet2	src/wallet/wallet_args.cpp:207	Logging to: C:\msys64\home\user\monero\build\release\bin\monero-wallet-cli.log
2018-08-02 16:05:57.182	17116	INFO 	msgwriter	src/common/scoped_message_writer.h:102	Logging to C:\msys64\home\user\monero\build\release\bin\monero-wallet-cli.log
2018-08-02 16:05:59.421	17116	DEBUG	device.ledger	src/device/device_ledger.cpp:195	Device 0 Created
2018-08-02 16:05:59.563	17116	INFO 	wallet.wallet2	src/wallet/wallet2.cpp:5822	ringdb path set to C:\ProgramData\.shared-ringdb
2018-08-02 16:05:59.643	17116	DEBUG	device	src/cryptonote_basic/account.cpp:61	account_keys::set_device device type: N2hw6ledger13device_ledgerE
2018-08-02 16:05:59.643	17116	DEBUG	ledger	src/cryptonote_basic/account.cpp:138	device type: N2hw6ledger13device_ledgerE
2018-08-02 16:05:59.644	17116	DEBUG	device.ledger	src/device/device_ledger.cpp:566	Device 0 HIDUSB inited
2018-08-02 16:05:59.655	17116	DEBUG	device.ledger	src/device/log.cpp:55	io_hid_open: HID Device found: \\?\hid#vid_2c97&pid_0001&...(redacted)
2018-08-02 16:05:59.655	17116	DEBUG	device.ledger	src/device/device_ledger.cpp:485	CMD  : 00 02 00 00 01 00
2018-08-02 16:05:59.655	17116	DEBUG	device.ledger	src/device/device_ledger.cpp:258	HID > : 01010500000006000200000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2018-08-02 16:05:59.658	17116	DEBUG	device.ledger	src/device/device_ledger.cpp:258	HID < : 01050000000000020000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2018-08-02 16:05:59.658	17116	ERROR	device.ledger	src/device/device_ledger.cpp:372	Wrong Channel
2018-08-02 16:05:59.658	17116	ERROR	msgwriter	src/common/scoped_message_writer.h:102	Error: failed to generate new wallet: Wrong Channel
2018-08-02 16:05:59.658	17116	ERROR	wallet.simplewallet	src/simplewallet/simplewallet.cpp:3120	account creation failed
2018-08-02 16:05:59.658	17116	ERROR	wallet.simplewallet	src/simplewallet/simplewallet.cpp:7730	Failed to initialize wallet
2018-08-02 16:05:59.659	17116	DEBUG	net	contrib/epee/include/net/net_helper.h:512	Problems at cancel: The file handle supplied is not valid
2018-08-02 16:05:59.659	17116	DEBUG	net	contrib/epee/include/net/net_helper.h:515	Problems at shutdown: The file handle supplied is not valid
2018-08-02 16:05:59.659	17116	DEBUG	device.ledger	src/device/device_ledger.cpp:200	Device 0 Destroyed

@m2049r
Copy link
Contributor

m2049r commented Aug 16, 2018

Please move the lowlevel USB code out of the main device_ledger. Maybe you could provide the API (or similar) from #4267 in C? Basically your code provides all functionality. If you could encapsulate it and conditionally link the C code or not, we would both be happy :) Also, I have no issue removing all references to monerujo.

(I can't use libusbhid in Android)

@TheCharlatan
Copy link
Contributor

@m2049r why does libhidapi not work on android? There even seems to be a makefile for it authored by Trezor developer Stick: https://github.com/signal11/hidapi/blob/master/android/jni/Android.mk .

@m2049r
Copy link
Contributor

m2049r commented Aug 16, 2018

@TheCharlatan thanks for the pointer - it looks like it may work on android (although i doubt it since permissions don't work well for ndk). but why add an extra unnecessary dependency? imo, there is nothing in the code of this PR which cannot be handled by android usb support. and there is nothing wrong with encapsulating lowlevel functions like hardware communication even if we didn't care about android, ios, whatnot..

also, if ledger communication goes through the android usb stack provided by monerujo JNI, i can show the status of communications (as i can intercept it) - otherwise i cannot. and the user sits there wondering why nothing is happening for minutes...

@cslashm
Copy link
Contributor Author

cslashm commented Aug 17, 2018

@m2049r I'm looking at your request. It seems legitimate.

Also I agree with the lake of feedback on what happen. I has some draft code to give info about tx processing. I hope a new PR on this subject beginning of September.

@cslashm
Copy link
Contributor Author

cslashm commented Aug 20, 2018

@m2049r do the last commit answer to your request?

@moneromooo-monero
Copy link
Collaborator

Are you planning on having this for 0.13 (that is, soon :)) ?

@cslashm
Copy link
Contributor Author

cslashm commented Sep 1, 2018 via email

BYTE buffer_recv[BUFFER_RECV_SIZE];
unsigned int id;
//IO
hw::io::device_io_hid hw_device;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hw::io::device_io hw_device


/* -------------------------------------------------------------- */
device_ledger::device_ledger() {
device_ledger::device_ledger(): hw_device(0x0101, 0x05, 64, 10000) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please construct hw_device explicitly so some else (me) can instantiate their own implementation of hw::io::device_io (with #ifdefs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you suggest to declare:

hw::io::device_io *hw_device;

and do a:

hw_device = new hw::io::device_io_hid((0x0101, 0x05, 64, 10000);

or do you suggest the contructor take the pointer as parameter?
how to you plan to use that? I'm not sure it solve your pb
It is not clear for me what I zm suppose to mod :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i was over-thinking it - leave it like it is.

Copy link
Contributor Author

@cslashm cslashm Sep 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm ...

Ok So I will

  • leave as is for now,
  • make some cleanup,
  • squash all
  • remove the "donot merge"

:)

#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#endif
#include "device_io_hid.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you just need device_io.hpp here

@iDunk5400
Copy link
Contributor

I built current master (fad88e1) with b932d0d and dd6888c cherry-picked on top.

Generating the wallet on Windows works fine. I haven't tried making transactions.

On Ubuntu 16.04, I get a segfault when trying to open an existing Ledger wallet or when creating a new one. Sorry, I don't have a core. libhidapi-dev and libhidapi-libusb0 are installed on my machine. libhidapi-hidraw0 got installed automatically along with the other two libs.

2018-09-08 11:08:08.803     7fbd4a71e780        INFO    logging contrib/epee/src/mlog.cpp:237   New log categories: *:WARNING,net:FATAL,net.p2p:FATAL,net.cn:FATAL,global:INFO,verify:FATAL,stacktrace:INFO,logging:INFO,msgwriter:INFO
2018-09-08 11:08:08.803     7fbd4a71e780        INFO    logging contrib/epee/src/mlog.cpp:237   New log categories: *:DEBUG
2018-09-08 11:08:08.803     7fbd4a71e780        INFO    msgwriter       src/common/scoped_message_writer.h:102  This is the command line monero wallet. It needs to connect to a monero
daemon to work correctly.
WARNING: Do not reuse your Monero keys on another fork, UNLESS this fork has key reuse mitigations built in. Doing so will harm your privacy.

2018-09-08 11:08:08.803     7fbd4a71e780        INFO    msgwriter       src/common/scoped_message_writer.h:102  Monero 'Lithium Luna' (v0.12.3.0-master-12971da)
2018-09-08 11:08:08.803     7fbd4a71e780        INFO    wallet.wallet2  src/wallet/wallet_args.cpp:207  Setting log level = 2
2018-09-08 11:08:08.803     7fbd4a71e780        INFO    wallet.wallet2  src/wallet/wallet_args.cpp:210  Logging to: /path/to/monero-wallet-cli.log
2018-09-08 11:08:08.803     7fbd4a71e780        INFO    msgwriter       src/common/scoped_message_writer.h:102  Logging to /path/to/monero-wallet-cli.log
2018-09-08 11:08:21.161     7fbd4a71e780        DEBUG   net.http        src/common/util.cpp:849 Address 'http://localhost:18081' is local
2018-09-08 11:08:21.161     7fbd4a71e780        INFO    wallet.wallet2  src/wallet/wallet2.cpp:255      Daemon is local, assuming trusted
2018-09-08 11:08:21.161     7fbd4a71e780        DEBUG   device.ledger   src/device/device_ledger.cpp:193        Device 0 Created
2018-09-08 11:08:21.162     7fbd4a71e780        INFO    wallet.wallet2  src/wallet/wallet2.cpp:6055     ringdb path set to /home/user/.shared-ringdb
2018-09-08 11:08:21.183     7fbd4a71e780        DEBUG   device  src/cryptonote_basic/account.cpp:64     account_keys::set_device device type: N2hw6ledger13device_ledgerE
2018-09-08 11:08:21.183     7fbd4a71e780        DEBUG   ledger  src/cryptonote_basic/account.cpp:204    device type: N2hw6ledger13device_ledgerE
2018-09-08 11:08:21.186     7fbd4a71e780        DEBUG   device.ledger   src/device/device_ledger.cpp:346        Device 0 HIDUSB inited
2018-09-08 11:08:21.187     7fbd4a71e780        DEBUG   device.io       src/device/device_io_hid.cpp:73 HID Device found: 0004:000e:00
$ ldd build/release/bin/monero-wallet-cli
        linux-vdso.so.1 =>  (0x00007ffe49de3000)
        libboost_chrono.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_chrono.so.1.58.0 (0x00007f98fc560000)
        libboost_program_options.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_program_options.so.1.58.0 (0x00007f98fc2e0000)
        libboost_filesystem.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.58.0 (0x00007f98fc0c8000)
        libboost_thread.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0 (0x00007f98fbea0000)
        libreadline.so.6 => /lib/x86_64-linux-gnu/libreadline.so.6 (0x00007f98fbc58000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f98fba50000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f98fb848000)
        libhidapi-libusb.so.0 => /usr/lib/x86_64-linux-gnu/libhidapi-libusb.so.0 (0x00007f98fb640000)
        libboost_serialization.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_serialization.so.1.58.0 (0x00007f98fb3d8000)
        libsodium.so.18 => /usr/lib/x86_64-linux-gnu/libsodium.so.18 (0x00007f98fb178000)
        libunbound.so.2 => /usr/lib/x86_64-linux-gnu/libunbound.so.2 (0x00007f98faed8000)
        libboost_system.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0 (0x00007f98facd0000)
        libboost_regex.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_regex.so.1.58.0 (0x00007f98fa9c8000)
        libssl.so.1.0.0 => /lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f98fa758000)
        libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f98fa310000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f98f9f88000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f98f9c78000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f98f9a60000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f98f9840000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f98f9470000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f98fd298000)
        libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f98f9240000)
        libusb-1.0.so.0 => /lib/x86_64-linux-gnu/libusb-1.0.so.0 (0x00007f98f9028000)
        libicui18n.so.55 => /usr/lib/x86_64-linux-gnu/libicui18n.so.55 (0x00007f98f8bc0000)
        libicuuc.so.55 => /usr/lib/x86_64-linux-gnu/libicuuc.so.55 (0x00007f98f8828000)
        libudev.so.1 => /lib/x86_64-linux-gnu/libudev.so.1 (0x00007f98fd478000)
        libicudata.so.55 => /usr/lib/x86_64-linux-gnu/libicudata.so.55 (0x00007f98f6d70000)

@cslashm cslashm changed the title [DONOT MERGE] Replace USB-CCID (smartcard) by USB-HID Replace USB-CCID (smartcard) by USB-HID Sep 10, 2018
@iDunk5400
Copy link
Contributor

iDunk5400 commented Sep 10, 2018

I tested c926461 on Windows 10 and on Ubuntu 16.04.

On Windows I tested sweep_all (on mainnet) and it worked Fine.

On two Linux boxes, both Ubuntu 16.04, I get segfaults when opening an existing wallet or creating a new one.

$ gdb --args ~/monero/build/release/bin/monero-wallet-cli --wallet-file ledger1 --log-level 2
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/user/monero/build/release/bin/monero-wallet-cli...done.
(gdb) run
Starting program: /home/user/monero/build/release/bin/monero-wallet-cli --wallet-file ledger1 --log-level 2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
2018-09-10 17:03:04,454 INFO  [default] Page size: 4096
This is the command line monero wallet. It needs to connect to a monero
daemon to work correctly.
WARNING: Do not reuse your Monero keys on another fork, UNLESS this fork has key reuse mitigations built in. Doing so will harm your privacy.

Monero 'Lithium Luna' (v0.12.3.0-master-b845bfe)
Logging to /home/user/monero/build/release/bin/monero-wallet-cli.log
[New Thread 0x7ffff2027700 (LWP 31986)]
Wallet password:
[New Thread 0x7ffff04fe700 (LWP 31987)]

Thread 1 "monero-wallet-c" received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106     ../sysdeps/x86_64/strlen.S: No such file or directory.
(gdb) bt
#0  strlen () at ../sysdeps/x86_64/strlen.S:106
#1  0x0000555555bd121c in hw::io::device_io_hid::connect(unsigned int, unsigned int, unsigned int, unsigned int, bool) ()
#2  0x0000555555bc5372 in hw::ledger::device_ledger::connect() ()
#3  0x0000555555954f73 in tools::wallet2::load_keys(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, epee::wipeable_string const&) ()
#4  0x0000555555995732 in tools::wallet2::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, epee::wipeable_string const&) ()
#5  0x000055555599784e in tools::wallet2::make_from_file(boost::program_options::variables_map const&, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<boost::optional<tools::password_container> (char const*, bool)> const&) ()
#6  0x0000555555864f5c in cryptonote::simple_wallet::open_wallet(boost::program_options::variables_map const&) ()
#7  0x00005555558660a2 in cryptonote::simple_wallet::init(boost::program_options::variables_map const&) ()
#8  0x000055555583031a in main ()
(gdb)
$ gdb --args build/release/bin/monero-wallet-cli --generate-from-device ledger --restore-height 1601000 --subaddress-lookahead 3:50 --log-level 2
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from build/release/bin/monero-wallet-cli...done.
(gdb) run
Starting program: /home/user/monero/build/release/bin/monero-wallet-cli --generate-from-device ledger --restore-height 1601000 --subaddress-lookahead 3:50 --log-level 2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
2018-09-10 17:08:56,331 INFO  [default] Page size: 4096
This is the command line monero wallet. It needs to connect to a monero
daemon to work correctly.
WARNING: Do not reuse your Monero keys on another fork, UNLESS this fork has key reuse mitigations built in. Doing so will harm your privacy.

Monero 'Lithium Luna' (v0.12.3.0-master-e5afbcc)
Logging to /home/user/monero/build/release/bin/monero-wallet-cli.log
[New Thread 0x7ffff22c2700 (LWP 8241)]
Enter a new password for the wallet:
Confirm password:
[New Thread 0x7ffff12fe700 (LWP 8242)]

Thread 1 "monero-wallet-c" received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106     ../sysdeps/x86_64/strlen.S: No such file or directory.
(gdb) bt
#0  strlen () at ../sysdeps/x86_64/strlen.S:106
#1  0x0000555555bf068c in hw::io::device_io_hid::connect(unsigned int, unsigned int, unsigned int, unsigned int, bool) ()
#2  0x0000555555be47da in hw::ledger::device_ledger::connect() ()
#3  0x0000555555bb12fb in cryptonote::account_base::create_from_device(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#4  0x00005555559becc1 in tools::wallet2::restore(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, epee::wipeable_string const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#5  0x000055555586158f in cryptonote::simple_wallet::new_wallet(boost::program_options::variables_map const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
#6  0x0000555555885fe0 in cryptonote::simple_wallet::init(boost::program_options::variables_map const&) ()
#7  0x000055555584f02a in main ()
(gdb)

/Edit: libhidapi-* version in Ubuntu 16.04 is 0.8.0~rc1+git20140818.d17, if that might be the cause of these segfaults.

@cslashm
Copy link
Contributor Author

cslashm commented Sep 14, 2018

I think the segfault came from debug/trace message. I fixed it (I hope).

@iDunk5400
Copy link
Contributor

The good news is it doesn't segfault any more. The bad news is

2018-09-14 09:05:46.608	    7f4233d6d780	DEBUG	net.http	src/common/util.cpp:835	Address 'http://localhost:18081' is local
2018-09-14 09:05:46.608	    7f4233d6d780	INFO 	wallet.wallet2	src/wallet/wallet2.cpp:257	Daemon is local, assuming trusted
2018-09-14 09:05:46.609	    7f4233d6d780	DEBUG	device.ledger	src/device/device_ledger.cpp:193	Device 0 Created
2018-09-14 09:05:46.610	    7f4233d6d780	INFO 	wallet.wallet2	src/wallet/wallet2.cpp:6171	ringdb path set to /home/user/.shared-ringdb
2018-09-14 09:05:46.640	    7f4233d6d780	WARN 	wallet.wallet2	src/wallet/wallet2.cpp:3226	Account on device. Initing device...
2018-09-14 09:05:46.644	    7f4233d6d780	DEBUG	device.ledger	src/device/device_ledger.cpp:346	Device 0 HIDUSB inited
2018-09-14 09:05:46.644	    7f4233d6d780	DEBUG	device.io	src/device/device_io_hid.cpp:87	HID Device found: 0004:0006:00
2018-09-14 09:05:46.644	    7f4233d6d780	ERROR	device.io	src/device/device_io_hid.cpp:96	Unable to open device 1:11415
2018-09-14 09:05:46.644	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:133	Exception: std::runtime_error
2018-09-14 09:05:46.644	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:134	Unwound call stack:
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [1] /home/user/monero/build/release/bin/monero-wallet-cli:__cxa_throw+0x10b [0x55b62c494c4b]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [2] /home/user/monero/build/release/bin/monero-wallet-cli:hw::io::device_io_hid::connect(unsigned int, unsigned int, unsigned int, unsigned int, bool)+0x86c [0x55b62c559774]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [3] /home/user/monero/build/release/bin/monero-wallet-cli:hw::ledger::device_ledger::connect()+0x6a [0x55b62c5524f2]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [4] /home/user/monero/build/release/bin/monero-wallet-cli:tools::wallet2::load_keys(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, epee::wipeable_string const&)+0xc53 [0x55b62c2e34b3]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [5] /home/user/monero/build/release/bin/monero-wallet-cli:tools::wallet2::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, epee::wipeable_string const&)+0x342 [0x55b62c31dab2]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [6] /home/user/monero/build/release/bin/monero-wallet-cli:tools::wallet2::make_from_file(boost::program_options::variables_map const&, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<boost::optional<tools::password_container> (char const*, bool)> const&)+0x10e [0x55b62c31fbce]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [7] /home/user/monero/build/release/bin/monero-wallet-cli:cryptonote::simple_wallet::open_wallet(boost::program_options::variables_map const&)+0x13c [0x55b62c1eccd4]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [8] /home/user/monero/build/release/bin/monero-wallet-cli:cryptonote::simple_wallet::init(boost::program_options::variables_map const&)+0x452 [0x55b62c1ede12]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [9] /home/user/monero/build/release/bin/monero-wallet-cli:main+0x3aa [0x55b62c1b7cea]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [10] /lib/x86_64-linux-gnu/libc.so.6:__libc_start_main+0xf0 [0x7f42308c0830]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	    [11] /home/user/monero/build/release/bin/monero-wallet-cli:_start+0x29 [0x55b62c1c24d9]
2018-09-14 09:05:46.646	    7f4233d6d780	INFO 	stacktrace	src/common/stack_trace.cpp:172	
2018-09-14 09:05:46.646	    7f4233d6d780	DEBUG	net	contrib/epee/include/net/net_helper.h:512	Problems at cancel: Bad file descriptor
2018-09-14 09:05:46.646	    7f4233d6d780	DEBUG	net	contrib/epee/include/net/net_helper.h:515	Problems at shutdown: Bad file descriptor
2018-09-14 09:05:46.647	    7f4233d6d780	ERROR	msgwriter	src/common/scoped_message_writer.h:102	Error: failed to load wallet: Unable to open device 1:11415
2018-09-14 09:05:46.647	    7f4233d6d780	ERROR	wallet.simplewallet	src/simplewallet/simplewallet.cpp:3389	failed to open account
2018-09-14 09:05:46.647	    7f4233d6d780	ERROR	wallet.simplewallet	src/simplewallet/simplewallet.cpp:7997	Failed to initialize wallet
2018-09-14 09:05:46.647	    7f4233d6d780	DEBUG	device.ledger	src/device/device_ledger.cpp:198	Device 0 Destroyed

Connecting to my Nano S works fine with v0.12.3.0-release (PCSC).

@iDunk5400
Copy link
Contributor

PEBKAC

After looking at the ldd output again, I realised the binaries link to libudev. Adding an udev rule solved my "Unable to open device" problem on Ubuntu. That's probably explained somewhere in Ledger documentation, but not in the commit or PR message. Creating a wallet now works fine for me on Linux and Windows.

@m2049r
Copy link
Contributor

m2049r commented Sep 16, 2018

@iDunk5400 you need different udev rules than before? that doesn't sound right. what rule did you have to add?

@iDunk5400
Copy link
Contributor

There was no "before" :) I didn't use my Nano S for anything other than Monero on my Ubuntu box, and Ledger used libpcsclite (smartcard mode) prior to this PR.
I made a rule with SUBSYSTEM=="usb", ATTRS{idVendor}=="2c97", ATTRS{idProduct}=="0001", GROUP="plugdev", MODE="0660" in it.

@cslashm
Copy link
Contributor Author

cslashm commented Sep 17, 2018

udev rules are reported here: https://support.ledgerwallet.com/hc/en-us/articles/115005165269-Fix-connection-issues

I should add this pointer in the user doc.

@iDunk5400 does it mean there is no more trouble now ? \o/

Remove PCSC dependencies which is a bit hard (not user friendly) to install on linux and Mac

Split Ledger logic and device IO
Copy link
Contributor

@fluffypony fluffypony left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed

@fluffypony fluffypony merged commit bb6e3bb into monero-project:master Sep 24, 2018
fluffypony added a commit that referenced this pull request Sep 24, 2018
bb6e3bb Replace USB-CCID (smartcard) by USB-HID (cslashm)
@m2049r
Copy link
Contributor

m2049r commented Sep 25, 2018

what's with all the whitespace (lots of lines end with a random amount of blanks)?

@TheCharlatan
Copy link
Contributor

@TheCharlatan thanks for the pointer - it looks like it may work on android (although i doubt it since permissions don't work well for ndk). but why add an extra unnecessary dependency? imo, there is nothing in the code of this PR which cannot be handled by android usb support. and there is nothing wrong with encapsulating lowlevel functions like hardware communication even if we didn't care about android, ios, whatnot..

also, if ledger communication goes through the android usb stack provided by monerujo JNI, i can show the status of communications (as i can intercept it) - otherwise i cannot. and the user sits there wondering why nothing is happening for minutes...

I think it would make sense if an rpc/public function is available, that returns the status of different devices. This would be useful for a whole bunch of applications, not just monerujo. From what I have read so far and the research we made at our company, it should be quite easy to make use of hidapi in an android application.

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.

7 participants