aboutsummaryrefslogtreecommitdiff
path: root/src/device_trezor
diff options
context:
space:
mode:
Diffstat (limited to 'src/device_trezor')
-rw-r--r--src/device_trezor/device_trezor_base.cpp13
-rw-r--r--src/device_trezor/device_trezor_base.hpp2
-rw-r--r--src/device_trezor/trezor/transport.cpp64
-rw-r--r--src/device_trezor/trezor/transport.hpp10
4 files changed, 67 insertions, 22 deletions
diff --git a/src/device_trezor/device_trezor_base.cpp b/src/device_trezor/device_trezor_base.cpp
index f3d15c5e2..58abde1d1 100644
--- a/src/device_trezor/device_trezor_base.cpp
+++ b/src/device_trezor/device_trezor_base.cpp
@@ -43,7 +43,7 @@ namespace trezor {
const uint32_t device_trezor_base::DEFAULT_BIP44_PATH[] = {0x8000002c, 0x80000080};
- device_trezor_base::device_trezor_base(): m_callback(nullptr) {
+ device_trezor_base::device_trezor_base(): m_callback(nullptr), m_last_msg_type(messages::MessageType_Success) {
#ifdef WITH_TREZOR_DEBUGGING
m_debug = false;
#endif
@@ -275,6 +275,12 @@ namespace trezor {
// Later if needed this generic message handler can be replaced by a pointer to
// a protocol message handler which by default points to the device class which implements
// the default handler.
+
+ if (m_last_msg_type == messages::MessageType_ButtonRequest){
+ on_button_pressed();
+ }
+ m_last_msg_type = input.m_type;
+
switch(input.m_type){
case messages::MessageType_ButtonRequest:
on_button_request(input, dynamic_cast<const messages::common::ButtonRequest*>(input.m_msg.get()));
@@ -413,6 +419,11 @@ namespace trezor {
resp = read_raw();
}
+ void device_trezor_base::on_button_pressed()
+ {
+ TREZOR_CALLBACK(on_button_pressed);
+ }
+
void device_trezor_base::on_pin_request(GenericMessage & resp, const messages::common::PinMatrixRequest * msg)
{
MDEBUG("on_pin_request");
diff --git a/src/device_trezor/device_trezor_base.hpp b/src/device_trezor/device_trezor_base.hpp
index 8c3c14b29..c106d2099 100644
--- a/src/device_trezor/device_trezor_base.hpp
+++ b/src/device_trezor/device_trezor_base.hpp
@@ -98,6 +98,7 @@ namespace trezor {
std::shared_ptr<messages::management::Features> m_features; // features from the last device reset
boost::optional<epee::wipeable_string> m_pin;
boost::optional<epee::wipeable_string> m_passphrase;
+ messages::MessageType m_last_msg_type;
cryptonote::network_type network_type;
@@ -311,6 +312,7 @@ namespace trezor {
// Protocol callbacks
void on_button_request(GenericMessage & resp, const messages::common::ButtonRequest * msg);
+ void on_button_pressed();
void on_pin_request(GenericMessage & resp, const messages::common::PinMatrixRequest * msg);
void on_passphrase_request(GenericMessage & resp, const messages::common::PassphraseRequest * msg);
void on_passphrase_state_request(GenericMessage & resp, const messages::common::PassphraseStateRequest * msg);
diff --git a/src/device_trezor/trezor/transport.cpp b/src/device_trezor/trezor/transport.cpp
index 991ba3395..dd9b0b52f 100644
--- a/src/device_trezor/trezor/transport.cpp
+++ b/src/device_trezor/trezor/transport.cpp
@@ -223,6 +223,11 @@ namespace trezor{
msg = msg_wrap;
}
+ static void assert_port_number(uint32_t port)
+ {
+ CHECK_AND_ASSERT_THROW_MES(port >= 1024 && port < 65535, "Invalid port number: " << port);
+ }
+
Transport::Transport(): m_open_counter(0) {
}
@@ -263,6 +268,29 @@ namespace trezor{
const char * BridgeTransport::PATH_PREFIX = "bridge:";
+ BridgeTransport::BridgeTransport(
+ boost::optional<std::string> device_path,
+ boost::optional<std::string> bridge_host):
+ m_device_path(device_path),
+ m_bridge_host(bridge_host ? bridge_host.get() : DEFAULT_BRIDGE),
+ m_response(boost::none),
+ m_session(boost::none),
+ m_device_info(boost::none)
+ {
+ const char *env_bridge_port = nullptr;
+ if (!bridge_host && (env_bridge_port = getenv("TREZOR_BRIDGE_PORT")) != nullptr)
+ {
+ uint16_t bridge_port;
+ CHECK_AND_ASSERT_THROW_MES(epee::string_tools::get_xtype_from_string(bridge_port, env_bridge_port), "Invalid bridge port: " << env_bridge_port);
+ assert_port_number(bridge_port);
+
+ m_bridge_host = std::string("127.0.0.1:") + boost::lexical_cast<std::string>(env_bridge_port);
+ MDEBUG("Bridge host: " << m_bridge_host);
+ }
+
+ m_http_client.set_server(m_bridge_host, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled);
+ }
+
std::string BridgeTransport::get_path() const {
if (!m_device_path){
return "";
@@ -401,28 +429,40 @@ namespace trezor{
const char * UdpTransport::DEFAULT_HOST = "127.0.0.1";
const int UdpTransport::DEFAULT_PORT = 21324;
+ static void parse_udp_path(std::string &host, int &port, std::string path)
+ {
+ if (boost::starts_with(path, UdpTransport::PATH_PREFIX))
+ {
+ path = path.substr(strlen(UdpTransport::PATH_PREFIX));
+ }
+
+ auto delim = path.find(':');
+ if (delim == std::string::npos) {
+ host = path;
+ } else {
+ host = path.substr(0, delim);
+ port = std::stoi(path.substr(delim + 1));
+ }
+ }
+
UdpTransport::UdpTransport(boost::optional<std::string> device_path,
boost::optional<std::shared_ptr<Protocol>> proto) :
m_io_service(), m_deadline(m_io_service)
{
+ m_device_host = DEFAULT_HOST;
m_device_port = DEFAULT_PORT;
+ const char *env_trezor_path = nullptr;
+
if (device_path) {
- const std::string device_str = device_path.get();
- auto delim = device_str.find(':');
- if (delim == std::string::npos) {
- m_device_host = device_str;
- } else {
- m_device_host = device_str.substr(0, delim);
- m_device_port = std::stoi(device_str.substr(delim + 1));
- }
+ parse_udp_path(m_device_host, m_device_port, device_path.get());
+ } else if ((env_trezor_path = getenv("TREZOR_PATH")) != nullptr && boost::starts_with(env_trezor_path, UdpTransport::PATH_PREFIX)){
+ parse_udp_path(m_device_host, m_device_port, std::string(env_trezor_path));
+ MDEBUG("Applied TREZOR_PATH: " << m_device_host << ":" << m_device_port);
} else {
m_device_host = DEFAULT_HOST;
}
- if (m_device_port <= 1024 || m_device_port > 65535){
- throw std::invalid_argument("Port number invalid");
- }
-
+ assert_port_number((uint32_t)m_device_port);
if (m_device_host != "localhost" && m_device_host != DEFAULT_HOST){
throw std::invalid_argument("Local endpoint allowed only");
}
diff --git a/src/device_trezor/trezor/transport.hpp b/src/device_trezor/trezor/transport.hpp
index 2945b3184..cde862547 100644
--- a/src/device_trezor/trezor/transport.hpp
+++ b/src/device_trezor/trezor/transport.hpp
@@ -163,15 +163,7 @@ namespace trezor {
public:
BridgeTransport(
boost::optional<std::string> device_path = boost::none,
- boost::optional<std::string> bridge_host = boost::none):
- m_device_path(device_path),
- m_bridge_host(bridge_host ? bridge_host.get() : DEFAULT_BRIDGE),
- m_response(boost::none),
- m_session(boost::none),
- m_device_info(boost::none)
- {
- m_http_client.set_server(m_bridge_host, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled);
- }
+ boost::optional<std::string> bridge_host = boost::none);
virtual ~BridgeTransport() = default;