Skip to content

Commit

Permalink
device/trezor: add button pressed request
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Apr 7, 2019
1 parent 827f52a commit c68fe78
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/device/device.hpp
Expand Up @@ -76,6 +76,7 @@ namespace hw {
class i_device_callback {
public:
virtual void on_button_request(uint64_t code=0) {}
virtual void on_button_pressed() {}
virtual boost::optional<epee::wipeable_string> on_pin_request() { return boost::none; }
virtual boost::optional<epee::wipeable_string> on_passphrase_request(bool on_device) { return boost::none; }
virtual void on_progress(const device_progress& event) {}
Expand Down
13 changes: 12 additions & 1 deletion src/device_trezor/device_trezor_base.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/device_trezor/device_trezor_base.hpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/wallet/api/wallet.cpp
Expand Up @@ -249,6 +249,13 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
}
}

virtual void on_device_button_pressed()
{
if (m_listener) {
m_listener->onDeviceButtonPressed();
}
}

virtual boost::optional<epee::wipeable_string> on_device_pin_request()
{
if (m_listener) {
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/api/wallet2_api.h
Expand Up @@ -385,6 +385,11 @@ struct WalletListener
*/
virtual void onDeviceButtonRequest(uint64_t code) { (void)code; }

/**
* @brief called by device if the button was pressed
*/
virtual void onDeviceButtonPressed() { }

/**
* @brief called by device when PIN is needed
*/
Expand Down
12 changes: 12 additions & 0 deletions src/wallet/wallet2.cpp
Expand Up @@ -978,6 +978,12 @@ void wallet_device_callback::on_button_request(uint64_t code)
wallet->on_device_button_request(code);
}

void wallet_device_callback::on_button_pressed()
{
if (wallet)
wallet->on_device_button_pressed();
}

boost::optional<epee::wipeable_string> wallet_device_callback::on_pin_request()
{
if (wallet)
Expand Down Expand Up @@ -12855,6 +12861,12 @@ void wallet2::on_device_button_request(uint64_t code)
m_callback->on_device_button_request(code);
}
//----------------------------------------------------------------------------------------------------
void wallet2::on_device_button_pressed()
{
if (nullptr != m_callback)
m_callback->on_device_button_pressed();
}
//----------------------------------------------------------------------------------------------------
boost::optional<epee::wipeable_string> wallet2::on_device_pin_request()
{
if (nullptr != m_callback)
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/wallet2.h
Expand Up @@ -105,6 +105,7 @@ namespace tools
virtual void on_lw_money_spent(uint64_t height, const crypto::hash &txid, uint64_t amount) {}
// Device callbacks
virtual void on_device_button_request(uint64_t code) {}
virtual void on_device_button_pressed() {}
virtual boost::optional<epee::wipeable_string> on_device_pin_request() { return boost::none; }
virtual boost::optional<epee::wipeable_string> on_device_passphrase_request(bool on_device) { return boost::none; }
virtual void on_device_progress(const hw::device_progress& event) {};
Expand All @@ -118,6 +119,7 @@ namespace tools
public:
wallet_device_callback(wallet2 * wallet): wallet(wallet) {};
void on_button_request(uint64_t code=0) override;
void on_button_pressed() override;
boost::optional<epee::wipeable_string> on_pin_request() override;
boost::optional<epee::wipeable_string> on_passphrase_request(bool on_device) override;
void on_progress(const hw::device_progress& event) override;
Expand Down Expand Up @@ -1367,6 +1369,7 @@ namespace tools

wallet_device_callback * get_device_callback();
void on_device_button_request(uint64_t code);
void on_device_button_pressed();
boost::optional<epee::wipeable_string> on_device_pin_request();
boost::optional<epee::wipeable_string> on_device_passphrase_request(bool on_device);
void on_device_progress(const hw::device_progress& event);
Expand Down

0 comments on commit c68fe78

Please sign in to comment.