30 #ifndef MONERO_DEVICE_TREZOR_BASE_H 31 #define MONERO_DEVICE_TREZOR_BASE_H 39 #include <boost/scope_exit.hpp> 40 #include <boost/thread/mutex.hpp> 41 #include <boost/thread/recursive_mutex.hpp> 45 #ifdef WITH_TREZOR_DEBUGGING 50 #define TREZOR_AUTO_LOCK_CMD() \ 52 boost::lock(device_locker, command_locker); \ 54 boost::lock_guard<boost::recursive_mutex> lock1(device_locker, boost::adopt_lock); \ 55 boost::lock_guard<boost::mutex> lock2(command_locker, boost::adopt_lock) 57 #define TREZOR_AUTO_LOCK_DEVICE() boost::lock_guard<boost::recursive_mutex> lock1_device(device_locker) 62 #ifdef WITH_DEVICE_TREZOR 63 class device_trezor_base;
65 #ifdef WITH_TREZOR_DEBUGGING 68 trezor_debug_callback()=
default;
69 explicit trezor_debug_callback(std::shared_ptr<Transport> & debug_transport);
72 boost::optional<epee::wipeable_string> on_pin_request()
override;
73 boost::optional<epee::wipeable_string> on_passphrase_request(
bool & on_device)
override;
77 std::shared_ptr<DebugLink> m_debug_link;
89 mutable boost::recursive_mutex device_locker;
90 mutable boost::mutex command_locker;
92 std::shared_ptr<Transport> m_transport;
93 i_device_callback * m_callback;
96 std::vector<unsigned int> m_wallet_deriv_path;
98 std::shared_ptr<messages::management::Features> m_features;
99 boost::optional<epee::wipeable_string> m_pin;
100 boost::optional<epee::wipeable_string> m_passphrase;
101 messages::MessageType m_last_msg_type;
104 bool m_reply_with_empty_passphrase;
105 bool m_always_use_empty_passphrase;
106 bool m_seen_passphrase_entry_message;
108 #ifdef WITH_TREZOR_DEBUGGING 109 std::shared_ptr<trezor_debug_callback> m_debug_callback;
119 void require_connected()
const;
120 void require_initialized()
const;
121 void call_ping_unsafe();
123 virtual void device_state_initialize_unsafe();
124 void ensure_derivation_path() noexcept;
128 void write_raw(
const google::protobuf::Message * msg);
129 GenericMessage read_raw();
130 GenericMessage call_raw(
const google::protobuf::Message * msg);
133 bool message_handler(GenericMessage & input);
141 template<
class t_message=google::protobuf::Message>
142 std::shared_ptr<t_message>
143 client_exchange(
const std::shared_ptr<const google::protobuf::Message> &req,
144 const boost::optional<messages::MessageType> & resp_type = boost::none,
145 const boost::optional<std::vector<messages::MessageType>> & resp_types = boost::none,
146 const boost::optional<messages::MessageType*> & resp_type_ptr = boost::none,
147 bool open_session =
false)
152 if (resp_types && !accepting_base){
153 throw std::invalid_argument(
"Cannot specify list of accepted types and not using generic response");
157 const messages::MessageType required_type = accepting_base ? messages::MessageType_Success :
158 (resp_type ? resp_type.get() : MessageMapper::get_message_wire_number<t_message>());
164 }
catch (
const std::exception&
e) {
165 std::throw_with_nested(exc::SessionException(
"Could not open session"));
170 BOOST_SCOPE_EXIT_ALL(&,
this) {
171 if (open_session && this->get_transport()){
172 this->get_transport()->close();
177 CHECK_AND_ASSERT_THROW_MES(req,
"Request is null");
178 auto msg_resp = call_raw(req.get());
180 bool processed =
false;
182 processed = message_handler(msg_resp);
187 *(resp_type_ptr.get()) = msg_resp.m_type;
190 if (msg_resp.m_type == messages::MessageType_Failure) {
193 }
else if (!accepting_base && msg_resp.m_type == required_type) {
194 return message_ptr_retype<t_message>(msg_resp.m_msg);
196 }
else if (accepting_base && (!resp_types ||
197 std::find(resp_types.get().begin(), resp_types.get().end(), msg_resp.m_type) != resp_types.get().end())) {
198 return message_ptr_retype<t_message>(msg_resp.m_msg);
201 throw exc::UnexpectedMessageException(msg_resp.m_type, msg_resp.m_msg);
208 template<
class t_message>
209 void set_msg_addr(t_message * msg,
210 const boost::optional<std::vector<uint32_t>> & path = boost::none,
211 const boost::optional<cryptonote::network_type> &
network_type = boost::none)
213 CHECK_AND_ASSERT_THROW_MES(msg,
"Message is null");
214 msg->clear_address_n();
216 for(
auto x : path.get()){
217 msg->add_address_n(x);
220 ensure_derivation_path();
221 for (
unsigned int i : DEFAULT_BIP44_PATH) {
222 msg->add_address_n(
i);
224 for (
unsigned int i : m_wallet_deriv_path) {
225 msg->add_address_n(
i);
230 msg->set_network_type(static_cast<uint32_t>(
network_type.get()));
232 msg->set_network_type(static_cast<uint32_t>(this->network_type));
237 device_trezor_base();
238 ~device_trezor_base()
override;
240 device_trezor_base(
const device_trezor_base &
device) = delete ;
241 device_trezor_base& operator=(
const device_trezor_base &
device) =
delete;
243 explicit operator bool()
const override {
return true;}
244 device_type get_type()
const override {
return device_type::TREZOR;};
249 static const uint32_t DEFAULT_BIP44_PATH[2];
251 std::shared_ptr<Transport> get_transport(){
255 void set_callback(i_device_callback * callback)
override {
256 m_callback = callback;
259 i_device_callback * get_callback(){
263 std::shared_ptr<messages::management::Features> & get_features() {
268 CHECK_AND_ASSERT_THROW_MES(m_features,
"Features not loaded");
269 CHECK_AND_ASSERT_THROW_MES(m_features->has_major_version() && m_features->has_minor_version() && m_features->has_patch_version(),
"Invalid Trezor firmware version information");
270 return pack_version(m_features->major_version(), m_features->minor_version(), m_features->patch_version());
273 void set_derivation_path(
const std::string &deriv_path)
override;
275 virtual bool has_ki_live_refresh(
void)
const override {
return false; }
281 m_passphrase = passphrase;
290 bool init()
override;
292 bool connect()
override;
293 bool disconnect()
override;
298 void lock()
override;
299 void unlock()
override;
300 bool try_lock()
override;
314 void device_state_reset();
317 void on_button_request(GenericMessage & resp,
const messages::common::ButtonRequest * msg);
318 void on_button_pressed();
319 void on_pin_request(GenericMessage & resp,
const messages::common::PinMatrixRequest * msg);
320 void on_passphrase_request(GenericMessage & resp,
const messages::common::PassphraseRequest * msg);
321 void on_passphrase_state_request(GenericMessage & resp,
const messages::common::Deprecated_PassphraseStateRequest * msg);
323 #ifdef WITH_TREZOR_DEBUGGING 324 void set_debug(
bool debug){
328 void set_debug_callback(std::shared_ptr<trezor_debug_callback> & debug_callback){
329 m_debug_callback = debug_callback;
334 void load_device(
const std::string & mnemonic,
const std::string & pin=
"",
bool passphrase_protection=
false,
336 bool skip_checksum=
false,
bool expand=
false);
345 #endif //MONERO_DEVICE_TREZOR_BASE_H static int init(int argc, char **argv, struct runtime_vars *v)
Definition: miniupnpd.c:1149
def release
Definition: support.py:307
int i
Definition: pymoduletest.py:23
::std::string string
Definition: gtest-port.h:1097
Definition: minissdpd.c:75
e
Definition: pymoduletest.py:79
const portMappingElt code
Definition: portlistingparse.c:22
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
get_version
Definition: console.py:54
Definition: device.cpp:38
void throw_failure_exception(const messages::common::Failure *failure)
Definition: transport.cpp:1244
uint64_t pack_version(uint32_t major, uint32_t minor, uint32_t patch)
Definition: transport.cpp:100
network_type
Definition: cryptonote_config.h:301
Definition: blake256.h:36
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
int bool
Definition: stdbool.h:35
Definition: device_default.hpp:40
Definition: wipeable_string.h:40
const char * name
Definition: options.c:30
Definition: device.hpp:77