summaryrefslogtreecommitdiff
path: root/patches/monero/0016-add-dummy-device-for-ledger.patch
blob: 11b5a3c81a5acc1708ee9a845cb52071eb98a213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
From 77f328ba6befb9b964f764a27f8a03b71bf9095d Mon Sep 17 00:00:00 2001
From: Konstantin Ullrich <konstantin@cakewallet.com>
Date: Fri, 22 Nov 2024 11:43:18 +0100
Subject: [PATCH 16/16] add dummy device for ledger

---
 CMakeLists.txt               |  6 +++++-
 src/device/CMakeLists.txt    |  6 ++++--
 src/device/device.cpp        | 10 ++++++----
 src/device/device.hpp        | 12 +-----------
 src/device/device_ledger.cpp |  6 ++++--
 src/device/device_ledger.hpp |  7 ++++++-
 src/wallet/api/wallet.cpp    | 13 +++++++++++++
 src/wallet/api/wallet.h      |  2 ++
 src/wallet/api/wallet2_api.h |  3 +++
 9 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6028c0961..e7fa90abb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -694,8 +694,12 @@ include_directories(${LMDB_INCLUDE})
 include_directories(${LIBUNWIND_INCLUDE})
 link_directories(${LIBUNWIND_LIBRARY_DIRS})
 
+if (HIDAPI_DUMMY)
+  add_definitions(-DHIDAPI_DUMMY)
+endif()
+
 # Final setup for hid
-if (HIDAPI_FOUND) 
+if (HIDAPI_FOUND)
   message(STATUS "Using HIDAPI include dir at ${HIDAPI_INCLUDE_DIR}")
   add_definitions(-DHAVE_HIDAPI)
   include_directories(${HIDAPI_INCLUDE_DIR})
diff --git a/src/device/CMakeLists.txt b/src/device/CMakeLists.txt
index e4f1159b5..14d398f87 100644
--- a/src/device/CMakeLists.txt
+++ b/src/device/CMakeLists.txt
@@ -29,10 +29,11 @@
 set(device_sources
   device.cpp
   device_default.cpp
+  device_io_dummy.cpp
   log.cpp
   )
 
-if(HIDAPI_FOUND)
+if(HIDAPI_FOUND OR HIDAPI_DUMMY)
   set(device_sources 
     ${device_sources} 
     device_ledger.cpp
@@ -45,10 +46,11 @@ set(device_headers
   device_io.hpp
   device_default.hpp
   device_cold.hpp
+  device_io_dummy.hpp
   log.hpp
  )
 
-if(HIDAPI_FOUND)
+if(HIDAPI_FOUND OR HIDAPI_DUMMY)
   set(device_headers 
     ${device_headers} 
     device_ledger.hpp
diff --git a/src/device/device.cpp b/src/device/device.cpp
index e6cd358b6..636929feb 100644
--- a/src/device/device.cpp
+++ b/src/device/device.cpp
@@ -29,7 +29,7 @@
 
 #include "device.hpp"
 #include "device_default.hpp"
-#ifdef WITH_DEVICE_LEDGER
+#if defined(WITH_DEVICE_LEDGER) || defined(HIDAPI_DUMMY)
 #include "device_ledger.hpp"
 #endif
 #include "misc_log_ex.h"
@@ -57,7 +57,7 @@ namespace hw {
 
     device_registry::device_registry(){
         hw::core::register_all(registry);
-        #ifdef WITH_DEVICE_LEDGER
+        #if defined(WITH_DEVICE_LEDGER) || defined(HIDAPI_DUMMY)
         hw::ledger::register_all(registry);
         #endif
         atexit(clear_device_registry);
@@ -83,11 +83,13 @@ namespace hw {
 
         auto device = registry.find(device_descriptor_lookup);
         if (device == registry.end()) {
-            MERROR("Device not found in registry: '" << device_descriptor << "'. Known devices: ");
+            std::stringstream ss("Device not found in registry: '" + device_descriptor + "'. Known devices: ");
+            MERROR("Device not found in registry: '" << device_descriptor << "'. Known devices: \n");
             for( const auto& sm_pair : registry ) {
+                ss << "\n- " + sm_pair.first;
                 MERROR(" - " << sm_pair.first);
             }
-            throw std::runtime_error("device not found: " + device_descriptor);
+            throw std::runtime_error("device not found: " + device_descriptor + "\nlalala\n" + ss.str());
         }
         return *device->second;
     }
diff --git a/src/device/device.hpp b/src/device/device.hpp
index 392703a24..ffd419779 100644
--- a/src/device/device.hpp
+++ b/src/device/device.hpp
@@ -34,17 +34,7 @@
 #include "ringct/rctTypes.h"
 #include "cryptonote_config.h"
 
-
-#ifndef USE_DEVICE_LEDGER
-#define USE_DEVICE_LEDGER 1
-#endif
-
-#if !defined(HAVE_HIDAPI) 
-#undef  USE_DEVICE_LEDGER
-#define USE_DEVICE_LEDGER 0
-#endif
-
-#if USE_DEVICE_LEDGER
+#if defined(HAVE_HIDAPI) || defined(HIDAPI_DUMMY)
 #define WITH_DEVICE_LEDGER
 #endif
 
diff --git a/src/device/device_ledger.cpp b/src/device/device_ledger.cpp
index bb5b6f497..d405b86f2 100644
--- a/src/device/device_ledger.cpp
+++ b/src/device/device_ledger.cpp
@@ -41,7 +41,7 @@ namespace hw {
 
   namespace ledger {
 
-  #ifdef WITH_DEVICE_LEDGER
+  #if defined(WITH_DEVICE_LEDGER) || defined(HIDAPI_DUMMY)
 
     #undef MONERO_DEFAULT_LOG_CATEGORY
     #define MONERO_DEFAULT_LOG_CATEGORY "device.ledger"
@@ -299,7 +299,7 @@ namespace hw {
 
     device_ledger::device_ledger(): hw_device(0x0101, 0x05, 64, 2000) {
       this->id = device_id++;
-      this->reset_buffer();      
+      this->reset_buffer();
       this->mode = NONE;
       this->has_view_key = false;
       this->tx_in_progress = false;
@@ -533,7 +533,9 @@ namespace hw {
 
     bool device_ledger::connect(void) {
       this->disconnect();
+      #ifndef HIDAPI_DUMMY
       hw_device.connect(known_devices);
+      #endif
       this->reset();
       #ifdef DEBUG_HWDEVICE
       cryptonote::account_public_address pubkey;
diff --git a/src/device/device_ledger.hpp b/src/device/device_ledger.hpp
index 03058c4f1..506f27c4a 100644
--- a/src/device/device_ledger.hpp
+++ b/src/device/device_ledger.hpp
@@ -35,6 +35,7 @@
 #include "device.hpp"
 #include "log.hpp"
 #include "device_io_hid.hpp"
+#include "device_io_dummy.hpp"
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/recursive_mutex.hpp>
 
@@ -56,7 +57,7 @@ namespace hw {
 
     void register_all(std::map<std::string, std::unique_ptr<device>> &registry);
 
-    #ifdef WITH_DEVICE_LEDGER
+    #if defined(WITH_DEVICE_LEDGER) || defined(HIDAPI_DUMMY)
 
     // Origin: https://github.com/LedgerHQ/ledger-app-monero/blob/master/src/monero_types.h
     #define SW_OK                                   0x9000
@@ -148,7 +149,11 @@ namespace hw {
         mutable boost::mutex   command_locker;
 
         //IO
+#ifdef  HIDAPI_DUMMY
+        hw::io::device_io_dummy hw_device;
+#else
         hw::io::device_io_hid hw_device;
+#endif
         unsigned int  length_send;
         unsigned char buffer_send[BUFFER_SEND_SIZE];
         unsigned int  length_recv;
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp
index a307d35a7..6f1f9714e 100644
--- a/src/wallet/api/wallet.cpp
+++ b/src/wallet/api/wallet.cpp
@@ -49,6 +49,9 @@
 #include <boost/locale.hpp>
 #include <boost/filesystem.hpp>
 #include "bc-ur/src/bc-ur.hpp"
+#ifdef HIDAPI_DUMMY
+#include "device/device_io_dummy.hpp"
+#endif
 
 using namespace std;
 using namespace cryptonote;
@@ -3298,4 +3301,14 @@ uint64_t WalletImpl::getBytesSent()
     return m_wallet->get_bytes_sent();
 }
 
+// HIDAPI_DUMMY
+void WalletImpl::setLedgerExchange(int (*sendToLedgerDevice) (unsigned char *command, unsigned int cmd_len, unsigned char *response, unsigned int max_resp_len)) {
+    #ifndef HIDAPI_DUMMY
+    setStatusError("MONERO compiled with -DHIDAPI_DUMMY");
+    return;
+    #else
+    hw::io::device_io_dummy::sendToLedgerDevice = sendToLedgerDevice;
+    #endif
+}
+
 } // namespace
diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h
index febc93119..bfe2eb34f 100644
--- a/src/wallet/api/wallet.h
+++ b/src/wallet/api/wallet.h
@@ -321,6 +321,8 @@ private:
     // cache connection status to avoid unnecessary RPC calls
     mutable std::atomic<bool>   m_is_connected;
     boost::optional<epee::net_utils::http::login> m_daemon_login{};
+
+    void setLedgerExchange(int (*sendToLedgerDevice) (unsigned char *command, unsigned int cmd_len, unsigned char *response, unsigned int max_resp_len));
 };
 
 
diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h
index 2bbb32c8b..bc1f733aa 100644
--- a/src/wallet/api/wallet2_api.h
+++ b/src/wallet/api/wallet2_api.h
@@ -1204,6 +1204,9 @@ struct Wallet
 
     //! get bytes sent
     virtual uint64_t getBytesSent() = 0;
+
+    // HIDAPI_DUMMY
+    virtual void setLedgerExchange(int (*sendToLedgerDevice) (unsigned char *command, unsigned int cmd_len, unsigned char *response, unsigned int max_resp_len)) = 0;
 };
 
 /**
-- 
2.47.0