Electroneum
device_trezor.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-Present, Electroneum
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 ELECTRONEUM_DEVICE_TREZOR_H
31 #define ELECTRONEUM_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  void transaction_versions_check(const ::tools::wallet2::unsigned_tx_set & unsigned_tx, hw::tx_aux_data & aux_data);
71  void transaction_pre_check(std::shared_ptr<messages::Electroneum::ElectroneumTransactionInitRequest> init_msg);
72  void transaction_check(const protocol::tx::TData & tdata, const hw::tx_aux_data & aux_data);
73  void device_state_reset_unsafe() override;
74  void live_refresh_start_unsafe();
75  void live_refresh_finish_unsafe();
76  void live_refresh_thread_main();
77 
81  virtual void tx_sign(wallet_shim * wallet,
83  size_t idx,
84  hw::tx_aux_data & aux_data,
85  std::shared_ptr<protocol::tx::Signer> & signer);
86 
87  public:
88  device_trezor();
89  virtual ~device_trezor() override;
90 
91  device_trezor(const device_trezor &device) = delete ;
92  device_trezor& operator=(const device_trezor &device) = delete;
93 
94  explicit operator bool() const override {return true;}
95 
96  bool init() override;
97  bool release() override;
98  bool disconnect() override;
99 
100  device_protocol_t device_protocol() const override { return PROTOCOL_COLD; };
101 
102  bool has_ki_cold_sync() const override { return true; }
103  bool has_tx_cold_sign() const override { return true; }
104  void set_network_type(cryptonote::network_type network_type) override { this->network_type = network_type; }
105  void set_live_refresh_enabled(bool enabled) { m_live_refresh_enabled = enabled; }
106  bool live_refresh_enabled() const { return m_live_refresh_enabled; }
107 
108  /* ======================================================================= */
109  /* WALLET & ADDRESS */
110  /* ======================================================================= */
111  bool get_public_address(cryptonote::account_public_address &pubkey) override;
112  bool get_secret_keys(crypto::secret_key &viewkey , crypto::secret_key &spendkey) override;
113 
114  /* ======================================================================= */
115  /* TREZOR PROTOCOL */
116  /* ======================================================================= */
117 
121  std::shared_ptr<messages::Electroneum::ElectroneumAddress> get_address(
122  const boost::optional<std::vector<uint32_t>> & path = boost::none,
123  const boost::optional<cryptonote::network_type> & network_type = boost::none);
124 
128  std::shared_ptr<messages::Electroneum::ElectroneumWatchKey> get_view_key(
129  const boost::optional<std::vector<uint32_t>> & path = boost::none,
130  const boost::optional<cryptonote::network_type> & network_type = boost::none);
131 
135  bool is_get_tx_key_supported() const override;
136 
140  void load_tx_key_data(::hw::device_cold::tx_key_data_t & res, const std::string & tx_aux_data) override;
141 
145  void get_tx_key(
146  std::vector<::crypto::secret_key> & tx_keys,
147  const ::hw::device_cold::tx_key_data_t & tx_aux_data,
148  const ::crypto::secret_key & view_key_priv) override;
149 
153  void ki_sync(wallet_shim * wallet,
154  const std::vector<::tools::wallet2::transfer_details> & transfers,
155  hw::device_cold::exported_key_image & ski) override;
156 
157  bool is_live_refresh_supported() const override;
158 
159  bool is_live_refresh_enabled() const;
160 
161  bool has_ki_live_refresh() const override;
162 
163  void live_refresh_start() override;
164 
165  void live_refresh(
166  const ::crypto::secret_key & view_key_priv,
167  const crypto::public_key& out_key,
168  const crypto::key_derivation& recv_derivation,
169  size_t real_output_index,
170  const cryptonote::subaddress_index& received_index,
171  cryptonote::keypair& in_ephemeral,
173  ) override;
174 
175  void live_refresh_finish() override;
176 
181  void computing_key_images(bool started) override;
182 
187  bool compute_key_image(
188  const ::cryptonote::account_keys& ack,
190  const ::crypto::key_derivation& recv_derivation,
191  size_t real_output_index,
192  const ::cryptonote::subaddress_index& received_index,
193  ::cryptonote::keypair& in_ephemeral,
194  ::crypto::key_image& ki) override;
195 
199  void tx_sign(wallet_shim * wallet,
201  ::tools::wallet2::signed_tx_set & signed_tx,
202  hw::tx_aux_data & aux_data) override;
203  };
204 
205 #endif
206 
207 }
208 }
209 #endif //ELECTRONEUM_DEVICE_TREZOR_H
const char * res
Definition: hmac_keccak.cpp:41
void register_all(std::map< std::string, std::unique_ptr< device >> &registry)
::std::string string
Definition: gtest-port.h:1097
POD_CLASS key_derivation
Definition: crypto.h:98
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:82
std::shared_ptr< messages::Electroneum::ElectroneumGetTxKeyRequest > get_tx_key(const hw::device_cold::tx_key_data_t &tx_data)
Definition: protocol.cpp:1038
tools::wallet2::unsigned_tx_set unsigned_tx_set
Definition: protocol.hpp:157
void load_tx_key_data(hw::device_cold::tx_key_data_t &res, const std::string &data)
Definition: protocol.cpp:1007
struct hw::wallet_shim wallet_shim
Definition: device.cpp:38
cryptonote::account_public_address get_address(const var_addr_t &inp)
Definition: chaingen.cpp:665
POD_CLASS public_key
Definition: crypto.h:76
POD_CLASS key_image
Definition: crypto.h:102
int bool
Definition: stdbool.h:36
connection< TProtocol > & operator=(const connection< TProtocol > &obj)
const char * spendkey
Definition: multisig.cpp:38
std::vector< std::pair< crypto::key_image, crypto::signature > > exported_key_image
Definition: device_cold.hpp:55