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;
74 void on_passphrase_state_request(
const std::string &
state);
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;
95 std::string m_full_name;
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)
150 BOOST_STATIC_ASSERT(boost::is_base_of<google::protobuf::Message, t_message>::value);
151 const bool accepting_base = boost::is_same<google::protobuf::Message, t_message>::value;
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) {
191 throw_failure_exception(
dynamic_cast<messages::common::Failure *
>(msg_resp.m_msg.get()));
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;
287 bool set_name(
const std::string &
name)
override;
289 const std::string get_name()
const override;
290 bool init()
override;
291 bool release()
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,
335 const std::string & label=
"test",
const std::string & language=
"english",
336 bool skip_checksum=
false,
bool expand=
false);
Definition wipeable_string.h:41
Definition device_default.hpp:40
static void init()
Definition logging.cpp:42
network_type
Definition cryptonote_config.h:302
const char * name
Definition options.c:30
const portMappingElt code
Definition portlistingparse.c:22
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
Definition minissdpd.c:75