30#ifndef MONERO_DEVICE_TREZOR_BASE_H
31#define MONERO_DEVICE_TREZOR_BASE_H
40#include <boost/scope_exit.hpp>
41#include <boost/thread/mutex.hpp>
42#include <boost/thread/recursive_mutex.hpp>
46#ifdef WITH_TREZOR_DEBUGGING
51#define TREZOR_AUTO_LOCK_CMD() \
53 boost::lock(device_locker, command_locker); \
55 boost::lock_guard<boost::recursive_mutex> lock1(device_locker, boost::adopt_lock); \
56 boost::lock_guard<boost::mutex> lock2(command_locker, boost::adopt_lock)
58#define TREZOR_AUTO_LOCK_DEVICE() boost::lock_guard<boost::recursive_mutex> lock1_device(device_locker)
63#ifdef WITH_DEVICE_TREZOR
64 class device_trezor_base;
66#ifdef WITH_TREZOR_DEBUGGING
69 trezor_debug_callback()=
default;
70 explicit trezor_debug_callback(std::shared_ptr<Transport> & debug_transport);
73 boost::optional<epee::wipeable_string> on_pin_request()
override;
74 boost::optional<epee::wipeable_string> on_passphrase_request(
bool & on_device)
override;
75 void on_passphrase_state_request(
const std::string &
state);
78 std::shared_ptr<DebugLink> m_debug_link;
90 mutable boost::recursive_mutex device_locker;
91 mutable boost::mutex command_locker;
93 std::shared_ptr<Transport> m_transport;
94 i_device_callback * m_callback;
96 std::string m_full_name;
97 std::vector<unsigned int> m_wallet_deriv_path;
99 std::shared_ptr<messages::management::Features> m_features;
100 boost::optional<epee::wipeable_string> m_pin;
101 boost::optional<epee::wipeable_string> m_passphrase;
102 messages::MessageType m_last_msg_type;
105 bool m_reply_with_empty_passphrase;
106 bool m_always_use_empty_passphrase;
107 bool m_seen_passphrase_entry_message;
109#ifdef WITH_TREZOR_DEBUGGING
110 std::shared_ptr<trezor_debug_callback> m_debug_callback;
120 void require_connected()
const;
121 void require_initialized()
const;
122 void call_ping_unsafe();
124 virtual void device_state_initialize_unsafe();
125 void ensure_derivation_path()
noexcept;
129 void write_raw(
const google::protobuf::Message * msg);
130 GenericMessage read_raw();
131 GenericMessage call_raw(
const google::protobuf::Message * msg);
134 bool message_handler(GenericMessage & input);
142 template<
class t_message=google::protobuf::Message>
143 std::shared_ptr<t_message>
144 client_exchange(
const std::shared_ptr<const google::protobuf::Message> &req,
145 const boost::optional<messages::MessageType> & resp_type = boost::none,
146 const boost::optional<std::vector<messages::MessageType>> & resp_types = boost::none,
147 const boost::optional<messages::MessageType*> & resp_type_ptr = boost::none,
148 bool open_session =
false)
151 static_assert(std::is_base_of<google::protobuf::Message, t_message>::value);
152 const bool accepting_base = boost::is_same<google::protobuf::Message, t_message>::value;
153 if (resp_types && !accepting_base){
154 throw std::invalid_argument(
"Cannot specify list of accepted types and not using generic response");
158 const messages::MessageType required_type = accepting_base ? messages::MessageType_Success :
159 (resp_type ? resp_type.get() : MessageMapper::get_message_wire_number<t_message>());
165 }
catch (
const std::exception& e) {
166 std::throw_with_nested(exc::SessionException(
"Could not open session"));
171 BOOST_SCOPE_EXIT_ALL(&,
this) {
172 if (open_session && this->get_transport()){
173 this->get_transport()->close();
178 CHECK_AND_ASSERT_THROW_MES(req,
"Request is null");
179 auto msg_resp = call_raw(req.get());
181 bool processed =
false;
183 processed = message_handler(msg_resp);
188 *(resp_type_ptr.get()) = msg_resp.m_type;
191 if (msg_resp.m_type == messages::MessageType_Failure) {
192 throw_failure_exception(
dynamic_cast<messages::common::Failure *
>(msg_resp.m_msg.get()));
194 }
else if (!accepting_base && msg_resp.m_type == required_type) {
195 return message_ptr_retype<t_message>(msg_resp.m_msg);
197 }
else if (accepting_base && (!resp_types ||
198 std::find(resp_types.get().begin(), resp_types.get().end(), msg_resp.m_type) != resp_types.get().end())) {
199 return message_ptr_retype<t_message>(msg_resp.m_msg);
202 throw exc::UnexpectedMessageException(msg_resp.m_type, msg_resp.m_msg);
209 template<
class t_message>
210 void set_msg_addr(t_message * msg,
211 const boost::optional<std::vector<uint32_t>> & path = boost::none,
212 const boost::optional<cryptonote::network_type> & network_type = boost::none)
214 CHECK_AND_ASSERT_THROW_MES(msg,
"Message is null");
215 msg->clear_address_n();
217 for(
auto x : path.get()){
218 msg->add_address_n(x);
221 ensure_derivation_path();
222 for (
unsigned int i : DEFAULT_BIP44_PATH) {
223 msg->add_address_n(i);
225 for (
unsigned int i : m_wallet_deriv_path) {
226 msg->add_address_n(i);
231 msg->set_network_type(
static_cast<uint32_t>(network_type.get()));
233 msg->set_network_type(
static_cast<uint32_t>(this->network_type));
238 device_trezor_base();
239 ~device_trezor_base()
override;
241 device_trezor_base(
const device_trezor_base &device) = delete ;
242 device_trezor_base& operator=(
const device_trezor_base &device) =
delete;
244 explicit operator bool()
const override {
return true;}
245 device_type get_type()
const override {
return device_type::TREZOR;};
250 static const uint32_t DEFAULT_BIP44_PATH[2];
252 std::shared_ptr<Transport> get_transport(){
256 void set_callback(i_device_callback * callback)
override {
257 m_callback = callback;
260 i_device_callback * get_callback(){
264 std::shared_ptr<messages::management::Features> & get_features() {
269 CHECK_AND_ASSERT_THROW_MES(m_features,
"Features not loaded");
270 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");
271 return pack_version(m_features->major_version(), m_features->minor_version(), m_features->patch_version());
274 void set_derivation_path(
const std::string &deriv_path)
override;
276 virtual bool has_ki_live_refresh(
void)
const override {
return false; }
282 m_passphrase = passphrase;
288 bool set_name(
const std::string &name)
override;
290 const std::string get_name()
const override;
291 bool init()
override;
292 bool release()
override;
293 bool connect()
override;
294 bool disconnect()
override;
299 void lock()
override;
300 void unlock()
override;
301 bool try_lock()
override;
315 void device_state_reset();
318 void on_button_request(GenericMessage & resp,
const messages::common::ButtonRequest * msg);
319 void on_button_pressed();
320 void on_pin_request(GenericMessage & resp,
const messages::common::PinMatrixRequest * msg);
321 void on_passphrase_request(GenericMessage & resp,
const messages::common::PassphraseRequest * msg);
322 void on_passphrase_state_request(GenericMessage & resp,
const messages::common::Deprecated_PassphraseStateRequest * msg);
324#ifdef WITH_TREZOR_DEBUGGING
325 void set_debug(
bool debug){
329 void set_debug_callback(std::shared_ptr<trezor_debug_callback> & debug_callback){
330 m_debug_callback = debug_callback;
335 void load_device(
const std::string & mnemonic,
const std::string & pin=
"",
bool passphrase_protection=
false,
336 const std::string & label=
"test",
const std::string & language=
"english",
337 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
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136