aboutsummaryrefslogtreecommitdiff
path: root/src/device_trezor/device_trezor_base.cpp
diff options
context:
space:
mode:
authorDusan Klinec <dusan.klinec@gmail.com>2018-11-28 22:22:11 +0100
committerDusan Klinec <dusan.klinec@gmail.com>2018-12-04 20:12:03 +0100
commitd71f89e2a26720021f7509f8c1eee87645f48529 (patch)
tree9086c377f1e47f862e064251a92f8b260f21540a /src/device_trezor/device_trezor_base.cpp
parentdevice/trezor: python2 compatibility - bundle dependencies (diff)
downloadmonero-d71f89e2a26720021f7509f8c1eee87645f48529.tar.xz
device/trezor: device/trezor: correct device initialization, status check
- checks if the device is in the correct usable state - implements check for the v2.0.9 firmware which does not support payment IDs - simple transacttion check, payment id fmt consistency - minor fixes, refactoring, webusb session counting fix
Diffstat (limited to 'src/device_trezor/device_trezor_base.cpp')
-rw-r--r--src/device_trezor/device_trezor_base.cpp58
1 files changed, 53 insertions, 5 deletions
diff --git a/src/device_trezor/device_trezor_base.cpp b/src/device_trezor/device_trezor_base.cpp
index fddc6082c..5071932ee 100644
--- a/src/device_trezor/device_trezor_base.cpp
+++ b/src/device_trezor/device_trezor_base.cpp
@@ -65,7 +65,7 @@ namespace trezor {
}
bool device_trezor_base::set_name(const std::string & name) {
- this->full_name = name;
+ this->m_full_name = name;
this->name = "";
auto delim = name.find(':');
@@ -77,10 +77,10 @@ namespace trezor {
}
const std::string device_trezor_base::get_name() const {
- if (this->full_name.empty()) {
+ if (this->m_full_name.empty()) {
return std::string("<disconnected:").append(this->name).append(">");
}
- return this->full_name;
+ return this->m_full_name;
}
bool device_trezor_base::init() {
@@ -139,6 +139,9 @@ namespace trezor {
}
bool device_trezor_base::disconnect() {
+ m_device_state.clear();
+ m_features.reset();
+
if (m_transport){
try {
m_transport->close();
@@ -193,6 +196,25 @@ namespace trezor {
}
}
+ void device_trezor_base::require_initialized(){
+ if (!m_features){
+ throw exc::TrezorException("Device state not initialized");
+ }
+
+ if (m_features->has_bootloader_mode() && m_features->bootloader_mode()){
+ throw exc::TrezorException("Device is in the bootloader mode");
+ }
+
+ if (m_features->has_firmware_present() && !m_features->firmware_present()){
+ throw exc::TrezorException("Device has no firmware loaded");
+ }
+
+ // Hard requirement on initialized field, has to be there.
+ if (!m_features->has_initialized() || !m_features->initialized()){
+ throw exc::TrezorException("Device is not initialized");
+ }
+ }
+
void device_trezor_base::call_ping_unsafe(){
auto pingMsg = std::make_shared<messages::management::Ping>();
pingMsg->set_message("PING");
@@ -217,7 +239,7 @@ namespace trezor {
void device_trezor_base::write_raw(const google::protobuf::Message * msg){
require_connected();
CHECK_AND_ASSERT_THROW_MES(msg, "Empty message");
- this->getTransport()->write(*msg);
+ this->get_transport()->write(*msg);
}
GenericMessage device_trezor_base::read_raw(){
@@ -225,7 +247,7 @@ namespace trezor {
std::shared_ptr<google::protobuf::Message> msg_resp;
hw::trezor::messages::MessageType msg_resp_type;
- this->getTransport()->read(msg_resp, &msg_resp_type);
+ this->get_transport()->read(msg_resp, &msg_resp_type);
return GenericMessage(msg_resp_type, msg_resp);
}
@@ -314,6 +336,25 @@ namespace trezor {
return false;
}
+ void device_trezor_base::device_state_reset_unsafe()
+ {
+ require_connected();
+ auto initMsg = std::make_shared<messages::management::Initialize>();
+
+ if(!m_device_state.empty()) {
+ initMsg->set_allocated_state(&m_device_state);
+ }
+
+ m_features = this->client_exchange<messages::management::Features>(initMsg);
+ initMsg->release_state();
+ }
+
+ void device_trezor_base::device_state_reset()
+ {
+ AUTO_LOCK_CMD();
+ device_state_reset_unsafe();
+ }
+
void device_trezor_base::on_button_request(GenericMessage & resp, const messages::common::ButtonRequest * msg)
{
CHECK_AND_ASSERT_THROW_MES(msg, "Empty message");
@@ -361,7 +402,13 @@ namespace trezor {
// TODO: remove passphrase from memory
m.set_passphrase(passphrase.data(), passphrase.size());
}
+
+ if (!m_device_state.empty()){
+ m.set_allocated_state(&m_device_state);
+ }
+
resp = call_raw(&m);
+ m.release_state();
}
void device_trezor_base::on_passphrase_state_request(GenericMessage & resp, const messages::common::PassphraseStateRequest * msg)
@@ -369,6 +416,7 @@ namespace trezor {
MDEBUG("on_passhprase_state_request");
CHECK_AND_ASSERT_THROW_MES(msg, "Empty message");
+ m_device_state = msg->state();
messages::common::PassphraseStateAck m;
resp = call_raw(&m);
}