30 #ifndef ELECTRONEUM_DEVICE_TREZOR_BASE_H 31 #define ELECTRONEUM_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);
71 void on_button_request(
uint64_t code=0)
override;
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;
105 #ifdef WITH_TREZOR_DEBUGGING 106 std::shared_ptr<trezor_debug_callback> m_debug_callback;
116 void require_connected()
const;
117 void require_initialized()
const;
118 void call_ping_unsafe();
120 virtual void device_state_reset_unsafe();
121 void ensure_derivation_path() noexcept;
125 void write_raw(
const google::protobuf::Message * msg);
126 GenericMessage read_raw();
127 GenericMessage call_raw(
const google::protobuf::Message * msg);
130 bool message_handler(GenericMessage & input);
138 template<
class t_message=google::protobuf::Message>
139 std::shared_ptr<t_message>
140 client_exchange(
const std::shared_ptr<const google::protobuf::Message> &req,
141 const boost::optional<messages::MessageType> & resp_type = boost::none,
142 const boost::optional<std::vector<messages::MessageType>> & resp_types = boost::none,
143 const boost::optional<messages::MessageType*> & resp_type_ptr = boost::none,
144 bool open_session =
false)
149 if (resp_types && !accepting_base){
150 throw std::invalid_argument(
"Cannot specify list of accepted types and not using generic response");
154 const messages::MessageType required_type = accepting_base ? messages::MessageType_Success :
155 (resp_type ? resp_type.get() : MessageMapper::get_message_wire_number<t_message>());
161 }
catch (
const std::exception& e) {
162 std::throw_with_nested(exc::SessionException(
"Could not open session"));
167 BOOST_SCOPE_EXIT_ALL(&,
this) {
169 this->get_transport()->close();
175 auto msg_resp = call_raw(req.get());
177 bool processed =
false;
179 processed = message_handler(msg_resp);
184 *(resp_type_ptr.get()) = msg_resp.m_type;
187 if (msg_resp.m_type == messages::MessageType_Failure) {
190 }
else if (!accepting_base && msg_resp.m_type == required_type) {
191 return message_ptr_retype<t_message>(msg_resp.m_msg);
193 }
else if (accepting_base && (!resp_types ||
194 std::find(resp_types.get().begin(), resp_types.get().end(), msg_resp.m_type) != resp_types.get().end())) {
195 return message_ptr_retype<t_message>(msg_resp.m_msg);
198 throw exc::UnexpectedMessageException(msg_resp.m_type, msg_resp.m_msg);
205 template<
class t_message>
206 void set_msg_addr(t_message * msg,
207 const boost::optional<std::vector<uint32_t>> & path = boost::none,
208 const boost::optional<cryptonote::network_type> &
network_type = boost::none)
211 msg->clear_address_n();
213 for(
auto x : path.get()){
214 msg->add_address_n(x);
217 ensure_derivation_path();
218 for (
unsigned int i : DEFAULT_BIP44_PATH) {
219 msg->add_address_n(i);
221 for (
unsigned int i : m_wallet_deriv_path) {
222 msg->add_address_n(i);
227 msg->set_network_type(static_cast<uint32_t>(
network_type.get()));
229 msg->set_network_type(static_cast<uint32_t>(this->network_type));
234 device_trezor_base();
235 ~device_trezor_base()
override;
237 device_trezor_base(
const device_trezor_base &device) = delete ;
238 device_trezor_base&
operator=(
const device_trezor_base &device) =
delete;
240 explicit operator bool()
const override {
return true;}
241 device_type get_type()
const override {
return device_type::TREZOR;};
246 static const uint32_t DEFAULT_BIP44_PATH[2];
248 std::shared_ptr<Transport> get_transport(){
252 void set_callback(i_device_callback * callback)
override {
253 m_callback = callback;
256 i_device_callback * get_callback(){
260 std::shared_ptr<messages::management::Features> & get_features() {
266 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");
267 return pack_version(m_features->major_version(), m_features->minor_version(), m_features->patch_version());
270 void set_derivation_path(
const std::string &deriv_path)
override;
272 virtual bool has_ki_live_refresh(
void)
const override {
return false; }
278 m_passphrase = passphrase;
287 bool init()
override;
288 bool release()
override;
289 bool connect()
override;
290 bool disconnect()
override;
295 void lock()
override;
296 void unlock()
override;
297 bool try_lock()
override;
311 void device_state_reset();
314 void on_button_request(GenericMessage & resp,
const messages::common::ButtonRequest * msg);
315 void on_button_pressed();
316 void on_pin_request(GenericMessage & resp,
const messages::common::PinMatrixRequest * msg);
317 void on_passphrase_request(GenericMessage & resp,
const messages::common::PassphraseRequest * msg);
318 void on_passphrase_state_request(GenericMessage & resp,
const messages::common::PassphraseStateRequest * msg);
320 #ifdef WITH_TREZOR_DEBUGGING 321 void set_debug(
bool debug){
325 void set_debug_callback(std::shared_ptr<trezor_debug_callback> & debug_callback){
326 m_debug_callback = debug_callback;
331 void load_device(
const std::string & mnemonic,
const std::string & pin=
"",
bool passphrase_protection=
false,
333 bool skip_checksum=
false,
bool expand=
false);
342 #endif //ELECTRONEUM_DEVICE_TREZOR_BASE_H
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
unsigned __int64 uint64_t
void throw_failure_exception(const messages::common::Failure *failure)
uint64_t pack_version(uint32_t major, uint32_t minor, uint32_t patch)
const GenericPointer< typename T::ValueType > T2 value
connection< TProtocol > & operator=(const connection< TProtocol > &obj)