Monero
device_trezor.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef MONERO_DEVICE_TREZOR_H
31 #define MONERO_DEVICE_TREZOR_H
32 
33 #include "trezor.hpp"
34 #include "device/device.hpp"
35 
36 #ifdef WITH_DEVICE_TREZOR
37 #include <cstddef>
38 #include <string>
39 #include <boost/scope_exit.hpp>
40 #include <boost/thread/mutex.hpp>
41 #include <boost/thread/recursive_mutex.hpp>
42 
44 #include "device/device_cold.hpp"
45 #include "cryptonote_config.h"
46 #include "device_trezor_base.hpp"
47 #endif
48 
49 namespace hw {
50 namespace trezor {
51 
52  void register_all();
53  void register_all(std::map<std::string, std::unique_ptr<device>> &registry);
54 
55 #ifdef WITH_DEVICE_TREZOR
56  class device_trezor;
57 
61  class device_trezor : public hw::trezor::device_trezor_base, public hw::device_cold {
62  protected:
63  std::atomic<bool> m_live_refresh_in_progress;
64  std::chrono::steady_clock::time_point m_last_live_refresh_time;
65  std::unique_ptr<boost::thread> m_live_refresh_thread;
66  std::atomic<bool> m_live_refresh_thread_running;
67  bool m_live_refresh_enabled;
68  size_t m_num_transations_to_sign;
69 
70  unsigned client_version();
71  void transaction_versions_check(const ::tools::wallet2::unsigned_tx_set & unsigned_tx, hw::tx_aux_data & aux_data);
72  void transaction_pre_check(std::shared_ptr<messages::monero::MoneroTransactionInitRequest> init_msg);
73  void transaction_check(const protocol::tx::TData & tdata, const hw::tx_aux_data & aux_data);
74  void device_state_initialize_unsafe() override;
75  void live_refresh_start_unsafe();
76  void live_refresh_finish_unsafe();
77  void live_refresh_thread_main();
78 
82  virtual void tx_sign(wallet_shim * wallet,
84  size_t idx,
85  hw::tx_aux_data & aux_data,
86  std::shared_ptr<protocol::tx::Signer> & signer);
87 
88  public:
89  device_trezor();
90  virtual ~device_trezor() override;
91 
92  device_trezor(const device_trezor &device) = delete ;
93  device_trezor& operator=(const device_trezor &device) = delete;
94 
95  explicit operator bool() const override {return true;}
96 
97  bool init() override;
98  bool release() override;
99  bool disconnect() override;
100 
101  device_protocol_t device_protocol() const override { return PROTOCOL_COLD; };
102 
103  bool has_ki_cold_sync() const override { return true; }
104  bool has_tx_cold_sign() const override { return true; }
105  void set_network_type(cryptonote::network_type network_type) override { this->network_type = network_type; }
106  void set_live_refresh_enabled(bool enabled) { m_live_refresh_enabled = enabled; }
107  bool live_refresh_enabled() const { return m_live_refresh_enabled; }
108 
109  /* ======================================================================= */
110  /* WALLET & ADDRESS */
111  /* ======================================================================= */
112  bool get_public_address(cryptonote::account_public_address &pubkey) override;
113  bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override;
114  void display_address(const cryptonote::subaddress_index& index, const boost::optional<crypto::hash8> &payment_id) override;
115 
116  /* ======================================================================= */
117  /* TREZOR PROTOCOL */
118  /* ======================================================================= */
119 
123  std::shared_ptr<messages::monero::MoneroAddress> get_address(
124  const boost::optional<cryptonote::subaddress_index> & subaddress = boost::none,
125  const boost::optional<crypto::hash8> & payment_id = boost::none,
126  bool show_address = false,
127  const boost::optional<std::vector<uint32_t>> & path = boost::none,
128  const boost::optional<cryptonote::network_type> & network_type = boost::none);
129 
133  std::shared_ptr<messages::monero::MoneroWatchKey> get_view_key(
134  const boost::optional<std::vector<uint32_t>> & path = boost::none,
135  const boost::optional<cryptonote::network_type> & network_type = boost::none);
136 
140  bool is_get_tx_key_supported() const override;
141 
145  void load_tx_key_data(::hw::device_cold::tx_key_data_t & res, const std::string & tx_aux_data) override;
146 
150  void get_tx_key(
151  std::vector<::crypto::secret_key> & tx_keys,
152  const ::hw::device_cold::tx_key_data_t & tx_aux_data,
153  const ::crypto::secret_key & view_key_priv) override;
154 
158  void ki_sync(wallet_shim * wallet,
159  const std::vector<::tools::wallet2::transfer_details> & transfers,
160  hw::device_cold::exported_key_image & ski) override;
161 
162  bool is_live_refresh_supported() const override;
163 
164  bool is_live_refresh_enabled() const;
165 
166  bool has_ki_live_refresh() const override;
167 
168  void live_refresh_start() override;
169 
170  void live_refresh(
171  const ::crypto::secret_key & view_key_priv,
172  const crypto::public_key& out_key,
173  const crypto::key_derivation& recv_derivation,
174  size_t real_output_index,
175  const cryptonote::subaddress_index& received_index,
176  cryptonote::keypair& in_ephemeral,
178  ) override;
179 
180  void live_refresh_finish() override;
181 
186  void computing_key_images(bool started) override;
187 
192  bool compute_key_image(
193  const ::cryptonote::account_keys& ack,
195  const ::crypto::key_derivation& recv_derivation,
196  size_t real_output_index,
197  const ::cryptonote::subaddress_index& received_index,
198  ::cryptonote::keypair& in_ephemeral,
199  ::crypto::key_image& ki) override;
200 
204  void tx_sign(wallet_shim * wallet,
206  ::tools::wallet2::signed_tx_set & signed_tx,
207  hw::tx_aux_data & aux_data) override;
208 
212  bool get_public_address_with_no_passphrase(cryptonote::account_public_address &pubkey) override;
213 
217  virtual void reset_session() override;
218 
222  bool seen_passphrase_entry_prompt() override;
223 
227  void set_use_empty_passphrase(bool use_always_empty_passphrase) override;
228  };
229 
230 #endif
231 
232 }
233 }
234 #endif //MONERO_DEVICE_TREZOR_H
const char * res
Definition: hmac_keccak.cpp:42
static int init(int argc, char **argv, struct runtime_vars *v)
Definition: miniupnpd.c:1149
Definition: wallet2.h:728
void register_all(std::map< std::string, std::unique_ptr< device >> &registry)
Definition: device_trezor.cpp:776
def release
Definition: support.py:307
Definition: cryptonote_basic.h:538
::std::string string
Definition: gtest-port.h:1097
POD_CLASS key_derivation
Definition: crypto.h:88
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:72
Definition: minissdpd.c:75
tools::wallet2::unsigned_tx_set unsigned_tx_set
Definition: protocol.hpp:155
void load_tx_key_data(hw::device_cold::tx_key_data_t &res, const std::string &data)
Definition: protocol.cpp:1022
struct hw::wallet_shim wallet_shim
Definition: subaddress_index.h:38
Definition: device.cpp:38
Definition: wallet.py:1
cryptonote::account_public_address get_address(const var_addr_t &inp)
Definition: chaingen.cpp:817
POD_CLASS public_key
Definition: crypto.h:61
crypto::secret_key spendkey
Definition: cold-outputs.cpp:44
Definition: device_cold.hpp:44
network_type
Definition: cryptonote_config.h:301
Definition: cryptonote_basic.h:511
Definition: device_cold.hpp:53
POD_CLASS key_image
Definition: crypto.h:92
int bool
Definition: stdbool.h:35
std::shared_ptr< messages::monero::MoneroGetTxKeyRequest > get_tx_key(const hw::device_cold::tx_key_data_t &tx_data)
Definition: protocol.cpp:1053
Definition: device_cold.hpp:98
std::vector< std::pair< crypto::key_image, crypto::signature > > exported_key_image
Definition: device_cold.hpp:56